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.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            @Override
040            public void applyActions(
041                            List<MDRAction> mdrActions, HttpServletRequest request,
042                            HttpServletResponse response)
043                    throws PortalException, SystemException {
044    
045                    for (MDRAction mdrAction : mdrActions) {
046                            applyAction(mdrAction, request, response);
047                    }
048            }
049    
050            @Override
051            public ActionHandler getActionHandler(String actionType) {
052                    return _deviceActionHandlers.get(actionType);
053            }
054    
055            @Override
056            public Collection<ActionHandler> getActionHandlers() {
057                    return Collections.unmodifiableCollection(
058                            _deviceActionHandlers.values());
059            }
060    
061            @Override
062            public Collection<String> getActionHandlerTypes() {
063                    return _deviceActionHandlers.keySet();
064            }
065    
066            @Override
067            public void registerActionHandler(ActionHandler actionHandler) {
068                    ActionHandler oldActionHandler = _deviceActionHandlers.put(
069                            actionHandler.getType(), actionHandler);
070    
071                    if ((oldActionHandler != null) && _log.isWarnEnabled()) {
072                            _log.warn(
073                                    "Replacing existing action handler type " +
074                                            actionHandler.getType());
075                    }
076            }
077    
078            public void setActionHandlers(Collection<ActionHandler> actionHandlers) {
079                    for (ActionHandler actionHandler : actionHandlers) {
080                            registerActionHandler(actionHandler);
081                    }
082            }
083    
084            @Override
085            public ActionHandler unregisterActionHandler(String actionType) {
086                    return _deviceActionHandlers.remove(actionType);
087            }
088    
089            protected void applyAction(
090                            MDRAction mdrAction, HttpServletRequest request,
091                            HttpServletResponse response)
092                    throws PortalException, SystemException {
093    
094                    ActionHandler actionHandler = _deviceActionHandlers.get(
095                            mdrAction.getType());
096    
097                    if (actionHandler != null) {
098                            actionHandler.applyAction(mdrAction, request, response);
099                    }
100                    else if (_log.isWarnEnabled()) {
101                            _log.warn(
102                                    "No action handler registered for type " + mdrAction.getType());
103                    }
104            }
105    
106            private static Log _log = LogFactoryUtil.getLog(
107                    DefaultActionHandlerManagerImpl.class);
108    
109            private Map<String, ActionHandler> _deviceActionHandlers =
110                    new HashMap<String, ActionHandler>();
111    
112    }