001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.events;
016    
017    import com.liferay.portal.kernel.events.ActionException;
018    import com.liferay.portal.kernel.events.LifecycleAction;
019    import com.liferay.portal.kernel.events.LifecycleEvent;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.InstancePool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.registry.collections.ServiceTrackerCollections;
025    
026    import java.util.Collection;
027    import java.util.HashMap;
028    import java.util.Map;
029    import java.util.concurrent.ConcurrentHashMap;
030    import java.util.concurrent.ConcurrentMap;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpServletResponse;
034    import javax.servlet.http.HttpSession;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Michael Young
039     * @author Raymond Aug??
040     */
041    public class EventsProcessorUtil {
042    
043            public static void process(String key, String[] classes)
044                    throws ActionException {
045    
046                    _instance._process(key, classes, new LifecycleEvent());
047            }
048    
049            public static void process(
050                            String key, String[] classes, HttpServletRequest request,
051                            HttpServletResponse response)
052                    throws ActionException {
053    
054                    _instance._process(key, classes, new LifecycleEvent(request, response));
055            }
056    
057            public static void process(
058                            String key, String[] classes, HttpSession session)
059                    throws ActionException {
060    
061                    _instance._process(key, classes, new LifecycleEvent(session));
062            }
063    
064            public static void process(
065                            String key, String[] classes, LifecycleEvent lifecycleEvent)
066                    throws ActionException {
067    
068                    _instance._process(key, classes, lifecycleEvent);
069            }
070    
071            public static void process(String key, String[] classes, String[] ids)
072                    throws ActionException {
073    
074                    _instance._process(key, classes, new LifecycleEvent(ids));
075            }
076    
077            public static void processEvent(
078                            LifecycleAction lifecycleAction, LifecycleEvent lifecycleEvent)
079                    throws ActionException {
080    
081                    _instance._processEvent(lifecycleAction, lifecycleEvent);
082            }
083    
084            public static void registerEvent(String key, Object event) {
085                    _instance._registerEvent(key, event);
086            }
087    
088            public static void unregisterEvent(String key, Object event) {
089                    _instance._unregisterEvent(key, event);
090            }
091    
092            protected EventsProcessorUtil() {
093            }
094    
095            protected Collection<LifecycleAction> _getLifecycleActions(String key) {
096                    Collection<LifecycleAction> lifecycleActions = _lifecycleActions.get(
097                            key);
098    
099                    if (lifecycleActions == null) {
100                            Map<String, Object> properties = new HashMap<>();
101    
102                            properties.put("key", key);
103    
104                            lifecycleActions = ServiceTrackerCollections.openList(
105                                    LifecycleAction.class, "(key=" + key + ")", properties);
106    
107                            _lifecycleActions.putIfAbsent(key, lifecycleActions);
108                    }
109    
110                    return lifecycleActions;
111            }
112    
113            protected void _process(
114                            String key, String[] classes, LifecycleEvent lifecycleEvent)
115                    throws ActionException {
116    
117                    for (String className : classes) {
118                            if (Validator.isNull(className)) {
119                                    return;
120                            }
121    
122                            if (_log.isDebugEnabled()) {
123                                    _log.debug("Process event " + className);
124                            }
125    
126                            LifecycleAction lifecycleAction = (LifecycleAction)InstancePool.get(
127                                    className);
128    
129                            lifecycleAction.processLifecycleEvent(lifecycleEvent);
130                    }
131    
132                    if (Validator.isNull(key)) {
133                            return;
134                    }
135    
136                    for (LifecycleAction lifecycleAction : _instance._getLifecycleActions(
137                                    key)) {
138    
139                            lifecycleAction.processLifecycleEvent(lifecycleEvent);
140                    }
141            }
142    
143            protected void _processEvent(
144                            LifecycleAction lifecycleAction, LifecycleEvent lifecycleEvent)
145                    throws ActionException {
146    
147                    lifecycleAction.processLifecycleEvent(lifecycleEvent);
148            }
149    
150            protected void _registerEvent(String key, Object event) {
151                    Collection<LifecycleAction> lifecycleActions =
152                            _instance._getLifecycleActions(key);
153    
154                    lifecycleActions.add((LifecycleAction)event);
155            }
156    
157            protected void _unregisterEvent(String key, Object event) {
158                    Collection<LifecycleAction> lifecycleActions =
159                            _instance._getLifecycleActions(key);
160    
161                    lifecycleActions.remove(event);
162            }
163    
164            private static final Log _log = LogFactoryUtil.getLog(
165                    EventsProcessorUtil.class);
166    
167            private static final EventsProcessorUtil _instance =
168                    new EventsProcessorUtil();
169    
170            private final ConcurrentMap<String, Collection<LifecycleAction>>
171                    _lifecycleActions = new ConcurrentHashMap<>();
172    
173    }