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