📄 facesconfigurator.java
字号:
{ if (entry.getName().equals("META-INF/faces-config.xml")) { if (log.isDebugEnabled()) log.debug("faces-config.xml found in " + jarPath); found = true; break; } entry = jar.getNextJarEntry(); } jar.close(); File tmp = null; // 3. if faces-config.xml was found, extract the jar and copy it to a temp file; hand over the temp file // to the parser and delete it afterwards if (found) { tmp = File.createTempFile("myfaces", ".jar"); in = _externalContext.getResourceAsStream(jarPath); FileOutputStream out = new FileOutputStream(tmp); byte[] buffer = new byte[4096]; int r; while ((r = in.read(buffer)) != -1) { out.write(buffer, 0, r); } out.close(); JarFile jarFile = new JarFile(tmp); try { JarEntry configFile = jarFile.getJarEntry("META-INF/faces-config.xml"); if (configFile != null) { if (log.isDebugEnabled()) log.debug("faces-config.xml found in jar " + jarPath); InputStream stream = jarFile.getInputStream(configFile); String systemId = "jar:" + tmp.toURL() + "!/" + configFile.getName(); if (log.isInfoEnabled()) log.info("Reading config " + systemId); _dispenser.feed(_unmarshaller.getFacesConfig(stream, systemId)); } } finally { jarFile.close(); tmp.delete(); } } else { if (log.isDebugEnabled()) log.debug("Jar " + jarPath + " contains no faces-config.xml"); } } catch (Exception e) { throw new FacesException(e); } } private void feedContextSpecifiedConfig() throws IOException, SAXException { String configFiles = _externalContext.getInitParameter(FacesServlet.CONFIG_FILES_ATTR); if (configFiles != null) { StringTokenizer st = new StringTokenizer(configFiles, ",", false); while (st.hasMoreTokens()) { String systemId = st.nextToken().trim(); InputStream stream = _externalContext.getResourceAsStream(systemId); if (stream == null) { log.error("Faces config resource " + systemId + " not found"); continue; } if (log.isInfoEnabled()) log.info("Reading config " + systemId); _dispenser.feed(_unmarshaller.getFacesConfig(stream, systemId)); } } } private void feedWebAppConfig() throws IOException, SAXException { //web application config String systemId = "/WEB-INF/faces-config.xml"; InputStream stream = _externalContext.getResourceAsStream(systemId); if (stream != null) { if (log.isInfoEnabled()) log.info("Reading config /WEB-INF/faces-config.xml"); _dispenser.feed(_unmarshaller.getFacesConfig(stream, systemId)); } } private void configureFactories() { setFactories(FactoryFinder.APPLICATION_FACTORY, _dispenser.getApplicationFactoryIterator(), DEFAULT_APPLICATION_FACTORY); setFactories(FactoryFinder.FACES_CONTEXT_FACTORY, _dispenser.getFacesContextFactoryIterator(), DEFAULT_FACES_CONTEXT_FACTORY); setFactories(FactoryFinder.LIFECYCLE_FACTORY, _dispenser.getLifecycleFactoryIterator(), DEFAULT_LIFECYCLE_FACTORY); setFactories(FactoryFinder.RENDER_KIT_FACTORY, _dispenser.getRenderKitFactoryIterator(), DEFAULT_RENDER_KIT_FACTORY); } private void setFactories(String factoryName, Iterator factories, String defaultFactory) { FactoryFinder.setFactory(factoryName, defaultFactory); while (factories.hasNext()) { FactoryFinder.setFactory(factoryName, (String) factories.next()); } } private void configureApplication() { Application application = ((ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY)).getApplication(); application.setActionListener((ActionListener) getApplicationObject(ActionListener.class, _dispenser.getActionListenerIterator(), null)); if (_dispenser.getDefaultLocale() != null) { application.setDefaultLocale( LocaleUtils.toLocale(_dispenser.getDefaultLocale())); } if (_dispenser.getDefaultRenderKitId() != null) { application.setDefaultRenderKitId(_dispenser.getDefaultRenderKitId()); } if (_dispenser.getMessageBundle() != null) { application.setMessageBundle(_dispenser.getMessageBundle()); } application.setNavigationHandler((NavigationHandler) getApplicationObject(NavigationHandler.class, _dispenser.getNavigationHandlerIterator(), application.getNavigationHandler())); application.setPropertyResolver((PropertyResolver) getApplicationObject(PropertyResolver.class, _dispenser.getPropertyResolverIterator(), application.getPropertyResolver())); application.setStateManager((StateManager) getApplicationObject(StateManager.class, _dispenser.getStateManagerIterator(), application.getStateManager())); List locales = new ArrayList(); for (Iterator it = _dispenser.getSupportedLocalesIterator(); it.hasNext();) { locales.add(LocaleUtils.toLocale((String) it.next())); } application.setSupportedLocales(locales); application.setVariableResolver((VariableResolver) getApplicationObject(VariableResolver.class, _dispenser.getVariableResolverIterator(), application.getVariableResolver())); application.setViewHandler((ViewHandler) getApplicationObject(ViewHandler.class, _dispenser.getViewHandlerIterator(), application.getViewHandler())); for (Iterator it = _dispenser.getComponentTypes(); it.hasNext();) { String componentType = (String) it.next(); application.addComponent(componentType, _dispenser.getComponentClass(componentType)); } for (Iterator it = _dispenser.getConverterIds(); it.hasNext();) { String converterId = (String) it.next(); application.addConverter(converterId, _dispenser.getConverterClassById(converterId)); } for (Iterator it = _dispenser.getConverterClasses(); it.hasNext();) { String converterClass = (String) it.next(); try { application.addConverter(ClassUtils.simpleClassForName(converterClass), _dispenser.getConverterClassByClass(converterClass)); } catch(Exception ex) { log.error("Converter could not be added. Reason:",ex); } } if(application instanceof ApplicationImpl) { for (Iterator it = _dispenser.getConverterConfigurationByClassName(); it.hasNext();) { String converterClassName = (String) it.next(); ((ApplicationImpl) application).addConverterConfiguration(converterClassName, _dispenser.getConverterConfiguration(converterClassName)); } } for (Iterator it = _dispenser.getValidatorIds(); it.hasNext();) { String validatorId = (String) it.next(); application.addValidator(validatorId, _dispenser.getValidatorClass(validatorId)); } } private Object getApplicationObject(Class interfaceClass, Iterator classNamesIterator, Object defaultObject) { Object current = defaultObject; while (classNamesIterator.hasNext()) { String implClassName = (String) classNamesIterator.next(); Class implClass = ClassUtils.simpleClassForName(implClassName); // check, if class is of expected interface type if (!interfaceClass.isAssignableFrom(implClass)) { throw new IllegalArgumentException("Class " + implClassName + " is no " + interfaceClass.getName()); } if (current == null) { // nothing to decorate current = ClassUtils.newInstance(implClass); } else { // let's check if class supports the decorator pattern try { Constructor delegationConstructor = implClass.getConstructor(new Class[]{interfaceClass}); // impl class supports decorator pattern, try { // create new decorator wrapping current current = delegationConstructor.newInstance(new Object[]{current}); } catch (InstantiationException e) { log.error(e.getMessage(), e); throw new FacesException(e); } catch (IllegalAccessException e) { log.error(e.getMessage(), e); throw new FacesException(e); } catch (InvocationTargetException e) { log.error(e.getMessage(), e); throw new FacesException(e); } } catch (NoSuchMethodException e) { // no decorator pattern support current = ClassUtils.newInstance(implClass); } } } return current; } private void configureRuntimeConfig() { RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(_externalContext); for (Iterator iterator = _dispenser.getManagedBeans(); iterator.hasNext();) { ManagedBean bean = (ManagedBean) iterator.next(); runtimeConfig.addManagedBean(bean.getManagedBeanName(), bean); } for (Iterator iterator = _dispenser.getNavigationRules(); iterator.hasNext();) { NavigationRule rule = (NavigationRule) iterator.next(); runtimeConfig.addNavigationRule(rule); } } private void configureRenderKits() { RenderKitFactory renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); for (Iterator iterator = _dispenser.getRenderKitIds(); iterator.hasNext();) { String renderKitId = (String) iterator.next(); String renderKitClass = _dispenser.getRenderKitClass(renderKitId); if (renderKitClass == null) { renderKitClass = DEFAULT_RENDER_KIT_CLASS; } RenderKit renderKit = (RenderKit) ClassUtils.newInstance(renderKitClass); for (Iterator renderers = _dispenser.getRenderers(renderKitId); renderers.hasNext();) { Renderer element = (Renderer) renderers.next(); javax.faces.render.Renderer renderer = null; try { renderer = (javax.faces.render.Renderer) ClassUtils.newInstance(element.getRendererClass()); } catch(FacesException e) { // ignore the failure so that the render kit is configured log.error("failed to configure class " + element.getRendererClass(), e); continue; } renderKit.addRenderer(element.getComponentFamily(), element.getRendererType(), renderer); } renderKitFactory.addRenderKit(renderKitId, renderKit); } } private void configureLifecycle() { // create the lifecycle used by the app LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); Lifecycle lifecycle = lifecycleFactory.getLifecycle(getLifecycleId()); // add phase listeners for (Iterator iterator = _dispenser.getLifecyclePhaseListeners(); iterator.hasNext();) { String listenerClassName = (String) iterator.next(); try { lifecycle.addPhaseListener((PhaseListener) ClassUtils.newInstance(listenerClassName)); } catch (ClassCastException e) { log.error("Class " + listenerClassName + " does not implement PhaseListener"); } } } private String getLifecycleId() { String id = _externalContext.getInitParameter(FacesServlet.LIFECYCLE_ID_ATTR); if (id != null) { return id; } return LifecycleFactory.DEFAULT_LIFECYCLE; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -