1 package org.apache.turbine.modules;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.List;
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 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 name)
93 throws Exception
94 {
95
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 name)
108 throws Exception
109 {
110 Action action = null;
111
112
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
125 action = (Action) ab.getAssembler(
126 AssemblerBrokerService.ACTION_TYPE, name);
127 }
128 catch (ClassCastException cce)
129 {
130
131
132
133 action = null;
134 }
135
136 if (action == null)
137 {
138
139
140
141
142 List packages = Turbine.getConfiguration()
143 .getList(TurbineConstants.MODULE_PACKAGES);
144
145 ObjectUtils.addOnce(packages,
146 GenericLoader.getBasePackage());
147
148 throw new ClassNotFoundException(
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
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 }