Source Code Cross Referenced for ConfigHelper.java in  » Database-ORM » hibernate » org » hibernate » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » hibernate » org.hibernate.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // $Id: ConfigHelper.java 9972 2006-05-31 16:59:05Z steve.ebersole@jboss.com $
002:        package org.hibernate.util;
003:
004:        import java.io.IOException;
005:        import java.io.InputStream;
006:        import java.io.InputStreamReader;
007:        import java.io.Reader;
008:        import java.net.MalformedURLException;
009:        import java.net.URL;
010:        import java.util.Properties;
011:
012:        import org.apache.commons.logging.Log;
013:        import org.apache.commons.logging.LogFactory;
014:        import org.hibernate.HibernateException;
015:        import org.hibernate.cfg.Environment;
016:
017:        /**
018:         * A simple class to centralize logic needed to locate config files on the system.
019:         *
020:         * @author Steve
021:         */
022:        public final class ConfigHelper {
023:            private static final Log log = LogFactory
024:                    .getLog(ConfigHelper.class);
025:
026:            /** Try to locate a local URL representing the incoming path.  The first attempt
027:             * assumes that the incoming path is an actual URL string (file://, etc).  If this
028:             * does not work, then the next attempts try to locate this UURL as a java system
029:             * resource.
030:             *
031:             * @param path The path representing the config location.
032:             * @return An appropriate URL or null.
033:             */
034:            public static final URL locateConfig(final String path) {
035:                try {
036:                    return new URL(path);
037:                } catch (MalformedURLException e) {
038:                    return findAsResource(path);
039:                }
040:            }
041:
042:            /** 
043:             * Try to locate a local URL representing the incoming path.  
044:             * This method <b>only</b> attempts to locate this URL as a 
045:             * java system resource.
046:             *
047:             * @param path The path representing the config location.
048:             * @return An appropriate URL or null.
049:             */
050:            public static final URL findAsResource(final String path) {
051:                URL url = null;
052:
053:                // First, try to locate this resource through the current
054:                // context classloader.
055:                ClassLoader contextClassLoader = Thread.currentThread()
056:                        .getContextClassLoader();
057:                if (contextClassLoader != null) {
058:                    url = contextClassLoader.getResource(path);
059:                }
060:                if (url != null)
061:                    return url;
062:
063:                // Next, try to locate this resource through this class's classloader
064:                url = ConfigHelper.class.getClassLoader().getResource(path);
065:                if (url != null)
066:                    return url;
067:
068:                // Next, try to locate this resource through the system classloader
069:                url = ClassLoader.getSystemClassLoader().getResource(path);
070:
071:                // Anywhere else we should look?
072:                return url;
073:            }
074:
075:            /** Open an InputStream to the URL represented by the incoming path.  First makes a call
076:             * to {@link #locateConfig(java.lang.String)} in order to find an appropriate URL.
077:             * {@link java.net.URL#openStream()} is then called to obtain the stream.
078:             *
079:             * @param path The path representing the config location.
080:             * @return An input stream to the requested config resource.
081:             * @throws HibernateException Unable to open stream to that resource.
082:             */
083:            public static final InputStream getConfigStream(final String path)
084:                    throws HibernateException {
085:                final URL url = ConfigHelper.locateConfig(path);
086:
087:                if (url == null) {
088:                    String msg = "Unable to locate config file: " + path;
089:                    log.fatal(msg);
090:                    throw new HibernateException(msg);
091:                }
092:
093:                try {
094:                    return url.openStream();
095:                } catch (IOException e) {
096:                    throw new HibernateException("Unable to open config file: "
097:                            + path, e);
098:                }
099:            }
100:
101:            /** Open an Reader to the URL represented by the incoming path.  First makes a call
102:             * to {@link #locateConfig(java.lang.String)} in order to find an appropriate URL.
103:             * {@link java.net.URL#openStream()} is then called to obtain a stream, which is then
104:             * wrapped in a Reader.
105:             *
106:             * @param path The path representing the config location.
107:             * @return An input stream to the requested config resource.
108:             * @throws HibernateException Unable to open reader to that resource.
109:             */
110:            public static final Reader getConfigStreamReader(final String path)
111:                    throws HibernateException {
112:                return new InputStreamReader(getConfigStream(path));
113:            }
114:
115:            /** Loads a properties instance based on the data at the incoming config location.
116:             *
117:             * @param path The path representing the config location.
118:             * @return The loaded properties instance.
119:             * @throws HibernateException Unable to load properties from that resource.
120:             */
121:            public static final Properties getConfigProperties(String path)
122:                    throws HibernateException {
123:                try {
124:                    Properties properties = new Properties();
125:                    properties.load(getConfigStream(path));
126:                    return properties;
127:                } catch (IOException e) {
128:                    throw new HibernateException(
129:                            "Unable to load properties from specified config file: "
130:                                    + path, e);
131:                }
132:            }
133:
134:            private ConfigHelper() {
135:            }
136:
137:            public static InputStream getResourceAsStream(String resource) {
138:                String stripped = resource.startsWith("/") ? resource
139:                        .substring(1) : resource;
140:
141:                InputStream stream = null;
142:                ClassLoader classLoader = Thread.currentThread()
143:                        .getContextClassLoader();
144:                if (classLoader != null) {
145:                    stream = classLoader.getResourceAsStream(stripped);
146:                }
147:                if (stream == null) {
148:                    stream = Environment.class.getResourceAsStream(resource);
149:                }
150:                if (stream == null) {
151:                    stream = Environment.class.getClassLoader()
152:                            .getResourceAsStream(stripped);
153:                }
154:                if (stream == null) {
155:                    throw new HibernateException(resource + " not found");
156:                }
157:                return stream;
158:            }
159:
160:            public static InputStream getUserResourceAsStream(String resource) {
161:                boolean hasLeadingSlash = resource.startsWith("/");
162:                String stripped = hasLeadingSlash ? resource.substring(1)
163:                        : resource;
164:
165:                InputStream stream = null;
166:
167:                ClassLoader classLoader = Thread.currentThread()
168:                        .getContextClassLoader();
169:                if (classLoader != null) {
170:                    stream = classLoader.getResourceAsStream(resource);
171:                    if (stream == null && hasLeadingSlash) {
172:                        stream = classLoader.getResourceAsStream(stripped);
173:                    }
174:                }
175:
176:                if (stream == null) {
177:                    stream = Environment.class.getClassLoader()
178:                            .getResourceAsStream(resource);
179:                }
180:                if (stream == null && hasLeadingSlash) {
181:                    stream = Environment.class.getClassLoader()
182:                            .getResourceAsStream(stripped);
183:                }
184:
185:                if (stream == null) {
186:                    throw new HibernateException(resource + " not found");
187:                }
188:
189:                return stream;
190:            }
191:
192:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.