001    /**
002     * Copyright (c) 2000-present 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.kernel.portlet.bridges.mvc;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.InstanceFactory;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.registry.Filter;
025    import com.liferay.registry.Registry;
026    import com.liferay.registry.RegistryUtil;
027    import com.liferay.registry.ServiceReference;
028    import com.liferay.registry.ServiceTracker;
029    import com.liferay.registry.ServiceTrackerCustomizer;
030    
031    import java.util.ArrayList;
032    import java.util.List;
033    import java.util.Map;
034    import java.util.concurrent.ConcurrentHashMap;
035    
036    import javax.portlet.PortletRequest;
037    import javax.portlet.PortletResponse;
038    
039    /**
040     * @author Michael C. Han
041     */
042    public class ActionCommandCache {
043    
044            public static final String ACTION_PACKAGE_NAME = "action.package.prefix";
045    
046            public static final ActionCommand EMPTY = new ActionCommand() {
047    
048                    @Override
049                    public boolean processCommand(
050                            PortletRequest portletRequest, PortletResponse portletResponse) {
051    
052                            return false;
053                    }
054    
055            };
056    
057            public ActionCommandCache(String packagePrefix, String portletName) {
058                    if (Validator.isNotNull(packagePrefix) &&
059                            !packagePrefix.endsWith(StringPool.PERIOD)) {
060    
061                            packagePrefix = packagePrefix + StringPool.PERIOD;
062                    }
063    
064                    _packagePrefix = packagePrefix;
065    
066                    Registry registry = RegistryUtil.getRegistry();
067    
068                    Filter filter = registry.getFilter(
069                            "(&(action.command.name=*)(javax.portlet.name=" + portletName +
070                                    ")(objectClass=" + ActionCommand.class.getName() + "))");
071    
072                    _serviceTracker = registry.trackServices(
073                            filter, new ActionCommandServiceTrackerCustomizer());
074    
075                    _serviceTracker.open();
076            }
077    
078            public void close() {
079                    _serviceTracker.close();
080            }
081    
082            public ActionCommand getActionCommand(String actionCommandName) {
083                    String className = null;
084    
085                    try {
086                            ActionCommand actionCommand = _actionCommandCache.get(
087                                    actionCommandName);
088    
089                            if (actionCommand != null) {
090                                    return actionCommand;
091                            }
092    
093                            if (Validator.isNull(_packagePrefix)) {
094                                    return EMPTY;
095                            }
096    
097                            StringBundler sb = new StringBundler(4);
098    
099                            sb.append(_packagePrefix);
100                            sb.append(Character.toUpperCase(actionCommandName.charAt(0)));
101                            sb.append(actionCommandName.substring(1));
102                            sb.append(_ACTION_COMMAND_POSTFIX);
103    
104                            className = sb.toString();
105    
106                            actionCommand = (ActionCommand)InstanceFactory.newInstance(
107                                    className);
108    
109                            _actionCommandCache.put(actionCommandName, actionCommand);
110    
111                            return actionCommand;
112                    }
113                    catch (Exception e) {
114                            if (_log.isWarnEnabled()) {
115                                    _log.warn("Unable to instantiate ActionCommand " + className);
116                            }
117    
118                            _actionCommandCache.put(actionCommandName, EMPTY);
119    
120                            return EMPTY;
121                    }
122            }
123    
124            public List<ActionCommand> getActionCommandChain(
125                    String actionCommandChain) {
126    
127                    List<ActionCommand> actionCommands = _actionCommandChainCache.get(
128                            actionCommandChain);
129    
130                    if (actionCommands != null) {
131                            return actionCommands;
132                    }
133    
134                    actionCommands = new ArrayList<ActionCommand>();
135    
136                    String[] actionCommandNames = StringUtil.split(actionCommandChain);
137    
138                    for (String actionCommandName : actionCommandNames) {
139                            ActionCommand actionCommand = getActionCommand(actionCommandName);
140    
141                            if (actionCommand != EMPTY) {
142                                    actionCommands.add(actionCommand);
143                            }
144                            else {
145                                    if (_log.isWarnEnabled()) {
146                                            _log.warn(
147                                                    "Unable to find ActionCommand " + actionCommandChain);
148                                    }
149                            }
150                    }
151    
152                    _actionCommandChainCache.put(actionCommandChain, actionCommands);
153    
154                    return actionCommands;
155            }
156    
157            public boolean isEmpty() {
158                    return _actionCommandCache.isEmpty();
159            }
160    
161            private static final String _ACTION_COMMAND_POSTFIX = "ActionCommand";
162    
163            private static final Log _log = LogFactoryUtil.getLog(
164                    ActionCommandCache.class);
165    
166            private final Map<String, ActionCommand> _actionCommandCache =
167                    new ConcurrentHashMap<String, ActionCommand>();
168            private final Map<String, List<ActionCommand>> _actionCommandChainCache =
169                    new ConcurrentHashMap<String, List<ActionCommand>>();
170            private final String _packagePrefix;
171            private final ServiceTracker<ActionCommand, ActionCommand> _serviceTracker;
172    
173            private class ActionCommandServiceTrackerCustomizer
174                    implements ServiceTrackerCustomizer<ActionCommand, ActionCommand> {
175    
176                    @Override
177                    public ActionCommand addingService(
178                            ServiceReference<ActionCommand> serviceReference) {
179    
180                            Registry registry = RegistryUtil.getRegistry();
181    
182                            ActionCommand actionCommand = registry.getService(serviceReference);
183    
184                            String actionCommandName = (String)serviceReference.getProperty(
185                                    "action.command.name");
186    
187                            _actionCommandCache.put(actionCommandName, actionCommand);
188    
189                            return actionCommand;
190                    }
191    
192                    @Override
193                    public void modifiedService(
194                            ServiceReference<ActionCommand> serviceReference,
195                            ActionCommand actionCommand) {
196                    }
197    
198                    @Override
199                    public void removedService(
200                            ServiceReference<ActionCommand> serviceReference,
201                            ActionCommand actionCommand) {
202    
203                            Registry registry = RegistryUtil.getRegistry();
204    
205                            registry.ungetService(serviceReference);
206    
207                            String actionCommandName = (String)serviceReference.getProperty(
208                                    "action.command.name");
209    
210                            _actionCommandCache.remove(actionCommandName);
211    
212                            for (List<ActionCommand> actionCommands :
213                                            _actionCommandChainCache.values()) {
214    
215                                    actionCommands.remove(actionCommand);
216                            }
217                    }
218    
219            }
220    
221    }