Question

I am writing a JSR-168 portlet that can be added to a container multiple times. Each container (Liferay, JBoss, etc.) has its own internal way of differentiating between multiple instantiations of the same portlet.

I, however, would like to uniquely identify my portlet instance inside the doView() method itself.

Is there any standard, JSR-168 mechanism to retrieve some unique identifier that's different for each instance of my portlet? I've seen various solutions where people randomly generate unique IDs and save them in the session, but I'd prefer a standard mechanism if one exists.

Was it helpful?

Solution

Portlet 1.0 (168) provides the RenderResponse.getNamespace() method, which should be unique per portlet instance.

From spec: PLT.12.3.4 Namespace encoding:

The getNamespace method must provide the portlet with a mechanism that ensures the uniqueness of the returned string in the whole portal page. For example, the getNamespace method would return a unique string that could be prefixed to a JavaScript variable name within the content generated by the portlet, ensuring its 5 uniqueness in the whole page. The getNamespace method must return the same value if invoked multiple times within a render request.

If you want to access it in processAction, you'll probably want to store it in the session or as an actionURL parameter.

If upgrading is an option, Portlet 2.0 (286) changes the underlying PortletResponse interface to provide the getNamespace() method and also adds a PortletRequest.getWindowID() method which might be of some use to you.

OTHER TIPS

No, there is no common ID for the instance. I have implemented a portlet container myself, there is no per instance id in the public api - the container has one, of cause. The portlet session (javax.portlet.PortletRequest#getPortletSession()) is unique for one portlet (definition by tag in portlet.xml) and one user (javax.servlet.http.HttpSession), that is not enough for you.

So imho an id generated (can also be a simple (sync) counter in the portletl class) and stored in the portlet session is the only portable way. THe portlet class itself is typically shared beween instances, so the java.lang.System#identityHashCode(Object x) is also useless.

Why do you need it?

I am surprised that this unique ID does not seem to exist as per Ame. The Instance ID can be used for storing all the portlet preferences with in our own database rather than the container provided one. One of the reason we need to store this on our own is the preferences provided by container does not support locale specific preferences.

i.e one portlet instance may have different preferences per locale.

We are trying to use Liferay for our needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top