1 package org.apache.turbine.services.assemblerbroker.util.java;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Iterator;
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
32 import org.apache.turbine.Turbine;
33 import org.apache.turbine.TurbineConstants;
34 import org.apache.turbine.modules.Assembler;
35 import org.apache.turbine.modules.GenericLoader;
36 import org.apache.turbine.services.assemblerbroker.util.AssemblerFactory;
37 import org.apache.turbine.util.ObjectUtils;
38
39 /***
40 * A screen factory that attempts to load a java class from
41 * the module packages defined in the TurbineResource.properties.
42 *
43 * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
44 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
45 * @version $Id: JavaBaseFactory.java 677309 2008-07-16 15:35:24Z tv $
46 */
47 public abstract class JavaBaseFactory
48 implements AssemblerFactory
49 {
50 /**</package-summary/html">A vector of packages/ *//package-summary.html">em>* A vector of packages. */
51 private static List packages =/package-summary.html">ong> static List packages =
52 Turbine.getConfiguration().getList(TurbineConstants.MODULE_PACKAGES);
53
54 /*** Logging */
55 protected Log log = LogFactory.getLog(this.getClass());
56
57 /***
58 * A cache for previously obtained Class instances, which we keep in order
59 * to reduce the Class.forName() overhead (which can be sizable).
60 */
61 private Map classCache = Collections.synchronizedMap(new HashMap());
62
63 static
64 {
65 ObjectUtils.addOnce(packages, GenericLoader.getBasePackage());
66 }
67
68 /***
69 * Get an Assembler.
70 *
71 * @param packageName java package name
72 * @param name name of the requested Assembler
73 * @return an Assembler
74 */
75 public Assembler getAssembler(String packageName, String name)/package-summary.html">ong> Assembler getAssembler(String packageName, String name)
76 {
77 Assembler assembler = null;
78
79 log.debug("Class Fragment is " + name);
80
81 if (StringUtils.isNotEmpty(name))
82 {
83 for (Iterator it = packages.iterator(); it.hasNext();)
84 {
85 StringBuffer sb = new StringBuffer();
86
87 sb.append(it.next()).append('.').append(packageName).append('.').append(name);
88
89 String className = sb.toString();
90
91 log.debug("Trying " + className);
92
93 try
94 {
95 Class servClass = (Class) classCache.get(className);
96 if(servClass == null)
97 {
98 servClass = Class.forName(className.toString());
99 classCache.put(className, servClass);
100 }
101 assembler = (Assembler) servClass.newInstance();
102 break;
103 }
104 catch (ClassNotFoundException cnfe)
105 {
106
107 log.debug(className + ": Not found");
108 }
109 catch (NoClassDefFoundError ncdfe)
110 {
111
112 log.debug(className + ": No Class Definition found");
113 }
114 catch (ClassCastException cce)
115 {
116
117
118
119 log.error("Could not load "+className, cce);
120 break;
121 }
122 catch (InstantiationException ine)
123 {
124
125
126
127 log.error("Could not load "+className, ine);
128 break;
129 }
130 catch (IllegalAccessException ilae)
131 {
132
133
134
135 log.error("Could not load "+className, ilae);
136 break;
137 }
138
139 }
140 }
141 log.debug("Returning: " + assembler);
142
143 return assembler;
144 }
145 }