Calling Liferay Portal impl class from custom portlet in Liferay 6.2

Calling Liferay Portal impl class from custom portlet in Liferay 6.2

1. Get the class object of desired portal impl class
Syntax: Class declaringClass = PortalClassLoaderUtil.getClassLoader().loadClass(String fullyQualifiedClassName)
2. Create a method key object by passing the loaded class object, method name, Class objects of method parameter types
Syntax: MethodKey methodKey = new MethodKey(Class<?> declaringClass, String methodName, Class<?>... parameterTypes);

3. Call the PortalClassInvoker and type cast the returned object
Syntax: Object resultObject = PortalClassInvoker.invoke(boolean newInstance, MethodKey methodKey, Object... arguments)

Target: We need to get all portlet categories. This data can be accessed only through Portal Impl class WebAppPool.

In EXT / Hook: You can directly call the method as portal impl classes always available.
Respective code for this is:
Object object = WebAppPool.get(long companyId, ""PORTLET_CATEGORY"");

In Custom Portlet: We can achieve above code in custom portlet like below:

//Loading the WebAppPool class
Class declaringClass = PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.util.WebAppPool");

//Creating method key object. Passed Long, String classes as get method expects takes these as parameters
MethodKey methodKey = new MethodKey(declaringClass,"get",Long.class,String.class);

//Calling invoke method by passing method key object, parameter values. Type casted the returned object to PortletCategory as  got this type object.
PortletCategory portletCategory = (PortletCategory)PortalClassInvoker.invoke(false, methodKey, new Object[] {new Long(themeDisplay.getCompanyId()),"PORTLET_CATEGORY"});

//Retrieving all categories from the result object.
List<PortletCategory> categories = ListUtil.fromCollection(portletCategory.getCategories());

More Blog Entries

Integration of Sakai with Liferay

Sakai configuration: Follow ...


Blogs Settings
Lock Status

Arrange Element

Your changes published Successfully
Web Content Display Settings
Lock Status

Arrange Element