資料 リソース
Liferayは、コミュニティにてテクノロジーをより良く使うために役立つ豊富なリソースと知識を提供しています。
Overriding a Portal Service
All of the functionality provided by Liferay is encapsulated behind a layer of services that is accessed from the frontend layer (the portlets). One of the benefits of this architecture is that it is possible to change how a core portlet of Liferay behaves without changing the portlet itself, customizing the backend services that it uses. This section explains how to do that from a hook plugin.
Liferay automatically generates dummy wrapper classes for all of its services, for example UserLocalServiceWrapper is created as a wrapper of the UserLocalService that is used to add, remove and retrieve user accounts. To modify the functionality of UserLocalService from our hook, all we have to do is create a class that extends from UserLocalServiceWrapper, override some of its methods, and then instruct Liferay to use our class instead of the original.
First, inside example-hook/docroot/WEB-INF/src/com/sample/hook create a new file called MyUserLocalServiceImpl.java with the following content:
package com.sample.hook;
import com.liferay.portal.kernel.exception.PortalException;
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 MyUserLocalServiceImpl extends UserLocalServiceWrapper {
public MyUserLocalServiceImpl(UserLocalService userLocalService) {
super(userLocalService);
}
public User getUserById(long userId)
throws PortalException, SystemException {
System.out.println(
"## MyUserLocalServiceImpl.getUserById(" + userId + ")");
return super.getUserById(userId);
}
}
Tip: Note that the wrapper class (MyUserLocalServiceImpl in this example) will be loaded in the hook's class loader. That means that it will have access to any other class included within the same WAR file, but it won't have access to internal classes of Liferay.
Next, edit liferay-hook.xml inside example-hook/docroot/WEB-INF and add the following after </custom-jsp-dir>:
<service>
<service-type>com.liferay.portal.service.UserLocalService</service-type>
<service-impl>com.sample.hook.MyUserLocalServiceImpl</service-impl>
</service>
Redeploy your hook, then refresh your browser. In the terminal window containing Liferay you should see the messages printed by our hook.
Here are some other services of Liferay that you may need to extend to meet advanced requirements:
OrganizationLocalService: add, delete and retrieve organizations. Also assign users to organizations and retrieve the list of organizations of a given user.
GroupLocalService: add, delete and retrieve communities.
LayoutLocalService: add, delete, retrieve and manage pages of communities, organizations and users.
For a complete list of the services available and the methods of each of them check the javadocs distributed with your version of Liferay.