1   /**
2    * Copyright (c) 2000-2007 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.util.InstancePool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.struts.Action;
28  import com.liferay.portal.struts.ActionException;
29  import com.liferay.portal.struts.SessionAction;
30  import com.liferay.portal.struts.SimpleAction;
31  import com.liferay.util.CollectionFactory;
32  
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 = CollectionFactory.getHashSet();
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 (int i = 0; i < classes.length; i++) {
108             String className = classes[i];
109 
110             if (Validator.isNotNull(className)) {
111                 if (_log.isDebugEnabled()) {
112                     _log.debug("Process event " + className);
113                 }
114 
115                 if (single) {
116                     synchronized (_processPool) {
117                         if (_processPool.contains(className)) {
118                             break;
119                         }
120                         else {
121                             _processPool.add(className);
122                         }
123                     }
124                 }
125 
126                 Object obj = InstancePool.get(classes[i]);
127 
128                 if (obj instanceof Action) {
129                     Action a = (Action)obj;
130 
131                     try {
132                         a.run(req, res);
133                     }
134                     catch (Exception e) {
135                         throw new ActionException(e);
136                     }
137                 }
138                 else if (obj instanceof SessionAction) {
139                     SessionAction sa = (SessionAction)obj;
140 
141                     try {
142                         sa.run(ses);
143                     }
144                     catch (Exception e) {
145                         throw new ActionException(e);
146                     }
147                 }
148                 else if (obj instanceof SimpleAction) {
149                     SimpleAction sa = (SimpleAction)obj;
150 
151                     sa.run(ids);
152                 }
153             }
154         }
155     }
156 
157     private static Log _log = LogFactory.getLog(EventsProcessor.class);
158 
159     private static EventsProcessor _instance = new EventsProcessor();
160 
161     private Set _processPool;
162 
163 }