tags:

views:

687

answers:

2

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.

A: 

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?

Arne Burmeister
I'm hosting an Opensocial gadget inside my portlet and need a unique instance ID to differentiate between gadgets for security and persistence purposes. Looks like sync counter it is!
David Citron
+3  A: 

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.

McDowell
Interesting! I believe I looked at getNamespace() and it wasn't unique under JBoss, but now I need to look again.
David Citron
Yup! I was confusing the portlet context name with the namespace. The namespace is, indeed, sufficient. I wonder why I didn't notice it before. Thanks!
David Citron