1
22
23 package com.liferay.util.bridges.mvc;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.util.StringPool;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.concurrent.ConcurrentHashMap;
33
34 import javax.portlet.PortletRequest;
35 import javax.portlet.PortletResponse;
36
37
42 public class ActionCommandCache {
43
44 public static final String ACTION_PACKAGE_NAME = "action.package.prefix";
45
46 public static final ActionCommand EMPTY = new ActionCommand() {
47
48 public boolean processCommand(
49 PortletRequest portletRequest, PortletResponse portletResponse) {
50
51 return false;
52 }
53
54 };
55
56 public ActionCommandCache(String packagePrefix) {
57 if (!packagePrefix.endsWith(StringPool.PERIOD)) {
58 packagePrefix = packagePrefix + StringPool.PERIOD;
59 }
60
61 _packagePrefix = packagePrefix;
62 }
63
64 public ActionCommand getActionCommand(String actionCommandName) {
65 String className = null;
66
67 try {
68 ActionCommand actionCommand = _actionCommandCache.get(
69 actionCommandName);
70
71 if (actionCommand == null) {
72 StringBuilder sb = new StringBuilder();
73
74 sb.append(_packagePrefix);
75 sb.append(Character.toUpperCase(actionCommandName.charAt(0)));
76 sb.append(
77 actionCommandName.substring(1, actionCommandName.length()));
78 sb.append(_ACTION_COMMAND_POSTFIX);
79
80 className = sb.toString();
81
82 actionCommand =
83 (ActionCommand)Class.forName(className).newInstance();
84
85 _actionCommandCache.put(actionCommandName, actionCommand);
86 }
87
88 return actionCommand;
89 }
90 catch (Exception e) {
91 if (_log.isWarnEnabled()) {
92 _log.warn("Unable to instantiate ActionCommand " + className);
93 }
94
95 _actionCommandCache.put(actionCommandName, EMPTY);
96
97 return EMPTY;
98 }
99 }
100
101 public List<ActionCommand> getActionCommandChain(
102 String actionCommandChain) {
103
104 List<ActionCommand> actionCommands = _actionCommandChainCache.get(
105 actionCommandChain);
106
107 if (actionCommands == null) {
108 actionCommands = new ArrayList<ActionCommand>();
109
110 int nextSeparator = actionCommandChain.indexOf(StringPool.COMMA);
111
112 int currentIndex = 0;
113
114 while (currentIndex < actionCommandChain.length()) {
115 String parsedName = actionCommandChain.substring(
116 currentIndex, nextSeparator);
117
118 ActionCommand actionCommand = getActionCommand(
119 parsedName);
120
121 if (actionCommand != EMPTY) {
122 actionCommands.add(actionCommand);
123 }
124 else {
125 if (_log.isWarnEnabled()) {
126 _log.warn(
127 "Unable to find ActionCommand " +
128 actionCommandChain);
129 }
130 }
131
132 currentIndex = nextSeparator + 1;
133
134 nextSeparator = actionCommandChain.indexOf(
135 StringPool.COMMA, currentIndex);
136
137 if (nextSeparator == -1) {
138 break;
139 }
140 }
141
142 _actionCommandChainCache.put(actionCommandChain, actionCommands);
143 }
144
145 return actionCommands;
146 }
147
148 public boolean isEmpty() {
149 return _actionCommandCache.isEmpty();
150 }
151
152 private static final String _ACTION_COMMAND_POSTFIX = "ActionCommand";
153
154 private static final Log _log =
155 LogFactoryUtil.getLog(ActionCommandCache.class);
156
157 private Map<String, ActionCommand> _actionCommandCache =
158 new ConcurrentHashMap<String, ActionCommand>();
159 private Map<String, List<ActionCommand>> _actionCommandChainCache =
160 new ConcurrentHashMap<String, List<ActionCommand>>();
161 private String _packagePrefix;
162
163 }