1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.chain.commands;
19
20 import org.apache.commons.chain.Catalog;
21 import org.apache.commons.chain.CatalogFactory;
22 import org.apache.commons.chain.Command;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.struts.chain.contexts.ActionContext;
26 import org.apache.struts.config.ActionConfig;
27
28 /***
29 * <p>Invoke the appropriate <code>Command</code> for this request. If the
30 * context's <code>ActionConfig</code> has no <code>command</code> property
31 * defined, no action will be taken. If the specified command cannot be
32 * found, a warning will be logged, but processing will continue. Depending
33 * on how the chain is configured, this can be used in place of an
34 * <code>Action</code> or as a method of performing pre-processing. </p>
35 *
36 * <p>If used instead of an action, the command which is looked up should put
37 * an ActionForward into the context, unless it has already dealt with the
38 * response.</p>
39 *
40 * @version $Id: ExecuteCommand.java 421119 2006-07-12 04:49:11Z wsmoak $
41 */
42 public class ExecuteCommand extends ActionCommandBase {
43
44
45 /***
46 * Provide Commons Logging instance for this class.
47 */
48 private static final Log LOG = LogFactory.getLog(ExecuteCommand.class);
49
50
51
52 /***
53 * <p>If the <code>context</code> is "valid", lookup a command and execute
54 * it.</p>
55 *
56 * @param actionCtx The <code>Context</code> for the current request
57 * @return the result of the lookup command's <code>execute</code> method,
58 * if executed, or <code>false</code> if it was not executed.
59 * @throws Exception on any error
60 */
61 public boolean execute(ActionContext actionCtx)
62 throws Exception {
63 if (shouldProcess(actionCtx)) {
64 Command command = getCommand(actionCtx);
65
66 if (command != null) {
67 return (command.execute(actionCtx));
68 }
69 }
70
71 return (false);
72 }
73
74 /***
75 * <p>Evaluate the current context to see if a command should even be
76 * executed.</p>
77 *
78 * @param context A valid ActionContext
79 * @return TRUE if the pending Command should be executed
80 */
81 protected boolean shouldProcess(ActionContext context) {
82
83 Boolean valid = context.getFormValid();
84
85 return ((valid != null) && valid.booleanValue());
86 }
87
88 /***
89 * <p>Find the <code>ActionConfig</code> in the current context and, if it
90 * is properly configured, lookup the appropriate <code>commons-chain</code>
91 * command.</p>
92 *
93 * @param context A valid ActionContext
94 * @return a <code>Command</code> to execute, or null if none is specified
95 * or if the specified command cannot be found.
96 */
97 protected Command getCommand(ActionContext context) {
98 ActionConfig actionConfig = context.getActionConfig();
99
100 String commandName = actionConfig.getCommand();
101
102 if (commandName == null) {
103 return null;
104 }
105
106 String catalogName = actionConfig.getCatalog();
107
108 return getCommand(commandName, catalogName);
109 }
110
111 /***
112 * <p> Retrieve the specified Command from the specified Catalog. </p>
113 *
114 * @param commandName The Command to retrieve.
115 * @param catalogName The Catalog to search.
116 * @return Instantiated Command, or null
117 */
118 protected Command getCommand(String commandName, String catalogName) {
119 if (commandName == null) {
120 return null;
121 }
122
123 Catalog catalog;
124
125 if (catalogName != null) {
126 catalog = CatalogFactory.getInstance().getCatalog(catalogName);
127
128 if (catalog == null) {
129 LOG.warn("When looking up " + commandName + ","
130 + " no catalog found under " + catalogName);
131
132 return null;
133 }
134 } else {
135 catalogName = "the default catalog";
136 catalog = CatalogFactory.getInstance().getCatalog();
137
138 if (catalog == null) {
139 LOG.warn("When looking up " + commandName + ","
140 + " no default catalog found.");
141
142 return null;
143 }
144 }
145
146 if (LOG.isDebugEnabled()) {
147 LOG.debug("looking up command " + commandName + " in "
148 + catalogName);
149 }
150
151 return catalog.getCommand(commandName);
152 }
153 }