0

I have a portlet which is configured as preferences-commpany-wide, so same preferences are shared between everyone

Now I would like to put a default value on those preferences, so this code should be needed only first execution on whole lifecycle. That is why I believe init() method is the best option for setting this default values in case they are not already set before.

How can I retrieve PortletPreferences object from there?

EDIT I am looking for portletContext and portletConfig retrievable from GenericPortlet but no luck so far

Whimusical
  • 6,401
  • 11
  • 62
  • 105

1 Answers1

1

If you really never ever ever ever change the values, so that it's fine to initialize them once and keep them until you restart your server, I'd hardcode them in your portlet.

If there is a slight chance that the config might change (if only initially, after adding a portlet to a page), read them when you need them. And provide defaults in portlet.xml:

<portlet>
    <portlet-name>my-portlet</portlet-name>
    <portlet-class>
        com.example.MyPortlet
    </portlet-class>
    <portlet-preferences>
        <preference>
            <name>my-first-pref</name>
            <value>some default value</value>
        </preference>
    </portlet-preferences>
</portlet>

Remember: if you ever change the portlet prefs, you'd have to restart the server in order to activate them.

If you happen to worry about performance for fetching the preferences: Measure if there's an impact, post the numbers. My bet is that you can gain a lot more performance in other places. And if you absolutely can't gain performance in other places: Congratulations, you have a well tuned system.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • My problem is that writing preferences as init-params is not nice since I need a programtically way of doing it every deploy using information from the web context in realtime (so init is perfect) – Whimusical Aug 15 '12 at 14:05
  • I am sure it's a typo, Olaf, shouldnt be the preferences go into the tag for eg: Name Value – Sharana Aug 15 '12 at 14:07
  • @Sharanbm thanks - stupid copy/paste/edit mistake, left the wrong section in. Corrected. – Olaf Kock Aug 15 '12 at 15:37
  • @user1352530 if you derive the values through other means than actual configuration (where you'd use PortletPreferences for), why bother with PortletPreferences at all? Just derive the values you need, store them somewhere (in this case even member variables might be good). Also, note the previous comments: I've edited the xml. – Olaf Kock Aug 15 '12 at 15:40