Wrapper Plugins

タグ: development hook

How to use Liferay's Wrapper Plugins#

If you want to extend functionallity of the core classes, there's has been (since Liferay 5.1) an improvement in build-service that creates a wrapper for all models.

(You can search Wrapper in the code and you'll realize that there are wrapper classes for all the portal models)

Note: I'll use a example that extends JournalArticle deletion to be more illustrative.

The steps to do to use this functionallity are the following:

  • Create a file called liferay-hook.xml with the following content and place it docroot/WEB-INF:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 5.2.5//EN" "http://www.liferay.com/dtd/liferay-hook_5_2_0.dtd">
 
<hook>
 <service>
 <service-type>com.liferay.portlet.journal.service.JournalArticleLocalService</service-type>
 <service-impl>com.liferay.portlet.asset.service.impl.JournalArticleLocalServiceWrapperImpl</service-impl>
 </service>
</hook>

The first class is the service you want to extend The second class is the one that implements the modifications

  • Create a class that extends the wrapper you want to use and add the basic constructor (in my case it is JournalArticleLocalServiceWrapper):
public class JournalArticleLocalServiceWrapperImpl
 extends JournalArticleLocalServiceWrapper{

 public JournalArticleLocalServiceWrapperImpl(
      JournalArticleLocalService journalArticleLocalService){
 super(journalArticleLocalService);
   }
}  
  • Add the methods you want to overwrite in that class:
 public void deleteArticle(long groupId, java.lang.String articleId,
 double version, java.lang.String articleURL,
 com.liferay.portal.service.ServiceContext serviceContext)
 throws com.liferay.portal.PortalException,
 com.liferay.portal.SystemException {
 
                 [... all my custom code here...]
 
 super.deleteArticle(article, articleURL, serviceContext);
 }

And that's all: you deploy your portlet/plugin and you'll see it in action!

By the way, you have a great example by Brian Chan in the test-hook-portlet

0 添付ファイル
29080 参照数
平均 (0 投票)
平均評価は0.0星中の5です。
コメント
コメント 作成者 日時
I am following exactly that, but when I deploy... Waseem Zawaideh 2010/08/17 3:49
Sorry Guys, My bad I was overriding the... Waseem Zawaideh 2010/08/17 4:02

I am following exactly that, but when I deploy the hook nothing seems to change, even in the debug mode my overridden methods are not referenced at all.
投稿日時:10/08/17 3:49
Sorry Guys, My bad I was overriding the incorrect methods, It works perfectly well.
Waseem Zawaidehへのコメント。投稿日時:10/08/17 4:02