Save This Page
Home » hibernate-distribution-3.3.1.GA-dist » org.hibernate » util » [javadoc | source]
    1   /*
    2    * Hibernate, Relational Persistence for Idiomatic Java
    3    *
    4    * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
    5    * indicated by the @author tags or express copyright attribution
    6    * statements applied by the authors.  All third-party contributions are
    7    * distributed under license by Red Hat Middleware LLC.
    8    *
    9    * This copyrighted material is made available to anyone wishing to use, modify,
   10    * copy, or redistribute it subject to the terms and conditions of the GNU
   11    * Lesser General Public License, as published by the Free Software Foundation.
   12    *
   13    * This program is distributed in the hope that it will be useful,
   14    * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15    * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
   16    * for more details.
   17    *
   18    * You should have received a copy of the GNU Lesser General Public License
   19    * along with this distribution; if not, write to:
   20    * Free Software Foundation, Inc.
   21    * 51 Franklin Street, Fifth Floor
   22    * Boston, MA  02110-1301  USA
   23    *
   24    */
   25   package org.hibernate.util;
   26   
   27   import java.io.IOException;
   28   import java.io.InputStream;
   29   import java.io.InputStreamReader;
   30   import java.io.Reader;
   31   import java.net.MalformedURLException;
   32   import java.net.URL;
   33   import java.util.Properties;
   34   
   35   import org.slf4j.Logger;
   36   import org.slf4j.LoggerFactory;
   37   import org.hibernate.HibernateException;
   38   import org.hibernate.cfg.Environment;
   39   
   40   /**
   41    * A simple class to centralize logic needed to locate config files on the system.
   42    *
   43    * @author Steve
   44    */
   45   public final class ConfigHelper {
   46   	private static final Logger log = LoggerFactory.getLogger(ConfigHelper.class);
   47   
   48   	/** Try to locate a local URL representing the incoming path.  The first attempt
   49   	 * assumes that the incoming path is an actual URL string (file://, etc).  If this
   50   	 * does not work, then the next attempts try to locate this UURL as a java system
   51   	 * resource.
   52   	 *
   53   	 * @param path The path representing the config location.
   54   	 * @return An appropriate URL or null.
   55   	 */
   56   	public static final URL locateConfig(final String path) {
   57   		try {
   58   			return new URL(path);
   59   		}
   60   		catch(MalformedURLException e) {
   61   			return findAsResource(path);
   62   		}
   63   	}
   64   
   65   	/** 
   66   	 * Try to locate a local URL representing the incoming path.  
   67   	 * This method <b>only</b> attempts to locate this URL as a 
   68   	 * java system resource.
   69   	 *
   70   	 * @param path The path representing the config location.
   71   	 * @return An appropriate URL or null.
   72   	 */
   73   	public static final URL findAsResource(final String path) {
   74   		URL url = null;
   75   
   76   		// First, try to locate this resource through the current
   77   		// context classloader.
   78   		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
   79   		if (contextClassLoader!=null) {
   80   			url = contextClassLoader.getResource(path);
   81   		}
   82   		if (url != null)
   83   			return url;
   84   
   85   		// Next, try to locate this resource through this class's classloader
   86   		url = ConfigHelper.class.getClassLoader().getResource(path);
   87   		if (url != null)
   88   			return url;
   89   
   90   		// Next, try to locate this resource through the system classloader
   91   		url = ClassLoader.getSystemClassLoader().getResource(path);
   92   
   93   		// Anywhere else we should look?
   94   		return url;
   95   	}
   96   
   97   	/** Open an InputStream to the URL represented by the incoming path.  First makes a call
   98   	 * to {@link #locateConfig(java.lang.String)} in order to find an appropriate URL.
   99   	 * {@link java.net.URL#openStream()} is then called to obtain the stream.
  100   	 *
  101   	 * @param path The path representing the config location.
  102   	 * @return An input stream to the requested config resource.
  103   	 * @throws HibernateException Unable to open stream to that resource.
  104   	 */
  105   	public static final InputStream getConfigStream(final String path) throws HibernateException {
  106   		final URL url = ConfigHelper.locateConfig(path);
  107   
  108   		if (url == null) {
  109   			String msg = "Unable to locate config file: " + path;
  110   			log.error( msg );
  111   			throw new HibernateException(msg);
  112   		}
  113   
  114   		try {
  115   			return url.openStream();
  116           }
  117   		catch(IOException e) {
  118   	        throw new HibernateException("Unable to open config file: " + path, e);
  119           }
  120   	}
  121   
  122   	/** Open an Reader to the URL represented by the incoming path.  First makes a call
  123   	 * to {@link #locateConfig(java.lang.String)} in order to find an appropriate URL.
  124   	 * {@link java.net.URL#openStream()} is then called to obtain a stream, which is then
  125   	 * wrapped in a Reader.
  126   	 *
  127   	 * @param path The path representing the config location.
  128   	 * @return An input stream to the requested config resource.
  129   	 * @throws HibernateException Unable to open reader to that resource.
  130   	 */
  131   	public static final Reader getConfigStreamReader(final String path) throws HibernateException {
  132   		return new InputStreamReader( getConfigStream(path) );
  133   	}
  134   
  135   	/** Loads a properties instance based on the data at the incoming config location.
  136   	 *
  137   	 * @param path The path representing the config location.
  138   	 * @return The loaded properties instance.
  139   	 * @throws HibernateException Unable to load properties from that resource.
  140   	 */
  141   	public static final Properties getConfigProperties(String path) throws HibernateException {
  142   		try {
  143   			Properties properties = new Properties();
  144   			properties.load( getConfigStream(path) );
  145   			return properties;
  146   		}
  147   		catch(IOException e) {
  148   			throw new HibernateException("Unable to load properties from specified config file: " + path, e);
  149   		}
  150   	}
  151   	
  152   	private ConfigHelper() {}
  153   
  154   	public static InputStream getResourceAsStream(String resource) {
  155   		String stripped = resource.startsWith("/") ?
  156   				resource.substring(1) : resource;
  157   
  158   		InputStream stream = null;
  159   		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  160   		if (classLoader!=null) {
  161   			stream = classLoader.getResourceAsStream( stripped );
  162   		}
  163   		if ( stream == null ) {
  164   			stream = Environment.class.getResourceAsStream( resource );
  165   		}
  166   		if ( stream == null ) {
  167   			stream = Environment.class.getClassLoader().getResourceAsStream( stripped );
  168   		}
  169   		if ( stream == null ) {
  170   			throw new HibernateException( resource + " not found" );
  171   		}
  172   		return stream;
  173   	}
  174   
  175   
  176   	public static InputStream getUserResourceAsStream(String resource) {
  177   		boolean hasLeadingSlash = resource.startsWith( "/" );
  178   		String stripped = hasLeadingSlash ? resource.substring(1) : resource;
  179   
  180   		InputStream stream = null;
  181   
  182   		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  183   		if ( classLoader != null ) {
  184   			stream = classLoader.getResourceAsStream( resource );
  185   			if ( stream == null && hasLeadingSlash ) {
  186   				stream = classLoader.getResourceAsStream( stripped );
  187   			}
  188   		}
  189   
  190   		if ( stream == null ) {
  191   			stream = Environment.class.getClassLoader().getResourceAsStream( resource );
  192   		}
  193   		if ( stream == null && hasLeadingSlash ) {
  194   			stream = Environment.class.getClassLoader().getResourceAsStream( stripped );
  195   		}
  196   
  197   		if ( stream == null ) {
  198   			throw new HibernateException( resource + " not found" );
  199   		}
  200   
  201   		return stream;
  202   	}
  203   
  204   }

Save This Page
Home » hibernate-distribution-3.3.1.GA-dist » org.hibernate » util » [javadoc | source]