View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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, boolean checkParents)
50          throws ClassNotFoundException
51      {
52          URL url =null;
53          ClassLoader loader=Thread.currentThread().getContextClassLoader();
54          while (url==null && loader!=null )
55          {
56              url=loader.getResource(name); 
57              loader=(url==null&&checkParents)?loader.getParent():null;
58          }      
59          
60          loader=loadClass==null?null:loadClass.getClassLoader();
61          while (url==null && loader!=null )
62          {
63              url=loader.getResource(name); 
64              loader=(url==null&&checkParents)?loader.getParent():null;
65          }       
66  
67          if (url==null)
68          {
69              url=ClassLoader.getSystemResource(name);
70          }   
71  
72          return url;
73      }
74  
75      /* ------------------------------------------------------------ */
76      @SuppressWarnings("rawtypes")
77      public static Class loadClass(Class loadClass,String name)
78          throws ClassNotFoundException
79      {
80          return loadClass(loadClass,name,false);
81      }
82      
83      /* ------------------------------------------------------------ */
84      /** Load a class.
85       * 
86       * @param loadClass
87       * @param name
88       * @param checkParents If true, try loading directly from parent classloaders.
89       * @return Class
90       * @throws ClassNotFoundException
91       */
92      @SuppressWarnings("rawtypes")
93      public static Class loadClass(Class loadClass,String name,boolean checkParents)
94          throws ClassNotFoundException
95      {
96          ClassNotFoundException ex=null;
97          Class<?> c =null;
98          ClassLoader loader=Thread.currentThread().getContextClassLoader();
99          while (c==null && loader!=null )
100         {
101             try { c=loader.loadClass(name); }
102             catch (ClassNotFoundException e) {if(ex==null)ex=e;}
103             loader=(c==null&&checkParents)?loader.getParent():null;
104         }      
105         
106         loader=loadClass==null?null:loadClass.getClassLoader();
107         while (c==null && loader!=null )
108         {
109             try { c=loader.loadClass(name); }
110             catch (ClassNotFoundException e) {if(ex==null)ex=e;}
111             loader=(c==null&&checkParents)?loader.getParent():null;
112         }       
113 
114         if (c==null)
115         {
116             try { c=Class.forName(name); }
117             catch (ClassNotFoundException e) {if(ex==null)ex=e;}
118         }   
119 
120         if (c!=null)
121             return c;
122         throw ex;
123     }
124     
125     
126     
127     /* ------------------------------------------------------------ */
128     public static ResourceBundle getResourceBundle(Class<?> loadClass,String name,boolean checkParents, Locale locale)
129         throws MissingResourceException
130     {
131         MissingResourceException ex=null;
132         ResourceBundle bundle =null;
133         ClassLoader loader=Thread.currentThread().getContextClassLoader();
134         while (bundle==null && loader!=null )
135         {
136             try { bundle=ResourceBundle.getBundle(name, locale, loader); }
137             catch (MissingResourceException e) {if(ex==null)ex=e;}
138             loader=(bundle==null&&checkParents)?loader.getParent():null;
139         }      
140         
141         loader=loadClass==null?null:loadClass.getClassLoader();
142         while (bundle==null && loader!=null )
143         {
144             try { bundle=ResourceBundle.getBundle(name, locale, loader); }
145             catch (MissingResourceException e) {if(ex==null)ex=e;}
146             loader=(bundle==null&&checkParents)?loader.getParent():null;
147         }       
148 
149         if (bundle==null)
150         {
151             try { bundle=ResourceBundle.getBundle(name, locale); }
152             catch (MissingResourceException e) {if(ex==null)ex=e;}
153         }   
154 
155         if (bundle!=null)
156             return bundle;
157         throw ex;
158     }
159     
160     
161     /* ------------------------------------------------------------ */
162     /**
163      * Generate the classpath (as a string) of all classloaders
164      * above the given classloader.
165      * 
166      * This is primarily used for jasper.
167      * @return the system class path
168      */
169     public static String getClassPath(ClassLoader loader) throws Exception
170     {
171         StringBuilder classpath=new StringBuilder();
172         while (loader != null && (loader instanceof URLClassLoader))
173         {
174             URL[] urls = ((URLClassLoader)loader).getURLs();
175             if (urls != null)
176             {     
177                 for (int i=0;i<urls.length;i++)
178                 {
179                     Resource resource = Resource.newResource(urls[i]);
180                     File file=resource.getFile();
181                     if (file!=null && file.exists())
182                     {
183                         if (classpath.length()>0)
184                             classpath.append(File.pathSeparatorChar);
185                         classpath.append(file.getAbsolutePath());
186                     }
187                 }
188             }
189             loader = loader.getParent();
190         }
191         return classpath.toString();
192     }
193 }
194