View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util;
20  
21  import java.io.File;
22  import java.net.URL;
23  import java.net.URLClassLoader;
24  import java.util.Locale;
25  import java.util.MissingResourceException;
26  import java.util.ResourceBundle;
27  
28  import org.eclipse.jetty.util.resource.Resource;
29  
30  /* ------------------------------------------------------------ */
31  /** ClassLoader Helper.
32   * This helper class allows classes to be loaded either from the
33   * Thread's ContextClassLoader, the classloader of the derived class
34   * or the system ClassLoader.
35   *
36   * <B>Usage:</B><PRE>
37   * public class MyClass {
38   *     void myMethod() {
39   *          ...
40   *          Class c=Loader.loadClass(this.getClass(),classname);
41   *          ...
42   *     }
43   * </PRE>          
44   * 
45   */
46  public class Loader
47  {
48      /* ------------------------------------------------------------ */
49      public static URL getResource(Class<?> loadClass,String name)
50      {
51          URL url =null;
52          ClassLoader context_loader=Thread.currentThread().getContextClassLoader();
53          if (context_loader!=null)
54              url=context_loader.getResource(name); 
55          
56          if (url==null && loadClass!=null)
57          {
58              ClassLoader load_loader=loadClass.getClassLoader();
59              if (load_loader!=null && load_loader!=context_loader)
60                  url=load_loader.getResource(name);
61          }
62  
63          if (url==null)
64              url=ClassLoader.getSystemResource(name);
65  
66          return url;
67      }
68  
69      /* ------------------------------------------------------------ */
70      /** Load a class.
71       * 
72       * @param loadClass the class to use for the ClassLoader that was used
73       * @param name the name of the new class to load, using the same ClassLoader as the <code>loadClass</code> 
74       * @return Class
75       * @throws ClassNotFoundException if not able to find the class
76       */
77      @SuppressWarnings("rawtypes")
78      public static Class loadClass(Class loadClass,String name)
79          throws ClassNotFoundException
80      {
81          ClassNotFoundException ex=null;
82          Class<?> c =null;
83          ClassLoader context_loader=Thread.currentThread().getContextClassLoader();
84          if (context_loader!=null )
85          {
86              try { c=context_loader.loadClass(name); }
87              catch (ClassNotFoundException e) {ex=e;}
88          }    
89          
90          if (c==null && loadClass!=null)
91          {
92              ClassLoader load_loader=loadClass.getClassLoader();
93              if (load_loader!=null && load_loader!=context_loader)
94              {
95                  try { c=load_loader.loadClass(name); }
96                  catch (ClassNotFoundException e) {if(ex==null)ex=e;}
97              }
98          }
99  
100         if (c==null)
101         {
102             try { c=Class.forName(name); }
103             catch (ClassNotFoundException e) 
104             {
105                 if(ex!=null)
106                     throw ex;
107                 throw e;
108             }
109         }   
110 
111         return c;
112     }
113     
114     
115     
116     /* ------------------------------------------------------------ */
117     public static ResourceBundle getResourceBundle(Class<?> loadClass,String name,boolean checkParents, Locale locale)
118         throws MissingResourceException
119     {
120         MissingResourceException ex=null;
121         ResourceBundle bundle =null;
122         ClassLoader loader=Thread.currentThread().getContextClassLoader();
123         while (bundle==null && loader!=null )
124         {
125             try { bundle=ResourceBundle.getBundle(name, locale, loader); }
126             catch (MissingResourceException e) {if(ex==null)ex=e;}
127             loader=(bundle==null&&checkParents)?loader.getParent():null;
128         }      
129         
130         loader=loadClass==null?null:loadClass.getClassLoader();
131         while (bundle==null && loader!=null )
132         {
133             try { bundle=ResourceBundle.getBundle(name, locale, loader); }
134             catch (MissingResourceException e) {if(ex==null)ex=e;}
135             loader=(bundle==null&&checkParents)?loader.getParent():null;
136         }       
137 
138         if (bundle==null)
139         {
140             try { bundle=ResourceBundle.getBundle(name, locale); }
141             catch (MissingResourceException e) {if(ex==null)ex=e;}
142         }   
143 
144         if (bundle!=null)
145             return bundle;
146         throw ex;
147     }
148     
149     
150     /* ------------------------------------------------------------ */
151     /**
152      * Generate the classpath (as a string) of all classloaders
153      * above the given classloader.
154      * 
155      * This is primarily used for jasper.
156      * @param loader the classloader to use
157      * @return the system class path
158      * @throws Exception if unable to generate the classpath from the resource references
159      */
160     public static String getClassPath(ClassLoader loader) throws Exception
161     {
162         StringBuilder classpath=new StringBuilder();
163         while (loader != null && (loader instanceof URLClassLoader))
164         {
165             URL[] urls = ((URLClassLoader)loader).getURLs();
166             if (urls != null)
167             {     
168                 for (int i=0;i<urls.length;i++)
169                 {
170                     Resource resource = Resource.newResource(urls[i]);
171                     File file=resource.getFile();
172                     if (file!=null && file.exists())
173                     {
174                         if (classpath.length()>0)
175                             classpath.append(File.pathSeparatorChar);
176                         classpath.append(file.getAbsolutePath());
177                     }
178                 }
179             }
180             loader = loader.getParent();
181         }
182         return classpath.toString();
183     }
184 }
185