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.InstanceFactory;
021 import com.liferay.portal.kernel.util.StringBundler;
022 import com.liferay.portal.kernel.util.StringPool;
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 public boolean processCommand(
042 PortletRequest portletRequest, PortletResponse portletResponse) {
043
044 return false;
045 }
046
047 };
048
049 public ActionCommandCache(String packagePrefix) {
050 if (!packagePrefix.endsWith(StringPool.PERIOD)) {
051 packagePrefix = packagePrefix + StringPool.PERIOD;
052 }
053
054 _packagePrefix = packagePrefix;
055 }
056
057 public ActionCommand getActionCommand(String actionCommandName) {
058 String className = null;
059
060 try {
061 ActionCommand actionCommand = _actionCommandCache.get(
062 actionCommandName);
063
064 if (actionCommand != null) {
065 return actionCommand;
066 }
067
068 StringBundler sb = new StringBundler(4);
069
070 sb.append(_packagePrefix);
071 sb.append(Character.toUpperCase(actionCommandName.charAt(0)));
072 sb.append(
073 actionCommandName.substring(1, actionCommandName.length()));
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 int nextSeparator = actionCommandChain.indexOf(CharPool.COMMA);
109
110 int currentIndex = 0;
111
112 while (currentIndex < actionCommandChain.length()) {
113 String actionCommandName = actionCommandChain.substring(
114 currentIndex, nextSeparator);
115
116 ActionCommand actionCommand = getActionCommand(actionCommandName);
117
118 if (actionCommand != EMPTY) {
119 actionCommands.add(actionCommand);
120 }
121 else {
122 if (_log.isWarnEnabled()) {
123 _log.warn(
124 "Unable to find ActionCommand " + actionCommandChain);
125 }
126 }
127
128 currentIndex = nextSeparator + 1;
129
130 nextSeparator = actionCommandChain.indexOf(
131 CharPool.COMMA, currentIndex);
132
133 if (nextSeparator == -1) {
134 break;
135 }
136 }
137
138 _actionCommandChainCache.put(actionCommandChain, actionCommands);
139
140 return actionCommands;
141 }
142
143 public boolean isEmpty() {
144 return _actionCommandCache.isEmpty();
145 }
146
147 private static final String _ACTION_COMMAND_POSTFIX = "ActionCommand";
148
149 private static Log _log = LogFactoryUtil.getLog(ActionCommandCache.class);
150
151 private Map<String, ActionCommand> _actionCommandCache =
152 new ConcurrentHashMap<String, ActionCommand>();
153 private Map<String, List<ActionCommand>> _actionCommandChainCache =
154 new ConcurrentHashMap<String, List<ActionCommand>>();
155 private String _packagePrefix;
156
157 }