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      @SuppressWarnings("rawtypes")
68      public static Class loadClass(Class loadClass,String name)
69          throws ClassNotFoundException
70      {
71          return loadClass(loadClass,name,false);
72      }
73      
74      /* ------------------------------------------------------------ */
75      /** Load a class.
76       * 
77       * @param loadClass
78       * @param name
79       * @param checkParents If true, try loading directly from parent classloaders.
80       * @return Class
81       * @throws ClassNotFoundException
82       */
83      @SuppressWarnings("rawtypes")
84      public static Class loadClass(Class loadClass,String name,boolean checkParents)
85          throws ClassNotFoundException
86      {
87          ClassNotFoundException ex=null;
88          Class<?> c =null;
89          ClassLoader loader=Thread.currentThread().getContextClassLoader();
90          while (c==null && loader!=null )
91          {
92              try { c=loader.loadClass(name); }
93              catch (ClassNotFoundException e) {if(ex==null)ex=e;}
94              loader=(c==null&&checkParents)?loader.getParent():null;
95          }      
96          
97          loader=loadClass==null?null:loadClass.getClassLoader();
98          while (c==null && loader!=null )
99          {
100             try { c=loader.loadClass(name); }
101             catch (ClassNotFoundException e) {if(ex==null)ex=e;}
102             loader=(c==null&&checkParents)?loader.getParent():null;
103         }       
104 
105         if (c==null)
106         {
107             try { c=Class.forName(name); }
108             catch (ClassNotFoundException e) {if(ex==null)ex=e;}
109         }   
110 
111         if (c!=null)
112             return c;
113         throw ex;
114     }
115 
116     public static ResourceBundle getResourceBundle(Class<?> loadClass,String name,boolean checkParents, Locale locale)
117         throws MissingResourceException
118     {
119         MissingResourceException ex=null;
120         ResourceBundle bundle =null;
121         ClassLoader loader=Thread.currentThread().getContextClassLoader();
122         while (bundle==null && loader!=null )
123         {
124             try { bundle=ResourceBundle.getBundle(name, locale, loader); }
125             catch (MissingResourceException e) {if(ex==null)ex=e;}
126             loader=(bundle==null&&checkParents)?loader.getParent():null;
127         }      
128         
129         loader=loadClass==null?null:loadClass.getClassLoader();
130         while (bundle==null && loader!=null )
131         {
132             try { bundle=ResourceBundle.getBundle(name, locale, loader); }
133             catch (MissingResourceException e) {if(ex==null)ex=e;}
134             loader=(bundle==null&&checkParents)?loader.getParent():null;
135         }       
136 
137         if (bundle==null)
138         {
139             try { bundle=ResourceBundle.getBundle(name, locale); }
140             catch (MissingResourceException e) {if(ex==null)ex=e;}
141         }   
142 
143         if (bundle!=null)
144             return bundle;
145         throw ex;
146     }
147     
148 
149 }
150