i am using custom porltet in my liferay portal 6.i have some global variable that i want to use through out my portlet classes so for that i have written that variable in portlet.xml file as follows..
<init-param>
<name>Host</name>
<value>smtp.mail.yahoo.com</value>
</init-param>
<init-param>
<name>Port</name>
<value>465</value>
</init-param>
function which working perfect in my portlet action class
publicList<String> ReadSmtpDataForMailNotification() {
List<String> ValuesListObj = new ArrayList<String>();
ValuesListObj.add(this.getInitParameter("Host"));
ValuesListObj.add(this.getInitParameter("Port"));
return ValuesListObj;
}
now problem is that when i excute the function withing the portlet action class then its working perfect but when i want to access this variable outside my portlet class..e.g :- in my local service impl class then i cant access that varible and the value come is always null..so please if anyone can suggest me how can i do get the value of initparam in other then portlet class.
@Advaita Gosvami
have written my.custom.host=liferay.css.com in portlet.properties file
and when i try to fetch value with the following
System.out.println("Property value : - " + PropsUtil.get("my.custom.host"));
its giving me null value..
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();
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 */
}
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.
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.
Hope this helps ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With