1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.events;
24  
25  import com.liferay.portal.kernel.events.Action;
26  import com.liferay.portal.kernel.events.ActionException;
27  import com.liferay.portal.kernel.events.SessionAction;
28  import com.liferay.portal.kernel.events.SimpleAction;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.util.InstancePool;
32  import com.liferay.portal.kernel.util.Validator;
33  
34  import java.util.ArrayList;
35  import java.util.HashMap;
36  import java.util.List;
37  import java.util.Map;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  import javax.servlet.http.HttpSession;
42  
43  /**
44   * <a href="EventsProcessor.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class EventsProcessor {
50  
51      public static void process(String key, String[] classes)
52          throws ActionException {
53  
54          _instance._process(key, classes, null, null, null, null);
55      }
56  
57      public static void process(String key, String[] classes, String[] ids)
58          throws ActionException {
59  
60          _instance._process(key, classes, ids, null, null, null);
61      }
62  
63      public static void process(
64              String key, String[] classes, HttpSession session)
65          throws ActionException {
66  
67          _instance._process(key, classes, null, null, null, session);
68      }
69  
70      public static void process(
71              String key, String[] classes, HttpServletRequest request,
72              HttpServletResponse response)
73          throws ActionException {
74  
75          _instance._process(key, classes, null, request, response, null);
76      }
77  
78      public static void registerEvent(String key, Object event) {
79          _instance._registerEvent(key, event);
80      }
81  
82      public static void unregisterEvent(String key, Object event) {
83          _instance._unregisterEvent(key, event);
84      }
85  
86      private EventsProcessor() {
87      }
88  
89      private List<Object> _getEvents(String key) {
90          List<Object> events = _eventsMap.get(key);
91  
92          if (events == null) {
93              events = new ArrayList<Object>();
94  
95              _eventsMap.put(key, events);
96          }
97  
98          return events;
99      }
100 
101     private void _process(
102             String key, String[] classes, String[] ids,
103             HttpServletRequest request, HttpServletResponse response,
104             HttpSession session)
105         throws ActionException {
106 
107         for (String className : classes) {
108             if (Validator.isNull(className)) {
109                 return;
110             }
111 
112             if (_log.isDebugEnabled()) {
113                 _log.debug("Process event " + className);
114             }
115 
116             Object event = InstancePool.get(className);
117 
118             _processEvent(event, ids, request, response, session);
119         }
120 
121         if (Validator.isNull(key)) {
122             return;
123         }
124 
125         List<Object> events = _getEvents(key);
126 
127         for (Object event : events) {
128             _processEvent(event, ids, request, response, session);
129         }
130     }
131 
132     private void _processEvent(
133             Object event, String[] ids, HttpServletRequest request,
134             HttpServletResponse response, HttpSession session)
135         throws ActionException {
136 
137         if (event instanceof Action) {
138             Action action = (Action)event;
139 
140             try {
141                 action.run(request, response);
142             }
143             catch (ActionException ae) {
144                 throw ae;
145             }
146             catch (Exception e) {
147                 throw new ActionException(e);
148             }
149         }
150         else if (event instanceof SessionAction) {
151             SessionAction sessionAction = (SessionAction)event;
152 
153             try {
154                 sessionAction.run(session);
155             }
156             catch (ActionException ae) {
157                 throw ae;
158             }
159             catch (Exception e) {
160                 throw new ActionException(e);
161             }
162         }
163         else if (event instanceof SimpleAction) {
164             SimpleAction simpleAction = (SimpleAction)event;
165 
166             simpleAction.run(ids);
167         }
168     }
169 
170     private void _registerEvent(String key, Object event) {
171         List<Object> events = _getEvents(key);
172 
173         events.add(event);
174     }
175 
176     private void _unregisterEvent(String key, Object event) {
177         List<Object> events = _getEvents(key);
178 
179         events.remove(event);
180     }
181 
182     private static Log _log = LogFactoryUtil.getLog(EventsProcessor.class);
183 
184     private static EventsProcessor _instance = new EventsProcessor();
185 
186     private Map<String, List<Object>> _eventsMap =
187         new HashMap<String, List<Object>>();
188 
189 }