Liferay Service Hook


Liferay Service Hook

Liferay Service  Hook are used to override methods in the Liferay Service Layer. You can override the all methods defined {EntityName}ServiceWrapper.java classes, but you can not add new methods. You can add methods but those are not accessible through LocalServiceUtil.

As you all know, liferay-hook.xml file holds all hook configuration. below is sample config for Service Wrapper Hook:

<hook>
 <service>
 <service-type>com.liferay.portal.service.{Entity}LocalService</service-type>
 <service-impl>com.javasavvy.hook.services.CustomUserLocalService</service-impl>
 </service>
</hook>
  1. create user-service-hook plugin.
    • In IDE,File -> New -> Liferay Plugin Project
    • Select plugin type as “Hook” and click Finish
      Liferay Service Hook
  2. Create package :  com.javasavvy.hook.services
  3. Create HOOK config:
    • Right click on project, click on  New -> Liferay Hook Configuration
    • Select Services and click NextLiferay Hooks Configuration
    • Click on Add and Select UserLocalService Service Hook tutorial
    • Provide ClassName as CustomUserLocalService and package name Custom User Local Service
  4. Generated liferay-hook.xml file wiil be:
    1. <hook>
       <service>
       <service-type>com.liferay.portal.service.UserLocalService</service-type>
       <service-impl>com.javasavvy.hook.services.CustomUserLocalService</service-impl>
       </service>
      </hook>
  5. open the CustomUserLocalService  and override the method: authenticateByEmailAddress().
    1. public class CustomUserLocalService extends UserLocalServiceWrapper {
       
       
       public CustomUserLocalService(UserLocalService userLocalService) {
       super(userLocalService);
       }
      
       @Override
       public int authenticateByEmailAddress(long companyId, String emailAddress,
       String password, Map<String, String[]> headerMap,
       Map<String, String[]> parameterMap, Map<String, Object> resultsMap)
       throws PortalException, SystemException {
       
       System.out.println("Custom Login Authentication is called");
       return super.authenticateByEmailAddress(companyId, emailAddress, password,
       headerMap, parameterMap, resultsMap);
       }
      }
  6. Now deploy the Hook now

 

Download the Liferay ServiceWrapper Hook