/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.strecks.controller.chain.command;
import java.util.HashMap;
import java.util.Map;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.chain.Constants;
import org.apache.struts.chain.commands.AbstractCreateAction;
import org.apache.struts.chain.contexts.ActionContext;
import org.apache.struts.chain.contexts.ServletActionContext;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ModuleConfig;
import org.strecks.controller.ActionCreator;
import org.strecks.controller.ActionCreatorImpl;
/**
* Replaces <code>CreateAction</code> in chain
* @author Phil Zoio
*/
public class CreateAction extends AbstractCreateAction
{
private ActionCreator actionCreator;
public CreateAction()
{
super();
actionCreator = newActionCreator();
}
protected ActionCreator newActionCreator()
{
return new ActionCreatorImpl();
}
@SuppressWarnings("unchecked")
@Override
protected Action getAction(ActionContext context, String type, ActionConfig actionConfig) throws Exception
{
String className = type;
Action action = null;
ModuleConfig moduleConfig = actionConfig.getModuleConfig();
String actionsKey = Constants.ACTIONS_KEY + moduleConfig.getPrefix();
final Map applicationScope = context.getApplicationScope();
Map<String, Action> actions = (Map<String, Action>) applicationScope.get(actionsKey);
if (actions == null)
{
actions = new HashMap<String, Action>();
applicationScope.put(actionsKey, actions);
}
boolean isNew = false;
synchronized (actions)
{
action = (Action) actions.get(className);
if (action == null)
{
// load the action class
Class clazz = Class.forName(className);
// delegate action creation to ActionCreator
action = actionCreator.createAction(clazz);
isNew = true;
}
if (isNew)
{
actions.put(className, action);
if (action.getServlet() == null)
{
ServletActionContext saContext = (ServletActionContext) context;
ActionServlet actionServlet = saContext.getActionServlet();
action.setServlet(actionServlet);
}
}
}
return action;
}
}