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.Registry;
025    import com.liferay.registry.RegistryUtil;
026    import com.liferay.registry.ServiceRegistration;
027    import com.liferay.registry.collections.ServiceTrackerCollections;
028    import com.liferay.registry.collections.ServiceTrackerMap;
029    
030    import java.util.Collection;
031    import java.util.Collections;
032    import java.util.HashMap;
033    import java.util.List;
034    import java.util.Map;
035    import java.util.concurrent.ConcurrentHashMap;
036    import java.util.concurrent.ConcurrentMap;
037    
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    import javax.servlet.http.HttpSession;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Michael Young
045     * @author Raymond Aug??
046     */
047    public class EventsProcessorUtil {
048    
049            public static void process(String key, String[] classes)
050                    throws ActionException {
051    
052                    _instance._process(key, classes, new LifecycleEvent());
053            }
054    
055            public static void process(
056                            String key, String[] classes, HttpServletRequest request,
057                            HttpServletResponse response)
058                    throws ActionException {
059    
060                    _instance._process(key, classes, new LifecycleEvent(request, response));
061            }
062    
063            public static void process(
064                            String key, String[] classes, HttpSession session)
065                    throws ActionException {
066    
067                    _instance._process(key, classes, new LifecycleEvent(session));
068            }
069    
070            public static void process(
071                            String key, String[] classes, LifecycleEvent lifecycleEvent)
072                    throws ActionException {
073    
074                    _instance._process(key, classes, lifecycleEvent);
075            }
076    
077            public static void process(String key, String[] classes, String[] ids)
078                    throws ActionException {
079    
080                    _instance._process(key, classes, new LifecycleEvent(ids));
081            }
082    
083            public static void processEvent(
084                            LifecycleAction lifecycleAction, LifecycleEvent lifecycleEvent)
085                    throws ActionException {
086    
087                    _instance._processEvent(lifecycleAction, lifecycleEvent);
088            }
089    
090            public static void registerEvent(String key, Object event) {
091                    _instance._registerEvent(key, event);
092            }
093    
094            public static void unregisterEvent(String key, Object event) {
095                    _instance._unregisterEvent(key, event);
096            }
097    
098            protected EventsProcessorUtil() {
099            }
100    
101            protected Collection<LifecycleAction> _getLifecycleActions(String key) {
102                    List<LifecycleAction> lifecycleActions = _lifecycleActions.getService(
103                            key);
104    
105                    if (lifecycleActions == null) {
106                            lifecycleActions = Collections.emptyList();
107                    }
108    
109                    return lifecycleActions;
110            }
111    
112            protected void _process(
113                            String key, String[] classes, LifecycleEvent lifecycleEvent)
114                    throws ActionException {
115    
116                    for (String className : classes) {
117                            if (Validator.isNull(className)) {
118                                    return;
119                            }
120    
121                            if (_log.isDebugEnabled()) {
122                                    _log.debug("Process event " + className);
123                            }
124    
125                            LifecycleAction lifecycleAction = (LifecycleAction)InstancePool.get(
126                                    className);
127    
128                            lifecycleAction.processLifecycleEvent(lifecycleEvent);
129                    }
130    
131                    if (Validator.isNull(key)) {
132                            return;
133                    }
134    
135                    for (LifecycleAction lifecycleAction : _instance._getLifecycleActions(
136                                    key)) {
137    
138                            lifecycleAction.processLifecycleEvent(lifecycleEvent);
139                    }
140            }
141    
142            protected void _processEvent(
143                            LifecycleAction lifecycleAction, LifecycleEvent lifecycleEvent)
144                    throws ActionException {
145    
146                    lifecycleAction.processLifecycleEvent(lifecycleEvent);
147            }
148    
149            protected void _registerEvent(String key, Object event) {
150                    Registry registry = RegistryUtil.getRegistry();
151    
152                    Map<String, Object> properties = new HashMap<>();
153    
154                    properties.put("key", key);
155    
156                    ServiceRegistration<LifecycleAction> serviceRegistration =
157                            registry.registerService(
158                                    LifecycleAction.class, (LifecycleAction)event, properties);
159    
160                    Map<Object, ServiceRegistration<LifecycleAction>>
161                            serviceRegistrationMap = _serviceRegistrationMaps.get(key);
162    
163                    if (serviceRegistrationMap == null) {
164                            _serviceRegistrationMaps.putIfAbsent(
165                                    key,
166                                    new ConcurrentHashMap
167                                            <Object, ServiceRegistration<LifecycleAction>>());
168    
169                            serviceRegistrationMap = _serviceRegistrationMaps.get(key);
170                    }
171    
172                    serviceRegistrationMap.put(event, serviceRegistration);
173            }
174    
175            protected void _unregisterEvent(String key, Object event) {
176                    Map<Object, ServiceRegistration<LifecycleAction>>
177                            serviceRegistrationMap = _serviceRegistrationMaps.get(key);
178    
179                    if (serviceRegistrationMap != null) {
180                            ServiceRegistration<LifecycleAction> serviceRegistration =
181                                    serviceRegistrationMap.remove(event);
182    
183                            if (serviceRegistration != null) {
184                                    serviceRegistration.unregister();
185                            }
186    
187                            _serviceRegistrationMaps.remove(key, Collections.emptyList());
188                    }
189            }
190    
191            private static final Log _log = LogFactoryUtil.getLog(
192                    EventsProcessorUtil.class);
193    
194            private static final EventsProcessorUtil _instance =
195                    new EventsProcessorUtil();
196    
197            private final ServiceTrackerMap<String, List<LifecycleAction>>
198                    _lifecycleActions = ServiceTrackerCollections.openMultiValueMap(
199                            LifecycleAction.class, "key");
200            private final
201                    ConcurrentMap<String, Map<Object, ServiceRegistration<LifecycleAction>>>
202                            _serviceRegistrationMaps = new ConcurrentHashMap<>();
203    
204    }