001: // ========================================================================
002: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.util;
016:
017: import java.net.URL;
018: import java.util.Locale;
019: import java.util.MissingResourceException;
020: import java.util.ResourceBundle;
021:
022: /* ------------------------------------------------------------ */
023: /** ClassLoader Helper.
024: * This helper class allows classes to be loaded either from the
025: * Thread's ContextClassLoader, the classloader of the derived class
026: * or the system ClassLoader.
027: *
028: * <B>Usage:</B><PRE>
029: * public class MyClass {
030: * void myMethod() {
031: * ...
032: * Class c=Loader.loadClass(this.getClass(),classname);
033: * ...
034: * }
035: * </PRE>
036: * @author Greg Wilkins (gregw)
037: */
038: public class Loader {
039: /* ------------------------------------------------------------ */
040: public static URL getResource(Class loadClass, String name,
041: boolean checkParents) throws ClassNotFoundException {
042: URL url = null;
043: ClassLoader loader = Thread.currentThread()
044: .getContextClassLoader();
045: while (url == null && loader != null) {
046: url = loader.getResource(name);
047: loader = (url == null && checkParents) ? loader.getParent()
048: : null;
049: }
050:
051: loader = loadClass == null ? null : loadClass.getClassLoader();
052: while (url == null && loader != null) {
053: url = loader.getResource(name);
054: loader = (url == null && checkParents) ? loader.getParent()
055: : null;
056: }
057:
058: if (url == null) {
059: url = ClassLoader.getSystemResource(name);
060: }
061:
062: return url;
063: }
064:
065: /* ------------------------------------------------------------ */
066: public static Class loadClass(Class loadClass, String name)
067: throws ClassNotFoundException {
068: return loadClass(loadClass, name, false);
069: }
070:
071: /* ------------------------------------------------------------ */
072: /** Load a class.
073: *
074: * @param loadClass
075: * @param name
076: * @param checkParents If true, try loading directly from parent classloaders.
077: * @return Class
078: * @throws ClassNotFoundException
079: */
080: public static Class loadClass(Class loadClass, String name,
081: boolean checkParents) throws ClassNotFoundException {
082: ClassNotFoundException ex = null;
083: Class c = null;
084: ClassLoader loader = Thread.currentThread()
085: .getContextClassLoader();
086: while (c == null && loader != null) {
087: try {
088: c = loader.loadClass(name);
089: } catch (ClassNotFoundException e) {
090: if (ex == null)
091: ex = e;
092: }
093: loader = (c == null && checkParents) ? loader.getParent()
094: : null;
095: }
096:
097: loader = loadClass == null ? null : loadClass.getClassLoader();
098: while (c == null && loader != null) {
099: try {
100: c = loader.loadClass(name);
101: } catch (ClassNotFoundException e) {
102: if (ex == null)
103: ex = e;
104: }
105: loader = (c == null && checkParents) ? loader.getParent()
106: : null;
107: }
108:
109: if (c == null) {
110: try {
111: c = Class.forName(name);
112: } catch (ClassNotFoundException e) {
113: if (ex == null)
114: ex = e;
115: }
116: }
117:
118: if (c != null)
119: return c;
120: throw ex;
121: }
122:
123: public static ResourceBundle getResourceBundle(Class loadClass,
124: String name, boolean checkParents, Locale locale)
125: throws MissingResourceException {
126: MissingResourceException ex = null;
127: ResourceBundle bundle = null;
128: ClassLoader loader = Thread.currentThread()
129: .getContextClassLoader();
130: while (bundle == null && loader != null) {
131: try {
132: bundle = ResourceBundle.getBundle(name, locale, loader);
133: } catch (MissingResourceException e) {
134: if (ex == null)
135: ex = e;
136: }
137: loader = (bundle == null && checkParents) ? loader
138: .getParent() : null;
139: }
140:
141: loader = loadClass == null ? null : loadClass.getClassLoader();
142: while (bundle == null && loader != null) {
143: try {
144: bundle = ResourceBundle.getBundle(name, locale, loader);
145: } catch (MissingResourceException e) {
146: if (ex == null)
147: ex = e;
148: }
149: loader = (bundle == null && checkParents) ? loader
150: .getParent() : null;
151: }
152:
153: if (bundle == null) {
154: try {
155: bundle = ResourceBundle.getBundle(name, locale);
156: } catch (MissingResourceException e) {
157: if (ex == null)
158: ex = e;
159: }
160: }
161:
162: if (bundle != null)
163: return bundle;
164: throw ex;
165: }
166:
167: }
|