View Javadoc

    1   /*
    2    * $Id: CreateAction.java 510851 2007-02-23 07:05:18Z pbenedict $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts.chain.commands.servlet;
    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.action.ActionServlet;
    27  import org.apache.struts.chain.Constants;
    28  import org.apache.struts.chain.commands.util.ClassUtils;
    29  import org.apache.struts.chain.contexts.ActionContext;
    30  import org.apache.struts.chain.contexts.ServletActionContext;
    31  import org.apache.struts.config.ActionConfig;
    32  import org.apache.struts.config.ModuleConfig;
    33
    34  import java.util.HashMap;
    35  import java.util.Map;
    36
    37  /**
    38   * <p>Concrete implementation of <code>AbstractCreateAction</code> for use in
    39   * a Servlet API chain.  Expects that the ActionContext passed into it can
    40   * safely be cast to <code>ServletActionContext</code>.</p>
    41   */
    42  public class CreateAction
    43      extends org.apache.struts.chain.commands.AbstractCreateAction {
    44      // ------------------------------------------------------ Instance Variables
    45      private static final Log log = LogFactory.getLog(CreateAction.class);
    46
    47      /* :TODO The Action class' dependency on having its "servlet" property set
    48       * requires this API-dependent subclass of AbstractCreateAction.
    49       */
    50      protected synchronized Action getAction(ActionContext context, String type,
    51          ActionConfig actionConfig)
    52          throws Exception {
    53          ModuleConfig moduleConfig = actionConfig.getModuleConfig();
    54          String actionsKey = Constants.ACTIONS_KEY + moduleConfig.getPrefix();
    55          Map actions = (Map) context.getApplicationScope().get(actionsKey);
    56
    57          if (actions == null) {
    58              actions = new HashMap();
    59              context.getApplicationScope().put(actionsKey, actions);
    60          }
    61
    62          Action action = null;
    63
    64          synchronized (actions) {
    65              action = (Action) actions.get(type);
    66
    67              if (action == null) {
    68                  action = createAction(context, type);
    69                  actions.put(type, action);
    70              }
    71          }
    72
    73          if (action.getServlet() == null) {
    74              ServletActionContext saContext = (ServletActionContext) context;
    75              ActionServlet actionServlet = saContext.getActionServlet();
    76
    77              action.setServlet(actionServlet);
    78          }
    79
    80          return (action);
    81      }
    82
    83
    84      /**
    85       * <p>Invoked by <code>getAction</code> when the <code>Action</code>
    86       * actually has to be created. If the instance is already created and
    87       * cached, this method will not be called. </p>
    88       *
    89       * @param context      The <code>Context</code> for this request
    90       * @param type         Name of class to instantiate
    91       * @return Instantiated Action class
    92       * @throws Exception if there are any problems instantiating the Action
    93       *                   class.
    94       * @since Struts 1.3.7
    95       */
    96      protected Action createAction(ActionContext context, String type) throws Exception {
    97          log.info("Initialize action of type: " + type);
    98          return (Action) ClassUtils.getApplicationInstance(type);
    99      }
    100 }