1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.events;
16  
17  import com.liferay.portal.kernel.events.Action;
18  import com.liferay.portal.kernel.events.ActionException;
19  import com.liferay.portal.kernel.events.SessionAction;
20  import com.liferay.portal.kernel.events.SimpleAction;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.kernel.util.InstancePool;
24  import com.liferay.portal.kernel.util.Validator;
25  
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  import javax.servlet.http.HttpSession;
34  
35  /**
36   * <a href="EventsProcessorImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   * @author Michael Young
40   */
41  public class EventsProcessorImpl implements EventsProcessor {
42  
43      public void process(
44              String key, String[] classes, String[] ids,
45              HttpServletRequest request, HttpServletResponse response,
46              HttpSession session)
47          throws ActionException {
48  
49          for (String className : classes) {
50              if (Validator.isNull(className)) {
51                  return;
52              }
53  
54              if (_log.isDebugEnabled()) {
55                  _log.debug("Process event " + className);
56              }
57  
58              Object event = InstancePool.get(className);
59  
60              processEvent(event, ids, request, response, session);
61          }
62  
63          if (Validator.isNull(key)) {
64              return;
65          }
66  
67          List<Object> events = _getEvents(key);
68  
69          for (Object event : events) {
70              processEvent(event, ids, request, response, session);
71          }
72      }
73  
74      public void processEvent(
75              Object event, String[] ids, HttpServletRequest request,
76              HttpServletResponse response, HttpSession session)
77          throws ActionException {
78  
79          if (event instanceof Action) {
80              Action action = (Action)event;
81  
82              try {
83                  action.run(request, response);
84              }
85              catch (ActionException ae) {
86                  throw ae;
87              }
88              catch (Exception e) {
89                  throw new ActionException(e);
90              }
91          }
92          else if (event instanceof SessionAction) {
93              SessionAction sessionAction = (SessionAction)event;
94  
95              try {
96                  sessionAction.run(session);
97              }
98              catch (ActionException ae) {
99                  throw ae;
100             }
101             catch (Exception e) {
102                 throw new ActionException(e);
103             }
104         }
105         else if (event instanceof SimpleAction) {
106             SimpleAction simpleAction = (SimpleAction)event;
107 
108             simpleAction.run(ids);
109         }
110     }
111 
112     public void registerEvent(String key, Object event) {
113         List<Object> events = _getEvents(key);
114 
115         events.add(event);
116     }
117 
118     public void unregisterEvent(String key, Object event) {
119         List<Object> events = _getEvents(key);
120 
121         events.remove(event);
122     }
123 
124     private List<Object> _getEvents(String key) {
125         List<Object> events = _eventsMap.get(key);
126 
127         if (events == null) {
128             events = new ArrayList<Object>();
129 
130             _eventsMap.put(key, events);
131         }
132 
133         return events;
134     }
135 
136     private static Log _log = LogFactoryUtil.getLog(EventsProcessorImpl.class);
137 
138     private Map<String, List<Object>> _eventsMap =
139         new HashMap<String, List<Object>>();
140 
141 }