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 import java.util.function.IntSupplier;
24 import java.util.stream.Collectors;
25
26 import org.apache.turbine.Turbine;
27 import org.apache.turbine.TurbineConstants;
28 import org.apache.turbine.pipeline.PipelineData;
29 import org.apache.turbine.services.TurbineServices;
30 import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
31
32 /**
33 * This is the base class for the loaders. It contains code that is
34 * used across all of the loaders. It also specifies the interface
35 * that is required to be called a Loader.
36 *
37 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
38 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
39 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
40 * @param <T> the specialized assembler type
41 */
42 public abstract class GenericLoader<T extends Assembler> implements Loader<T>
43 {
44 /** The Assembler Broker Service */
45 protected AssemblerBrokerService ab;
46
47 /** Class of loaded assembler */
48 private final Class<T> assemblerClass;
49
50 /** Supplier of configured cache for this assembler Class */
51 private final IntSupplier cacheSizeSupplier;
52
53 /** @serial This can be serialized */
54 private boolean reload = false;
55
56 /** Base packages path for Turbine */
57 private static final String TURBINE_PACKAGE = "org.apache.turbine.modules";
58
59 /** Packages paths for Turbine */
60 private static List<String> TURBINE_PACKAGES = null;
61
62 /**
63 * Basic constructor for creating a loader.
64 *
65 * @param assemblerClass Class of loaded assembler
66 * @param cacheSizeSupplier Supplier of configured cache size for this assembler Class
67 */
68 public GenericLoader(Class<T> assemblerClass, IntSupplier cacheSizeSupplier)
69 {
70 super();
71 this.assemblerClass = assemblerClass;
72 this.cacheSizeSupplier = cacheSizeSupplier;
73 this.ab = (AssemblerBrokerService)TurbineServices.getInstance()
74 .getService(AssemblerBrokerService.SERVICE_NAME);
75 }
76
77 /**
78 * Attempts to load and execute the external action that has been
79 * set.
80 * @param pipelineData the Turbine request
81 * @param name the name of the assembler module
82 * @throws Exception a generic exception.
83 */
84 public abstract void exec(PipelineData pipelineData, String name)
85 throws Exception;
86
87 /**
88 * Returns whether or not this external action is reload itself.
89 * This is in cases where the Next button would be clicked, but
90 * since we are checking for that, we would go into an endless
91 * loop.
92 *
93 * @return True if the action is reload.
94 */
95 public boolean reload()
96 {
97 return this.reload;
98 }
99
100 /**
101 * Sets whether or not this external action is reload itself.
102 * This is in cases where the Next button would be clicked, but
103 * since we are checking for that, we would go into an endless
104 * loop.
105 *
106 * @param reload True if the action must be marked as reload.
107 * @return Itself.
108 */
109 public GenericLoader<T> setReload(boolean reload)
110 {
111 this.reload = reload;
112 return this;
113 }
114
115 /**
116 * Gets the base package where Turbine should find its default
117 * modules.
118 *
119 * @return A String with the base package name.
120 */
121 public static String getBasePackage()
122 {
123 return TURBINE_PACKAGE;
124 }
125
126 /**
127 * Gets the package list where Turbine should find its
128 * modules.
129 *
130 * @return A List with the package names (including the base package).
131 */
132 public static List<String> getPackages()
133 {
134 if (TURBINE_PACKAGES == null)
135 {
136 List<String> turbinePackages = Turbine.getConfiguration()
137 .getList(TurbineConstants.MODULE_PACKAGES).stream().map( o -> (String) o ).collect( Collectors.toList() );
138
139 TURBINE_PACKAGES = turbinePackages;
140 }
141
142 List<String> packages = TURBINE_PACKAGES;
143
144 if (!packages.contains(TURBINE_PACKAGE))
145 {
146 packages.add(TURBINE_PACKAGE);
147 }
148
149 return packages;
150 }
151
152 /**
153 * Pulls out an instance of the object by name. Name is just the
154 * single name of the object.
155 *
156 * @param name Name of object instance.
157 * @return An Action with the specified name, or null.
158 * @throws Exception a generic exception.
159 */
160 @Override
161 public T getAssembler(String name)
162 throws Exception
163 {
164 return getAssembler(assemblerClass, name);
165 }
166
167 /**
168 * Pulls out an instance of the object by name. Name is just the
169 * single name of the object.
170 *
171 * @param type Type of the assembler.
172 * @param name Name of object instance.
173 * @return A Screen with the specified name, or null.
174 * @throws Exception a generic exception.
175 */
176 protected T getAssembler(Class<T> type, String name)
177 throws Exception
178 {
179 T asm = null;
180
181 try
182 {
183 if (ab != null)
184 {
185 // Attempt to load the assembler
186 asm = ab.getAssembler(type, name);
187 }
188 }
189 catch (ClassCastException cce)
190 {
191 // This can alternatively let this exception be thrown
192 // So that the ClassCastException is shown in the
193 // browser window. Like this it shows "Screen not Found"
194 asm = null;
195 }
196
197 if (asm == null)
198 {
199 // If we did not find a screen we should try and give
200 // the user a reason for that...
201 // FIX ME: The AssemblerFactories should each add it's
202 // own string here...
203 List<String> packages = GenericLoader.getPackages();
204
205 throw new ClassNotFoundException(
206 "\n\n\tRequested " + type + " not found: " + name +
207 "\n\tTurbine looked in the following " +
208 "modules.packages path: \n\t" + packages.toString() + "\n");
209 }
210
211 return asm;
212 }
213
214 /**
215 * @see org.apache.turbine.modules.Loader#getCacheSize()
216 */
217 @Override
218 public int getCacheSize()
219 {
220 return cacheSizeSupplier.getAsInt();
221 }
222 }