1   /**
2    * Copyright (c) 2000-2008 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.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  /**
43   * <a href="EventsProcessor.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
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 }