1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package javax.faces; |
20 | |
|
21 | |
|
22 | |
import javax.faces.application.ApplicationFactory; |
23 | |
import javax.faces.context.FacesContextFactory; |
24 | |
import javax.faces.lifecycle.LifecycleFactory; |
25 | |
import javax.faces.render.RenderKitFactory; |
26 | |
import java.lang.reflect.Constructor; |
27 | |
import java.lang.reflect.InvocationTargetException; |
28 | |
import java.util.*; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public final class FactoryFinder |
37 | |
{ |
38 | |
public static final String APPLICATION_FACTORY = "javax.faces.application.ApplicationFactory"; |
39 | |
public static final String FACES_CONTEXT_FACTORY = "javax.faces.context.FacesContextFactory"; |
40 | |
public static final String LIFECYCLE_FACTORY = "javax.faces.lifecycle.LifecycleFactory"; |
41 | |
public static final String RENDER_KIT_FACTORY = "javax.faces.render.RenderKitFactory"; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | private static Map<ClassLoader, Map> _registeredFactoryNames = new HashMap<ClassLoader, Map>(); |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | private static Map<ClassLoader, Map> _factories = new HashMap<ClassLoader, Map>(); |
57 | |
|
58 | 0 | private static final Set<String> VALID_FACTORY_NAMES = new HashSet<String>(); |
59 | 0 | private static final Map<String, Class> ABSTRACT_FACTORY_CLASSES = new HashMap<String, Class>(); |
60 | |
static { |
61 | 0 | VALID_FACTORY_NAMES.add(APPLICATION_FACTORY); |
62 | 0 | VALID_FACTORY_NAMES.add(FACES_CONTEXT_FACTORY); |
63 | 0 | VALID_FACTORY_NAMES.add(LIFECYCLE_FACTORY); |
64 | 0 | VALID_FACTORY_NAMES.add(RENDER_KIT_FACTORY); |
65 | |
|
66 | 0 | ABSTRACT_FACTORY_CLASSES.put(APPLICATION_FACTORY, ApplicationFactory.class); |
67 | 0 | ABSTRACT_FACTORY_CLASSES.put(FACES_CONTEXT_FACTORY, FacesContextFactory.class); |
68 | 0 | ABSTRACT_FACTORY_CLASSES.put(LIFECYCLE_FACTORY, LifecycleFactory.class); |
69 | 0 | ABSTRACT_FACTORY_CLASSES.put(RENDER_KIT_FACTORY, RenderKitFactory.class); |
70 | 0 | } |
71 | |
|
72 | |
|
73 | |
|
74 | 0 | FactoryFinder() { |
75 | 0 | } |
76 | |
|
77 | |
public static Object getFactory(String factoryName) |
78 | |
throws FacesException |
79 | |
{ |
80 | 0 | if(factoryName == null) |
81 | 0 | throw new NullPointerException("factoryName may not be null"); |
82 | |
|
83 | 0 | ClassLoader classLoader = getClassLoader(); |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | 0 | Map factoryClassNames = null; |
89 | 0 | Map<String, Object> factoryMap = null; |
90 | |
|
91 | 0 | synchronized(_registeredFactoryNames) |
92 | |
{ |
93 | 0 | factoryClassNames = _registeredFactoryNames.get(classLoader); |
94 | |
|
95 | 0 | if (factoryClassNames == null) |
96 | |
{ |
97 | 0 | String message = "No Factories configured for this Application. This happens if the faces-initialization "+ |
98 | |
"does not work at all - make sure that you properly include all configuration settings necessary for a basic faces application " + |
99 | |
"and that all the necessary libs are included. Also check the logging output of your web application and your container for any exceptions!" + |
100 | |
"\nIf you did that and find nothing, the mistake might be due to the fact that you use some special web-containers which "+ |
101 | |
"do not support registering context-listeners via TLD files and " + |
102 | |
"a context listener is not setup in your web.xml.\n" + |
103 | |
"A typical config looks like this;\n<listener>\n" + |
104 | |
" <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>\n" + |
105 | |
"</listener>\n"; |
106 | 0 | throw new IllegalStateException(message); |
107 | |
} |
108 | |
|
109 | 0 | if (! factoryClassNames.containsKey(factoryName)) |
110 | |
{ |
111 | 0 | throw new IllegalArgumentException("no factory " + factoryName + " configured for this application."); |
112 | |
} |
113 | |
|
114 | 0 | factoryMap = _factories.get(classLoader); |
115 | |
|
116 | 0 | if (factoryMap == null) |
117 | |
{ |
118 | 0 | factoryMap = new HashMap<String, Object>(); |
119 | 0 | _factories.put(classLoader, factoryMap); |
120 | |
} |
121 | 0 | } |
122 | |
|
123 | 0 | List classNames = null; |
124 | 0 | Object factory = null; |
125 | 0 | synchronized (factoryClassNames) |
126 | |
{ |
127 | 0 | factory = factoryMap.get(factoryName); |
128 | 0 | if (factory != null) |
129 | |
{ |
130 | 0 | return factory; |
131 | |
} |
132 | 0 | classNames = (List) factoryClassNames.get(factoryName); |
133 | 0 | } |
134 | |
|
135 | |
|
136 | 0 | factory = newFactoryInstance(ABSTRACT_FACTORY_CLASSES.get(factoryName), classNames.iterator(), classLoader); |
137 | |
|
138 | 0 | synchronized (factoryClassNames) |
139 | |
{ |
140 | |
|
141 | 0 | if (factoryMap.get(factoryName) == null) |
142 | |
{ |
143 | 0 | factoryMap.put(factoryName, factory); |
144 | |
} |
145 | 0 | } |
146 | 0 | return factory; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
private static Object newFactoryInstance(Class interfaceClass, Iterator classNamesIterator, ClassLoader classLoader) |
151 | |
{ |
152 | |
try |
153 | |
{ |
154 | 0 | Object current = null; |
155 | |
|
156 | 0 | while (classNamesIterator.hasNext()) |
157 | |
{ |
158 | 0 | String implClassName = (String) classNamesIterator.next(); |
159 | 0 | Class implClass = classLoader.loadClass(implClassName); |
160 | |
|
161 | |
|
162 | 0 | if (!interfaceClass.isAssignableFrom(implClass)) |
163 | |
{ |
164 | 0 | throw new IllegalArgumentException("Class " + implClassName + " is no " + interfaceClass.getName()); |
165 | |
} |
166 | |
|
167 | 0 | if (current == null) |
168 | |
{ |
169 | |
|
170 | 0 | current = implClass.newInstance(); |
171 | |
} else |
172 | |
{ |
173 | |
|
174 | |
try |
175 | |
{ |
176 | 0 | Constructor delegationConstructor = implClass.getConstructor(new Class[]{interfaceClass}); |
177 | |
|
178 | |
try |
179 | |
{ |
180 | |
|
181 | 0 | current = delegationConstructor.newInstance(new Object[]{current}); |
182 | 0 | } catch (InstantiationException e) |
183 | |
{ |
184 | 0 | throw new FacesException(e); |
185 | 0 | } catch (IllegalAccessException e) |
186 | |
{ |
187 | 0 | throw new FacesException(e); |
188 | 0 | } catch (InvocationTargetException e) |
189 | |
{ |
190 | 0 | throw new FacesException(e); |
191 | 0 | } |
192 | 0 | } catch (NoSuchMethodException e) |
193 | |
{ |
194 | |
|
195 | 0 | current = implClass.newInstance(); |
196 | 0 | } |
197 | |
} |
198 | 0 | } |
199 | |
|
200 | 0 | return current; |
201 | 0 | } catch (ClassNotFoundException e) |
202 | |
{ |
203 | 0 | throw new FacesException(e); |
204 | 0 | } catch (InstantiationException e) |
205 | |
{ |
206 | 0 | throw new FacesException(e); |
207 | 0 | } catch (IllegalAccessException e) |
208 | |
{ |
209 | 0 | throw new FacesException(e); |
210 | |
} |
211 | |
} |
212 | |
|
213 | |
|
214 | |
public static void setFactory(String factoryName, |
215 | |
String implName) |
216 | |
{ |
217 | 0 | checkFactoryName(factoryName); |
218 | |
|
219 | 0 | ClassLoader classLoader = getClassLoader(); |
220 | 0 | Map<String, List> factoryClassNames = null; |
221 | 0 | synchronized(_registeredFactoryNames) |
222 | |
{ |
223 | 0 | Map factories = _factories.get(classLoader); |
224 | |
|
225 | 0 | if (factories != null && factories.containsKey(factoryName)) { |
226 | |
|
227 | |
|
228 | 0 | return; |
229 | |
} |
230 | |
|
231 | 0 | factoryClassNames = _registeredFactoryNames.get(classLoader); |
232 | |
|
233 | 0 | if (factoryClassNames == null) |
234 | |
{ |
235 | 0 | factoryClassNames = new HashMap<String, List>(); |
236 | 0 | _registeredFactoryNames.put(classLoader, factoryClassNames); |
237 | |
} |
238 | 0 | } |
239 | 0 | synchronized (factoryClassNames) |
240 | |
{ |
241 | 0 | List<String> classNameList = factoryClassNames.get(factoryName); |
242 | |
|
243 | 0 | if (classNameList == null) |
244 | |
{ |
245 | 0 | classNameList = new ArrayList<String>(); |
246 | 0 | factoryClassNames.put(factoryName, classNameList); |
247 | |
} |
248 | 0 | classNameList.add(implName); |
249 | 0 | } |
250 | 0 | } |
251 | |
|
252 | |
|
253 | |
public static void releaseFactories() |
254 | |
throws FacesException |
255 | |
{ |
256 | 0 | ClassLoader classLoader = getClassLoader(); |
257 | |
|
258 | |
|
259 | 0 | synchronized(_registeredFactoryNames) |
260 | |
{ |
261 | 0 | _factories.remove(classLoader); |
262 | |
|
263 | |
|
264 | |
|
265 | 0 | Map factoryClassNames = (Map) _registeredFactoryNames.get(classLoader); |
266 | 0 | if (factoryClassNames != null) factoryClassNames.clear(); |
267 | 0 | _registeredFactoryNames.remove(classLoader); |
268 | 0 | } |
269 | 0 | } |
270 | |
|
271 | |
private static void checkFactoryName(String factoryName) |
272 | |
{ |
273 | 0 | if (! VALID_FACTORY_NAMES.contains(factoryName)) |
274 | |
{ |
275 | 0 | throw new IllegalArgumentException("factoryName '" + factoryName + "'"); |
276 | |
} |
277 | 0 | } |
278 | |
|
279 | |
|
280 | |
private static ClassLoader getClassLoader() |
281 | |
{ |
282 | |
try |
283 | |
{ |
284 | 0 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
285 | 0 | if (classLoader == null) |
286 | |
{ |
287 | 0 | throw new FacesException("web application class loader cannot be identified", null); |
288 | |
} |
289 | 0 | return classLoader; |
290 | |
} |
291 | 0 | catch (Exception e) |
292 | |
{ |
293 | 0 | throw new FacesException("web application class loader cannot be identified", e); |
294 | |
} |
295 | |
} |
296 | |
} |