« Back

Minimizing the ext environment

February 19, 2010 By Amos Fong

There has been a lot of work trying to minimize the use of the ext environment to keep your site more maintainable and easier to customize. Here are some tips that has helped me keep my ext small and more maintainable/manageable which will also make upgrades more smooth.

 

Move anything possible in ext to hooks.

1. Do you need to override a *ServiceImpl.java class? (v6.0 or later)

For example, to override the UserLocalServiceImp class put this in your liferay-hook.xml

<hook>
    <service>
        <service-type>com.liferay.portal.service.UserLocalService</service-type>
        <service-impl>com.liferay.testhook.hook.service.impl.TestUserLocalServiceImpl</service-impl>
    </service>
</hook>

Then in your class just extend the UserLocalServiceWrapper and override any methods you want.

public class TestUserLocalServiceImpl extends UserLocalServiceWrapper {

Check out the test-hook portlet located in svn://svn.liferay.com/repos/public/plugins/trunk/portlets/test-hook-portlet/ for more info.

 

2. Do you need to override any classes listed in the liferay-hook.dtd or do you need to add event actions?

For example, do you need to override the ScreennameValidator, ScreennameGenerator, Document library hook, or add postLoginActions, servicePreActions, etc.? These can be overriden through portal.properties in a hook and even allow hot deploy overriding! That means you don't need to restart tomcat to test a new change, you can just redeploy your hook. See the full list in lifeary-hook_6_0_0.dtd. New properties will continually be added so keep an eye for new ways to minimize your ext.

 

Use custom attributes.

1. Do you need to customize user attributes?

Instead of modifying service.xml to add a new column and regenerating all the services, use a custom attribute. There is practically no performance difference, String attributes (only string!) are indexed, and you can do it through the admin GUI. One thing you need to keep in mind is to give view or update permissions apprpriately.

They are even picked up automatically in the create account form! You don't even need to modify any java classes to add a new attribute for registering. Just create a JSP hook and add this easy taglib to the create_account.jsp.

        <liferay-ui:custom-attribute
            className="com.liferay.portal.model.User"
            classPK="<%= 0 %>"
            editable="<%= true %>"
            label="<%= true %>"
            name="favoriteColor"
        />

 In the action class, it will pick any custom attributes and update them accordingly. The same is true for most all other models too (ie Documents, Images, Web content).

 

Extend rather than override.

1. Do you need to override any *Action.java classes?

Any classes in struts-config.xml or liferay-portlet.xml should be extended rather than overrided. This will hopefully make upgrading less painfulby keeping the current code and just adding your own or tweaking variables so you get the behaviour you want.

For example, say you created a custom attribute "Favorite Color" and you want to make it required to be filled out.


<struts-config>
    <action-mappings>
        <action path="/login/create_account" type="com.liferay.test.ext.portlet.login.action.TestCreateAccountAction">
            <forward name="portlet.login.create_account" path="portlet.login.create_account" />
        </action>
</struts-config>

Then in your class:

public class TestCreateAccountAction extends CreateAccountAction {
    protected void addUser(ActionRequest actionRequest, ActionResponse actionResponse)
        throws Exception {

        Map<String, Serializable> expandoBridgeAttributes =
            PortalUtil.getExpandoBridgeAttributes(
                ExpandoBridgeFactoryUtil.getExpandoBridge(User.class.getName()), actionRequest);

         String favoriteColor = (String)expandoBridgeAttributes.get("favoriteColor");

         if (Validator.isNull(favoriteColor)) {
                throw new RequiredFieldException("favoriteColor", "favoriteColorLabel"); // v6.0/EE specific code
         
         }

        super.addUser(actionRequest, actionResponse);  // Don't touch current code to make upgrades painless =)
    }
}

 

2. Do you need to override JSPs?

Use a JSP hook. (See www.liferay.com/community/wiki/-/wiki/Main/Portal+Hook+Plugins)

And use the buffer util taglib to achieve a similar affect to what I did with CreateAccountAction.java class above. Let's say we want to remove the javascript on the bottom, change the word "save" to "create" and add the custom attribute favorite color to the registration form after the last name.

 

<%@ include file="/html/portlet/login/init.jsp" %>

<liferay-util:buffer var="html">
    <liferay-util:include page="/html/portlet/login/create_account.portal.jsp" />
</liferay-util:buffer>

<liferay-util:buffer var="customHtml">
   <liferay-ui:custom-attribute
        className="com.liferay.portal.model.User"
        classPK="<%= 0 %>"
        editable="<%= true %>"
        label="<%= true %>"
        name="favoriteColor"
    />
</liferay-util:buffer>

<%
int x = html.lastIndexOf("<script type=\"text/javascript\"");

if (x != -1) {
    y = html.indexOf("</script>", x);

    html = html.substring(0, x) + html.substring(y + 9);
}

html = html.replace(LanguageUtil.get(pageContext, "save"), LanguageUtil.get(pageContext, "create"));

x = html.indexOf(LanguageUtil.get(pageContext, "last-name"));

if (x != -1) {
    y = html.indexOf("</div>", x);

    html = html.substring(0, y) + customHtml + html.substring(y);
}
%>

<%= html %>

 

Conclusion

Hopefully that was useful to you. Of course, this isn't always possible and you'll have to resort to direct overwriting. But I hope this can minimize the pain of upgrading for some people.

If there are commonly overrided classes, let's see if we can figure out ways to make it easier, make it possible to use a technique mentioned above, or be moved to a hook =). Or...contribute to trunk if it's useful, that way we maintain it for you and others also benefit from it.

If you guys have any other methods you like to use or if I missed anything, please leave a comment.

Showing 46 Comments

Brian Chan
2/19/10 11:38 PM

Thanks for writing this Amos emoticon

Jonas Yuan
2/20/10 2:17 PM

Very nice. Thank you, Amos.

Julio Camarero
2/21/10 3:24 PM

Great summary Amos, this is very helpful!

Tina Agrawal
2/21/10 10:25 PM

Really helpful!!

Bavithra Rajendran
2/22/10 10:52 PM

Sir, Thanks for this helpful feature emoticon

Gwenaël Gourevich
2/23/10 12:38 AM

That is very useful ... thank you!

Shagul Khajamohideen
2/23/10 9:26 AM

Nice job Amos.

Jignesh Vachhani
2/25/10 1:40 AM

Really helpful amos,

Thanks

Joao Sequeira
3/3/10 4:44 AM

Very nice article emoticon

I was trying to use your example but the class "UserLocalServiceWrapper" or "UserWrapper" are not on the liferay distribution (5.2.3), only on SVN, is it possible to use this with the official distribution?

Best Regards,
Joao Sequeira

Anand Barhate
3/9/10 12:27 AM

Hi Amos,
Thanks for the article I have couple of questions. I am using 5.2.2 in the first half it says about using plugin for extending the user attributes. When I added that in hooks I am getting error about service tags.
Secondly in later part it talks about changing struts xml. Does it need to be done in plugin/hooks?
Please let me know.
Thanks

Amos Fong
3/9/10 9:52 AM

Hi Joao,

Sorry I didn't specify versions. Number 1 is only available in 6.0, 5.2 EE SP 2, 5.1 EE SP 5 or later.

Amos Fong
3/9/10 9:54 AM

Hi Anand,

Making changes to struts-config.xml has to be done in ext environment.

Peter Pastrnak
6/12/10 9:27 AM

Nice article, thank you.

I would like to ask you, how is possible to use the extended User object (TestUserImpl) from the test-hook example in a JSP code - f.e. in the portlet/blogs/view.jsp.
It seems the JSP compiler does not see the class, so I cannot import it:

<%@ page import="com.liferay.testhook.hook.model.impl.TestUserImpl" %>

Only a type can be imported. com.liferay.testhook.hook.model.impl.TestUserImpl resolves to a package

Is there an easy way, how to do it?

Thanks.

Alessandro Cosenza
7/22/10 7:03 AM

Hello
if I want to override the AssetEntryLocalServiceImpl.updateEntry method in a hook, what I have to do?

Extending AssetEntryLocalServiceWrapper seems to be very tricky, because I have to substitute all the *persistence variables with corresponding *LocalServiceUtil class. And it still doesn't work, because how can I replace assetEntryPersistence.setAssetCategories? Does anyone help me?

Brian Chan
7/22/10 2:09 PM

See svn://svn.liferay.com/repos/public/plugins/trunk

There's one called /hooks/sample-wrapper-hook

We'll be releasing that soon.

Fernando O
8/12/10 9:09 AM

Hi!

I'm triying to override a method in com.liferay.portal.service.LayoutLocalService.

I use this code in liferay-hook.xml:

<service>
<service-type>com.liferay.portal.service.LayoutLocalSe­rvice</service-type>
<service-impl>com.prueba.portal.service.MyLayoutLocalService­</service-impl>
</service>

and override the method in this class (MyLayoutLocalService) (it extends LayoutLocalServiceWrapper).

But when I deploy the hook and use the method, it doesn't use the new method and stills using the old one.

How can I made this work? Where could I find lifeary-hook_6_0_0.dtd?

Kind regards! and thank you for the blog entry!

Fernando O
8/16/10 6:48 AM

Finally I found the problem, there were some code that was executed before tue function I wanted to override, once I remove this wrong code everithing works as expected.

Prasanna Kumar
9/14/10 3:57 AM

Hi All,

I need some solution on user management module.

When we create new user in liferay it will go to user_ table in lportal database. But our requirement is we want to store the userdetails to be stored in our database as well.

I mean.. When we create new user, details should be stored in both the database (user_ table in lportal database and user table in our database).

Can anybody suggest where to write the code to do above task?
we are working in ext environment...

Thanks in advance....

Amos Fong
9/14/10 9:45 AM

Hi Prasana,

There a couple ways to do this, the best way I can think of is to create a model listener for add user and update user that will update your own database.

Prasanna Kumar
9/14/10 10:35 AM

Amos Fong, Thanks for your reply...

I am very new to liferay Can you explain in detail?

Amos Fong
9/14/10 11:05 AM

There is a short tutorial in this wiki:

http://www.liferay.com/community/wiki/-/wiki/Main/Portal+Hook+Plugins

And there is a sample test hook here that has an example of a model listener:

http://svn.liferay.com/repos/public/plugins/trunk/portlets/test-hook-po­rtlet/

user: guest, no password

Prasanna Kumar
9/14/10 11:18 AM

Thanks Amos...

We are using openSSO ... we need to sync openSSO data with liferay data. Can you suggest some solution?

prakash harigopal
10/7/10 9:56 AM

Hi Amos,
Can you give me some instructions to implement the custom services(which connects to a different database) in liferay action classes. Plz let me know the way to handle creating services through service builder and Spring and Hibernate configurations in the plugin hook.

thanks
prakash

Jakub Liska
10/18/10 10:37 AM

Hi Amos,
regarding the hint about overriding classes listed in liferay-hook.dtd, I suppose we can't change for example " login.events.post=com.liferay.portal.events.LoginPostAction " to " login.events.post=com.liferay.portal.events.MyOwnLoginPostAction "

in other words, we can override the properties, but the new classes that we need to use instead, like MyOwnLoginPostAction, must be added via ext, right? Overriding portal-impl or portal-service classes, can be done only via ext, except of the custom services that can be overridden by hook.

Jakub Liska
10/18/10 11:07 AM

I got it, we can do it. It's like a list of classes that can be overridden no matter if they are portal-impl. It's good to know, thank you Amos

Deepak Kenchamba
10/26/10 1:30 AM

Hi Team,

You need to edit the "struts-config-ext.xml" file to point to your custom Action class.
Assuming Liferay is deployed in "D:\liferay-portal-6.0.5" folder. A blank "struts-config-ext.xml" can be found at
D:\liferay-portal-6.0.5\tomcat-6.0.26\webapps\ROOT\WEB-INF\ folder
<action path="/login/create_account" type="ae.rak.hook.portlet.login.action.MyCreateAccountAction">
<forward name="portlet.login.create_account" path="portlet.login.create_account" />
</action>

Next add the custom action class into the classes folder of the portal.
D:\liferay-portal-6.0.5\tomcat-6.0.26\webapps\ROOT\WEB-INF\classes

It is easy to compile your custom Action java file.
1. Install Liferay IDE
2. Create a new 'Liferay Hook Plug-in Project'
3. Create your custom Action java file & compile
The IDE provides all help in compiling your .java file to a .class file

Sandeep Nair
10/28/10 11:30 PM

In sample wrapper user of type SampleUserImpl is returned. How can it be accessed from other class??

Brahim TARNAOUI
11/8/10 3:59 AM

Hello,

i use a hook to extend a jsp page and i will use in my jsp a code like this <%@page import="my.packahe.my.classes"%>
where can'i put my jar file?

Peter Davison
11/10/10 6:41 AM

Hi.

Does anyone know if it is possible to override a workflow handler via the hook mechanism, or does that need to be done via ext?

Thanks,
Pete

prakash harigopal
11/10/10 8:59 AM

Hi Peter,
If i listen correctly , you want to override the default workflow thats available in liferay. You can override that by selecting the workflow from controlpanel and paste the XML document thats available in Kaleo-web\web-inf\src\meta-inf\definitions\single-approver.xml. Liferay by default provided single approver workflow , you can override how ever you want by adding multiple tasks and transitions.

And you can add a new workflow using controlpanel also , just go to workflow and click on add and upload the xml document to that ....

thanks
Prakash.

Peter Davison
11/10/10 10:54 AM

Thanks for your reply. Yeah, I've created my own workflow which is essentially the single approver workflow with an additional step added when the workflow is approved. I want to send an email notification out to all interested users that a new piece of content is available in the portal. In this case it happens to be a new document library document.

I want to include in the email a link to the new document. I'm struggling trying to build that URL given the data that is available in the velocity context that is available at the point that the email is generated.

I'd like to override the code that creates the starts the workflow so that the URL to the document (as well as some other data like the list of roles that have access to the folder in which the document lives) is pre-populated in the workflow context. The code I'd like to override is the DLFileEntryWorkflowHandler. This seemed like a good place to build the URL to the newly uploaded document given that the DLFileEntry is passed into the startWorkflowInstance method. The default behaviour of this method (inherited from BaseWorkflowHandler) is to ignore the the model object passed in. I'd like to override that method to do something useful like build the URL to the document and stick it in the workflowContext. I'm attempting to do this via the ext method of overriding but if it is at all possible to do this via a hook I would prefer that approach.

Pete

Henrique Fernandes
1/21/11 7:52 AM

Hi Deepak,

Is there any way of doing this using the deploy process of a Hook Plug-in?

Luis Rodríguez Fernández
3/9/11 12:01 AM

Thanks Amos, great post, really useful!!!

1. Do you need to override any *Action.java classes?

For doing this couldn't you create a hook pluging?

Thanks in advance,

Luis

Luis Rodríguez Fernández
3/9/11 12:06 AM

Hi Henrique,

I have tried overriding the action class with a hook plugin, but it seems to be problems with the classloader: http://www.liferay.com/community/forums/-/message_boards/message/7817764#_19_mes­sage_7817764

I am afraid that the EXT approach is going to be the best solution...

Hope it helps,

Luis

Nguyen Trung Kien
3/17/11 10:35 AM

Hi Amos
If I define new method in TestUserLocalServiceImpl, can I use it in other portlet made by Liferay Plugin SDK?
Can I use hook plugin to define new method in userService?
Thanks.

Amos Fong
3/21/11 8:37 PM

No you cannot define a new method using a hook because it doesn't override the Util class, only the Impl class. You should use ext to define a new method in a Liferay Service.
But it actually might be better to create your own service/class so there's better separation between your code and Liferay's.

5/23/11 2:06 PM

[...] Shahin Ali: Can i change the sorting order of message thread posts in the message boards to 'last posted first' present order is first posted first using liferay 6 EE Thanks. Try a hook on the... [...] Read More

Jakub Liska
8/25/11 6:15 AM

I'm wondering what is the best way to implement custom Store. I'd need an alternative to JcrStore. I'm not using JackRabbit but ModeShape. And I'm wondering if I should use Hook or Ext.

Jakub Liska
8/25/11 9:48 AM

I implemented it with Ext, no other means possible I think...

Amos Fong
8/25/11 7:19 PM

@Jakub, do you mean another Document Library store using this property, (dl.store.impl)? I know it used to be possible and I think it should still be. Did you try specifying that property in your hook's portal.properties and making your store extend BaseStore?

Rajesh Chaurasia
9/18/11 7:41 AM

I want to extend the EditPagesAction class of Lifreay I want to add a tab in look and feel section of edit_pages_look_and_feel.jsp.I want to know how I Can use hook here .

public class NPPEditPagesAction extends EditPagesAction {

public void processAction(ActionMapping mapping, ActionForm form,
PortletConfig portletConfig, ActionRequest actionRequest,
ActionResponse actionResponse) throws Exception {

System.out.println("Inside processAction...createTheme()");
String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

try {
if (cmd.equals("createtheme")) {
createTheme(actionRequest);
}

super­.processAction(mapping, form, portletConfig, actionRequest,actionResponse);

} catch (Exception e) {

}
}

public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
RenderRequest renderRequest, RenderResponse renderResponse)
throws Exception {

return super.render(mapping, form, portletConfig, renderRequest, renderResponse);
//return mapping.findForward(getForward(renderRequest, "portlet.communities.edit_pages"));

}

@SuppressWarnings("static-access")
publ­ic static void createTheme(ActionRequest actionRequest) {

System.out.println("Inside createTheme...");
ThemeUtil themeUtil = new ThemeUtil();
final String themeZipFilePath = ThemeUtil.SAMPLE_THEMES_PATH;
final String themeConfigFilePath = ThemeUtil.THEMES_CONFIG_PATH;
try{
//To Modify liferay-look-and-feel.xml
themeUtil.getXMLDetails(themeConfigFilePath);
­
//To Extract the sample themes zip folder to tomcat location
themeUtil.extractFolder(themeZipFilePath);

} catch (Exception ex) {
ex.printStackTrace();
}

}

<custom-jsp-dir>/WEB-INF</custom-jsp-dir>
<­struts-action>
<struts-action-path>/layout_management/edit_pages</struts-actio­n-path>
<struts-action-impl>com.liferay.portlet.communities.action.NPPEditPage­sAction</struts-action-impl>
</struts-action>

I am unable to execute my java code.please let me know where I am going wrong

Amos Fong
9/18/11 11:51 PM

Hi Rajesh,

You cannot override struts action in a hook. You'll need to use the ext-plugin/environment to do that.

Brian Chan
9/19/11 10:47 AM

No, you can now.

See http://www.liferay.com/web/mika.koivisto/blog/-/blogs/overriding-and-adding-stru­ts-actions-from-hook-plugins

10/17/11 9:02 AM

[...] The below blog may help on how to extend a services via hook. http://www.liferay.com/web/amos.fong/blog/-/blogs/minimizing-the-ext-environment­ Mark as an Answer [...] Read More

Suraj Bihari
11/15/11 1:41 PM

Thanks Amos!

Abu Muhammad
11/24/11 1:12 AM

Hi Amos, how to make user can post comment ? In my liferay, error "Blogs in temporarily unavailable" when submit comment.