Liferay Portal 6.0 - Development Guide

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>: