001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }