One of the features provided by the portal is the ability to define a properties file inside a custom plugin.
Let's see how to define the file and how to read the values within Liferay 6.2.
It'a all quite simple; first of all we must define the WEB-INF/src/portlet.properties
file where we'll define the needed properties:
my.integer.property=100 my.string.property=Hello my.array.property=1,5,10
After that we create the PortletPropsKeys
interface in which we'll define all the constants that represent the names of the various properties:
public interface PortletPropsKeys { public static final String MY_INTEGER_PROPERTY = "my.integer.property"; public static final String MY_STRING_PROPERTY = "my.string.property"; public static final String MY_LONG_ARRAY_PROPERTY = "my.array.property"; }
Finally goes the PortletPropsValues
class that will provide the access to the properties:
public class PortletPropsValues { public static final int MY_INTEGER_PROPERTY = GetterUtil.getInteger(PortletProps.get(PortletPropsKeys.MY_INTEGER_PROPERTY)); public static final String MY_STRING_PROPERTY = PortletProps.get(PortletPropsKeys.MY_STRING_PROPERTY); public static final long[] MY_LONG_ARRAY_PROPERTY = GetterUtil.getLongValues(PortletProps.getArray(PortletPropsKeys.MY_LONG_ARRAY_PROPERTY)); }
The access to the property goes through the PortletProps
portal class and through the GetterUtil
class to convert the string values into what you need.
At this point you can use the properties at any point in the code, directly through the class PortletPropsValues
:
if(PortletPropsValues.MY_INTEGER_PROPERTY > 50) { ... } if(PortletPropsValues.MY_STRING_PROPERTY.equals("Hello")) { ... } for(long value: PortletPropsValues.MY_LONG_ARRAY_PROPERTY) { ... }