Liferay DXP ServiceWrapper HOOK

Liferay DXP ServiceWrapper HOOK tutorial will drive about customization liferay services. Older versions of Liferay provides Liferay ServiceWrapper Hook to customize Liferay Core Services where you need to configure the CustomService implementation in lifeary-hook.xml file, but in Liferay DXP there is no liferay-hook.xml at all, technically there is no HOOK concept in Liferay DXP.

How to Customize Liferay DXP Services?

Liferay DXP leverages and uses OSGI framework with White Board Pattern  to override the Service classes.

Installations:

Liferay DXP Services Customizations:

Let’s create simple activator project and create Service Component clas.

  1. In Eclipse IDE, Create New Module Project of Activator type Liferay DXP Service Wrapper Hook
  2. In this wizard, Just provide Component Class Name and Package Name. Give the proper package name as it reflects in Bundle Symbolic Name in the Manifest file.
  3. Delete the generated Bundle activator class from the package and Bundle-activator reference from bnd.bnd file.
  4.  bnd.bnd file should like below:
    1. Bundle-Name: hook-services
      Bundle-SymbolicName: org.javasavvy.hook.services
      Bundle-Version: 1.0.0
  5. Edit the build.gradle and update with below dependencies. Right click on the project and Go to Gradle and click on Refresh Gradle Project
    1. dependencies {
       compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
       compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
       compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
       compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
       compileOnly group: "jstl", name: "jstl", version: "1.2"
       compileOnly group: "org.osgi", name: "org.osgi.compendium", version: "5.0.0"
       compile group: "org.osgi", name:"org.osgi.service.component.annotations", version:"1.3.0"
      }
  6. Create New java class “CustomUserLocalService” in the package:org.javasavvy.hook.services
  7. CustomUserLocalService.java
    • Add the @Component with immediate to true and service=ServiceWrapper.class.
    • The class will provide implementation by extending UserLocalServiceWrapper.  Like this you can create one more component  to customize Journal LocalService. you can find the list of Services in the package  com.liferay.portal.kernal.service under portal-kernal folder.
    • Add the empty constructor
    • package org.javasavvy.hook.services;
      import java.util.Map;
      import org.osgi.service.component.annotations.Component;
      import com.liferay.portal.kernel.exception.PortalException;
      import com.liferay.portal.kernel.service.ServiceWrapper;
      import com.liferay.portal.kernel.service.UserLocalServiceWrapper;
      
      @Component(
       immediate=true,
       service = ServiceWrapper.class
        )
      public class CustomUserLocalService extends UserLocalServiceWrapper {
            public CustomUserLocalService() {
              super(null);
           }
            @Override
            public int authenticateByEmailAddress(long companyId, String emailAddress, String password,
               Map<String, String[]> headerMap, Map<String, String[]> parameterMap, Map<String, Object> resultsMap)
               throws PortalException {
              System.out.println("Customizing Authenticate by email Address");
              return super.authenticateByEmailAddress(companyId, emailAddress, password, headerMap, parameterMap, resultsMap);
        }
      }
  8. Now go to gradle build task and click on deploy
  9. if you configure liferay tomcat out side of bundles in liferay workspace directory then manually  copy the {liferay-workspace}/bundles/osgi/modules/{your-hook}  to deploy folder of Liferay Server
  10. Your OSGI bundle will be deployed

How to access OSGI Terminal in Liferay IDE?

Right click on tomcat server and click “Open Gogo Shell” and enter command lb or ss to see installed bundles.

Open Gogo Shell in Liferay IDE

 

 

Download Liferay DXP Hook Example Code Here

Leave a Comment

Your email address will not be published. Required fields are marked *