1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.chain.commands;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.struts.action.Action;
26 import org.apache.struts.chain.contexts.ActionContext;
27 import org.apache.struts.config.ActionConfig;
28
29 /**
30 * <p> Create (if necessary) and cache an <code>Action</code> for this
31 * request. </p>
32 *
33 * @version $Rev: 510851 $ $Date: 2005-11-12 13:01:44 -0500 (Sat, 12 Nov 2005)
34 * $
35 */
36 public abstract class AbstractCreateAction extends ActionCommandBase {
37
38
39 /**
40 * Provide a Commons logging instance for this class.
41 */
42 private static final Log LOG =
43 LogFactory.getLog(AbstractCreateAction.class);
44
45
46
47 /**
48 * <p>Create (if necessary) and cache an <code>Action</code> for this
49 * request.</p>
50 *
51 * @param actionCtx The <code>Context</code> for the current request
52 * @return <code>false</code> so that processing continues
53 * @throws Exception if there are any problems instantiating the Action
54 * class.
55 */
56 public boolean execute(ActionContext actionCtx)
57 throws Exception {
58
59 Boolean valid = actionCtx.getFormValid();
60
61 if ((valid == null) || !valid.booleanValue()) {
62 LOG.trace("Invalid form; not going to execute.");
63
64 return (false);
65 }
66
67
68 if (actionCtx.getAction() != null) {
69 LOG.trace("already have an action [" + actionCtx.getAction() + "]");
70
71 return (false);
72 }
73
74
75 ActionConfig actionConfig = actionCtx.getActionConfig();
76 String type = actionConfig.getType();
77
78 if (type == null) {
79 if ((actionConfig.getForward() == null)
80 && (actionConfig.getInclude() == null)) {
81 LOG.error("no type for " + actionConfig.getPath());
82 } else {
83 LOG.trace("no type for " + actionConfig.getPath());
84 }
85
86 return (false);
87 }
88
89
90 Action action = getAction(actionCtx, type, actionConfig);
91
92 if (LOG.isTraceEnabled()) {
93 LOG.trace("setting action to " + action);
94 }
95
96 actionCtx.setAction(action);
97
98 return (false);
99 }
100
101
102
103 /**
104 * <p> Create and return the appropriate <code>Action</code> class for the
105 * given <code>type</code> and <code>actionConfig</code>. </p> <p> NOTE:
106 * The dependence on ActionServlet suggests that this should be broken up
107 * along the lines of the other Abstract/concrete pairs in the
108 * org.apache.struts.chain.commands package. </p>
109 *
110 * @param context The <code>Context</code> for this request
111 * @param type Name of class to instantiate
112 * @param actionConfig The {@link ActionConfig} for this request
113 * @return Instantiated Action class
114 * @throws Exception if there are any problems instantiating the Action
115 * class.
116 */
117 protected abstract Action getAction(ActionContext context, String type,
118 ActionConfig actionConfig)
119 throws Exception;
120 }