Abstract :
If you want to get a portlet's init-params at runtime, you may use your com.liferay.portal.service.PortletLocalServiceUtil
to get your portlet.
Then you get an instance of com.liferay.portal.model.Portlet
.
This is a very convenient object as it offers you the possibility to call a handy method :
public java.util.Map<java.lang.String, java.lang.String> getInitParams();
Code Snippets :
In a portlet, a servlet running on your liferay server, a hook running at server startup, a quartz-job, a service ...
May call the following code to obtain a portlet's init-param map
import com.liferay.portal.model.Portlet;
import com.liferay.portal.service.PortletLocalServiceUtil;
import java.lang.String;
import java.util.Map;
...
/* Determine what is your portlet's id, you can go as follows or find it after placing one in a page and check it's id with a tool like firebug */
String myPortletId = "myPortletName_WAR_myPortletWar";
/* Get the actual portlet model object with the appropriate service call */
Portlet myPortletObject PortletLocalServiceUtil.getPortletById(myPortletId);
/* Get the map of init-params from the portlet model object */
Map<String,String> initParamMap = myPortletObject.getInitParams();
/* You can now iterate on your map as on any other */
for (String currentParamKey : initParamMap.keySet()) {
String currentParamValue = initParamMap.get(currentParamKey);
/* do stuff */
}
Principles behind the scene :
In Liferay, a portlet needs to be registered to be usable.
When deployed, the portal registers it, registers it's init-params
and a bunch of other informations.
What's most fortunate about this very fact is that Liferay will thus store all those datas in it's database.
Like pretty much all informations that Liferay stores in database, those informations are accessible through a LiferayService.
Those services are shipped with a bunch of utility methods.
- Some, very basic (like an advanced crud)
- Some more specific to the current service.
In my code example i got my portlet object thanks to a method that search portlets by portletId
. This portletId
is not the database primary key of the table Portlet
.
Hence, Liferay does it's own business to relate the portletId
(in reality, it's name) instead of forcedly asking the primary key which is a numeric meaningless long
.
You may use other methods to get your portlet's object as PortletLocalServiceUtil has many finders available.
I strongly recommend you check the file $LIFERAY_PORTAL_SRC/portal-service/src/com/liferay/portal/service/PortletLocalServiceUtil.java
in any IDE that offers an "outline" view to get a better idea of all it's possibilities.
Conclusion :
Hope this helps ;)