KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > modules > ActionLoader


1 package org.apache.turbine.modules;
2
3 /*
4  * Copyright 2001-2004 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
19 import java.util.List JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import org.apache.turbine.Turbine;
25 import org.apache.turbine.TurbineConstants;
26 import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
27 import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
28 import org.apache.turbine.util.ObjectUtils;
29 import org.apache.turbine.util.RunData;
30
31 /**
32  * The purpose of this class is to allow one to load and execute
33  * Action modules.
34  *
35  * @author <a HREF="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
36  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37  * @version $Id: ActionLoader.java,v 1.9.2.3 2004/08/16 22:57:50 henning Exp $
38  */

39 public class ActionLoader
40     extends GenericLoader
41 {
42     /** Logging */
43     private static Log log = LogFactory.getLog(ActionLoader.class);
44
45     /** The single instance of this class. */
46     private static ActionLoader instance = new ActionLoader(
47         Turbine.getConfiguration().getInt(TurbineConstants.ACTION_CACHE_SIZE_KEY,
48                                           TurbineConstants.ACTION_CACHE_SIZE_DEFAULT));
49
50     /** The Assembler Broker Service */
51     private static AssemblerBrokerService ab = TurbineAssemblerBroker.getService();
52
53     /**
54      * These ctor's are private to force clients to use getInstance()
55      * to access this class.
56      */

57     private ActionLoader()
58     {
59         super();
60     }
61
62     /**
63      * These ctor's are private to force clients to use getInstance()
64      * to access this class.
65      */

66     private ActionLoader(int i)
67     {
68         super(i);
69     }
70
71     /**
72      * Adds an instance of an object into the hashtable.
73      *
74      * @param name Name of object.
75      * @param action Action to be associated with name.
76      */

77     private void addInstance(String JavaDoc name, Action action)
78     {
79         if (cache())
80         {
81             this.put(name, (Action) action);
82         }
83     }
84
85     /**
86      * Attempts to load and execute the external action.
87      *
88      * @param data Turbine information.
89      * @param name Name of object that will execute the action.
90      * @exception Exception a generic exception.
91      */

92     public void exec(RunData data, String JavaDoc name)
93             throws Exception JavaDoc
94     {
95         // Execute action
96
getInstance(name).perform(data);
97     }
98
99     /**
100      * Pulls out an instance of the object by name. Name is just the
101      * single name of the object.
102      *
103      * @param name Name of object instance.
104      * @return An Action with the specified name, or null.
105      * @exception Exception a generic exception.
106      */

107     public Action getInstance(String JavaDoc name)
108             throws Exception JavaDoc
109     {
110         Action action = null;
111
112         // Check if the action is already in the cache
113
if (cache() && this.containsKey(name))
114         {
115             action = (Action) this.get(name);
116             log.debug("Found Action " + name + " in the cache!");
117         }
118         else
119         {
120             log.debug("Loading Action " + name + " from the Assembler Broker");
121
122             try
123             {
124                 // Attempt to load the screen
125
action = (Action) ab.getAssembler(
126                         AssemblerBrokerService.ACTION_TYPE, name);
127             }
128             catch (ClassCastException JavaDoc cce)
129             {
130                 // This can alternatively let this exception be thrown
131
// So that the ClassCastException is shown in the
132
// browser window. Like this it shows "Screen not Found"
133
action = null;
134             }
135
136             if (action == null)
137             {
138                 // If we did not find a screen we should try and give
139
// the user a reason for that...
140
// FIX ME: The AssemblerFactories should each add it's
141
// own string here...
142
List JavaDoc packages = Turbine.getConfiguration()
143                     .getList(TurbineConstants.MODULE_PACKAGES);
144
145                 ObjectUtils.addOnce(packages,
146                         GenericLoader.getBasePackage());
147
148                 throw new ClassNotFoundException JavaDoc(
149                         "\n\n\tRequested Action not found: " + name +
150                         "\n\tTurbine looked in the following " +
151                         "modules.packages path: \n\t" + packages.toString() + "\n");
152             }
153             else if (cache())
154             {
155                 // The new instance is added to the cache
156
addInstance(name, action);
157             }
158         }
159         return action;
160     }
161
162     /**
163      * The method through which this class is accessed.
164      *
165      * @return The single instance of this class.
166      */

167     public static ActionLoader getInstance()
168     {
169         return instance;
170     }
171 }
172
Popular Tags