1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.StringBundler;
20  import com.liferay.portal.kernel.util.StringPool;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.concurrent.ConcurrentHashMap;
26  
27  import javax.portlet.PortletRequest;
28  import javax.portlet.PortletResponse;
29  
30  /**
31   * <a href="ActionCommandCache.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Michael C. Han
34   */
35  public class ActionCommandCache {
36  
37      public static final String ACTION_PACKAGE_NAME = "action.package.prefix";
38  
39      public static final ActionCommand EMPTY = new ActionCommand() {
40  
41          public boolean processCommand(
42              PortletRequest portletRequest, PortletResponse portletResponse) {
43  
44              return false;
45          }
46  
47      };
48  
49      public ActionCommandCache(String packagePrefix) {
50          if (!packagePrefix.endsWith(StringPool.PERIOD)) {
51              packagePrefix = packagePrefix + StringPool.PERIOD;
52          }
53  
54          _packagePrefix = packagePrefix;
55      }
56  
57      public ActionCommand getActionCommand(String actionCommandName) {
58          String className = null;
59  
60          try {
61              ActionCommand actionCommand = _actionCommandCache.get(
62                  actionCommandName);
63  
64              if (actionCommand == null) {
65                  StringBundler sb = new StringBundler(4);
66  
67                  sb.append(_packagePrefix);
68                  sb.append(Character.toUpperCase(actionCommandName.charAt(0)));
69                  sb.append(
70                      actionCommandName.substring(1, actionCommandName.length()));
71                  sb.append(_ACTION_COMMAND_POSTFIX);
72  
73                  className = sb.toString();
74  
75                  actionCommand =
76                      (ActionCommand)Class.forName(className).newInstance();
77  
78                  _actionCommandCache.put(actionCommandName, actionCommand);
79              }
80  
81              return actionCommand;
82          }
83          catch (Exception e) {
84              if (_log.isWarnEnabled()) {
85                  _log.warn("Unable to instantiate ActionCommand " + className);
86              }
87  
88              _actionCommandCache.put(actionCommandName, EMPTY);
89  
90              return EMPTY;
91          }
92      }
93  
94      public List<ActionCommand> getActionCommandChain(
95          String actionCommandChain) {
96  
97          List<ActionCommand> actionCommands = _actionCommandChainCache.get(
98              actionCommandChain);
99  
100         if (actionCommands == null) {
101             actionCommands = new ArrayList<ActionCommand>();
102 
103             int nextSeparator = actionCommandChain.indexOf(StringPool.COMMA);
104 
105             int currentIndex = 0;
106 
107             while (currentIndex < actionCommandChain.length()) {
108                 String parsedName = actionCommandChain.substring(
109                     currentIndex, nextSeparator);
110 
111                 ActionCommand actionCommand = getActionCommand(
112                     parsedName);
113 
114                 if (actionCommand != EMPTY) {
115                     actionCommands.add(actionCommand);
116                 }
117                 else {
118                     if (_log.isWarnEnabled()) {
119                         _log.warn(
120                             "Unable to find ActionCommand " +
121                                 actionCommandChain);
122                     }
123                 }
124 
125                 currentIndex = nextSeparator + 1;
126 
127                 nextSeparator = actionCommandChain.indexOf(
128                     StringPool.COMMA, currentIndex);
129 
130                 if (nextSeparator == -1) {
131                     break;
132                 }
133             }
134 
135             _actionCommandChainCache.put(actionCommandChain, actionCommands);
136         }
137 
138         return actionCommands;
139     }
140 
141     public boolean isEmpty() {
142         return _actionCommandCache.isEmpty();
143     }
144 
145     private static final String _ACTION_COMMAND_POSTFIX = "ActionCommand";
146 
147     private static Log _log = LogFactoryUtil.getLog(ActionCommandCache.class);
148 
149     private Map<String, ActionCommand> _actionCommandCache =
150         new ConcurrentHashMap<String, ActionCommand>();
151     private Map<String, List<ActionCommand>> _actionCommandChainCache =
152         new ConcurrentHashMap<String, List<ActionCommand>>();
153     private String _packagePrefix;
154 
155 }