Monday, 20 June 2011

use portal-src/portal-impl class from plugins.

Hello,
        We cannot call portal-impl class of portal-src from plugins . If you use class directly it is deploy successfully but at run-time it gives error that class not found. So here is solution for calling portal-impl class from plugins.

Steps:
1) first make class in portal-ext in any package that you want
 
    Here for example, I'm creating class name as intranetCommon.java in package com.liferay.portal

2) Copy Method (and/or modify method if you want) from portal-src's class that you want to use in plugin
     environment.

    i.e. I want to use PortalLDAPUtil.java's public static long getLdapServerId(long companyId, String
    screenName) method.
         Create method (any name you want) but having return type as Object of that primitive type.
    getLdapServerId(long companyId, String screenName) that is long . So return type of our method is Long.
      
            public static Long getServerId(Long companyId, String screenName) throws Exception  //parameters
                                                                                                           //also in Object of that primitives
   {
return PortalLDAPUtil.getLdapServerId(companyId, screenName); // here you can put entire        
                                                                                                                               // code of
                                                                                                                               //getLdapServerId
                                                                                                                               //and modify if you want.
   }


3) In plugin environment , Write PortalClassInvoker.invoke method.

i.e.     long  serverId=(long) PortalClassInvoker.invoke(
                     "com.liferay.portal.intranetCommon", "getServerId",  new Object[]{companyId, screenName}
                ,false); // call ext environment method using invoke method.

               1st parameter : class path along with className
               2nd parameter : method name
               3rd parameter : object arguments (here we have 2 parameter- companyId, screenName)
               4th parameter : newInstance (true/false)

               Thank You.