Anda di halaman 1dari 7

Hooks In Liferay

Liferay Hook Plugin is used to customize liferay portal. Hooks are hot deployable plugins so
when we modify we need not to restart server to apply changes in the portal.
We can modify portal web resources such as JSP , JSPF, CSS, Java Scrip files, Language
properties files, some of portal properties, customize actions, modify portal struts actions,
portlet struts actions, overriding portal services, create servlet filters and create indexer
post processors.
When we create any hook Plugin we need to know one important file called
liferay-hook.xml configuration files.
liferay-hook.xml files consist set of predefined xml tags and there we will defined all

configuration so that portlet container modify the files according to this file.
There are following types of Hooks available in Liferay.
1.
2.
3.
4.
5.
6.
7.

JSP Hooks
Application Adopter Hooks
Portal Properties Hooks
Struts Action Hooks
Portal Service Hooks
Language Properties Hooks
Indexer Post Processor Hooks
1. JSP Hooks

When we have to add more or replace something in JSP, we use JSP Hook.
a) Open the liferay-hook.xml file and add below snippet in it.
<hook>
<custom-jsp-dir>/custom_jsps</custom-jsp-dir>
</hook>

b) Now modify the jsp file you want, some example below
<liferay-util:buffer var="html">
<liferay-util:include page="/html/portlet/blogs/search.portal.jsp" />
</liferay-util:buffer>
<%
html = StringUtil.add(html,

"Didn't find what you were looking for? Refine your search and " +
"try again!", "\n");
%>
<%= html %>

2. Application Adopter Hooks

Application Adapters are special hooks that let you make changes at the site level. They
are used for overriding JSP.
To create an Application Adapter, you need a hook with custom JSPs, and we need to
turn the hooks global custom JSP setting off.
a) We can do this by configuring your liferay-hook.xml with the following directives.
<hook>
<custom-jsp-dir>/custom_jsps</custom-jsp-dir>
<custom-jsp-global>false</custom-jsp-global>
</hook>

When you deploy your hook, Liferay installs the Application Adapter under the name of
the hook. An Application Adapter hook named Foo becomes available to sites and site
templates under the name Foo Hook.
If you override a JSP from the portal, its recommend you include the original JSP (when
possible).
As I already demonstrated above, including the original JSP file for global hooks is
accomplished by referencing the original JSP file from a <liferay-util:include> tag
and appending the suffix .portal.jsp to the original files name. Heres what including
the original Navigation portlets view JSP in a global hook looks like:
b) For Application Adapter hooks, we include the original JSP by setting the
<liferay-util:include> tags useCustomPage attribute to false, as below:
<liferay-util:include
page="/html/portlet/navigation/view.jsp" useCustomPage="false"
/>
<p>
This was modified by the Example Application Adapter.
</p>

c) Deploy your Application Adapter hook plugin.

3. Custom Action Hooks

Hooks are useful for triggering custom actions on common portal events, like user login or
system startup. The actions for each of these events are defined in portal.properties so
we need to extend this file to create a custom action.
a) Create a file called LoginAction.java inside it with this content:
public class LoginAction extends Action {
public void run(HttpServletRequest req, HttpServletResponse res) {
System.out.println("## My custom login action");
}
}
b) Create a portal.properties file inside your hook projects docroot/WEB-INF/src

folder with this content:


login.events.pre=com.liferay.sample.hook.LoginAction
c) Edit your docroot/WEB-INF/liferay-hook.xml file, adding the following line

above <custom-jsp-dir>:
<portal-properties>portal.properties</portal-properties>

d) Redeploy your hook. Once deployment is complete, log out and back in, and you
should see your custom message, ## My custom login action, displayed in the
terminal window running Liferay.
4. Struts Action Hooks

Struts Action Hooks are used to adding or overriding struts action Java classes. We have
portal struts actions and portlet struts actions. Struts Action Hooks can modify the both
struts actions.
a) Insert the following code in liferay-hook.xml
<struts-action>
<struts-action-path>
/portal/Nadeem
</struts-action-path>
<struts-action-impl>
com.liferay.nadeem.hook.action.ExampleStrutsAction
</struts-action-impl>
</struts-action>
<struts-action>
<struts-action-path>
/login/login

</struts-action-path>
<struts-action-impl>
com.liferay.nadeem.hook.action.ExampleStrutsPortletAction
</struts-action-impl>
</struts-action>

b) Create a new package com.liferay.sample.hook.action in your


example-hook/docroot/WEB-INF/src folder.

c) In your new package, create a class named ExampleStrutsPortletAction,which


will wrap the login portlet Struts action. Insert the following code:
public class ExampleStrutsPortletAction extends BaseStrutsPortletAction
{ }

d) Create a new class named ExampleStrutsAction in the


com.liferay.sample.hook.action package.It will implement your new portal
Struts action. Insert the following code:
public class ExampleStrutsAction extends BaseStrutsAction { }

5. Portal Service Hooks

Portal service hooks are used to modify the existed portal services. When we want
override the existing portal services then portal service hooks are best choice.
Lets say we want to modify the User Services then we can use portal service hooks.
Therefore, the best practice is to extend the Liferay Portals base implementations.
For example, if youd like to modify the implementation of the UserLocalService
interface, then extend UserLocalServiceWrapper.If youd like to modify the
SanitizerUtil class, then extend BaseSanitizer.Liferay generates dummy wrapper
classes for all its services. For example, UserLocalServiceWrapper is created as a
wrapper for UserLocalService, a service for adding, removing, and retrieving user
accounts.
To modify the functionality of UserLocalService from our hook, create a class that
extends UserLocalServiceWrapper, override the methods you want to modify, and
instruct Liferay to use your service class instead of the original.
a) Edit liferay-hook.xml located in the example-hook/docroot/WEB-INF directory,

by adding the following after </custom-jsp-dir>:


<service>
<service-type>
com.liferay.portal.service.UserLocalService
</service-type>
<service-impl>
com.liferay.nadeem.hook.MyUserLocalServiceImpl
</service-impl>
</service>

b) Create a new file called MyUserLocalServiceImpl.java inside your


example-hook projects /docroot/WEB-INF/src/com/liferay/nadeem/hook
folder with the following content:
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);
}
}

c) Deploy your hook and refresh your browser. In the terminal window running Liferay,
you should see ## MyUserLocalServiceImpl.getUserById() messages displayed
by your hook.

There are other Liferay services that you may need to extend to meet advanced
requirements:
OrganizationLocalService: Adds, deletes and retrieves organizations. Also assigns
users to organizations and retrieves the list of organizations of a given user.
GroupLocalService: Adds, deletes and retrieves sites.

LayoutLocalService: Adds, deletes, retrieves and manages pages of sites,

organizations and users.

6. Language Properties Hooks

Language Properties Hooks are used to override/modify the existing portal language
properties as well as add new properties.
To do so, create a Language file for the locale of the messages you want to customize,
then refer to your file from your liferay-hook.xml file.
For example, to override the Spanish and French message translations of portals
Language.properties file, create Language files for them in the same directory and
refer to these language files in your liferay-hook.xml file, like this:
<language-properties>content/Language_es.properties</language-properties>
<language-properties>content/Language_fr.properties</language-properties>

7. Indexer Post Processor Hooks

Indexer Post Processor Hooks can modify search summaries, indexes and queries in the
existing portal. We can create new Indexer post processor to create new search
summaries lets say if I want to perform search on all fields of User then we can do it by
this Hooks.
The indexer hook implements a post processing system on top of the existing indexer to
allow plugin hook developers to modify their search, index and query capabilities.
For our example, were going to add Job Title into the User Indexer so we can search
for Users by their Job Title.
a) Open the liferay-hook.xml file and insert the following lines:
<hook>
...
<indexer-post-processor>
<indexer-class-name>
com.liferay.portal.model.User
</indexer-class-name>
<indexer-post-processor-impl>
com.liferay.nadeem.hook.indexer.SampleIndexerPostProcessor
</indexer-post-processor-impl>
</indexer-post-processor>
...
</hook>

b) Create a new class in the


docroot/WEB-INF/src/com/liferay/nadeem/hook/indexer director of your

example-hook named SampleIndexerPostProcessor.Then replace the Java source


files contents with the following lines:
public class SampleIndexerPostProcessor extends BaseIndexerPostProcessor
{
public void postProcessContextQuery(BooleanQuery booleanQuery,
SearchContext searchcontext) throws Exception
{
if(_log.isDebugEnabled())
{
_log.debug(" postProcessContextQuery()");
}
public void postProcessDocument(Document document, Object object)
throws Exception
{
User userEntity = (User) object;
String indexerUserTitle = "";
Try
{
indexerUserTitle = userEntity.getJobTitle();
} catch (Exception e) {}
if(indexerUserTitle.length() > 0)
document.addText(Field.TITLE, indexerUserTitle);
}
public void postProcessFullQuery(BooleanQuery fullQuery, SearchContext
searchcontext) throws Exception
{
if(_log.isDebugEnabled())
_log.debug(" postProcessFullQuery()");
}
public void postProcessSearchQuery(BooleanQuery searchquery,
SearchContext searchcontext) throws Exception
{
if(_log.isDebugEnabled())
_log.debug(" postProcessSearchQuery()");
}
public void postProcessSummary(Summary summary, Document document, Locale
locale, String snippet, PortletURL portletURL)
{
if(_log.isDebugEnabled())
_log.debug("postProcessSummary()");
}
private static Log _log = LogFactoryUtil.getLog
(SampleIndexerPostProcessor.class);
}

Anda mungkin juga menyukai