001: /*
002: * $Id: CreateAction.java 510851 2007-02-23 07:05:18Z pbenedict $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.chain.commands.servlet;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.struts.action.Action;
026: import org.apache.struts.action.ActionServlet;
027: import org.apache.struts.chain.Constants;
028: import org.apache.struts.chain.commands.util.ClassUtils;
029: import org.apache.struts.chain.contexts.ActionContext;
030: import org.apache.struts.chain.contexts.ServletActionContext;
031: import org.apache.struts.config.ActionConfig;
032: import org.apache.struts.config.ModuleConfig;
033:
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: /**
038: * <p>Concrete implementation of <code>AbstractCreateAction</code> for use in
039: * a Servlet API chain. Expects that the ActionContext passed into it can
040: * safely be cast to <code>ServletActionContext</code>.</p>
041: */
042: public class CreateAction extends
043: org.apache.struts.chain.commands.AbstractCreateAction {
044: // ------------------------------------------------------ Instance Variables
045: private static final Log log = LogFactory
046: .getLog(CreateAction.class);
047:
048: /* :TODO The Action class' dependency on having its "servlet" property set
049: * requires this API-dependent subclass of AbstractCreateAction.
050: */
051: protected synchronized Action getAction(ActionContext context,
052: String type, ActionConfig actionConfig) throws Exception {
053: ModuleConfig moduleConfig = actionConfig.getModuleConfig();
054: String actionsKey = Constants.ACTIONS_KEY
055: + moduleConfig.getPrefix();
056: Map actions = (Map) context.getApplicationScope().get(
057: actionsKey);
058:
059: if (actions == null) {
060: actions = new HashMap();
061: context.getApplicationScope().put(actionsKey, actions);
062: }
063:
064: Action action = null;
065:
066: synchronized (actions) {
067: action = (Action) actions.get(type);
068:
069: if (action == null) {
070: action = createAction(context, type);
071: actions.put(type, action);
072: }
073: }
074:
075: if (action.getServlet() == null) {
076: ServletActionContext saContext = (ServletActionContext) context;
077: ActionServlet actionServlet = saContext.getActionServlet();
078:
079: action.setServlet(actionServlet);
080: }
081:
082: return (action);
083: }
084:
085: /**
086: * <p>Invoked by <code>getAction</code> when the <code>Action</code>
087: * actually has to be created. If the instance is already created and
088: * cached, this method will not be called. </p>
089: *
090: * @param context The <code>Context</code> for this request
091: * @param type Name of class to instantiate
092: * @return Instantiated Action class
093: * @throws Exception if there are any problems instantiating the Action
094: * class.
095: * @since Struts 1.3.7
096: */
097: protected Action createAction(ActionContext context, String type)
098: throws Exception {
099: log.info("Initialize action of type: " + type);
100: return (Action) ClassUtils.getApplicationInstance(type);
101: }
102: }
|