1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.events;
21  
22  import com.liferay.portal.kernel.events.Action;
23  import com.liferay.portal.kernel.events.ActionException;
24  import com.liferay.portal.kernel.events.SessionAction;
25  import com.liferay.portal.kernel.events.SimpleAction;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.InstancePool;
29  import com.liferay.portal.kernel.util.Validator;
30  
31  import java.util.ArrayList;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  import javax.servlet.http.HttpSession;
39  
40   /**
41    * <a href="EventsProcessorImpl.java.html"><b><i>View Source</i></b></a>
42    *
43    * @author Brian Wing Shun Chan
44    * @author Michael Young
45    *
46    */
47  public class EventsProcessorImpl implements EventsProcessor {
48  
49      public void process(
50              String key, String[] classes, String[] ids,
51              HttpServletRequest request, HttpServletResponse response,
52              HttpSession session)
53          throws ActionException {
54  
55          for (String className : classes) {
56              if (Validator.isNull(className)) {
57                  return;
58              }
59  
60              if (_log.isDebugEnabled()) {
61                  _log.debug("Process event " + className);
62              }
63  
64              Object event = InstancePool.get(className);
65  
66              processEvent(event, ids, request, response, session);
67          }
68  
69          if (Validator.isNull(key)) {
70              return;
71          }
72  
73          List<Object> events = _getEvents(key);
74  
75          for (Object event : events) {
76              processEvent(event, ids, request, response, session);
77          }
78      }
79  
80      public void processEvent(
81              Object event, String[] ids, HttpServletRequest request,
82              HttpServletResponse response, HttpSession session)
83          throws ActionException {
84  
85          if (event instanceof Action) {
86              Action action = (Action)event;
87  
88              try {
89                  action.run(request, response);
90              }
91              catch (ActionException ae) {
92                  throw ae;
93              }
94              catch (Exception e) {
95                  throw new ActionException(e);
96              }
97          }
98          else if (event instanceof SessionAction) {
99              SessionAction sessionAction = (SessionAction)event;
100 
101             try {
102                 sessionAction.run(session);
103             }
104             catch (ActionException ae) {
105                 throw ae;
106             }
107             catch (Exception e) {
108                 throw new ActionException(e);
109             }
110         }
111         else if (event instanceof SimpleAction) {
112             SimpleAction simpleAction = (SimpleAction)event;
113 
114             simpleAction.run(ids);
115         }
116     }
117 
118     public void registerEvent(String key, Object event) {
119         List<Object> events = _getEvents(key);
120 
121         events.add(event);
122     }
123 
124     public void unregisterEvent(String key, Object event) {
125         List<Object> events = _getEvents(key);
126 
127         events.remove(event);
128     }
129 
130     private List<Object> _getEvents(String key) {
131         List<Object> events = _eventsMap.get(key);
132 
133         if (events == null) {
134             events = new ArrayList<Object>();
135 
136             _eventsMap.put(key, events);
137         }
138 
139         return events;
140     }
141 
142     private static Log _log = LogFactoryUtil.getLog(EventsProcessorImpl.class);
143 
144     private Map<String, List<Object>> _eventsMap =
145         new HashMap<String, List<Object>>();
146 
147 }