aspose file tools*
The moose likes Portals and Portlets and the fly likes Silly problem: One user sees the information of another! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Portals and Portlets
Bookmark "Silly problem: One user sees the information of another!" Watch "Silly problem: One user sees the information of another!" New topic
Author

Silly problem: One user sees the information of another!

Pablo Daniel Mino
Greenhorn

Joined: Jan 02, 2010
Posts: 8
Hi! I am new to portlets. I am using liferay with tomcat.

I will try to explain the best I can the issue I am having.

I have a portlet class that has a reference to an arraylist<info> as an attribute of the class, info is the object representation of the different user's data.
I initialize the arraylist<info> in the setup() method as an empty list. The user begins using the portlet and the information is stored in the arraylist<info>.
When another user logs in he sees the information created by the first user!

I am sure this is very silly, but I haven't found a solution yet. Any ideas?

Thank you.
Guy deLyonesse
Ranch Hand

Joined: Apr 12, 2011
Posts: 199

Is this in a portal environment where the portlets are or can be instanced?

For example, in Liferay, a portlet can be defined as being instanceable (multiple instances of the portlet can exist independently in a community) or not instanceable (You can only have one per page, and additional portlets of the same type in the same community will share the data source.)
Pablo Daniel Mino
Greenhorn

Joined: Jan 02, 2010
Posts: 8
I made no configuration, what is the default? Where should I make them instanceable?
Thank you.
Pablo Daniel Mino
Greenhorn

Joined: Jan 02, 2010
Posts: 8
Hi!

In liferay-portlet.xml I changed the XML tag to <instanceable>true</instanceable>, it was set in false.
After this configuration I logged in with two different users and still happens the same.

Here is my code.

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import java.io.IOException;
import java.util.ArrayList;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.ProcessAction;
import javax.portlet.RenderMode;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

/**
* Portlet implementation class NewPortlet
*/
public class NewPortlet extends GenericPortlet {

public void init() {
viewJSP = getInitParameter("view-jsp");
this.infos = new ArrayList<Info>();
}

@ProcessAction(name = "removeInfo")
synchronized public void removeInfo(
ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException{

String id = actionRequest.getParameter("id");
if(!infos.isEmpty()){
if(infos.contains(new Info(id))){
int id1 = infos.indexOf(new Info(id));
infos.remove(id1);
}
}
}

@ProcessAction(name = "agregarInfo")
synchronized public void agregarInfo(
ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {

String id = actionRequest.getParameter("id");
campos.add(new Info(id));
}

@RenderMode(name = "VIEW")
public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
renderRequest.setAttribute("campos", campos);
String pagina = renderRequest.getParameter("jspPage");
if("agregarInfo".equalsIgnoreCase(pagina)){
include("/html/administrador/agregarInfo.jsp", renderRequest, renderResponse);
}else {
include(viewJSP, renderRequest, renderResponse);
}
}

protected void include(
String path, RenderRequest renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {

PortletRequestDispatcher portletRequestDispatcher =
getPortletContext().getRequestDispatcher(path);

if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}

protected String viewJSP;
private static Log _log = LogFactoryUtil.getLog(Administrador.class);
private ArrayList<Info> infos;
}

Code for Info class is ommited because its simple POJO, the equals method for the Info class has been overriden to make the comparison for the method removeInfo.

Please, tell me how should I aggregate these two objects.

Thank you.
Guy deLyonesse
Ranch Hand

Joined: Apr 12, 2011
Posts: 199

Which Liferay version are you using?
Pablo Daniel Mino
Greenhorn

Joined: Jan 02, 2010
Posts: 8
I am using liferay-portal-6.0.5
Guy deLyonesse
Ranch Hand

Joined: Apr 12, 2011
Posts: 199

It looks like your portlet may have too wide a scope. I write my portlets in JSF so I have direct control over the scope of the beans, but I'm not sure how you'd control that in JSP.

If not that, I wonder if your POJO have any static methods or variables?
Pablo Daniel Mino
Greenhorn

Joined: Jan 02, 2010
Posts: 8
The POJO has no static field or method. I believed that the <instanceable> tag would solve my issue, but still there...
Neil Francese
Greenhorn

Joined: Apr 05, 2012
Posts: 1
I am having a similar problem. Have you been able to resolve this?
Pablo Daniel Mino
Greenhorn

Joined: Jan 02, 2010
Posts: 8
Neil Francese wrote:I am having a similar problem. Have you been able to resolve this?


The member object infos declared in one of the last lines "private ArrayList<Info> infos;" is in a shared space!
There is only one instance of the portlet in the container, what uses threads is the methods marked as @ProcessAction. So if a object is stored in the portlet there will be only one reference of that object and many threads modifying it!!!

I solved this by making the @ProcessAction methods access a database.

I hope this help!

Regards

Pablo MiƱo
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Silly problem: One user sees the information of another!
 
Similar Threads
Sending an ArrayList using IBM Portlet Messaging
Portlet modes
how to pass an arraylist from portlet to JSF
iterator in struts 2 using nested ArrayList
Create and open PDF from JSF Portlet