001    /**
002     * Copyright (c) 2000-2013 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            @Override
042            public void process(
043                            String key, String[] classes, String[] ids,
044                            HttpServletRequest request, HttpServletResponse response,
045                            HttpSession session)
046                    throws ActionException {
047    
048                    for (String className : classes) {
049                            if (Validator.isNull(className)) {
050                                    return;
051                            }
052    
053                            if (_log.isDebugEnabled()) {
054                                    _log.debug("Process event " + className);
055                            }
056    
057                            Object event = InstancePool.get(className);
058    
059                            processEvent(event, ids, request, response, session);
060                    }
061    
062                    if (Validator.isNull(key)) {
063                            return;
064                    }
065    
066                    List<Object> events = _getEvents(key);
067    
068                    for (Object event : events) {
069                            processEvent(event, ids, request, response, session);
070                    }
071            }
072    
073            @Override
074            public void processEvent(
075                            Object event, String[] ids, HttpServletRequest request,
076                            HttpServletResponse response, HttpSession session)
077                    throws ActionException {
078    
079                    if (event instanceof Action) {
080                            Action action = (Action)event;
081    
082                            try {
083                                    action.run(request, response);
084                            }
085                            catch (ActionException ae) {
086                                    throw ae;
087                            }
088                            catch (Exception e) {
089                                    throw new ActionException(e);
090                            }
091                    }
092                    else if (event instanceof SessionAction) {
093                            SessionAction sessionAction = (SessionAction)event;
094    
095                            try {
096                                    sessionAction.run(session);
097                            }
098                            catch (ActionException ae) {
099                                    throw ae;
100                            }
101                            catch (Exception e) {
102                                    throw new ActionException(e);
103                            }
104                    }
105                    else if (event instanceof SimpleAction) {
106                            SimpleAction simpleAction = (SimpleAction)event;
107    
108                            simpleAction.run(ids);
109                    }
110            }
111    
112            @Override
113            public void registerEvent(String key, Object event) {
114                    List<Object> events = _getEvents(key);
115    
116                    events.add(event);
117            }
118    
119            @Override
120            public void unregisterEvent(String key, Object event) {
121                    List<Object> events = _getEvents(key);
122    
123                    events.remove(event);
124            }
125    
126            private List<Object> _getEvents(String key) {
127                    List<Object> events = _eventsMap.get(key);
128    
129                    if (events == null) {
130                            events = new ArrayList<Object>();
131    
132                            _eventsMap.put(key, events);
133                    }
134    
135                    return events;
136            }
137    
138            private static Log _log = LogFactoryUtil.getLog(EventsProcessorImpl.class);
139    
140            private Map<String, List<Object>> _eventsMap =
141                    new HashMap<String, List<Object>>();
142    
143    }