1 16 17 package org.apache.struts.chain; 18 19 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import org.apache.commons.chain.Command; 23 import org.apache.commons.chain.Context; 24 import org.apache.commons.chain.web.WebContext; 25 import org.apache.struts.action.Action; 26 import org.apache.struts.action.ActionServlet; 27 import org.apache.struts.chain.Constants; 28 import org.apache.struts.chain.util.ClassUtils; 29 import org.apache.struts.config.ActionConfig; 30 import org.apache.struts.config.ModuleConfig; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 35 42 43 public class CreateAction implements Command { 44 45 46 private static final Log log = LogFactory.getLog(CreateAction.class); 48 49 private String actionKey = Constants.ACTION_KEY; 50 private String actionConfigKey = Constants.ACTION_CONFIG_KEY; 51 private String actionServletKey = Constants.ACTION_SERVLET_KEY; 52 private String validKey = Constants.VALID_KEY; 53 54 55 57 58 63 public String getActionKey() { 64 65 return (this.actionKey); 66 67 } 68 69 70 77 public void setActionKey(String actionKey) { 78 79 this.actionKey = actionKey; 80 81 } 82 83 84 89 public String getActionConfigKey() { 90 91 return (this.actionConfigKey); 92 93 } 94 95 96 103 public void setActionConfigKey(String actionConfigKey) { 104 105 this.actionConfigKey = actionConfigKey; 106 107 } 108 109 110 115 public String getActionServletKey() { 116 117 return (this.actionServletKey); 118 119 } 120 121 122 129 public void setActionServletKey(String actionServletKey) { 130 131 this.actionServletKey = actionServletKey; 132 133 } 134 135 136 140 public String getValidKey() { 141 142 return (this.validKey); 143 144 } 145 146 147 153 public void setValidKey(String validKey) { 154 155 this.validKey = validKey; 156 157 } 158 159 160 162 163 171 public boolean execute(Context context) throws Exception { 172 173 Boolean valid = (Boolean ) context.get(getValidKey()); 175 if ((valid == null) || !valid.booleanValue()) { 176 return (false); 177 } 178 179 if (context.get(getActionKey()) != null) { 181 return (false); 182 } 183 184 ActionConfig actionConfig = (ActionConfig) 186 context.get(getActionConfigKey()); 187 String type = actionConfig.getType(); 188 189 if (type == null) { 190 return (false); 191 } 192 193 Action action = null; 195 Map actions = getActions(context, actionConfig.getModuleConfig()); 196 synchronized (actions) { 197 action = (Action) actions.get(type); 198 if (action == null) { 199 log.info("Initialize action of type: " + type + " for actionConfig " + actionConfig); 200 action = (Action) ClassUtils.getApplicationInstance(type); 201 ActionServlet actionServlet = (ActionServlet) 202 context.get(getActionServletKey()); 203 action.setServlet(actionServlet); 204 actions.put(type, action); 205 } 206 } 207 context.put(getActionKey(), action); 208 209 return (false); 210 211 } 212 213 214 216 217 225 protected synchronized Map getActions(Context context, 226 ModuleConfig moduleConfig) { 227 228 WebContext wcontext = (WebContext) context; 229 String actionsKey = Constants.ACTIONS_KEY + moduleConfig.getPrefix(); 230 Map actions = (Map ) wcontext.getApplicationScope().get(actionsKey); 231 if (actions == null) { 232 actions = new HashMap (); 233 wcontext.getApplicationScope().put(actionKey, actions); 234 } 235 return (actions); 236 237 } 238 239 240 241 } 242 | Popular Tags |