1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class EventsProcessorImpl implements EventsProcessor {
50  
51      public void process(
52              String key, String[] classes, String[] ids,
53              HttpServletRequest request, HttpServletResponse response,
54              HttpSession session)
55          throws ActionException {
56  
57          for (String className : classes) {
58              if (Validator.isNull(className)) {
59                  return;
60              }
61  
62              if (_log.isDebugEnabled()) {
63                  _log.debug("Process event " + className);
64              }
65  
66              Object event = InstancePool.get(className);
67  
68              processEvent(event, ids, request, response, session);
69          }
70  
71          if (Validator.isNull(key)) {
72              return;
73          }
74  
75          List<Object> events = _getEvents(key);
76  
77          for (Object event : events) {
78              processEvent(event, ids, request, response, session);
79          }
80      }
81  
82      public void processEvent(
83              Object event, String[] ids, HttpServletRequest request,
84              HttpServletResponse response, HttpSession session)
85          throws ActionException {
86  
87          if (event instanceof Action) {
88              Action action = (Action)event;
89  
90              try {
91                  action.run(request, response);
92              }
93              catch (ActionException ae) {
94                  throw ae;
95              }
96              catch (Exception e) {
97                  throw new ActionException(e);
98              }
99          }
100         else if (event instanceof SessionAction) {
101             SessionAction sessionAction = (SessionAction)event;
102 
103             try {
104                 sessionAction.run(session);
105             }
106             catch (ActionException ae) {
107                 throw ae;
108             }
109             catch (Exception e) {
110                 throw new ActionException(e);
111             }
112         }
113         else if (event instanceof SimpleAction) {
114             SimpleAction simpleAction = (SimpleAction)event;
115 
116             simpleAction.run(ids);
117         }
118     }
119 
120     public void registerEvent(String key, Object event) {
121         List<Object> events = _getEvents(key);
122 
123         events.add(event);
124     }
125 
126     public void unregisterEvent(String key, Object event) {
127         List<Object> events = _getEvents(key);
128 
129         events.remove(event);
130     }
131 
132     private List<Object> _getEvents(String key) {
133         List<Object> events = _eventsMap.get(key);
134 
135         if (events == null) {
136             events = new ArrayList<Object>();
137 
138             _eventsMap.put(key, events);
139         }
140 
141         return events;
142     }
143 
144     private static Log _log = LogFactoryUtil.getLog(EventsProcessorImpl.class);
145 
146     private Map<String, List<Object>> _eventsMap =
147         new HashMap<String, List<Object>>();
148 
149 }