001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.events;
016    
017    import com.liferay.portal.kernel.events.Action;
018    import com.liferay.portal.kernel.events.ActionException;
019    import com.liferay.portal.kernel.events.SessionAction;
020    import com.liferay.portal.kernel.events.SimpleAction;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.InstancePool;
024    import com.liferay.portal.kernel.util.Validator;
025    
026    import java.util.ArrayList;
027    import java.util.HashMap;
028    import java.util.List;
029    import java.util.Map;
030    
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    import javax.servlet.http.HttpSession;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Michael Young
038     */
039    public class EventsProcessorImpl implements EventsProcessor {
040    
041            public void process(
042                            String key, String[] classes, String[] ids,
043                            HttpServletRequest request, HttpServletResponse response,
044                            HttpSession session)
045                    throws ActionException {
046    
047                    for (String className : classes) {
048                            if (Validator.isNull(className)) {
049                                    return;
050                            }
051    
052                            if (_log.isDebugEnabled()) {
053                                    _log.debug("Process event " + className);
054                            }
055    
056                            Object event = InstancePool.get(className);
057    
058                            processEvent(event, ids, request, response, session);
059                    }
060    
061                    if (Validator.isNull(key)) {
062                            return;
063                    }
064    
065                    List<Object> events = _getEvents(key);
066    
067                    for (Object event : events) {
068                            processEvent(event, ids, request, response, session);
069                    }
070            }
071    
072            public void processEvent(
073                            Object event, String[] ids, HttpServletRequest request,
074                            HttpServletResponse response, HttpSession session)
075                    throws ActionException {
076    
077                    if (event instanceof Action) {
078                            Action action = (Action)event;
079    
080                            try {
081                                    action.run(request, response);
082                            }
083                            catch (ActionException ae) {
084                                    throw ae;
085                            }
086                            catch (Exception e) {
087                                    throw new ActionException(e);
088                            }
089                    }
090                    else if (event instanceof SessionAction) {
091                            SessionAction sessionAction = (SessionAction)event;
092    
093                            try {
094                                    sessionAction.run(session);
095                            }
096                            catch (ActionException ae) {
097                                    throw ae;
098                            }
099                            catch (Exception e) {
100                                    throw new ActionException(e);
101                            }
102                    }
103                    else if (event instanceof SimpleAction) {
104                            SimpleAction simpleAction = (SimpleAction)event;
105    
106                            simpleAction.run(ids);
107                    }
108            }
109    
110            public void registerEvent(String key, Object event) {
111                    List<Object> events = _getEvents(key);
112    
113                    events.add(event);
114            }
115    
116            public void unregisterEvent(String key, Object event) {
117                    List<Object> events = _getEvents(key);
118    
119                    events.remove(event);
120            }
121    
122            private List<Object> _getEvents(String key) {
123                    List<Object> events = _eventsMap.get(key);
124    
125                    if (events == null) {
126                            events = new ArrayList<Object>();
127    
128                            _eventsMap.put(key, events);
129                    }
130    
131                    return events;
132            }
133    
134            private static Log _log = LogFactoryUtil.getLog(EventsProcessorImpl.class);
135    
136            private Map<String, List<Object>> _eventsMap =
137                    new HashMap<String, List<Object>>();
138    
139    }