Suppose there is a need that I want to add a new method/behavior or change the behavior of existing services then Service Wrapper hook is needed. For this liferay provides a Wrapper class for each service interface.By extending this Service Wrapper we can change / add the functionality for existing service. Lets see how to to this.
1. Create a new hook
2. Create one class which extends wrapper class of interface which we are going to change existing one/adding new API.
3. Now open a liferay-hook.xml || select Service Wrappers in the left panel || Click on Add a Service Wrapper
4. Click on Add a Service Wrapper then you will get following screen where
Service Type : The existing Liferay service that the Service Impl will wrap
Service Impl : The custom class which extends Wrapper Class
While giving Service Impl value
When you save the file automatically respective xml file tags and its values will be created in the source of this file.You can see source of this file by clicking on the Source tab as follows
5. Now its time to add the new behavior to the existing service by writing the code in our custom class as we created previously.
Its upto you which method you want to override. You can see all the existing methods by going to eclipse menu option Source || Override/Implement Methods, You will get dialogue box where you can select method which you are going to override.
Use-Case:
Here i am overriding the addUser(User user) method.Requirement is while adding the user in liferay i want to create the user in other platforms also like in siebel/CRM. So for that one before adding the user to the Liferay in this i am calling the web service to the siebel server to create user in its platform.
6. Now build the hook and deploy it on server.
Enjoy!!!
1. Create a new hook
2. Create one class which extends wrapper class of interface which we are going to change existing one/adding new API.
import com.liferay.portal.service.UserLocalService; import com.liferay.portal.service.UserLocalServiceWrapper; public class CustomUserLocalServiceImpl extends UserLocalServiceWrapper{ public CustomUserLocalServiceImpl(UserLocalService userLocalService) { super(userLocalService); // TODO Auto-generated constructor stub } }
3. Now open a liferay-hook.xml || select Service Wrappers in the left panel || Click on Add a Service Wrapper
4. Click on Add a Service Wrapper then you will get following screen where
Service Type : The existing Liferay service that the Service Impl will wrap
Service Impl : The custom class which extends Wrapper Class
While giving Service Impl value
- If the class already created click browse button and select custom class
- Otherwise directly give the class name and click on create button then automatically class will be created.
When you save the file automatically respective xml file tags and its values will be created in the source of this file.You can see source of this file by clicking on the Source tab as follows
Here CustomLocalServiceImpl class is the extension to the UserLocalService wrapper class.com.liferay.portal.service.UserLocalService com.liferaygnosis.impl.CustomUserLocalServiceImpl
5. Now its time to add the new behavior to the existing service by writing the code in our custom class as we created previously.
Its upto you which method you want to override. You can see all the existing methods by going to eclipse menu option Source || Override/Implement Methods, You will get dialogue box where you can select method which you are going to override.
Use-Case:
Here i am overriding the addUser(User user) method.Requirement is while adding the user in liferay i want to create the user in other platforms also like in siebel/CRM. So for that one before adding the user to the Liferay in this i am calling the web service to the siebel server to create user in its platform.
package com.liferaygnosis.impl; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.model.User; import com.liferay.portal.service.UserLocalService; import com.liferay.portal.service.UserLocalServiceWrapper; public class CustomUserLocalServiceImpl extends UserLocalServiceWrapper{ public CustomUserLocalServiceImpl(UserLocalService userLocalService) { super(userLocalService); // TODO Auto-generated constructor stub } @Override public User addUser(User user) throws SystemException { /*My custom code to add the user to the siebel */ SiebelProgramclient.createUser(user); return super.addUser(user); } }
6. Now build the hook and deploy it on server.
Enjoy!!!