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 264148 2005-08-29 14:21:04Z henning $
38 */
39 public class ActionLoader
40 extends GenericLoader
41 {
42 /*** Serial Version UID */
43 private static final long serialVersionUID = -2285549057406921958L;
44
45 /*** Logging */
46 private static Log log = LogFactory.getLog(ActionLoader.class);
47
48 /*** The single instance of this class. */
49 private static ActionLoader instance = new ActionLoader(
50 Turbine.getConfiguration().getInt(TurbineConstants.ACTION_CACHE_SIZE_KEY,
51 TurbineConstants.ACTION_CACHE_SIZE_DEFAULT));
52
53 /*** The Assembler Broker Service */
54 private static AssemblerBrokerService ab = TurbineAssemblerBroker.getService();
55
56 /***
57 * These ctor's are private to force clients to use getInstance()
58 * to access this class.
59 */
60 private ActionLoader()
61 {
62 super();
63 }
64
65 /***
66 * These ctor's are private to force clients to use getInstance()
67 * to access this class.
68 */
69 private ActionLoader(int i)
70 {
71 super(i);
72 }
73
74 /***
75 * Adds an instance of an object into the hashtable.
76 *
77 * @param name Name of object.
78 * @param action Action to be associated with name.
79 */
80 private void addInstance(String name, Action action)
81 {
82 if (cache())
83 {
84 this.put(name, (Action) action);
85 }
86 }
87
88 /***
89 * Attempts to load and execute the external action.
90 *
91 * @param data Turbine information.
92 * @param name Name of object that will execute the action.
93 * @exception Exception a generic exception.
94 */
95 public void exec(RunData data, String name)
96 throws Exception
97 {
98
99 getInstance(name).perform(data);
100 }
101
102 /***
103 * Pulls out an instance of the object by name. Name is just the
104 * single name of the object.
105 *
106 * @param name Name of object instance.
107 * @return An Action with the specified name, or null.
108 * @exception Exception a generic exception.
109 */
110 public Action getInstance(String name)
111 throws Exception
112 {
113 Action action = null;
114
115
116 if (cache() && this.containsKey(name))
117 {
118 action = (Action) this.get(name);
119 log.debug("Found Action " + name + " in the cache!");
120 }
121 else
122 {
123 log.debug("Loading Action " + name + " from the Assembler Broker");
124
125 try
126 {
127
128 action = (Action) ab.getAssembler(
129 AssemblerBrokerService.ACTION_TYPE, name);
130 }
131 catch (ClassCastException cce)
132 {
133
134
135
136 action = null;
137 }
138
139 if (action == null)
140 {
141
142
143
144
145 List packages = Turbine.getConfiguration()
146 .getList(TurbineConstants.MODULE_PACKAGES);
147
148 ObjectUtils.addOnce(packages,
149 GenericLoader.getBasePackage());
150
151 throw new ClassNotFoundException(
152 "\n\n\tRequested Action not found: " + name +
153 "\n\tTurbine looked in the following " +
154 "modules.packages path: \n\t" + packages.toString() + "\n");
155 }
156 else if (cache())
157 {
158
159 addInstance(name, action);
160 }
161 }
162 return action;
163 }
164
165 /***
166 * The method through which this class is accessed.
167 *
168 * @return The single instance of this class.
169 */
170 public static ActionLoader getInstance()
171 {
172 return instance;
173 }
174 }