01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.controller.chain.command;
16:
17: import java.util.HashMap;
18: import java.util.Map;
19:
20: import org.apache.struts.action.Action;
21: import org.apache.struts.action.ActionServlet;
22: import org.apache.struts.chain.Constants;
23: import org.apache.struts.chain.commands.AbstractCreateAction;
24: import org.apache.struts.chain.contexts.ActionContext;
25: import org.apache.struts.chain.contexts.ServletActionContext;
26: import org.apache.struts.config.ActionConfig;
27: import org.apache.struts.config.ModuleConfig;
28: import org.strecks.controller.ActionCreator;
29: import org.strecks.controller.ActionCreatorImpl;
30:
31: /**
32: * Replaces <code>CreateAction</code> in chain
33: * @author Phil Zoio
34: */
35: public class CreateAction extends AbstractCreateAction {
36:
37: private ActionCreator actionCreator;
38:
39: public CreateAction() {
40: super ();
41: actionCreator = newActionCreator();
42: }
43:
44: protected ActionCreator newActionCreator() {
45: return new ActionCreatorImpl();
46: }
47:
48: @SuppressWarnings("unchecked")
49: @Override
50: protected Action getAction(ActionContext context, String type,
51: ActionConfig actionConfig) throws Exception {
52:
53: String className = type;
54: Action action = null;
55:
56: ModuleConfig moduleConfig = actionConfig.getModuleConfig();
57: String actionsKey = Constants.ACTIONS_KEY
58: + moduleConfig.getPrefix();
59:
60: final Map applicationScope = context.getApplicationScope();
61:
62: Map<String, Action> actions = (Map<String, Action>) applicationScope
63: .get(actionsKey);
64:
65: if (actions == null) {
66: actions = new HashMap<String, Action>();
67: applicationScope.put(actionsKey, actions);
68: }
69:
70: boolean isNew = false;
71:
72: synchronized (actions) {
73: action = (Action) actions.get(className);
74:
75: if (action == null) {
76: // load the action class
77: Class clazz = Class.forName(className);
78:
79: // delegate action creation to ActionCreator
80: action = actionCreator.createAction(clazz);
81: isNew = true;
82: }
83:
84: if (isNew) {
85: actions.put(className, action);
86: if (action.getServlet() == null) {
87: ServletActionContext saContext = (ServletActionContext) context;
88: ActionServlet actionServlet = saContext
89: .getActionServlet();
90:
91: action.setServlet(actionServlet);
92: }
93: }
94: }
95: return action;
96: }
97: }
|