All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ocpsoft.pretty.faces.config.PrettyConfigurator Maven / Gradle / Ivy

There is a newer version: 3.3.3
Show newest version
/*
 * PrettyFaces is an OpenSource JSF library to create bookmarkable URLs.
 * Copyright (C) 2009 - Lincoln Baxter, III  This program
 * is free software: you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version. This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. You should have received a copy of the GNU Lesser General
 * Public License along with this program. If not, see the file COPYING.LESSER3
 * or visit the GNU website at .
 */
package com.ocpsoft.pretty.faces.config;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;

import com.ocpsoft.pretty.PrettyContext;
import com.ocpsoft.pretty.PrettyException;
import com.ocpsoft.pretty.faces.DynaviewEngine;
import com.ocpsoft.pretty.faces.config.annotation.ClassFinder;
import com.ocpsoft.pretty.faces.config.annotation.PackageFilter;
import com.ocpsoft.pretty.faces.config.annotation.PrettyAnnotationHandler;
import com.ocpsoft.pretty.faces.config.annotation.WebClassesFinder;
import com.ocpsoft.pretty.faces.config.annotation.WebLibFinder;
import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;
import com.ocpsoft.pretty.faces.config.servlet.WebXmlParser;
import com.ocpsoft.pretty.faces.el.LazyBeanNameFinder;

/**
 * Implements the Pretty Faces configuration procedure.
 * 

* At application startup time, before any requests are processed, Pretty Faces * processes zero or more configuration resources, located according to the * following algorithm: *

*
    *
  • Search for classpath resources named * META-INF/pretty-config.xml in the ServletContext resource paths * for this web application, and load each as a configuration resource.
  • *
  • Check for the existence of a context initialization parameter named * com.ocpsoft.pretty.CONFIG_FILES. If it exists, treat it as a * comma-delimited list of context relative resource paths (starting with a * /), and load each of the specified resources.
  • *
  • Check for the existence of a web application configuration resource named * /WEB-INF/pretty-config.xml, and load it if the resource exists.
  • *
* * @author Aleksei Valikov * @author urls = getResourceLoader().getResources(PRETTY_CONFIG_RESOURCE); if (urls != null) { while (urls.hasMoreElements()) { final URL url = urls.nextElement(); if (url != null) { InputStream is = null; try { is = openStream(url); configParser.parse(builder, is); } finally { if (is != null) { try { is.close(); } catch (IOException ignored) { } } } } } } } private void feedContextSpecifiedConfig(final PrettyConfigBuilder builder) throws IOException, SAXException { final List configFilesList = getConfigFilesList(); for (final String systemId : configFilesList) { final InputStream is = servletContext.getResourceAsStream(systemId); if (is == null) { log.error("Pretty Faces config resource [" + systemId + "] not found."); continue; } log.trace("Reading config [" + systemId + "]."); try { configParser.parse(builder, is); } finally { try { is.close(); } catch (IOException ignored) { } } } } private List getConfigFilesList() { final String configFiles = servletContext.getInitParameter(PrettyContext.CONFIG_FILES_ATTR); final List configFilesList = new ArrayList(); if (configFiles != null) { final StringTokenizer st = new StringTokenizer(configFiles, ",", false); while (st.hasMoreTokens()) { final String systemId = st.nextToken().trim(); if (DEFAULT_PRETTY_FACES_CONFIG.equals(systemId)) { log.warn("The file [" + DEFAULT_PRETTY_FACES_CONFIG + "] has been specified in the [" + PrettyContext.CONFIG_FILES_ATTR + "] context parameter of " + "the deployment descriptor. This will automatically be removed, " + " otherwise, it would be loaded twice."); } else { configFilesList.add(systemId); } } } return configFilesList; } private void feedWebAppConfig(final PrettyConfigBuilder builder) throws IOException, SAXException { final InputStream is = servletContext.getResourceAsStream(DEFAULT_PRETTY_FACES_CONFIG); if (is != null) { log.trace("Reading config [" + DEFAULT_PRETTY_FACES_CONFIG + "]."); try { configParser.parse(builder, is); } finally { try { is.close(); } catch (IOException ignored) { } } } } /** * Method to process the PrettyFaces configuration via annotations. * * @param builder * The builder to add mappings to */ private void feedAnnotationConfigs(final PrettyConfigBuilder builder) { // get package filter configuration String filterConfig = servletContext.getInitParameter(CONFIG_BASE_PACKAGES); // annotation scanning disabled? if(filterConfig != null && filterConfig.trim().equalsIgnoreCase("none")) { log.debug("Annotation scanning has is disabled!"); return; } // build package filter PackageFilter packageFilter = new PackageFilter(filterConfig); // instance of the LazyBeanNameFinder used by LazyExpressions LazyBeanNameFinder beanNameFinder = new LazyBeanNameFinder(servletContext); // handler class to process class found via ClassFinder implementations PrettyAnnotationHandler annotationHandler = new PrettyAnnotationHandler(beanNameFinder); // ClassLoader used by finder implementations ClassLoader classloader = Thread.currentThread().getContextClassLoader(); if (classloader == null) { classloader = this.getClass().getClassLoader(); } // list of ClassFinders to use for the scanning process List classFinders = new ArrayList(); // we will always scan /WEB-INF/classes classFinders.add( new WebClassesFinder(servletContext, classloader, packageFilter) ); // does the user want to scan /WEB-INF/lib ? String jarConfig = servletContext.getInitParameter(CONFIG_SCAN_LIB_DIR); if (jarConfig != null && jarConfig.trim().equalsIgnoreCase("true")) { classFinders.add(new WebLibFinder(servletContext, classloader, packageFilter)); } // run finders for (ClassFinder finder : classFinders) { finder.findClasses(annotationHandler); } // finally build mapping annotationHandler.build(builder); } /** * Return class loader to be used to resolve resources. * * @return Class loader to be used to resolve resources */ protected ClassLoader getResourceLoader() { final ClassLoader resourceLoader = Thread.currentThread().getContextClassLoader(); return resourceLoader; } /** * Opens an input stream for the given URL. * * @param url target URL. * @return Opened input stream. * @throws IOException If connection could not be opened. */ protected InputStream openStream(final URL url) throws IOException { final URLConnection connection = url.openConnection(); connection.setUseCaches(false); return connection.getInputStream(); } public PrettyConfig getConfig() { return config; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy