001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.events;
022:
023: import com.liferay.portal.kernel.events.Action;
024: import com.liferay.portal.kernel.events.ActionException;
025: import com.liferay.portal.kernel.events.SessionAction;
026: import com.liferay.portal.kernel.events.SimpleAction;
027: import com.liferay.portal.kernel.util.InstancePool;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.util.CollectionFactory;
030:
031: import java.util.Set;
032:
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035: import javax.servlet.http.HttpSession;
036:
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: /**
041: * <a href="EventsProcessor.java.html"><b><i>View Source</i></b></a>
042: *
043: * @author Brian Wing Shun Chan
044: *
045: */
046: public class EventsProcessor {
047:
048: public static void process(String[] classes) throws ActionException {
049: _instance._process(classes, null, null, null, null, false);
050: }
051:
052: public static void process(String[] classes, String[] ids)
053: throws ActionException {
054:
055: _instance._process(classes, ids, null, null, null, false);
056: }
057:
058: public static void process(String[] classes, HttpSession ses)
059: throws ActionException {
060:
061: _instance._process(classes, null, null, null, ses, false);
062: }
063:
064: public static void process(String[] classes,
065: HttpServletRequest req, HttpServletResponse res)
066: throws ActionException {
067:
068: _instance._process(classes, null, req, res, null, false);
069: }
070:
071: public static void process(String[] classes, boolean single)
072: throws ActionException {
073:
074: _instance._process(classes, null, null, null, null, single);
075: }
076:
077: public static void process(String[] classes, HttpSession ses,
078: boolean single) throws ActionException {
079:
080: _instance._process(classes, null, null, null, ses, single);
081: }
082:
083: public static void process(String[] classes,
084: HttpServletRequest req, HttpServletResponse res,
085: boolean single) throws ActionException {
086:
087: _instance._process(classes, null, req, res, null, single);
088: }
089:
090: private EventsProcessor() {
091: _processPool = CollectionFactory.getHashSet();
092: }
093:
094: private void _process(String[] classes, String[] ids,
095: HttpServletRequest req, HttpServletResponse res,
096: HttpSession ses, boolean single) throws ActionException {
097:
098: if ((classes == null) || (classes.length == 0)) {
099: return;
100: }
101:
102: for (int i = 0; i < classes.length; i++) {
103: String className = classes[i];
104:
105: if (Validator.isNotNull(className)) {
106: if (_log.isDebugEnabled()) {
107: _log.debug("Process event " + className);
108: }
109:
110: if (single) {
111: synchronized (_processPool) {
112: if (_processPool.contains(className)) {
113: break;
114: } else {
115: _processPool.add(className);
116: }
117: }
118: }
119:
120: Object obj = InstancePool.get(classes[i]);
121:
122: if (obj instanceof Action) {
123: Action a = (Action) obj;
124:
125: try {
126: a.run(req, res);
127: } catch (Exception e) {
128: throw new ActionException(e);
129: }
130: } else if (obj instanceof SessionAction) {
131: SessionAction sa = (SessionAction) obj;
132:
133: try {
134: sa.run(ses);
135: } catch (Exception e) {
136: throw new ActionException(e);
137: }
138: } else if (obj instanceof SimpleAction) {
139: SimpleAction sa = (SimpleAction) obj;
140:
141: sa.run(ids);
142: }
143: }
144: }
145: }
146:
147: private static Log _log = LogFactory.getLog(EventsProcessor.class);
148:
149: private static EventsProcessor _instance = new EventsProcessor();
150:
151: private Set _processPool;
152:
153: }
|