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 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(actionCommandName.substring(1));
073 sb.append(_ACTION_COMMAND_POSTFIX);
074
075 className = sb.toString();
076
077 actionCommand = (ActionCommand)InstanceFactory.newInstance(
078 className);
079
080 _actionCommandCache.put(actionCommandName, actionCommand);
081
082 return actionCommand;
083 }
084 catch (Exception e) {
085 if (_log.isWarnEnabled()) {
086 _log.warn("Unable to instantiate ActionCommand " + className);
087 }
088
089 _actionCommandCache.put(actionCommandName, EMPTY);
090
091 return EMPTY;
092 }
093 }
094
095 public List<ActionCommand> getActionCommandChain(
096 String actionCommandChain) {
097
098 List<ActionCommand> actionCommands = _actionCommandChainCache.get(
099 actionCommandChain);
100
101 if (actionCommands != null) {
102 return actionCommands;
103 }
104
105 actionCommands = new ArrayList<ActionCommand>();
106
107 String[] actionCommandNames = StringUtil.split(actionCommandChain);
108
109 for (String actionCommandName : actionCommandNames) {
110 ActionCommand actionCommand = getActionCommand(actionCommandName);
111
112 if (actionCommand != EMPTY) {
113 actionCommands.add(actionCommand);
114 }
115 else {
116 if (_log.isWarnEnabled()) {
117 _log.warn(
118 "Unable to find ActionCommand " + actionCommandChain);
119 }
120 }
121 }
122
123 _actionCommandChainCache.put(actionCommandChain, actionCommands);
124
125 return actionCommands;
126 }
127
128 public boolean isEmpty() {
129 return _actionCommandCache.isEmpty();
130 }
131
132 private static final String _ACTION_COMMAND_POSTFIX = "ActionCommand";
133
134 private static Log _log = LogFactoryUtil.getLog(ActionCommandCache.class);
135
136 private Map<String, ActionCommand> _actionCommandCache =
137 new ConcurrentHashMap<String, ActionCommand>();
138 private Map<String, List<ActionCommand>> _actionCommandChainCache =
139 new ConcurrentHashMap<String, List<ActionCommand>>();
140 private String _packagePrefix;
141
142 }