/[Apache-SVN]/struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/servlet/CreateAction.java
ViewVC logotype

Contents of /struts/struts1/trunk/core/src/main/java/org/apache/struts/chain/commands/servlet/CreateAction.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 554496 - (hide annotations)
Mon Jul 9 00:25:54 2007 UTC (2 years, 4 months ago) by pbenedict
File size: 4278 byte(s)
STR-3067: Add singleton/prototype instantiation policy for actions
1 wsmoak 421119 /*
2     * $Id$
3     *
4 husted 471754 * 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 wsmoak 421119 *
12 husted 471754 * http://www.apache.org/licenses/LICENSE-2.0
13 wsmoak 421119 *
14 husted 471754 * 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 wsmoak 421119 */
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 pbenedict 553061
54     ServletActionContext saContext = (ServletActionContext) context;
55     ActionServlet actionServlet = saContext.getActionServlet();
56    
57 wsmoak 421119 ModuleConfig moduleConfig = actionConfig.getModuleConfig();
58     String actionsKey = Constants.ACTIONS_KEY + moduleConfig.getPrefix();
59     Map actions = (Map) context.getApplicationScope().get(actionsKey);
60    
61     if (actions == null) {
62     actions = new HashMap();
63     context.getApplicationScope().put(actionsKey, actions);
64     }
65    
66     Action action = null;
67    
68 pbenedict 554496 try {
69     if (actionConfig.isSingleton()) {
70     synchronized (actions) {
71     action = (Action) actions.get(type);
72     if (action == null) {
73     action = createAction(context, type);
74     actions.put(type, action);
75     }
76 pbenedict 553061 }
77 pbenedict 554496 } else {
78     action = createAction(context, type);
79 wsmoak 421119 }
80 pbenedict 554496 } catch (Exception e) {
81     log.error(actionServlet.getInternal().getMessage(
82     "actionCreate", actionConfig.getPath(),
83     actionConfig.toString()), e);
84     throw e;
85 wsmoak 421119 }
86 pbenedict 554496
87 wsmoak 421119 if (action.getServlet() == null) {
88     action.setServlet(actionServlet);
89     }
90    
91     return (action);
92     }
93 bayard 521984
94     /**
95     * <p>Invoked by <code>getAction</code> when the <code>Action</code>
96     * actually has to be created. If the instance is already created and
97     * cached, this method will not be called. </p>
98     *
99     * @param context The <code>Context</code> for this request
100     * @param type Name of class to instantiate
101     * @return Instantiated Action class
102     * @throws Exception if there are any problems instantiating the Action
103     * class.
104     * @since Struts 1.3.7
105     */
106     protected Action createAction(ActionContext context, String type) throws Exception {
107     log.info("Initialize action of type: " + type);
108     return (Action) ClassUtils.getApplicationInstance(type);
109     }
110 wsmoak 421119 }

Properties

Name Value
svn:eol-style native
svn:keywords Date Author Id Revision HeadURL

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2