Org. hibernate. util. confighelper. Java

Source: Internet
Author: User

This class is a tool for loading hibernate resources. Three types of loading classes of JDK are used during resource loading.

1. Current Thread class loader

Classloader contextclassloader = thread. currentthread (). getcontextclassloader ();

2. Current Class Loader

Classloader contextclassloader = confighelper. Class. getclassloader ();

3. System Class Loader

Classloader contextclassloader = classloader. getsystemclassloader ()

 

Load in sequence according to 1, 2, and 3. If the current Thread class loader is loaded successfully, it will return .........

 

The Org. hibernate. cfg. environment. Java class is used in this class. You can see from the name that this class is related to the hibernate environment.

 

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 = 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
* 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 shoshould 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 = 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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.