1 package org.apache.turbine.modules;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.List;
23
24 import org.apache.turbine.Turbine;
25 import org.apache.turbine.TurbineConstants;
26 import org.apache.turbine.pipeline.PipelineData;
27 import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
28 import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
29 import org.apache.turbine.util.RunData;
30
31 /**
32 * This is the base class for the loaders. It contains code that is
33 * used across all of the loaders. It also specifies the interface
34 * that is required to be called a Loader.
35 *
36 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
37 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
38 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
39 * @version $Id: GenericLoader.java 1078552 2011-03-06 19:58:46Z tv $
40 */
41 public abstract class GenericLoader<T extends Assembler>
42 {
43 /** The Assembler Broker Service */
44 protected AssemblerBrokerService ab = TurbineAssemblerBroker.getService();
45
46 /** @serial This can be serialized */
47 private boolean reload = false;
48
49 /** Base packages path for Turbine */
50 private static final String TURBINE_PACKAGE = "org.apache.turbine.modules";
51
52 /** Packages paths for Turbine */
53 private static List<String> TURBINE_PACKAGES = null;
54
55 /**
56 * Basic constructor for creating a loader.
57 */
58 public GenericLoader()
59 {
60 super();
61 }
62
63 /**
64 * Attempts to load and execute the external action that has been
65 * set.
66 * Should revert to abstract when RunData has gone.
67 * @exception Exception a generic exception.
68 */
69 public void exec(PipelineData pipelineData, String name)
70 throws Exception
71 {
72 RunData data = getRunData(pipelineData);
73 exec(data, name);
74 }
75
76
77 /**
78 * Attempts to load and execute the external action that has been
79 * set.
80 * @deprecated Use of this method should be avoided. Use
81 * <code>exec(PipelineData data, String name)</code> instead.
82 * @exception Exception a generic exception.
83 */
84 @Deprecated
85 public abstract void exec(RunData data, String name)
86 throws Exception;
87
88 /**
89 * Commented out.
90 * This method should return the complete classpath + name.
91 *
92 * @param name
93 * @return
94 *
95 public abstract String getClassName(String name);
96 */
97
98 /**
99 * Returns whether or not this external action is reload itself.
100 * This is in cases where the Next button would be clicked, but
101 * since we are checking for that, we would go into an endless
102 * loop.
103 *
104 * @return True if the action is reload.
105 */
106 public boolean reload()
107 {
108 return this.reload;
109 }
110
111 /**
112 * Sets whether or not this external action is reload itself.
113 * This is in cases where the Next button would be clicked, but
114 * since we are checking for that, we would go into an endless
115 * loop.
116 *
117 * @param reload True if the action must be marked as reload.
118 * @return Itself.
119 */
120 public GenericLoader setReload(boolean reload)
121 {
122 this.reload = reload;
123 return this;
124 }
125
126 /**
127 * Gets the base package where Turbine should find its default
128 * modules.
129 *
130 * @return A String with the base package name.
131 */
132 public static String getBasePackage()
133 {
134 return TURBINE_PACKAGE;
135 }
136
137 /**
138 * Gets the package list where Turbine should find its
139 * modules.
140 *
141 * @return A List with the package names (including the base package).
142 */
143 @SuppressWarnings("unchecked")
144 public static List<String> getPackages()
145 {
146 if (TURBINE_PACKAGES == null)
147 {
148 TURBINE_PACKAGES =
149 Turbine.getConfiguration().getList(TurbineConstants.MODULE_PACKAGES);
150 }
151
152 List<String> packages = TURBINE_PACKAGES;
153
154 if (!packages.contains(TURBINE_PACKAGE))
155 {
156 packages.add(TURBINE_PACKAGE);
157 }
158
159 return packages;
160 }
161
162 /**
163 * Pulls out an instance of the object by name. Name is just the
164 * single name of the object.
165 *
166 * @param type Type of the assembler.
167 * @param name Name of object instance.
168 * @return A Screen with the specified name, or null.
169 * @exception Exception a generic exception.
170 */
171 protected T getAssembler(String type, String name)
172 throws Exception
173 {
174 T asm = null;
175
176 try
177 {
178 if (ab != null)
179 {
180 // Attempt to load the assembler
181 asm = (T) ab.getAssembler(type, name);
182 }
183 }
184 catch (ClassCastException cce)
185 {
186 // This can alternatively let this exception be thrown
187 // So that the ClassCastException is shown in the
188 // browser window. Like this it shows "Screen not Found"
189 asm = null;
190 }
191
192 if (asm == null)
193 {
194 // If we did not find a screen we should try and give
195 // the user a reason for that...
196 // FIX ME: The AssemblerFactories should each add it's
197 // own string here...
198 List<String> packages = GenericLoader.getPackages();
199
200 throw new ClassNotFoundException(
201 "\n\n\tRequested " + type + " not found: " + name +
202 "\n\tTurbine looked in the following " +
203 "modules.packages path: \n\t" + packages.toString() + "\n");
204 }
205
206 return asm;
207 }
208
209 /**
210 * Helper method to cast from PipelineData to RunData. This will go when
211 * the pipeline is fully implemented and the RunData-methods are removed
212 *
213 * @param pipelineData a PipelineData object
214 *
215 * @return the input object casted to RunData
216 */
217 private RunData getRunData(PipelineData pipelineData)
218 {
219 if(!(pipelineData instanceof RunData))
220 {
221 throw new RuntimeException("Can't cast to rundata from pipeline data.");
222 }
223 return (RunData)pipelineData;
224 }
225 }