HttpSession, session.getAttribute(), problem - java

I have a problem related to Java servlet sessions. I don't understand why the getAttribute() function of the session object is used before setAttribute(); Here is the code:
Vector buylist=(Vector)session.getAttribute("Register");
if (action.equals("del")) {
String del = request.getParameter("deli");
int d = (new Integer(del)).intValue();
buylist.removeElementAt(d);
}
session.setAttribute("Register", buylist);
Thanks.

This code intends to save back the modified vector represented by Register session attribute.
However you dont need to set the attribute back even after some elements are removed or added because its the reference anyways thats stored in session and any changes to it are essentially being applied to the same object.

Because Register attribute may be set from some other place (like. from jsp(in bad case),Servlet or Filter . . )

The only explanation I can think of is: first of all you you retrieve the vector, then make the change and after store it back into session object.

The code is either broken or the setAttribute() is futile.
If you get a mutable container (like a list or a vector) from the session, then it's not necessary to put it again into the session. It's the equivalent of this code:
session.setAttribute("Register", buylist);
session.setAttribute("Register", buylist);
session.setAttribute("Register", buylist);
None but the first line have an effect.
The other possibility is that the code has a bug and what really was meant was this:
Vector buylist = session.getAttribute("Register");
if( buylist == null ) {
buylist = new Vector();
session.setAttribute("Register", buylist);
}
i.e. create a new vector if it doesn't exist already.

Related

When do I need to recycle?

I have an applicationScope managed bean that loads a bunch of information about a series of applications into a Map. Then the following method is part of the bean:
public Database getAppDB() {
Database appDB = null;
try{
Session s = ExtLibUtil.getCurrentSession();
serverName = s.createName(s.getCurrentDatabase().getServer()).getCommon();
appDB = s.getDbDirectory(serverName).openDatabaseByReplicaID(this.getAppRepID());
return appDB;
}catch (NotesException e){
System.out.println(e.toString());
return appDB;
}
}
Now this method declares two Objects (Session and appDB). Not sure if they need to be recycled before returning and if so how would one do that because appDB is the returned value. The Session can easily be recycled.
Now clearly if I call this method from some SSJS:
var thisDB:NotesDatabase = appProps[ssApplication].appDB;
I need to recycle thisDB in the SSJS.
also if I do something like this in SSJS:
var cNames = appProps[ssApplication].appDB.getView("vwFTSearch").getColumnNames();
I'm assuming that there is nothing to recycle?
Detailed answers are in other two questions Knut pasted.
The short answer to your specific question is that you shouldn't recycle those objects in the getAppDB() method.
Session will be automatically recycled after the page has been served. The database object should be recycled by the caller of this method (SSJS, for your case).
The general rule is: you destroy (recycle) what you created. You leave what was given. Session and database are system provided objects (given), so you leave them alone even if you used a different function to obtain them.
For the rest i found a reasonable Practise: in the function that created the object it gets recycled, not anywhere else. You have to take the "created" a little pragmatic. E.g. when you have something like:
var custDB = getCustDB(curUser);
then you wouldn't consider getCustDB as the creator, but that line of code and have the recycle(); in the function where that code is.

SharedPreferences are updated but reset when app is reset

Whenever I start my app I have the following code:
C.userPreferences = getSharedPreferences("default",0);
C.userPreferencesEditor = C.userPreferences.edit();
C.something = C.userPreferences.getStringSet(C.SOMETHING, null);
C.something = C.something == null ? new HashSet<String>() : C.something;
for(String str : C.something){
Log.d("debugging C.something",str);
}
And this correctly logs "one","two" from the string set.
Afterwards I have the following function:
C.something.add("name");
C.userPreferencesEditor.putStringSet(C.SOMETHING, C.something);
C.userPreferencesEditor.apply(); //or with .commit();
And debugging shows "one","two" and "name".
When I restart the app and debug for the first time I only obtain "one" and "two".
Any idea on why this happens? tyvm
This says
Objects that are returned from the various get methods must be treated as immutable by the application.
More specifically:
Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.
Can you try copying the HashSet you retrieved, adding a new entry to the copy, and saving it in preferences?
BTW - I'd be really curious to know why this is the way it is - not very intuitive...

Java Code to fetch Session Store attribute values

I am writing an Assertion Generator Plugin in Java to fetch a user details from Session Store and modify the values in Assertion(SAML 2.0) accordingly.
I am able to identify the method(Link) using which I can pull the user values from Session Store (agentAPIObject.getSessionVariables()) based on SessionID, but, I am having trouble writing a code to fetch specific parameters from the session store. (speficially around setting values for Attribute method and making it as an array)
Can someone post a sample code if you have ever seen/written around it, so that I can fetch user attributes from Session Store.
I am having trouble understanding Java docs around it.
Thanks in advance,
The API mentions this:
responseAttributeList - On successful return from this method (YES is
returned), this output parameter contains the retrieved variable names
and their values. If the method returns UNRESOLVED, this parameter
includes variables that could not be retrieved.
You'll need to create two AttributeList Objects. If the response of getSessionVariables(...) is YES, then the variable responseAttributeList will contain the session variables. Since Java uses references, that same variable responseAttributeList will be updated. You can then use getAttributeAt(...) to access the Attribute Objects.
String sessionID = "sampleID";
ResourceContextDef rcd = //whatever it needs to be equal to
AttributeList requestAttributeList = new AttributeList();
AttributeList responseAttributeList = new AttributeList();
if(getSessionVariables(sessionId, rcd, requestAttributeList, responseAttributeList) == YES){
Attribute att = responseAttributeList.getAttributeAt(0);//or whatever index.
}
Remember to carefully read the API.
NOTE: This is just pseudo code. I have not tested this. However, this should be plenty enough to get you going where you need to.

Changing an object property from an object list - every object is getting the property changed

i have a list of objects:
List<TaskDataValue> configuredTaskData = sourceofdata;
I am iterating the list, to change a property from an object WITHIN the object:
for (int i=0; i<configuredTaskData.size(); i++) {
FieldConfiguration fc = configuredTaskData.get(i).getSettings();
String fieldName = configuredTaskData.get(i).getName();
if (newLabels.containsKey(fieldName)) {
fc.setLabel(newLabels.get(fieldName));
configuredTaskData.get(i).setSettings(fc);
}
}
although what is happening is that every TaskDataValue.getSettings.label is set to the last one,
sounds like configuredTaskData.get(i).setSettings(fc); is setting not only to i but to evry one
What can be happening here ?
using java 1.6
Check where you create the TaskDataValues in sourceofdata. Is it possible that they all have been configured with the same FieldConfiguration instance?
The most likely reason for something like this to happen is that you only actually have one TaskDataValue object - or inside TaskDataValue there is an object that there is only one of.
Because that object is shared any change made to it from one reference is also seen from all the other references.
For example:
List<TaskDataValue> tdvs = ...;
TaskDataValue v = new TaskDataValue();
tdvs.add(v);
tdvs.add(v);
tdvs.add(v);
That code creates one single TaskDataValue object and adds three references to that object to the list. If you change a setting inside tdvs.get(1) you will also see 2 and 3 change too.

How can I modify or remove properties values? - Jena API

I'm using Jena. I would like to know if there is a method that allows to modify or remove properties values of an instance?
Thanks
Statements in Jena are, by design, immutable. To change the value of a property p of some subject s, you need to add a new statement with the same subject and predicate, and remove the old statement. This is always true in Jena, even if the API sometimes hides this from you. For example, OntResource and its subclasses have a variety of setProperty variants, but under the hood these are performing the same add-the-new-triple-and-delete-the-old process.
It depends which Jena API you are using. For instance, if you are using Jena 3.0 and the Model API, you can use Model.remove(Statement) to remove a property by choosing the appropriate subject/predicate/object for the Statement. Modification can be achieved by removing the old version of a Statement and adding the new version.
To only remove the statement itself, i.e. the relation between the instance and the property value, you can use:
OntResource.removeProperty(Property, RDFNode)
If you want to remove the property value altogether, i.e. the value and all relations to it, you can use: OntResource.remove()
I had the similar task: I need to delete the property with the specified value. Hope the following code snippet will help someone.
public void removeLabel(String language, String value) {
NodeIterator nodeIterator = resource.getModel().listObjectsOfProperty(RDFS.label);
RDFNode foundToDelete = null;
while (nodeIterator.hasNext()) {
RDFNode next = nodeIterator.next();
boolean langsAreIdentical = next.asLiteral().getLanguage().equals(language);
boolean valuesAreIdentical = next.asLiteral().getLexicalForm().equals(value);
if (langsAreIdentical && valuesAreIdentical) {
foundToDelete = next;
break;
}
}
resource.getModel().remove(resource, RDFS.label, foundToDelete);
}

Categories