Home Liferay How to leverage PortalClassInvoker in Liferay Portlet

How to leverage PortalClassInvoker in Liferay Portlet

Hello Friends,

Sometimes you would require to leverage the existing API of Liferay, which Liferay uses internally, to fulfill custom requirements in our custom portlet. Though it is not recommended to use any of such API call which is available in portal-impl.jar, but we think this is better way rather than creating duplicate codes (by copying the code from Liferay Source and Creating duplicate Class or Method Calls).By choosing experience Liferay Portal Development Company.

Recently we came across a requirement like following,

  • We have to create Liferay User without using existing API methods
  • We have to still generate random password (based on Password Policy) for each user generated from custom portlet.
  • We have to rely on PwdToolkit.java class of Liferay (in portal-impl.jar) to generate the password.

Basically there are two ways we can leverage the calls of such Util classes which are there in portal-impl.jar. We strongly believe that Liferay should put such classes in separate jar and should expose those.

  • Use of PortalClassInvoker, that internally uses reflection to call the methods of Util class.
  • As stated earlier copy the code from Liferay Source and create redundant classes and methods.

For this blog we just use PortalClassInvoker to call methods of utility classes which are not exposed by Liferay.

Example :

Let just use the same example which we have implemented recently. We need to call “generate” method of PwdToolkit.java file which accepts PasswordPolicy as parameter and return String (random password).

Steps :

1) We need to create method key (kind of signature) for every method we want call. This method key will inform PortalClassInvoker that this method has these number of parameters of such types.

Ex :

public static final MethodKey passwordMethodKey = new MethodKey(“com.liferay.portal.security.pwd.PwdToolkitUtil”, “generate”, PasswordPolicy.class);

2) Call invoke method of PortalClassInvoker with proper parameters like this, in our example we needed to get default password policy for password generation, so included that step as well.

Ex :

PasswordPolicy passwordPolicy = PasswordPolicyLocalServiceUtil.getDefaultPasswordPolicy(PortalUtil.getCompanyId(request));

PortalClassInvoker.invoke(false, passwordMethodKey, passwordPolicy);

3) This method will always return type Object, so we need to convert it into Data Type that original method returns. In our case “generate” method return String (random password).

(String) PortalClassInvoker.invoke(false, passwordMethodKey, passwordPolicy);

We hope that this blog will be useful for those who have similar kind of challenge in their project. In case of any question feel free to drop email on info@scalsys.com . We will be happy to help.

All the Best,

ScalSys Team

Share!Share on FacebookShare on Google+Share on LinkedInTweet about this on TwitterFlattr the authorDigg thisPin on PinterestEmail this to someoneShare on StumbleUponShare on RedditShare on TumblrBuffer this pagePrint this page
Copyright © 2017 - Scalsys. All Rights Reserved