1 package org.apache.turbine.services.assemblerbroker.util.java;
2
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.turbine.modules.Assembler;
32 import org.apache.turbine.modules.GenericLoader;
33 import org.apache.turbine.modules.Loader;
34 import org.apache.turbine.services.assemblerbroker.util.AssemblerFactory;
35
36 /**
37 * A screen factory that attempts to load a java class from
38 * the module packages defined in the TurbineResource.properties.
39 *
40 * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
41 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
42 * @version $Id: JavaBaseFactory.java 1078552 2011-03-06 19:58:46Z tv $
43 */
44 public abstract class JavaBaseFactory<T extends Assembler>
45 implements AssemblerFactory<T>
46 {
47 /** A vector of packages. */
48 private static List<String> packages = GenericLoader.getPackages();
49
50 /** Logging */
51 protected Log log = LogFactory.getLog(this.getClass());
52
53 /**
54 * A cache for previously obtained Class instances, which we keep in order
55 * to reduce the Class.forName() overhead (which can be sizable).
56 */
57 private final Map<String, Class<T>> classCache =
58 Collections.synchronizedMap(new HashMap<String, Class<T>>());
59
60 /**
61 * Get an Assembler.
62 *
63 * @param packageName java package name
64 * @param name name of the requested Assembler
65 * @return an Assembler
66 */
67 @SuppressWarnings("unchecked")
68 public T getAssembler(String packageName, String name)
69 {
70 T assembler = null;
71
72 log.debug("Class Fragment is " + name);
73
74 if (StringUtils.isNotEmpty(name))
75 {
76 for (String p : packages)
77 {
78 StringBuffer sb = new StringBuffer();
79
80 sb.append(p).append('.').append(packageName).append('.').append(name);
81
82 String className = sb.toString();
83
84 log.debug("Trying " + className);
85
86 try
87 {
88 Class<T> servClass = classCache.get(className);
89 if(servClass == null)
90 {
91 servClass = (Class<T>) Class.forName(className.toString());
92 classCache.put(className, servClass);
93 }
94 assembler = servClass.newInstance();
95 break; // for()
96 }
97 catch (ClassNotFoundException cnfe)
98 {
99 // Do this so we loop through all the packages.
100 log.debug(className + ": Not found");
101 }
102 catch (NoClassDefFoundError ncdfe)
103 {
104 // Do this so we loop through all the packages.
105 log.debug(className + ": No Class Definition found");
106 }
107 catch (ClassCastException cce)
108 {
109 // This means trouble!
110 // Alternatively we can throw this exception so
111 // that it will appear on the client browser
112 log.error("Could not load "+className, cce);
113 break; // for()
114 }
115 catch (InstantiationException ine)
116 {
117 // This means trouble!
118 // Alternatively we can throw this exception so
119 // that it will appear on the client browser
120 log.error("Could not load "+className, ine);
121 break; // for()
122 }
123 catch (IllegalAccessException ilae)
124 {
125 // This means trouble!
126 // Alternatively we can throw this exception so
127 // that it will appear on the client browser
128 log.error("Could not load "+className, ilae);
129 break; // for()
130 }
131 // With ClassCastException, InstantiationException we hit big problems
132 }
133 }
134 log.debug("Returning: " + assembler);
135
136 return assembler;
137 }
138
139 /**
140 * Get the loader for this type of assembler
141 *
142 * @return a Loader
143 */
144 public abstract Loader<T> getLoader();
145
146 /**
147 * Get the size of a possibly configured cache
148 *
149 * @return the size of the cache in bytes
150 */
151 public int getCacheSize()
152 {
153 return getLoader().getCacheSize();
154 }
155 }