001    /**
002     * Copyright (c) 2000-2012 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.mobile.device.rulegroup;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.mobile.device.rulegroup.ActionHandlerManager;
022    import com.liferay.portal.kernel.mobile.device.rulegroup.action.ActionHandler;
023    import com.liferay.portlet.mobiledevicerules.model.MDRAction;
024    
025    import java.util.Collection;
026    import java.util.Collections;
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    
034    /**
035     * @author Edward C. Han
036     */
037    public class DefaultActionHandlerManagerImpl implements ActionHandlerManager {
038    
039            public void applyActions(
040                            List<MDRAction> mdrActions, HttpServletRequest request,
041                            HttpServletResponse response)
042                    throws PortalException, SystemException {
043    
044                    for (MDRAction mdrAction : mdrActions) {
045                            applyAction(mdrAction, request, response);
046                    }
047            }
048    
049            public ActionHandler getActionHandler(String actionType) {
050                    return _deviceActionHandlers.get(actionType);
051            }
052    
053            public Collection<ActionHandler> getActionHandlers() {
054                    return Collections.unmodifiableCollection(
055                            _deviceActionHandlers.values());
056            }
057    
058            public Collection<String> getActionHandlerTypes() {
059                    return _deviceActionHandlers.keySet();
060            }
061    
062            public void registerActionHandler(ActionHandler actionHandler) {
063                    ActionHandler oldActionHandler = _deviceActionHandlers.put(
064                            actionHandler.getType(), actionHandler);
065    
066                    if ((oldActionHandler != null) && _log.isWarnEnabled()) {
067                            _log.warn(
068                                    "Replacing existing action handler type " +
069                                            actionHandler.getType());
070                    }
071            }
072    
073            public void setActionHandlers(Collection<ActionHandler> actionHandlers) {
074                    for (ActionHandler actionHandler : actionHandlers) {
075                            registerActionHandler(actionHandler);
076                    }
077            }
078    
079            public ActionHandler unregisterActionHandler(String actionType) {
080                    return _deviceActionHandlers.remove(actionType);
081            }
082    
083            protected void applyAction(
084                            MDRAction mdrAction, HttpServletRequest request,
085                            HttpServletResponse response)
086                    throws PortalException, SystemException {
087    
088                    ActionHandler actionHandler = _deviceActionHandlers.get(
089                            mdrAction.getType());
090    
091                    if (actionHandler != null) {
092                            actionHandler.applyAction(mdrAction, request, response);
093                    }
094                    else if (_log.isWarnEnabled()) {
095                            _log.warn(
096                                    "No action handler registered for type " + mdrAction.getType());
097                    }
098            }
099    
100            private static Log _log = LogFactoryUtil.getLog(
101                    DefaultActionHandlerManagerImpl.class);
102    
103            private Map<String, ActionHandler> _deviceActionHandlers =
104                    new HashMap<String, ActionHandler>();
105    
106    }