%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.services.assemblerbroker.util.java.JavaBaseFactory |
|
|
1 | package org.apache.turbine.services.assemblerbroker.util.java; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.util.Collections; |
|
20 | import java.util.HashMap; |
|
21 | import java.util.Iterator; |
|
22 | import java.util.List; |
|
23 | import java.util.Map; |
|
24 | ||
25 | import org.apache.commons.lang.StringUtils; |
|
26 | import org.apache.commons.logging.Log; |
|
27 | import org.apache.commons.logging.LogFactory; |
|
28 | ||
29 | import org.apache.turbine.Turbine; |
|
30 | import org.apache.turbine.TurbineConstants; |
|
31 | import org.apache.turbine.modules.Assembler; |
|
32 | import org.apache.turbine.modules.GenericLoader; |
|
33 | import org.apache.turbine.services.assemblerbroker.util.AssemblerFactory; |
|
34 | import org.apache.turbine.util.ObjectUtils; |
|
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 264148 2005-08-29 14:21:04Z henning $ |
|
43 | */ |
|
44 | 360 | public abstract class JavaBaseFactory |
45 | implements AssemblerFactory |
|
46 | { |
|
47 | /** A vector of packages. */ |
|
48 | 60 | private static List packages = |
49 | Turbine.getConfiguration().getList(TurbineConstants.MODULE_PACKAGES); |
|
50 | ||
51 | /** Logging */ |
|
52 | 240 | protected Log log = LogFactory.getLog(this.getClass()); |
53 | ||
54 | /** |
|
55 | * A cache for previously obtained Class instances, which we keep in order |
|
56 | * to reduce the Class.forName() overhead (which can be sizable). |
|
57 | */ |
|
58 | 240 | private Map classCache = Collections.synchronizedMap(new HashMap()); |
59 | ||
60 | static |
|
61 | { |
|
62 | 40 | ObjectUtils.addOnce(packages, GenericLoader.getBasePackage()); |
63 | 20 | } |
64 | ||
65 | /** |
|
66 | * Get an Assembler. |
|
67 | * |
|
68 | * @param packageName java package name |
|
69 | * @param name name of the requested Assembler |
|
70 | * @return an Assembler |
|
71 | */ |
|
72 | public Assembler getAssembler(String packageName, String name) |
|
73 | { |
|
74 | 138 | Assembler assembler = null; |
75 | ||
76 | 138 | log.debug("Class Fragment is " + name); |
77 | ||
78 | 138 | if (StringUtils.isNotEmpty(name)) |
79 | { |
|
80 | 333 | for (Iterator it = packages.iterator(); it.hasNext();) |
81 | { |
|
82 | 276 | StringBuffer className = new StringBuffer(); |
83 | ||
84 | 276 | className.append(it.next()); |
85 | 276 | className.append('.'); |
86 | 276 | className.append(packageName); |
87 | 276 | className.append('.'); |
88 | 276 | className.append(name); |
89 | ||
90 | 276 | log.debug("Trying " + className); |
91 | ||
92 | try |
|
93 | { |
|
94 | 276 | Class servClass = (Class) classCache.get(className); |
95 | 276 | if(servClass == null) |
96 | { |
|
97 | 276 | servClass = Class.forName(className.toString()); |
98 | 24 | classCache.put(className, servClass); |
99 | } |
|
100 | 24 | assembler = (Assembler) servClass.newInstance(); |
101 | 24 | break; // for() |
102 | } |
|
103 | 126 | catch (ClassNotFoundException cnfe) |
104 | { |
|
105 | // Do this so we loop through all the packages. |
|
106 | 252 | log.debug(className + ": Not found"); |
107 | 126 | } |
108 | 0 | catch (NoClassDefFoundError ncdfe) |
109 | { |
|
110 | // Do this so we loop through all the packages. |
|
111 | 0 | log.debug(className + ": No Class Definition found"); |
112 | } |
|
113 | 0 | catch (ClassCastException cce) |
114 | { |
|
115 | // This means trouble! |
|
116 | // Alternatively we can throw this exception so |
|
117 | // that it will appear on the client browser |
|
118 | 0 | log.error("Could not load "+className, cce); |
119 | 0 | break; // for() |
120 | } |
|
121 | 0 | catch (InstantiationException ine) |
122 | { |
|
123 | // This means trouble! |
|
124 | // Alternatively we can throw this exception so |
|
125 | // that it will appear on the client browser |
|
126 | 0 | log.error("Could not load "+className, ine); |
127 | 0 | break; // for() |
128 | } |
|
129 | 0 | catch (IllegalAccessException ilae) |
130 | { |
|
131 | // This means trouble! |
|
132 | // Alternatively we can throw this exception so |
|
133 | // that it will appear on the client browser |
|
134 | 0 | log.error("Could not load "+className, ilae); |
135 | 0 | break; // for() |
136 | 252 | } |
137 | // With ClassCastException, InstantiationException we hit big problems |
|
138 | } |
|
139 | } |
|
140 | 138 | log.debug("Returning: " + assembler); |
141 | ||
142 | 138 | return assembler; |
143 | } |
|
144 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |