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="EventsProcessorImpl.java.html"><b><i>View Source</i></b></a>
45    *
46    * @author Brian Wing Shun Chan
47    * @author Michael Young
48    *
49    */
50  public class EventsProcessorImpl implements EventsProcessor {
51  
52      public void process(
53              String key, String[] classes, String[] ids,
54              HttpServletRequest request, HttpServletResponse response,
55              HttpSession session)
56          throws ActionException {
57  
58          for (String className : classes) {
59              if (Validator.isNull(className)) {
60                  return;
61              }
62  
63              if (_log.isDebugEnabled()) {
64                  _log.debug("Process event " + className);
65              }
66  
67              Object event = InstancePool.get(className);
68  
69              processEvent(event, ids, request, response, session);
70          }
71  
72          if (Validator.isNull(key)) {
73              return;
74          }
75  
76          List<Object> events = _getEvents(key);
77  
78          for (Object event : events) {
79              processEvent(event, ids, request, response, session);
80          }
81      }
82  
83      public void processEvent(
84              Object event, String[] ids, HttpServletRequest request,
85              HttpServletResponse response, HttpSession session)
86          throws ActionException {
87  
88          if (event instanceof Action) {
89              Action action = (Action)event;
90  
91              try {
92                  action.run(request, response);
93              }
94              catch (ActionException ae) {
95                  throw ae;
96              }
97              catch (Exception e) {
98                  throw new ActionException(e);
99              }
100         }
101         else if (event instanceof SessionAction) {
102             SessionAction sessionAction = (SessionAction)event;
103 
104             try {
105                 sessionAction.run(session);
106             }
107             catch (ActionException ae) {
108                 throw ae;
109             }
110             catch (Exception e) {
111                 throw new ActionException(e);
112             }
113         }
114         else if (event instanceof SimpleAction) {
115             SimpleAction simpleAction = (SimpleAction)event;
116 
117             simpleAction.run(ids);
118         }
119     }
120 
121     public void registerEvent(String key, Object event) {
122         List<Object> events = _getEvents(key);
123 
124         events.add(event);
125     }
126 
127     public void unregisterEvent(String key, Object event) {
128         List<Object> events = _getEvents(key);
129 
130         events.remove(event);
131     }
132 
133     private List<Object> _getEvents(String key) {
134         List<Object> events = _eventsMap.get(key);
135 
136         if (events == null) {
137             events = new ArrayList<Object>();
138 
139             _eventsMap.put(key, events);
140         }
141 
142         return events;
143     }
144 
145     private static Log _log = LogFactoryUtil.getLog(EventsProcessorImpl.class);
146 
147     private Map<String, List<Object>> _eventsMap =
148         new HashMap<String, List<Object>>();
149 
150 }