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