1   /**
2    * Copyright (c) 2000-2008 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.util.InstancePool;
30  import com.liferay.portal.kernel.util.Validator;
31  
32  import java.util.ArrayList;
33  import java.util.HashMap;
34  import java.util.List;
35  import java.util.Map;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  import javax.servlet.http.HttpSession;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * <a href="EventsProcessor.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class EventsProcessor {
51  
52      public static void process(String key, String[] classes)
53          throws ActionException {
54  
55          _instance._process(key, classes, null, null, null, null);
56      }
57  
58      public static void process(String key, String[] classes, String[] ids)
59          throws ActionException {
60  
61          _instance._process(key, classes, ids, null, null, null);
62      }
63  
64      public static void process(
65              String key, String[] classes, HttpSession session)
66          throws ActionException {
67  
68          _instance._process(key, classes, null, null, null, session);
69      }
70  
71      public static void process(
72              String key, String[] classes, HttpServletRequest request,
73              HttpServletResponse response)
74          throws ActionException {
75  
76          _instance._process(key, classes, null, request, response, null);
77      }
78  
79      public static void registerEvent(String key, Object event) {
80          _instance._registerEvent(key, event);
81      }
82  
83      public static void unregisterEvent(String key, Object event) {
84          _instance._unregisterEvent(key, event);
85      }
86  
87      private EventsProcessor() {
88      }
89  
90      private List<Object> _getEvents(String key) {
91          List<Object> events = _eventsMap.get(key);
92  
93          if (events == null) {
94              events = new ArrayList<Object>();
95  
96              _eventsMap.put(key, events);
97          }
98  
99          return events;
100     }
101 
102     private void _process(
103             String key, String[] classes, String[] ids,
104             HttpServletRequest request, HttpServletResponse response,
105             HttpSession session)
106         throws ActionException {
107 
108         for (String className : classes) {
109             if (Validator.isNull(className)) {
110                 return;
111             }
112 
113             if (_log.isDebugEnabled()) {
114                 _log.debug("Process event " + className);
115             }
116 
117             Object event = InstancePool.get(className);
118 
119             _processEvent(event, ids, request, response, session);
120         }
121 
122         if (Validator.isNull(key)) {
123             return;
124         }
125 
126         List<Object> events = _getEvents(key);
127 
128         for (Object event : events) {
129             _processEvent(event, ids, request, response, session);
130         }
131     }
132 
133     private void _processEvent(
134             Object event, String[] ids, HttpServletRequest request,
135             HttpServletResponse response, HttpSession session)
136         throws ActionException {
137 
138         if (event instanceof Action) {
139             Action action = (Action)event;
140 
141             try {
142                 action.run(request, response);
143             }
144             catch (ActionException ae) {
145                 throw ae;
146             }
147             catch (Exception e) {
148                 throw new ActionException(e);
149             }
150         }
151         else if (event instanceof SessionAction) {
152             SessionAction sessionAction = (SessionAction)event;
153 
154             try {
155                 sessionAction.run(session);
156             }
157             catch (ActionException ae) {
158                 throw ae;
159             }
160             catch (Exception e) {
161                 throw new ActionException(e);
162             }
163         }
164         else if (event instanceof SimpleAction) {
165             SimpleAction simpleAction = (SimpleAction)event;
166 
167             simpleAction.run(ids);
168         }
169     }
170 
171     private void _registerEvent(String key, Object event) {
172         List<Object> events = _getEvents(key);
173 
174         events.add(event);
175     }
176 
177     private void _unregisterEvent(String key, Object event) {
178         List<Object> events = _getEvents(key);
179 
180         events.remove(event);
181     }
182 
183     private static Log _log = LogFactory.getLog(EventsProcessor.class);
184 
185     private static EventsProcessor _instance = new EventsProcessor();
186 
187     private Map<String, List<Object>> _eventsMap =
188         new HashMap<String, List<Object>>();
189 
190 }