org.hibernate.util.ConfigHelper.java

來源:互聯網
上載者:User

該類為hibernate資源載入工具類,在資源載入過程中,用到JDK的三種類載入類

1. 當前線程類載入器

    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();

2. 當前類載入器

    ClassLoader contextClassLoader = ConfigHelper.class.getClassLoader();

3. 系統類別載入器

    ClassLoader contextClassLoader = ClassLoader.getSystemClassLoader()

 

在這裡按照1、2、3依次載入,如果當前線程類載入器載入成功,則直接返回.........

 

在該類中用到一個類org.hibernate.cfg.Environment.java,從名稱可以看到該類為hibernate環境相關的處理

 

package org.hibernate.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;

/**
 * A simple class to centralize logic needed to locate config files on the system.
 *
 * @author Steve
 */
public final class ConfigHelper {
 private static final Log log = LogFactory.getLog(ConfigHelper.class);

 /** Try to locate a local URL representing the incoming path.  The first attempt
  * assumes that the incoming path is an actual URL string (file://, etc).  If this
  * does not work, then the next attempts try to locate this UURL as a java system
  * resource.
  *
  * @param path The path representing the config location.
  * @return An appropriate URL or null.
  */
 public static final URL locateConfig(final String path) {
  try {
   return new URL(path);
  }
  catch(MalformedURLException e) {
   return findAsResource(path);
  }
 }

 /**
  * Try to locate a local URL representing the incoming path. 
  * This method <b>only</b> attempts to locate this URL as a
  * java system resource.
  *
  * @param path The path representing the config location.
  * @return An appropriate URL or null.
  */
 public static final URL findAsResource(final String path) {
  URL url = null;

  // First, try to locate this resource through the current
  // context classloader.
  ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
  if (contextClassLoader!=null) {
   url = contextClassLoader.getResource(path);
  }
  if (url != null)
   return url;

  // Next, try to locate this resource through this class's classloader
  url = ConfigHelper.class.getClassLoader().getResource(path);
  if (url != null)
   return url;

  // Next, try to locate this resource through the system classloader
  url = ClassLoader.getSystemClassLoader().getResource(path);

  // Anywhere else we should look?
  return url;
 }

 /** Open an InputStream to the URL represented by the incoming path.  First makes a call
  * to {@link #locateConfig(java.lang.String)} in order to find an appropriate URL.
  * {@link java.net.URL#openStream()} is then called to obtain the stream.
  *
  * @param path The path representing the config location.
  * @return An input stream to the requested config resource.
  * @throws HibernateException Unable to open stream to that resource.
  */
 public static final InputStream getConfigStream(final String path) throws HibernateException {
  final URL url = ConfigHelper.locateConfig(path);

  if (url == null) {
   String msg = "Unable to locate config file: " + path;
   log.fatal(msg);
   throw new HibernateException(msg);
  }

  try {
   return url.openStream();
        }
  catch(IOException e) {
         throw new HibernateException("Unable to open config file: " + path, e);
        }
 }

 /** Open an Reader to the URL represented by the incoming path.  First makes a call
  * to {@link #locateConfig(java.lang.String)} in order to find an appropriate URL.
  * {@link java.net.URL#openStream()} is then called to obtain a stream, which is then
  * wrapped in a Reader.
  *
  * @param path The path representing the config location.
  * @return An input stream to the requested config resource.
  * @throws HibernateException Unable to open reader to that resource.
  */
 public static final Reader getConfigStreamReader(final String path) throws HibernateException {
  return new InputStreamReader( getConfigStream(path) );
 }

 /** Loads a properties instance based on the data at the incoming config location.
  *
  * @param path The path representing the config location.
  * @return The loaded properties instance.
  * @throws HibernateException Unable to load properties from that resource.
  */
 public static final Properties getConfigProperties(String path) throws HibernateException {
  try {
   Properties properties = new Properties();
   properties.load( getConfigStream(path) );
   return properties;
  }
  catch(IOException e) {
   throw new HibernateException("Unable to load properties from specified config file: " + path, e);
  }
 }
 
 private ConfigHelper() {}

 public static InputStream getResourceAsStream(String resource) {
  String stripped = resource.startsWith("/") ?
    resource.substring(1) : resource;
 
  InputStream stream = null;
  ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  if (classLoader!=null) {
   stream = classLoader.getResourceAsStream( stripped );
  }
  if ( stream == null ) {
   Environment.class.getResourceAsStream( resource );
  }
  if ( stream == null ) {
   stream = Environment.class.getClassLoader().getResourceAsStream( stripped );
  }
  if ( stream == null ) {
   throw new HibernateException( resource + " not found" );
  }
  return stream;
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.