View Javadoc

1   /*
2    * $Id: CreateAction.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.chain.commands.servlet;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.struts.action.Action;
23  import org.apache.struts.action.ActionServlet;
24  import org.apache.struts.chain.Constants;
25  import org.apache.struts.chain.commands.util.ClassUtils;
26  import org.apache.struts.chain.contexts.ActionContext;
27  import org.apache.struts.chain.contexts.ServletActionContext;
28  import org.apache.struts.config.ActionConfig;
29  import org.apache.struts.config.ModuleConfig;
30  
31  import java.util.HashMap;
32  import java.util.Map;
33  
34  /***
35   * <p>Concrete implementation of <code>AbstractCreateAction</code> for use in
36   * a Servlet API chain.  Expects that the ActionContext passed into it can
37   * safely be cast to <code>ServletActionContext</code>.</p>
38   */
39  public class CreateAction
40      extends org.apache.struts.chain.commands.AbstractCreateAction {
41      // ------------------------------------------------------ Instance Variables
42      private static final Log log = LogFactory.getLog(CreateAction.class);
43  
44      /* :TODO The Action class' dependency on having its "servlet" property set
45       * requires this API-dependent subclass of AbstractCreateAction.
46       */
47      protected synchronized Action getAction(ActionContext context, String type,
48          ActionConfig actionConfig)
49          throws Exception {
50          ModuleConfig moduleConfig = actionConfig.getModuleConfig();
51          String actionsKey = Constants.ACTIONS_KEY + moduleConfig.getPrefix();
52          Map actions = (Map) context.getApplicationScope().get(actionsKey);
53  
54          if (actions == null) {
55              actions = new HashMap();
56              context.getApplicationScope().put(actionsKey, actions);
57          }
58  
59          Action action = null;
60  
61          synchronized (actions) {
62              action = (Action) actions.get(type);
63  
64              if (action == null) {
65                  log.info("Initialize action of type: " + type);
66                  action = (Action) ClassUtils.getApplicationInstance(type);
67                  actions.put(type, action);
68              }
69          }
70  
71          if (action.getServlet() == null) {
72              ServletActionContext saContext = (ServletActionContext) context;
73              ActionServlet actionServlet = saContext.getActionServlet();
74  
75              action.setServlet(actionServlet);
76          }
77  
78          return (action);
79      }
80  }