Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Brian Russell
Multiple Hook Plugins with the same Model Listener
May 17, 2012 9:07 PM
Answer

Brian Russell

Rank: Junior Member

Posts: 39

Join Date: October 31, 2011

Recent Posts

I've searched through all of the documentation and message board posts I can find, but I have not found anything that addresses this question:

Is it possible to have more than one Hook Plugin deployed, which references the same value object listener model object?

My example is that I have 3 separate hook plugins, each needing to know when a new company is created, so that they will fire off their startup actions. I ended up having to use the Group listener, but regardless of what I am listening to, the issue is that only one of the listeners seems to get registered at a time (seemingly the last hook plugin loaded)

The portal.properties for the hooks have the following relevant properties set:
Hook 1
1application.startup.events=com.closerware.websitesocial.portal.events.CwCalendarAppStartupAction
2value.object.listener.com.liferay.portal.model.Group=com.closerware.websitesocial.portal.model.listeners.CalendarGroupListener

Hook 2
1application.startup.events=com.closerware.websitesocial.portal.events.CwPermissionAppStartupAction
2value.object.listener.com.liferay.portal.model.Group=com.closerware.websitesocial.portal.model.listeners.PermissionGroupListener

Hook 3
1application.startup.events=com.closerware.websitesocial.portal.events.CwShoppingAppStartupAction
2value.object.listener.com.liferay.portal.model.Group=com.closerware.websitesocial.portal.model.listeners.ShoppingGroupListener

The key here is that the listener classes all extend BaseModelListener<Group> to utilize the onAfterCreate(Group group) method, which fires off the startup events. The startup actions all work correctly during server startup. But only Hook 3's startup event is triggered upon creation of a new Group. So, it is probably all too obvious, but it seems that the last plugin to be loaded (Hook 3) is the only one actually having the value.object.listener property set for its Model Listener, as each hook likely overwrites the value.object.listener.com.liferay.portal.model.Group property of the previous hook.

The obvious solution is to combine the hooks into one and run all of the startup events from a single listener, but I'm trying to maintain a separation of interests in order to modularize the code.

Am I missing something here with declaring the value.object.listener properties, so that they do not overwrite each other? Or is it only possible to declare a single value.object.listener property per model object globally across all plugins?

Our Platform Setup
Liferay 6.1.0 CE GA1 (20120106155615760)
Tomcat 7.0.23
MySQL 5.5.20
Java 1.6.0_31
Brian Russell
RE: Multiple Hook Plugins with the same Model Listener
May 18, 2012 6:32 AM
Answer

Brian Russell

Rank: Junior Member

Posts: 39

Join Date: October 31, 2011

Recent Posts

Oh, and just to add some more evidence that the properties overwrite each other. If I undeploy Hook 3, then the listener for Hook 2 is the only one that fires. And so on, down the line.
Brian Russell
RE: Multiple Hook Plugins with the same Model Listener
September 17, 2012 8:37 AM
Answer

Brian Russell

Rank: Junior Member

Posts: 39

Join Date: October 31, 2011

Recent Posts

Is there anyone who has experienced the same issue and come up with a solution? Or any advice out there from the community?

This is still an issue with my hooks, where I have a number of different hooks which require something to fire on Group creation, but as it is, only one is fired.
Alireza Zare
RE: Multiple Hook Plugins with the same Model Listener
September 18, 2012 1:53 AM
Answer

Alireza Zare

Rank: Regular Member

Posts: 110

Join Date: September 3, 2010

Recent Posts

Why not handling them in the same code?
Alireza Zare
RE: Multiple Hook Plugins with the same Model Listener
September 18, 2012 3:34 AM
Answer

Alireza Zare

Rank: Regular Member

Posts: 110

Join Date: September 3, 2010

Recent Posts

for modularization purpose you can just use the same interface and do the modularization in your implementation.
Brian Russell
RE: Multiple Hook Plugins with the same Model Listener
September 18, 2012 7:33 AM
Answer

Brian Russell

Rank: Junior Member

Posts: 39

Join Date: October 31, 2011

Recent Posts

I agree that I can just use one hook and put all in one code. But I shouldn't *have* to. One of the advantages of separating my customizations into separate hooks is that I can add/remove those customizations as needed. Each hook is focused on different functionality (i.e. shopping portlet, message boards, dockbar, permissions, etc.)

In looking at the code where the listeners are initialized, it would seem that they could simply be added to the list, rather than overwrite the list.

Here's a little chunk of that code where the listener is set for the Group model object from com.liferay.portal.service.persistence.GroupPersistenceImpl:
 1   
 2        public void afterPropertiesSet() {
 3        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
 4                    com.liferay.portal.util.PropsUtil.get(
 5                        "value.object.listener.com.liferay.portal.model.Group")));
 6
 7        if (listenerClassNames.length > 0) {
 8            try {
 9                List<ModelListener<Group>> listenersList = new ArrayList<ModelListener<Group>>();
10
11                for (String listenerClassName : listenerClassNames) {
12                    listenersList.add((ModelListener<Group>)InstanceFactory.newInstance(
13                            listenerClassName));
14                }
15
16                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
17            }
18            catch (Exception e) {
19                _log.error(e);
20            }
21        }

So each time it is reinitializing the "listeners" array, which is an instance variable.

This also means that the base listeners will then no longer be called if you were to add a listener for say, User. If I set up, in my hook properties file, a value object listener for User, it would overwrite the call to com.liferay.portal.model.UserListener

So the base portal.properties has:
 1   
 2    value.object.listener.com.liferay.portal.model.Contact=com.liferay.portal.model.ContactListener
 3    value.object.listener.com.liferay.portal.model.Layout=com.liferay.portal.model.LayoutListener,com.liferay.portal.service.impl.LayoutSetPrototypeLayoutListener
 4    value.object.listener.com.liferay.portal.model.LayoutSet=com.liferay.portal.model.LayoutSetListener,com.liferay.portal.service.impl.LayoutSetPrototypeLayoutSetListener
 5    value.object.listener.com.liferay.portal.model.PortletPreferences=com.liferay.portal.model.PortletPreferencesListener
 6    value.object.listener.com.liferay.portal.model.User=com.liferay.portal.model.UserListener
 7    value.object.listener.com.liferay.portal.model.UserGroup=com.liferay.portal.model.UserGroupListener
 8    value.object.listener.com.liferay.portlet.journal.model.JournalArticle=com.liferay.portlet.journal.model.JournalArticleListener
 9    value.object.listener.com.liferay.portlet.journal.model.JournalTemplate=com.liferay.portlet.journal.model.JournalTemplateListener

If in your hook portal.properties you were to add your own listener to any of these model objects, you'd lose the listeners listed above. That can't be right can it? But I suppose it's possible that it would be exactly what someone would want, in order to override the base listeners.
Dave Weitzel
RE: Multiple Hook Plugins with the same Model Listener
April 26, 2013 5:42 AM
Answer

Dave Weitzel

Rank: Regular Member

Posts: 169

Join Date: November 18, 2009

Recent Posts

Did you ever find an answer to this problem?
I may be hitting the same issue (or at least "deleting" the standard Liferay Listener capability by writing my own, and with Liferay's base listener dependding on portal-impl I may not be able to clone it fully.
Brian Russell
RE: Multiple Hook Plugins with the same Model Listener
April 26, 2013 5:54 AM
Answer

Brian Russell

Rank: Junior Member

Posts: 39

Join Date: October 31, 2011

Recent Posts

Unfortunately Dave, I ended up just giving in and combining all of my hooks into one, which includes all of my code updating various modules. So by doing this, it cleared up this issue of hook 1 listener being overwritten by hook 2 listener. So that is the only solution I can offer you.

Though I do believe the base liferay listeners are still intact when doing this.
Ranga Rao Bobbili
RE: Multiple Hook Plugins with the same Model Listener
July 9, 2013 2:19 PM
Answer

Ranga Rao Bobbili

Rank: Regular Member

Posts: 148

Join Date: July 20, 2007

Recent Posts

Hi,

I am also facing similar kind of issue while loading portal properties from hook(overriding portal.properties) and ext environment(portal-ext.properties).

Basically session timeout related properties are not loading when i use properties hook and ext environment(portal-ext.properties).

Thanks in advance.......

Thanks,
Ranga Rao Bobbili
Adaequare INC