1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.config; |
20 | |
|
21 | |
import java.io.FileNotFoundException; |
22 | |
import org.apache.myfaces.config.annotation.AnnotationConfigurator; |
23 | |
import org.apache.myfaces.config.element.FacesConfig; |
24 | |
import org.apache.myfaces.config.impl.digester.DigesterFacesConfigUnmarshallerImpl; |
25 | |
import org.apache.myfaces.shared.config.MyfacesConfig; |
26 | |
import org.apache.myfaces.shared.util.ClassUtils; |
27 | |
import org.apache.myfaces.spi.FacesConfigResourceProvider; |
28 | |
import org.apache.myfaces.spi.FacesConfigResourceProviderFactory; |
29 | |
import org.apache.myfaces.spi.FacesConfigurationProvider; |
30 | |
import org.apache.myfaces.spi.ServiceProviderFinderFactory; |
31 | |
import org.xml.sax.SAXException; |
32 | |
|
33 | |
import javax.faces.FacesException; |
34 | |
import javax.faces.FactoryFinder; |
35 | |
import javax.faces.context.ExternalContext; |
36 | |
import javax.faces.webapp.FacesServlet; |
37 | |
import java.io.IOException; |
38 | |
import java.io.InputStream; |
39 | |
import java.io.PushbackInputStream; |
40 | |
import java.io.StringReader; |
41 | |
import java.net.MalformedURLException; |
42 | |
import java.net.URL; |
43 | |
import java.net.URLConnection; |
44 | |
import java.util.ArrayList; |
45 | |
import java.util.Collection; |
46 | |
import java.util.Collections; |
47 | |
import java.util.HashSet; |
48 | |
import java.util.List; |
49 | |
import java.util.ServiceLoader; |
50 | |
import java.util.Set; |
51 | |
import java.util.StringTokenizer; |
52 | |
import java.util.logging.Level; |
53 | |
import java.util.logging.Logger; |
54 | |
import javax.faces.application.ApplicationConfigurationPopulator; |
55 | |
import javax.faces.application.ViewHandler; |
56 | |
import javax.xml.parsers.DocumentBuilder; |
57 | |
import javax.xml.parsers.DocumentBuilderFactory; |
58 | |
import javax.xml.parsers.ParserConfigurationException; |
59 | |
import javax.xml.transform.OutputKeys; |
60 | |
import javax.xml.transform.Transformer; |
61 | |
import javax.xml.transform.TransformerConfigurationException; |
62 | |
import javax.xml.transform.TransformerException; |
63 | |
import javax.xml.transform.TransformerFactory; |
64 | |
import javax.xml.transform.dom.DOMSource; |
65 | |
import javax.xml.transform.stream.StreamResult; |
66 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam; |
67 | |
import org.apache.myfaces.config.element.FacesFlowDefinition; |
68 | |
import org.apache.myfaces.config.element.facelets.FaceletTagLibrary; |
69 | |
import org.apache.myfaces.config.impl.digester.elements.FacesConfigImpl; |
70 | |
import org.apache.myfaces.config.impl.digester.elements.FacesFlowDefinitionImpl; |
71 | |
import org.apache.myfaces.config.impl.digester.elements.FacesFlowReturnImpl; |
72 | |
import org.apache.myfaces.config.impl.digester.elements.NavigationCaseImpl; |
73 | |
import org.apache.myfaces.shared.util.FastWriter; |
74 | |
import org.apache.myfaces.shared.util.WebConfigParamUtils; |
75 | |
import org.apache.myfaces.spi.FaceletConfigResourceProvider; |
76 | |
import org.apache.myfaces.spi.FaceletConfigResourceProviderFactory; |
77 | |
import org.apache.myfaces.spi.ServiceProviderFinder; |
78 | |
import org.apache.myfaces.view.facelets.compiler.TagLibraryConfigUnmarshallerImpl; |
79 | |
import org.w3c.dom.DOMImplementation; |
80 | |
import org.w3c.dom.Document; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | 0 | public class DefaultFacesConfigurationProvider extends FacesConfigurationProvider |
88 | |
{ |
89 | |
|
90 | |
private static final String STANDARD_FACES_CONFIG_RESOURCE = "META-INF/standard-faces-config.xml"; |
91 | |
|
92 | |
|
93 | |
|
94 | |
private static final String DEFAULT_FACES_CONFIG = "/WEB-INF/faces-config.xml"; |
95 | |
|
96 | 0 | private static final Set<String> FACTORY_NAMES = new HashSet<String>(); |
97 | |
static |
98 | |
{ |
99 | 0 | FACTORY_NAMES.add(FactoryFinder.APPLICATION_FACTORY); |
100 | 0 | FACTORY_NAMES.add(FactoryFinder.CLIENT_WINDOW_FACTORY); |
101 | 0 | FACTORY_NAMES.add(FactoryFinder.EXCEPTION_HANDLER_FACTORY); |
102 | 0 | FACTORY_NAMES.add(FactoryFinder.EXTERNAL_CONTEXT_FACTORY); |
103 | 0 | FACTORY_NAMES.add(FactoryFinder.FACELET_CACHE_FACTORY); |
104 | 0 | FACTORY_NAMES.add(FactoryFinder.FACES_CONTEXT_FACTORY); |
105 | 0 | FACTORY_NAMES.add(FactoryFinder.FLASH_FACTORY); |
106 | 0 | FACTORY_NAMES.add(FactoryFinder.FLOW_HANDLER_FACTORY); |
107 | 0 | FACTORY_NAMES.add(FactoryFinder.LIFECYCLE_FACTORY); |
108 | 0 | FACTORY_NAMES.add(FactoryFinder.RENDER_KIT_FACTORY); |
109 | 0 | FACTORY_NAMES.add(FactoryFinder.TAG_HANDLER_DELEGATE_FACTORY); |
110 | 0 | FACTORY_NAMES.add(FactoryFinder.PARTIAL_VIEW_CONTEXT_FACTORY); |
111 | 0 | FACTORY_NAMES.add(FactoryFinder.VISIT_CONTEXT_FACTORY); |
112 | 0 | FACTORY_NAMES.add(FactoryFinder.VIEW_DECLARATION_LANGUAGE_FACTORY); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
@JSFWebConfigParam(since = "2.0", |
119 | |
desc = "Set of .taglib.xml files, separated by ';' that should be loaded by facelet engine.", |
120 | |
deprecated = true) |
121 | |
private final static String PARAM_LIBRARIES_DEPRECATED = "facelets.LIBRARIES"; |
122 | |
|
123 | 0 | private final static String[] PARAMS_LIBRARIES = {ViewHandler.FACELETS_LIBRARIES_PARAM_NAME, |
124 | |
PARAM_LIBRARIES_DEPRECATED}; |
125 | |
|
126 | 0 | private static final Logger log = Logger.getLogger(DefaultFacesConfigurationProvider.class.getName()); |
127 | |
|
128 | |
private FacesConfigUnmarshaller<? extends FacesConfig> _unmarshaller; |
129 | |
|
130 | |
private AnnotationConfigurator _annotationConfigurator; |
131 | |
|
132 | |
protected void setUnmarshaller(ExternalContext ectx, FacesConfigUnmarshaller<? extends FacesConfig> unmarshaller) |
133 | |
{ |
134 | 0 | _unmarshaller = unmarshaller; |
135 | 0 | } |
136 | |
|
137 | |
@SuppressWarnings("unchecked") |
138 | |
protected FacesConfigUnmarshaller<? extends FacesConfig> getUnmarshaller(ExternalContext ectx) |
139 | |
{ |
140 | 0 | if (_unmarshaller == null) |
141 | |
{ |
142 | 0 | _unmarshaller = new DigesterFacesConfigUnmarshallerImpl(ectx); |
143 | |
} |
144 | 0 | return _unmarshaller; |
145 | |
} |
146 | |
|
147 | |
protected void setAnnotationConfigurator(AnnotationConfigurator configurator) |
148 | |
{ |
149 | 0 | _annotationConfigurator = configurator; |
150 | 0 | } |
151 | |
|
152 | |
protected AnnotationConfigurator getAnnotationConfigurator() |
153 | |
{ |
154 | 0 | if (_annotationConfigurator == null) |
155 | |
{ |
156 | 0 | _annotationConfigurator = new AnnotationConfigurator(); |
157 | |
} |
158 | 0 | return _annotationConfigurator; |
159 | |
} |
160 | |
|
161 | |
@Override |
162 | |
public FacesConfig getStandardFacesConfig(ExternalContext ectx) |
163 | |
{ |
164 | |
try |
165 | |
{ |
166 | 0 | if (MyfacesConfig.getCurrentInstance(ectx).isValidateXML()) |
167 | |
{ |
168 | 0 | URL url = ClassUtils.getResource(STANDARD_FACES_CONFIG_RESOURCE); |
169 | 0 | if (url != null) |
170 | |
{ |
171 | 0 | validateFacesConfig(ectx, url); |
172 | |
} |
173 | |
} |
174 | 0 | InputStream stream = ClassUtils.getResourceAsStream(STANDARD_FACES_CONFIG_RESOURCE); |
175 | 0 | if (stream == null) |
176 | |
{ |
177 | 0 | throw new FacesException("Standard faces config " + STANDARD_FACES_CONFIG_RESOURCE + " not found"); |
178 | |
} |
179 | 0 | if (log.isLoggable(Level.INFO)) |
180 | |
{ |
181 | 0 | log.info("Reading standard config " + STANDARD_FACES_CONFIG_RESOURCE); |
182 | |
} |
183 | |
|
184 | 0 | FacesConfig facesConfig = getUnmarshaller(ectx).getFacesConfig(stream, STANDARD_FACES_CONFIG_RESOURCE); |
185 | 0 | stream.close(); |
186 | 0 | return facesConfig; |
187 | |
} |
188 | 0 | catch (IOException e) |
189 | |
{ |
190 | 0 | throw new FacesException(e); |
191 | |
} |
192 | 0 | catch (SAXException e) |
193 | |
{ |
194 | 0 | throw new FacesException(e); |
195 | |
} |
196 | |
} |
197 | |
|
198 | |
@Override |
199 | |
public FacesConfig getAnnotationsFacesConfig(ExternalContext ectx, boolean metadataComplete) |
200 | |
{ |
201 | 0 | return getAnnotationConfigurator().createFacesConfig(ectx, metadataComplete); |
202 | |
} |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
@Override |
208 | |
public FacesConfig getMetaInfServicesFacesConfig(ExternalContext ectx) |
209 | |
{ |
210 | |
try |
211 | |
{ |
212 | 0 | org.apache.myfaces.config.impl.digester.elements.FacesConfigImpl facesConfig |
213 | |
= new org.apache.myfaces.config.impl.digester.elements.FacesConfigImpl(); |
214 | 0 | org.apache.myfaces.config.impl.digester.elements.FactoryImpl factory |
215 | |
= new org.apache.myfaces.config.impl.digester.elements.FactoryImpl(); |
216 | |
|
217 | 0 | facesConfig.addFactory(factory); |
218 | |
|
219 | 0 | for (String factoryName : FACTORY_NAMES) |
220 | |
{ |
221 | 0 | List<String> classList = ServiceProviderFinderFactory.getServiceProviderFinder(ectx) |
222 | |
.getServiceProviderList(factoryName); |
223 | |
|
224 | 0 | for (String className : classList) |
225 | |
{ |
226 | 0 | if (log.isLoggable(Level.INFO)) |
227 | |
{ |
228 | 0 | log.info("Found " + factoryName + " factory implementation: " + className); |
229 | |
} |
230 | |
|
231 | 0 | if (factoryName.equals(FactoryFinder.APPLICATION_FACTORY)) |
232 | |
{ |
233 | 0 | factory.addApplicationFactory(className); |
234 | |
} |
235 | 0 | else if(factoryName.equals(FactoryFinder.EXCEPTION_HANDLER_FACTORY)) |
236 | |
{ |
237 | 0 | factory.addExceptionHandlerFactory(className); |
238 | |
} |
239 | 0 | else if (factoryName.equals(FactoryFinder.EXTERNAL_CONTEXT_FACTORY)) |
240 | |
{ |
241 | 0 | factory.addExternalContextFactory(className); |
242 | |
} |
243 | 0 | else if (factoryName.equals(FactoryFinder.FACES_CONTEXT_FACTORY)) |
244 | |
{ |
245 | 0 | factory.addFacesContextFactory(className); |
246 | |
} |
247 | 0 | else if (factoryName.equals(FactoryFinder.LIFECYCLE_FACTORY)) |
248 | |
{ |
249 | 0 | factory.addLifecycleFactory(className); |
250 | |
} |
251 | 0 | else if (factoryName.equals(FactoryFinder.RENDER_KIT_FACTORY)) |
252 | |
{ |
253 | 0 | factory.addRenderkitFactory(className); |
254 | |
} |
255 | 0 | else if(factoryName.equals(FactoryFinder.TAG_HANDLER_DELEGATE_FACTORY)) |
256 | |
{ |
257 | 0 | factory.addTagHandlerDelegateFactory(className); |
258 | |
} |
259 | 0 | else if (factoryName.equals(FactoryFinder.PARTIAL_VIEW_CONTEXT_FACTORY)) |
260 | |
{ |
261 | 0 | factory.addPartialViewContextFactory(className); |
262 | |
} |
263 | 0 | else if(factoryName.equals(FactoryFinder.VISIT_CONTEXT_FACTORY)) |
264 | |
{ |
265 | 0 | factory.addVisitContextFactory(className); |
266 | |
} |
267 | 0 | else if(factoryName.equals(FactoryFinder.VIEW_DECLARATION_LANGUAGE_FACTORY)) |
268 | |
{ |
269 | 0 | factory.addViewDeclarationLanguageFactory(className); |
270 | |
} |
271 | 0 | else if(factoryName.equals(FactoryFinder.FLASH_FACTORY)) |
272 | |
{ |
273 | 0 | factory.addFlashFactory(className); |
274 | |
} |
275 | 0 | else if(factoryName.equals(FactoryFinder.FLOW_HANDLER_FACTORY)) |
276 | |
{ |
277 | 0 | factory.addFlowHandlerFactory(className); |
278 | |
} |
279 | 0 | else if(factoryName.equals(FactoryFinder.CLIENT_WINDOW_FACTORY)) |
280 | |
{ |
281 | 0 | factory.addClientWindowFactory(className); |
282 | |
} |
283 | 0 | else if(factoryName.equals(FactoryFinder.FACELET_CACHE_FACTORY)) |
284 | |
{ |
285 | 0 | factory.addFaceletCacheFactory(className); |
286 | |
} |
287 | |
else |
288 | |
{ |
289 | 0 | throw new IllegalStateException("Unexpected factory name " + factoryName); |
290 | |
} |
291 | 0 | } |
292 | 0 | } |
293 | 0 | return facesConfig; |
294 | |
} |
295 | 0 | catch (Throwable e) |
296 | |
{ |
297 | 0 | throw new FacesException(e); |
298 | |
} |
299 | |
} |
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
@Override |
305 | |
public List<FacesConfig> getClassloaderFacesConfig(ExternalContext ectx) |
306 | |
{ |
307 | 0 | List<FacesConfig> appConfigResources = new ArrayList<FacesConfig>(); |
308 | |
try |
309 | |
{ |
310 | 0 | FacesConfigResourceProvider provider = FacesConfigResourceProviderFactory. |
311 | |
getFacesConfigResourceProviderFactory(ectx).createFacesConfigResourceProvider(ectx); |
312 | |
|
313 | 0 | Collection<URL> facesConfigs = provider.getMetaInfConfigurationResources(ectx); |
314 | |
|
315 | 0 | for (URL url : facesConfigs) |
316 | |
{ |
317 | 0 | if (MyfacesConfig.getCurrentInstance(ectx).isValidateXML()) |
318 | |
{ |
319 | 0 | validateFacesConfig(ectx, url); |
320 | |
} |
321 | 0 | InputStream stream = null; |
322 | |
try |
323 | |
{ |
324 | 0 | stream = openStreamWithoutCache(url); |
325 | 0 | if (log.isLoggable(Level.INFO)) |
326 | |
{ |
327 | 0 | log.info("Reading config : " + url.toExternalForm()); |
328 | |
} |
329 | 0 | appConfigResources.add(getUnmarshaller(ectx).getFacesConfig(stream, url.toExternalForm())); |
330 | |
|
331 | |
} |
332 | |
finally |
333 | |
{ |
334 | 0 | if (stream != null) |
335 | |
{ |
336 | 0 | stream.close(); |
337 | |
} |
338 | |
} |
339 | 0 | } |
340 | |
} |
341 | 0 | catch (Throwable e) |
342 | |
{ |
343 | 0 | throw new FacesException(e); |
344 | 0 | } |
345 | 0 | return appConfigResources; |
346 | |
} |
347 | |
|
348 | |
@Override |
349 | |
public List<FacesConfig> getContextSpecifiedFacesConfig(ExternalContext ectx) |
350 | |
{ |
351 | 0 | List<FacesConfig> appConfigResources = new ArrayList<FacesConfig>(); |
352 | |
try |
353 | |
{ |
354 | 0 | for (String systemId : getConfigFilesList(ectx)) |
355 | |
{ |
356 | 0 | if (MyfacesConfig.getCurrentInstance(ectx).isValidateXML()) |
357 | |
{ |
358 | 0 | URL url = ectx.getResource(systemId); |
359 | 0 | if (url != null) |
360 | |
{ |
361 | 0 | validateFacesConfig(ectx, url); |
362 | |
} |
363 | |
} |
364 | 0 | InputStream stream = ectx.getResourceAsStream(systemId); |
365 | 0 | if (stream == null) |
366 | |
{ |
367 | 0 | log.severe("Faces config resource " + systemId + " not found"); |
368 | 0 | continue; |
369 | |
} |
370 | |
|
371 | 0 | if (log.isLoggable(Level.INFO)) |
372 | |
{ |
373 | 0 | log.info("Reading config " + systemId); |
374 | |
} |
375 | 0 | appConfigResources.add(getUnmarshaller(ectx).getFacesConfig(stream, systemId)); |
376 | |
|
377 | 0 | stream.close(); |
378 | 0 | } |
379 | |
} |
380 | 0 | catch (Throwable e) |
381 | |
{ |
382 | 0 | throw new FacesException(e); |
383 | 0 | } |
384 | 0 | return appConfigResources; |
385 | |
} |
386 | |
|
387 | |
@Override |
388 | |
public FacesConfig getWebAppFacesConfig(ExternalContext ectx) |
389 | |
{ |
390 | |
try |
391 | |
{ |
392 | 0 | FacesConfig webAppConfig = null; |
393 | |
|
394 | 0 | if (MyfacesConfig.getCurrentInstance(ectx).isValidateXML()) |
395 | |
{ |
396 | 0 | URL url = ectx.getResource(DEFAULT_FACES_CONFIG); |
397 | 0 | if (url != null) |
398 | |
{ |
399 | 0 | validateFacesConfig(ectx, url); |
400 | |
} |
401 | |
} |
402 | 0 | InputStream stream = ectx.getResourceAsStream(DEFAULT_FACES_CONFIG); |
403 | 0 | if (stream != null) |
404 | |
{ |
405 | 0 | if (log.isLoggable(Level.INFO)) |
406 | |
{ |
407 | 0 | log.info("Reading config /WEB-INF/faces-config.xml"); |
408 | |
} |
409 | 0 | webAppConfig = getUnmarshaller(ectx).getFacesConfig(stream, DEFAULT_FACES_CONFIG); |
410 | |
|
411 | 0 | stream.close(); |
412 | |
} |
413 | 0 | return webAppConfig; |
414 | |
} |
415 | 0 | catch (IOException e) |
416 | |
{ |
417 | 0 | throw new FacesException(e); |
418 | |
} |
419 | 0 | catch (SAXException e) |
420 | |
{ |
421 | 0 | throw new FacesException(e); |
422 | |
} |
423 | |
|
424 | |
} |
425 | |
|
426 | |
private InputStream openStreamWithoutCache(URL url) throws IOException |
427 | |
{ |
428 | 0 | URLConnection connection = url.openConnection(); |
429 | 0 | connection.setUseCaches(false); |
430 | 0 | return connection.getInputStream(); |
431 | |
} |
432 | |
|
433 | |
private List<String> getConfigFilesList(ExternalContext ectx) |
434 | |
{ |
435 | 0 | String configFiles = ectx.getInitParameter(FacesServlet.CONFIG_FILES_ATTR); |
436 | 0 | List<String> configFilesList = new ArrayList<String>(); |
437 | 0 | if (configFiles != null) |
438 | |
{ |
439 | 0 | StringTokenizer st = new StringTokenizer(configFiles, ",", false); |
440 | 0 | while (st.hasMoreTokens()) |
441 | |
{ |
442 | 0 | String systemId = st.nextToken().trim(); |
443 | |
|
444 | 0 | if (DEFAULT_FACES_CONFIG.equals(systemId)) |
445 | |
{ |
446 | 0 | if (log.isLoggable(Level.WARNING)) |
447 | |
{ |
448 | 0 | log.warning(DEFAULT_FACES_CONFIG + " has been specified in the " |
449 | |
+ FacesServlet.CONFIG_FILES_ATTR |
450 | |
+ " context parameter of " |
451 | |
+ "the deployment descriptor. This will automatically be removed, " |
452 | |
+ "if we wouldn't do this, it would be loaded twice. See JSF spec 1.1, 10.3.2"); |
453 | |
} |
454 | |
} |
455 | |
else |
456 | |
{ |
457 | 0 | configFilesList.add(systemId); |
458 | |
} |
459 | 0 | } |
460 | |
} |
461 | 0 | return configFilesList; |
462 | |
} |
463 | |
|
464 | |
private void validateFacesConfig(ExternalContext ectx, URL url) throws IOException, SAXException |
465 | |
{ |
466 | 0 | String version = ConfigFilesXmlValidationUtils.getFacesConfigVersion(url); |
467 | 0 | if ("1.2".equals(version) || "2.0".equals(version) || "2.1".equals(version)) |
468 | |
{ |
469 | 0 | ConfigFilesXmlValidationUtils.validateFacesConfigFile(url, ectx, version); |
470 | |
} |
471 | 0 | } |
472 | |
|
473 | |
@Override |
474 | |
public List<FacesConfig> getApplicationConfigurationResourceDocumentPopulatorFacesConfig(ExternalContext ectx) |
475 | |
{ |
476 | 0 | ServiceProviderFinder spff = ServiceProviderFinderFactory.getServiceProviderFinder(ectx); |
477 | 0 | ServiceLoader<ApplicationConfigurationPopulator> instances = |
478 | |
spff.load(ApplicationConfigurationPopulator.class); |
479 | 0 | if (instances != null) |
480 | |
{ |
481 | 0 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
482 | |
|
483 | 0 | factory.setNamespaceAware(true); |
484 | |
|
485 | 0 | factory.setValidating(false); |
486 | |
|
487 | 0 | DocumentBuilder builder = null; |
488 | 0 | DOMImplementation domImpl = null; |
489 | |
try |
490 | |
{ |
491 | 0 | builder = factory.newDocumentBuilder(); |
492 | 0 | domImpl = builder.getDOMImplementation(); |
493 | |
} |
494 | 0 | catch (ParserConfigurationException ex) |
495 | |
{ |
496 | 0 | log.log(Level.SEVERE, "Cannot create dom document builder, skipping it", ex); |
497 | 0 | } |
498 | |
|
499 | 0 | if (builder != null) |
500 | |
{ |
501 | 0 | List<FacesConfig> facesConfigList = new ArrayList<FacesConfig>(); |
502 | 0 | List<Document> documentList = new ArrayList<Document>(); |
503 | 0 | for (ApplicationConfigurationPopulator populator : instances) |
504 | |
{ |
505 | |
|
506 | |
|
507 | |
|
508 | 0 | Document document = domImpl.createDocument( |
509 | |
"http://java.sun.com/xml/ns/javaee", "faces-config", null); |
510 | |
|
511 | 0 | populator.populateApplicationConfiguration(document); |
512 | 0 | documentList.add(document); |
513 | 0 | } |
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | 0 | Transformer trans = null; |
520 | |
try |
521 | |
{ |
522 | 0 | trans = TransformerFactory.newInstance().newTransformer(); |
523 | 0 | trans.setOutputProperty(OutputKeys.INDENT, "no"); |
524 | 0 | trans.setOutputProperty(OutputKeys.METHOD, "xml"); |
525 | 0 | trans.setOutputProperty(OutputKeys.VERSION, "1.0"); |
526 | 0 | trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); |
527 | |
} |
528 | 0 | catch (TransformerConfigurationException ex) |
529 | |
{ |
530 | 0 | Logger.getLogger(DefaultFacesConfigurationProvider.class.getName()).log(Level.SEVERE, null, ex); |
531 | 0 | } |
532 | |
|
533 | 0 | if (trans != null) |
534 | |
{ |
535 | 0 | FastWriter xmlAsWriter = new FastWriter(); |
536 | 0 | for (int i = 0; i < documentList.size(); i++) |
537 | |
{ |
538 | 0 | Document document = documentList.get(i); |
539 | 0 | xmlAsWriter.reset(); |
540 | |
try |
541 | |
{ |
542 | 0 | DOMSource source = new DOMSource(document); |
543 | 0 | StreamResult result = new StreamResult(xmlAsWriter); |
544 | |
|
545 | 0 | trans.transform(source, result); |
546 | |
|
547 | 0 | StringReader xmlReader = new StringReader(xmlAsWriter.toString()); |
548 | 0 | FacesConfig facesConfig = getUnmarshaller(ectx).getFacesConfig( |
549 | |
xmlReader); |
550 | 0 | facesConfigList.add(facesConfig); |
551 | |
} |
552 | 0 | catch (IOException ex) |
553 | |
{ |
554 | 0 | log.log(Level.SEVERE, "Error while reading faces-config from populator", ex); |
555 | |
} |
556 | 0 | catch (SAXException ex) |
557 | |
{ |
558 | 0 | log.log(Level.SEVERE, "Error while reading faces-config from populator", ex); |
559 | |
} |
560 | 0 | catch (TransformerConfigurationException ex) |
561 | |
{ |
562 | 0 | log.log(Level.SEVERE, "Error while reading faces-config from populator", ex); |
563 | |
} |
564 | 0 | catch (TransformerException ex) |
565 | |
{ |
566 | 0 | log.log(Level.SEVERE, "Error while reading faces-config from populator", ex); |
567 | 0 | } |
568 | |
} |
569 | 0 | return facesConfigList; |
570 | |
} |
571 | |
else |
572 | |
{ |
573 | 0 | log.log(Level.SEVERE, "Cannot create xml transformer, skipping it"); |
574 | |
} |
575 | |
} |
576 | |
} |
577 | 0 | return Collections.emptyList(); |
578 | |
} |
579 | |
|
580 | |
@Override |
581 | |
public List<FacesConfig> getFacesFlowFacesConfig(ExternalContext ectx) |
582 | |
{ |
583 | |
List<FacesConfig> configFilesList; |
584 | 0 | Set<String> directoryPaths = ectx.getResourcePaths("/"); |
585 | 0 | if (directoryPaths == null) |
586 | |
{ |
587 | 0 | return Collections.emptyList(); |
588 | |
} |
589 | 0 | configFilesList = new ArrayList<FacesConfig>(); |
590 | |
|
591 | 0 | List<String> contextSpecifiedList = getConfigFilesList(ectx); |
592 | |
|
593 | 0 | for (String dirPath : directoryPaths) |
594 | |
{ |
595 | 0 | if (dirPath.equals("/WEB-INF/")) |
596 | |
{ |
597 | |
|
598 | 0 | Set<String> webDirectoryPaths = ectx.getResourcePaths(dirPath); |
599 | 0 | for (String webDirPath : webDirectoryPaths) |
600 | |
{ |
601 | 0 | if (webDirPath.endsWith("/") && |
602 | |
!webDirPath.equals("/WEB-INF/classes/")) |
603 | |
{ |
604 | 0 | String flowName = webDirPath.substring(9, webDirPath.length() - 1); |
605 | 0 | String filePath = webDirPath+flowName+"-flow.xml"; |
606 | 0 | if (!contextSpecifiedList.contains(filePath)) |
607 | |
{ |
608 | |
try |
609 | |
{ |
610 | 0 | URL url = ectx.getResource(filePath); |
611 | 0 | if (url != null) |
612 | |
{ |
613 | 0 | FacesConfig fc = parseFacesConfig(ectx, filePath, url); |
614 | 0 | if (fc != null) |
615 | |
{ |
616 | 0 | configFilesList.add(fc); |
617 | |
} |
618 | |
} |
619 | |
} |
620 | 0 | catch (MalformedURLException ex) |
621 | |
{ |
622 | 0 | } |
623 | |
} |
624 | |
} |
625 | 0 | } |
626 | 0 | } |
627 | 0 | else if (!dirPath.startsWith("/META-INF") && dirPath.endsWith("/")) |
628 | |
{ |
629 | |
|
630 | 0 | String flowName = dirPath.substring(1, dirPath.length() - 1); |
631 | 0 | String filePath = dirPath+flowName+"-flow.xml"; |
632 | 0 | if (!contextSpecifiedList.contains(filePath)) |
633 | |
{ |
634 | |
try |
635 | |
{ |
636 | 0 | URL url = ectx.getResource(filePath); |
637 | 0 | if (url != null) |
638 | |
{ |
639 | 0 | FacesConfig fc = parseFacesConfig(ectx, filePath, url); |
640 | 0 | if (fc != null) |
641 | |
{ |
642 | 0 | configFilesList.add(fc); |
643 | |
} |
644 | |
} |
645 | |
} |
646 | 0 | catch (MalformedURLException ex) |
647 | |
{ |
648 | 0 | } |
649 | |
} |
650 | |
} |
651 | 0 | } |
652 | |
|
653 | 0 | return configFilesList; |
654 | |
} |
655 | |
|
656 | |
private FacesConfig parseFacesConfig(ExternalContext ectx, String systemId, URL url) |
657 | |
{ |
658 | |
try |
659 | |
{ |
660 | 0 | if (MyfacesConfig.getCurrentInstance(ectx).isValidateXML()) |
661 | |
{ |
662 | |
|
663 | |
|
664 | |
|
665 | 0 | validateFacesConfig(ectx, url); |
666 | |
|
667 | |
} |
668 | |
} |
669 | 0 | catch (IOException e) |
670 | |
{ |
671 | 0 | throw new FacesException(e); |
672 | |
} |
673 | 0 | catch (SAXException e) |
674 | |
{ |
675 | 0 | throw new FacesException(e); |
676 | 0 | } |
677 | 0 | InputStream stream = ectx.getResourceAsStream(systemId); |
678 | 0 | PushbackInputStream pbstream = new PushbackInputStream(stream, 10); |
679 | |
try |
680 | |
{ |
681 | 0 | if (stream == null) |
682 | |
{ |
683 | 0 | log.severe("Faces config resource " + systemId + " not found"); |
684 | 0 | return null; |
685 | |
} |
686 | 0 | String flowName = systemId.substring(systemId.lastIndexOf('/')+1, systemId.lastIndexOf("-flow.xml")); |
687 | 0 | int c = pbstream.read(); |
688 | 0 | if (c != -1) |
689 | |
{ |
690 | 0 | pbstream.unread(c); |
691 | |
} |
692 | |
else |
693 | |
{ |
694 | |
|
695 | |
|
696 | |
|
697 | 0 | FacesConfigImpl facesConfig = new FacesConfigImpl(); |
698 | 0 | FacesFlowDefinitionImpl flow = new FacesFlowDefinitionImpl(); |
699 | 0 | flow.setId(flowName); |
700 | |
|
701 | 0 | flow.setDefiningDocumentId(systemId); |
702 | |
|
703 | 0 | String startNodePath = systemId.substring(0, systemId.lastIndexOf('/')+1)+flowName+".xhtml"; |
704 | |
|
705 | |
|
706 | |
|
707 | 0 | flow.setStartNode(startNodePath); |
708 | |
|
709 | |
|
710 | |
|
711 | |
|
712 | 0 | FacesFlowReturnImpl returnNode = new FacesFlowReturnImpl(); |
713 | 0 | returnNode.setId(flowName+"-return"); |
714 | 0 | NavigationCaseImpl returnNavCase = new NavigationCaseImpl(); |
715 | 0 | returnNavCase.setFromOutcome("/"+flowName+"-return"); |
716 | 0 | returnNode.setNavigationCase(returnNavCase); |
717 | 0 | flow.addReturn(returnNode); |
718 | |
|
719 | 0 | facesConfig.addFacesFlowDefinition(flow); |
720 | 0 | return facesConfig; |
721 | |
} |
722 | |
|
723 | 0 | if (log.isLoggable(Level.INFO)) |
724 | |
{ |
725 | 0 | log.info("Reading config " + systemId); |
726 | |
} |
727 | |
|
728 | 0 | FacesConfigImpl facesConfig = (FacesConfigImpl) |
729 | |
getUnmarshaller(ectx).getFacesConfig(pbstream, systemId); |
730 | |
|
731 | |
|
732 | 0 | for (FacesFlowDefinition definition : facesConfig.getFacesFlowDefinitions()) |
733 | |
{ |
734 | 0 | if (flowName.equals(definition.getId())) |
735 | |
{ |
736 | 0 | FacesFlowDefinitionImpl flow = (FacesFlowDefinitionImpl) definition; |
737 | 0 | if (flow.getStartNode() == null) |
738 | |
{ |
739 | 0 | String startNodePath = systemId.substring(0, |
740 | |
systemId.lastIndexOf('/')+1)+flowName+".xhtml"; |
741 | 0 | flow.setStartNode(startNodePath); |
742 | |
} |
743 | |
} |
744 | 0 | } |
745 | 0 | return facesConfig; |
746 | |
|
747 | |
} |
748 | 0 | catch (IOException e) |
749 | |
{ |
750 | 0 | throw new FacesException(e); |
751 | |
} |
752 | 0 | catch (SAXException e) |
753 | |
{ |
754 | 0 | throw new FacesException(e); |
755 | |
} |
756 | |
finally |
757 | |
{ |
758 | 0 | if (stream != null) |
759 | |
{ |
760 | |
try |
761 | |
{ |
762 | 0 | stream.close(); |
763 | |
} |
764 | 0 | catch (IOException ex) |
765 | |
{ |
766 | |
|
767 | 0 | } |
768 | |
} |
769 | |
} |
770 | |
} |
771 | |
|
772 | |
@Override |
773 | |
public List<FacesConfig> getFaceletTaglibFacesConfig(ExternalContext externalContext) |
774 | |
{ |
775 | 0 | List<FacesConfig> facesConfigFilesList = new ArrayList<FacesConfig>(); |
776 | |
|
777 | 0 | String param = WebConfigParamUtils.getStringInitParameter(externalContext, PARAMS_LIBRARIES); |
778 | 0 | if (param != null) |
779 | |
{ |
780 | 0 | for (String library : param.split(";")) |
781 | |
{ |
782 | |
try |
783 | |
{ |
784 | 0 | URL src = externalContext.getResource(library.trim()); |
785 | 0 | if (src == null) |
786 | |
{ |
787 | 0 | throw new FileNotFoundException(library); |
788 | |
} |
789 | |
|
790 | 0 | FaceletTagLibrary tl = TagLibraryConfigUnmarshallerImpl.create(externalContext, src); |
791 | 0 | if (tl != null) |
792 | |
{ |
793 | 0 | org.apache.myfaces.config.impl.digester.elements.FacesConfigImpl config = |
794 | |
new org.apache.myfaces.config.impl.digester.elements.FacesConfigImpl(); |
795 | 0 | config.addFaceletTagLibrary(tl); |
796 | 0 | facesConfigFilesList.add(config); |
797 | |
} |
798 | 0 | if (log.isLoggable(Level.FINE)) |
799 | |
{ |
800 | 0 | log.fine("Successfully loaded library: " + library); |
801 | |
} |
802 | |
} |
803 | 0 | catch (IOException e) |
804 | |
{ |
805 | 0 | log.log(Level.SEVERE, "Error Loading library: " + library, e); |
806 | 0 | } |
807 | |
} |
808 | |
} |
809 | |
|
810 | |
try |
811 | |
{ |
812 | 0 | FaceletConfigResourceProvider provider = FaceletConfigResourceProviderFactory. |
813 | |
getFacesConfigResourceProviderFactory(externalContext). |
814 | |
createFaceletConfigResourceProvider(externalContext); |
815 | 0 | Collection<URL> urls = provider.getFaceletTagLibConfigurationResources(externalContext); |
816 | 0 | for (URL url : urls) |
817 | |
{ |
818 | |
try |
819 | |
{ |
820 | 0 | FaceletTagLibrary tl = TagLibraryConfigUnmarshallerImpl.create(externalContext, url); |
821 | 0 | if (tl != null) |
822 | |
{ |
823 | 0 | org.apache.myfaces.config.impl.digester.elements.FacesConfigImpl config = |
824 | |
new org.apache.myfaces.config.impl.digester.elements.FacesConfigImpl(); |
825 | 0 | config.addFaceletTagLibrary(tl); |
826 | 0 | facesConfigFilesList.add(config); |
827 | |
} |
828 | 0 | if (log.isLoggable(Level.FINE)) |
829 | |
{ |
830 | |
|
831 | 0 | log.fine("Added Library from: " + url); |
832 | |
} |
833 | |
} |
834 | 0 | catch (Exception e) |
835 | |
{ |
836 | |
|
837 | 0 | log.log(Level.SEVERE, "Error Loading Library: " + url, e); |
838 | 0 | } |
839 | 0 | } |
840 | |
} |
841 | 0 | catch (IOException e) |
842 | |
{ |
843 | 0 | log.log(Level.SEVERE, "Compiler Initialization Error", e); |
844 | 0 | } |
845 | 0 | return facesConfigFilesList; |
846 | |
} |
847 | |
} |