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.ecs.ConcreteElement;
28
29 import org.apache.turbine.Turbine;
30 import org.apache.turbine.TurbineConstants;
31 import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
32 import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
33 import org.apache.turbine.util.ObjectUtils;
34 import org.apache.turbine.util.RunData;
35
36 /***
37 * The purpose of this class is to allow one to load and execute
38 * Navigation modules.
39 *
40 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
41 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
42 * @version $Id: NavigationLoader.java 534527 2007-05-02 16:10:59Z tv $
43 */
44 public class NavigationLoader
45 extends GenericLoader
46 implements Loader
47 {
48 /*** Serial Version UID */
49 private static final long serialVersionUID = 5062299200802529612L;
50
51 /*** Logging */
52 private static Log log = LogFactory.getLog(NavigationLoader.class);
53
54 /*** The single instance of this class. */
55 private static NavigationLoader instance =
56 new NavigationLoader(Turbine.getConfiguration()
57 .getInt(TurbineConstants.NAVIGATION_CACHE_SIZE_KEY,
58 TurbineConstants.NAVIGATION_CACHE_SIZE_DEFAULT));
59
60 /*** The Assembler Broker Service */
61 private static AssemblerBrokerService ab = TurbineAssemblerBroker.getService();
62
63 /***
64 * These ctor's are private to force clients to use getInstance()
65 * to access this class.
66 */
67 private NavigationLoader()
68 {
69 super();
70 }
71
72 /***
73 * These ctor's are private to force clients to use getInstance()
74 * to access this class.
75 */
76 private NavigationLoader(int i)
77 {
78 super(i);
79 }
80
81 /***
82 * Adds an instance of an object into the hashtable.
83 *
84 * @param name Name of object.
85 * @param navigation Navigation to be associated with name.
86 */
87 private void addInstance(String name, Navigation navigation)
88 {
89 if (cache())
90 {
91 this.put(name, (Navigation) navigation);
92 }
93 }
94
95 /***
96 * Attempts to load and execute the external Navigation. This is
97 * used when you want to execute a Navigation which returns its
98 * output via a MultiPartElement instead of out the data.getPage()
99 * value. This allows you to easily chain the execution of
100 * Navigation modules together.
101 *
102 * @param data Turbine information.
103 * @param name Name of object that will execute the navigation.
104 * @exception Exception a generic exception.
105 */
106 public ConcreteElement eval(RunData data, String name)
107 throws Exception
108 {
109
110 return getInstance(name).build(data);
111 }
112
113 /***
114 * Attempts to load and execute the external Navigation.
115 *
116 * @param data Turbine information.
117 * @param name Name of object instance.
118 * @exception Exception a generic exception.
119 */
120 public void exec(RunData data, String name)
121 throws Exception
122 {
123 this.eval(data, name);
124 }
125
126 /***
127 * Pulls out an instance of the object by name. Name is just the
128 * single name of the object. This is equal to getInstance but
129 * returns an Assembler object and is needed to fulfil the Loader
130 * interface.
131 *
132 * @param name Name of object instance.
133 * @return A Layout with the specified name, or null.
134 * @exception Exception a generic exception.
135 */
136 public Assembler getAssembler(String name)
137 throws Exception
138 {
139 return getInstance(name);
140 }
141
142 /***
143 * Pulls out an instance of the Navigation by name. Name is just the
144 * single name of the Navigation.
145 *
146 * @param name Name of requested Navigation
147 * @return A Navigation with the specified name, or null.
148 * @exception Exception a generic exception.
149 */
150 public Navigation getInstance(String name)
151 throws Exception
152 {
153 Navigation navigation = null;
154
155
156 if (cache() && this.containsKey(name))
157 {
158 navigation = (Navigation) this.get(name);
159 log.debug("Found Navigation " + name + " in the cache!");
160 }
161 else
162 {
163 log.debug("Loading Navigation " + name + " from the Assembler Broker");
164
165 try
166 {
167 if (ab != null)
168 {
169
170 navigation = (Navigation) ab.getAssembler(
171 AssemblerBrokerService.NAVIGATION_TYPE, name);
172 }
173 }
174 catch (ClassCastException cce)
175 {
176
177
178
179 navigation = null;
180 }
181
182 if (navigation == null)
183 {
184
185
186
187
188 List packages = Turbine.getConfiguration()
189 .getList(TurbineConstants.MODULE_PACKAGES);
190
191 ObjectUtils.addOnce(packages,
192 GenericLoader.getBasePackage());
193
194 throw new ClassNotFoundException(
195 "\n\n\tRequested Navigation not found: " + name +
196 "\n\tTurbine looked in the following " +
197 "modules.packages path: \n\t" + packages.toString() + "\n");
198 }
199 else if (cache())
200 {
201
202 addInstance(name, navigation);
203 }
204 }
205 return navigation;
206 }
207
208 /***
209 * The method through which this class is accessed.
210 *
211 * @return The single instance of this class.
212 */
213 public static NavigationLoader getInstance()
214 {
215 return instance;
216 }
217 }