1
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.util.InstancePool;
30 import com.liferay.portal.kernel.util.Validator;
31
32 import java.util.HashSet;
33 import java.util.Set;
34
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 import javax.servlet.http.HttpSession;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42
48 public class EventsProcessor {
49
50 public static void process(String[] classes) throws ActionException {
51 _instance._process(classes, null, null, null, null, false);
52 }
53
54 public static void process(String[] classes, String[] ids)
55 throws ActionException {
56
57 _instance._process(classes, ids, null, null, null, false);
58 }
59
60 public static void process(String[] classes, HttpSession ses)
61 throws ActionException {
62
63 _instance._process(classes, null, null, null, ses, false);
64 }
65
66 public static void process(
67 String[] classes, HttpServletRequest req, HttpServletResponse res)
68 throws ActionException {
69
70 _instance._process(classes, null, req, res, null, false);
71 }
72
73 public static void process(String[] classes, boolean single)
74 throws ActionException {
75
76 _instance._process(classes, null, null, null, null, single);
77 }
78
79 public static void process(
80 String[] classes, HttpSession ses, boolean single)
81 throws ActionException {
82
83 _instance._process(classes, null, null, null, ses, single);
84 }
85
86 public static void process(
87 String[] classes, HttpServletRequest req, HttpServletResponse res,
88 boolean single)
89 throws ActionException {
90
91 _instance._process(classes, null, req, res, null, single);
92 }
93
94 private EventsProcessor() {
95 _processPool = new HashSet<String>();
96 }
97
98 private void _process(
99 String[] classes, String[] ids, HttpServletRequest req,
100 HttpServletResponse res, HttpSession ses, boolean single)
101 throws ActionException {
102
103 if ((classes == null) || (classes.length == 0)) {
104 return;
105 }
106
107 for (String className : classes) {
108 if (Validator.isNotNull(className)) {
109 if (_log.isDebugEnabled()) {
110 _log.debug("Process event " + className);
111 }
112
113 if (single) {
114 synchronized (_processPool) {
115 if (_processPool.contains(className)) {
116 break;
117 }
118 else {
119 _processPool.add(className);
120 }
121 }
122 }
123
124 Object obj = InstancePool.get(className);
125
126 if (obj instanceof Action) {
127 Action a = (Action)obj;
128
129 try {
130 a.run(req, res);
131 }
132 catch (Exception e) {
133 throw new ActionException(e);
134 }
135 }
136 else if (obj instanceof SessionAction) {
137 SessionAction sa = (SessionAction)obj;
138
139 try {
140 sa.run(ses);
141 }
142 catch (Exception e) {
143 throw new ActionException(e);
144 }
145 }
146 else if (obj instanceof SimpleAction) {
147 SimpleAction sa = (SimpleAction)obj;
148
149 sa.run(ids);
150 }
151 }
152 }
153 }
154
155 private static Log _log = LogFactory.getLog(EventsProcessor.class);
156
157 private static EventsProcessor _instance = new EventsProcessor();
158
159 private Set<String> _processPool;
160
161 }