001
014
015 package com.liferay.util.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
024 import java.util.ArrayList;
025 import java.util.List;
026 import java.util.Map;
027 import java.util.concurrent.ConcurrentHashMap;
028
029 import javax.portlet.PortletRequest;
030 import javax.portlet.PortletResponse;
031
032
035 public class ActionCommandCache {
036
037 public static final String ACTION_PACKAGE_NAME = "action.package.prefix";
038
039 public static final ActionCommand EMPTY = new ActionCommand() {
040
041 @Override
042 public boolean processCommand(
043 PortletRequest portletRequest, PortletResponse portletResponse) {
044
045 return false;
046 }
047
048 };
049
050 public ActionCommandCache(String packagePrefix) {
051 if (!packagePrefix.endsWith(StringPool.PERIOD)) {
052 packagePrefix = packagePrefix + StringPool.PERIOD;
053 }
054
055 _packagePrefix = packagePrefix;
056 }
057
058 public ActionCommand getActionCommand(String actionCommandName) {
059 String className = null;
060
061 try {
062 ActionCommand actionCommand = _actionCommandCache.get(
063 actionCommandName);
064
065 if (actionCommand != null) {
066 return actionCommand;
067 }
068
069 StringBundler sb = new StringBundler(4);
070
071 sb.append(_packagePrefix);
072 sb.append(Character.toUpperCase(actionCommandName.charAt(0)));
073 sb.append(actionCommandName.substring(1));
074 sb.append(_ACTION_COMMAND_POSTFIX);
075
076 className = sb.toString();
077
078 actionCommand = (ActionCommand)InstanceFactory.newInstance(
079 className);
080
081 _actionCommandCache.put(actionCommandName, actionCommand);
082
083 return actionCommand;
084 }
085 catch (Exception e) {
086 if (_log.isWarnEnabled()) {
087 _log.warn("Unable to instantiate ActionCommand " + className);
088 }
089
090 _actionCommandCache.put(actionCommandName, EMPTY);
091
092 return EMPTY;
093 }
094 }
095
096 public List<ActionCommand> getActionCommandChain(
097 String actionCommandChain) {
098
099 List<ActionCommand> actionCommands = _actionCommandChainCache.get(
100 actionCommandChain);
101
102 if (actionCommands != null) {
103 return actionCommands;
104 }
105
106 actionCommands = new ArrayList<ActionCommand>();
107
108 String[] actionCommandNames = StringUtil.split(actionCommandChain);
109
110 for (String actionCommandName : actionCommandNames) {
111 ActionCommand actionCommand = getActionCommand(actionCommandName);
112
113 if (actionCommand != EMPTY) {
114 actionCommands.add(actionCommand);
115 }
116 else {
117 if (_log.isWarnEnabled()) {
118 _log.warn(
119 "Unable to find ActionCommand " + actionCommandChain);
120 }
121 }
122 }
123
124 _actionCommandChainCache.put(actionCommandChain, actionCommands);
125
126 return actionCommands;
127 }
128
129 public boolean isEmpty() {
130 return _actionCommandCache.isEmpty();
131 }
132
133 private static final String _ACTION_COMMAND_POSTFIX = "ActionCommand";
134
135 private static Log _log = LogFactoryUtil.getLog(ActionCommandCache.class);
136
137 private Map<String, ActionCommand> _actionCommandCache =
138 new ConcurrentHashMap<String, ActionCommand>();
139 private Map<String, List<ActionCommand>> _actionCommandChainCache =
140 new ConcurrentHashMap<String, List<ActionCommand>>();
141 private String _packagePrefix;
142
143 }