フォーラム

ホーム » Liferay Portal » English » 3. Development

構造的に表示 平面上に表示 ツリー上に表示
スレッド [ 前へ | 次へ ]
toggle
Brian Russell
Multiple Hook Plugins with the same Model Listener
2012/05/17 21:07
答え

Brian Russell

ランク: Junior Member

投稿: 41

参加年月日: 2011/10/31

最近の投稿

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
2012/05/18 6:32
答え

Brian Russell

ランク: Junior Member

投稿: 41

参加年月日: 2011/10/31

最近の投稿

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
2012/09/17 8:37
答え

Brian Russell

ランク: Junior Member

投稿: 41

参加年月日: 2011/10/31

最近の投稿

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
2012/09/18 1:53
答え

Alireza Zare

ランク: Regular Member

投稿: 110

参加年月日: 2010/09/03

最近の投稿

Why not handling them in the same code?
Alireza Zare
RE: Multiple Hook Plugins with the same Model Listener
2012/09/18 3:34
答え

Alireza Zare

ランク: Regular Member

投稿: 110

参加年月日: 2010/09/03

最近の投稿

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
2012/09/18 7:33
答え

Brian Russell

ランク: Junior Member

投稿: 41

参加年月日: 2011/10/31

最近の投稿

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
2013/04/26 5:42
答え

Dave Weitzel

ランク: Regular Member

投稿: 203

参加年月日: 2009/11/18

最近の投稿

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
2013/04/26 5:54
答え

Brian Russell

ランク: Junior Member

投稿: 41

参加年月日: 2011/10/31

最近の投稿

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
2013/07/09 14:19
答え

Ranga Rao Bobbili

ランク: Regular Member

投稿: 152

参加年月日: 2007/07/20

最近の投稿

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