View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.util;
15  
16  import java.net.URL;
17  import java.util.Locale;
18  import java.util.MissingResourceException;
19  import java.util.ResourceBundle;
20  
21  /* ------------------------------------------------------------ */
22  /** ClassLoader Helper.
23   * This helper class allows classes to be loaded either from the
24   * Thread's ContextClassLoader, the classloader of the derived class
25   * or the system ClassLoader.
26   *
27   * <B>Usage:</B><PRE>
28   * public class MyClass {
29   *     void myMethod() {
30   *          ...
31   *          Class c=Loader.loadClass(this.getClass(),classname);
32   *          ...
33   *     }
34   * </PRE>          
35   * 
36   */
37  public class Loader
38  {
39      /* ------------------------------------------------------------ */
40      public static URL getResource(Class loadClass,String name, boolean checkParents)
41          throws ClassNotFoundException
42      {
43          URL url =null;
44          ClassLoader loader=Thread.currentThread().getContextClassLoader();
45          while (url==null && loader!=null )
46          {
47              url=loader.getResource(name); 
48              loader=(url==null&&checkParents)?loader.getParent():null;
49          }      
50          
51          loader=loadClass==null?null:loadClass.getClassLoader();
52          while (url==null && loader!=null )
53          {
54              url=loader.getResource(name); 
55              loader=(url==null&&checkParents)?loader.getParent():null;
56          }       
57  
58          if (url==null)
59          {
60              url=ClassLoader.getSystemResource(name);
61          }   
62  
63          return url;
64      }
65  
66      /* ------------------------------------------------------------ */
67      public static Class loadClass(Class loadClass,String name)
68          throws ClassNotFoundException
69      {
70          return loadClass(loadClass,name,false);
71      }
72      
73      /* ------------------------------------------------------------ */
74      /** Load a class.
75       * 
76       * @param loadClass
77       * @param name
78       * @param checkParents If true, try loading directly from parent classloaders.
79       * @return Class
80       * @throws ClassNotFoundException
81       */
82      public static Class loadClass(Class loadClass,String name,boolean checkParents)
83          throws ClassNotFoundException
84      {
85          ClassNotFoundException ex=null;
86          Class c =null;
87          ClassLoader loader=Thread.currentThread().getContextClassLoader();
88          while (c==null && loader!=null )
89          {
90              try { c=loader.loadClass(name); }
91              catch (ClassNotFoundException e) {if(ex==null)ex=e;}
92              loader=(c==null&&checkParents)?loader.getParent():null;
93          }      
94          
95          loader=loadClass==null?null:loadClass.getClassLoader();
96          while (c==null && loader!=null )
97          {
98              try { c=loader.loadClass(name); }
99              catch (ClassNotFoundException e) {if(ex==null)ex=e;}
100             loader=(c==null&&checkParents)?loader.getParent():null;
101         }       
102 
103         if (c==null)
104         {
105             try { c=Class.forName(name); }
106             catch (ClassNotFoundException e) {if(ex==null)ex=e;}
107         }   
108 
109         if (c!=null)
110             return c;
111         throw ex;
112     }
113 
114     public static ResourceBundle getResourceBundle(Class loadClass,String name,boolean checkParents, Locale locale)
115         throws MissingResourceException
116     {
117         MissingResourceException ex=null;
118         ResourceBundle bundle =null;
119         ClassLoader loader=Thread.currentThread().getContextClassLoader();
120         while (bundle==null && loader!=null )
121         {
122             try { bundle=ResourceBundle.getBundle(name, locale, loader); }
123             catch (MissingResourceException e) {if(ex==null)ex=e;}
124             loader=(bundle==null&&checkParents)?loader.getParent():null;
125         }      
126         
127         loader=loadClass==null?null:loadClass.getClassLoader();
128         while (bundle==null && loader!=null )
129         {
130             try { bundle=ResourceBundle.getBundle(name, locale, loader); }
131             catch (MissingResourceException e) {if(ex==null)ex=e;}
132             loader=(bundle==null&&checkParents)?loader.getParent():null;
133         }       
134 
135         if (bundle==null)
136         {
137             try { bundle=ResourceBundle.getBundle(name, locale); }
138             catch (MissingResourceException e) {if(ex==null)ex=e;}
139         }   
140 
141         if (bundle!=null)
142             return bundle;
143         throw ex;
144     }
145     
146 
147 }
148