1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util.bridges.mvc;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.CharPool;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.concurrent.ConcurrentHashMap;
27  
28  import javax.portlet.PortletRequest;
29  import javax.portlet.PortletResponse;
30  
31  /**
32   * <a href="ActionCommandCache.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Michael C. Han
35   */
36  public class ActionCommandCache {
37  
38      public static final String ACTION_PACKAGE_NAME = "action.package.prefix";
39  
40      public static final ActionCommand EMPTY = new ActionCommand() {
41  
42          public boolean processCommand(
43              PortletRequest portletRequest, PortletResponse portletResponse) {
44  
45              return false;
46          }
47  
48      };
49  
50      public ActionCommandCache(String packagePrefix) {
51          if (!packagePrefix.endsWith(StringPool.PERIOD)) {
52              packagePrefix = packagePrefix + StringPool.PERIOD;
53          }
54  
55          _packagePrefix = packagePrefix;
56      }
57  
58      public ActionCommand getActionCommand(String actionCommandName) {
59          String className = null;
60  
61          try {
62              ActionCommand actionCommand = _actionCommandCache.get(
63                  actionCommandName);
64  
65              if (actionCommand == null) {
66                  StringBundler sb = new StringBundler(4);
67  
68                  sb.append(_packagePrefix);
69                  sb.append(Character.toUpperCase(actionCommandName.charAt(0)));
70                  sb.append(
71                      actionCommandName.substring(1, actionCommandName.length()));
72                  sb.append(_ACTION_COMMAND_POSTFIX);
73  
74                  className = sb.toString();
75  
76                  actionCommand =
77                      (ActionCommand)Class.forName(className).newInstance();
78  
79                  _actionCommandCache.put(actionCommandName, actionCommand);
80              }
81  
82              return actionCommand;
83          }
84          catch (Exception e) {
85              if (_log.isWarnEnabled()) {
86                  _log.warn("Unable to instantiate ActionCommand " + className);
87              }
88  
89              _actionCommandCache.put(actionCommandName, EMPTY);
90  
91              return EMPTY;
92          }
93      }
94  
95      public List<ActionCommand> getActionCommandChain(
96          String actionCommandChain) {
97  
98          List<ActionCommand> actionCommands = _actionCommandChainCache.get(
99              actionCommandChain);
100 
101         if (actionCommands == null) {
102             actionCommands = new ArrayList<ActionCommand>();
103 
104             int nextSeparator = actionCommandChain.indexOf(CharPool.COMMA);
105 
106             int currentIndex = 0;
107 
108             while (currentIndex < actionCommandChain.length()) {
109                 String parsedName = actionCommandChain.substring(
110                     currentIndex, nextSeparator);
111 
112                 ActionCommand actionCommand = getActionCommand(
113                     parsedName);
114 
115                 if (actionCommand != EMPTY) {
116                     actionCommands.add(actionCommand);
117                 }
118                 else {
119                     if (_log.isWarnEnabled()) {
120                         _log.warn(
121                             "Unable to find ActionCommand " +
122                                 actionCommandChain);
123                     }
124                 }
125 
126                 currentIndex = nextSeparator + 1;
127 
128                 nextSeparator = actionCommandChain.indexOf(
129                     CharPool.COMMA, currentIndex);
130 
131                 if (nextSeparator == -1) {
132                     break;
133                 }
134             }
135 
136             _actionCommandChainCache.put(actionCommandChain, actionCommands);
137         }
138 
139         return actionCommands;
140     }
141 
142     public boolean isEmpty() {
143         return _actionCommandCache.isEmpty();
144     }
145 
146     private static final String _ACTION_COMMAND_POSTFIX = "ActionCommand";
147 
148     private static Log _log = LogFactoryUtil.getLog(ActionCommandCache.class);
149 
150     private Map<String, ActionCommand> _actionCommandCache =
151         new ConcurrentHashMap<String, ActionCommand>();
152     private Map<String, List<ActionCommand>> _actionCommandChainCache =
153         new ConcurrentHashMap<String, List<ActionCommand>>();
154     private String _packagePrefix;
155 
156 }