1 package org.apache.turbine.modules;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.List;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.apache.turbine.Turbine;
28 import org.apache.turbine.TurbineConstants;
29 import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
30 import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
31 import org.apache.turbine.util.ObjectUtils;
32 import org.apache.turbine.util.RunData;
33
34 /***
35 * The purpose of this class is to allow one to load and execute
36 * Action modules.
37 *
38 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
39 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
40 * @version $Id: ActionLoader.java 534527 2007-05-02 16:10:59Z tv $
41 */
42 public class ActionLoader
43 extends GenericLoader
44 {
45 /*** Serial Version UID */
46 private static final long serialVersionUID = -2285549057406921958L;
47
48 /*** Logging */
49 private static Log log = LogFactory.getLog(ActionLoader.class);
50
51 /*** The single instance of this class. */
52 private static ActionLoader instance = new ActionLoader(
53 Turbine.getConfiguration().getInt(TurbineConstants.ACTION_CACHE_SIZE_KEY,
54 TurbineConstants.ACTION_CACHE_SIZE_DEFAULT));
55
56 /*** The Assembler Broker Service */
57 private static AssemblerBrokerService ab = TurbineAssemblerBroker.getService();
58
59 /***
60 * These ctor's are private to force clients to use getInstance()
61 * to access this class.
62 */
63 private ActionLoader()
64 {
65 super();
66 }
67
68 /***
69 * These ctor's are private to force clients to use getInstance()
70 * to access this class.
71 */
72 private ActionLoader(int i)
73 {
74 super(i);
75 }
76
77 /***
78 * Adds an instance of an object into the hashtable.
79 *
80 * @param name Name of object.
81 * @param action Action to be associated with name.
82 */
83 private void addInstance(String name, Action action)
84 {
85 if (cache())
86 {
87 this.put(name, (Action) action);
88 }
89 }
90
91 /***
92 * Attempts to load and execute the external action.
93 *
94 * @param data Turbine information.
95 * @param name Name of object that will execute the action.
96 * @exception Exception a generic exception.
97 */
98 public void exec(RunData data, String name)
99 throws Exception
100 {
101
102 getInstance(name).perform(data);
103 }
104
105 /***
106 * Pulls out an instance of the object by name. Name is just the
107 * single name of the object.
108 *
109 * @param name Name of object instance.
110 * @return An Action with the specified name, or null.
111 * @exception Exception a generic exception.
112 */
113 public Action getInstance(String name)
114 throws Exception
115 {
116 Action action = null;
117
118
119 if (cache() && this.containsKey(name))
120 {
121 action = (Action) this.get(name);
122 log.debug("Found Action " + name + " in the cache!");
123 }
124 else
125 {
126 log.debug("Loading Action " + name + " from the Assembler Broker");
127
128 try
129 {
130
131 action = (Action) ab.getAssembler(
132 AssemblerBrokerService.ACTION_TYPE, name);
133 }
134 catch (ClassCastException cce)
135 {
136
137
138
139 action = null;
140 }
141
142 if (action == null)
143 {
144
145
146
147
148 List packages = Turbine.getConfiguration()
149 .getList(TurbineConstants.MODULE_PACKAGES);
150
151 ObjectUtils.addOnce(packages,
152 GenericLoader.getBasePackage());
153
154 throw new ClassNotFoundException(
155 "\n\n\tRequested Action not found: " + name +
156 "\n\tTurbine looked in the following " +
157 "modules.packages path: \n\t" + packages.toString() + "\n");
158 }
159 else if (cache())
160 {
161
162 addInstance(name, action);
163 }
164 }
165 return action;
166 }
167
168 /***
169 * The method through which this class is accessed.
170 *
171 * @return The single instance of this class.
172 */
173 public static ActionLoader getInstance()
174 {
175 return instance;
176 }
177 }