]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/util/Loader.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / util / Loader.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2014 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
73      * @param name
74      * @return Class
75      * @throws ClassNotFoundException
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      * @return the system class path
157      */
158     public static String getClassPath(ClassLoader loader) throws Exception
159     {
160         StringBuilder classpath=new StringBuilder();
161         while (loader != null && (loader instanceof URLClassLoader))
162         {
163             URL[] urls = ((URLClassLoader)loader).getURLs();
164             if (urls != null)
165             {     
166                 for (int i=0;i<urls.length;i++)
167                 {
168                     Resource resource = Resource.newResource(urls[i]);
169                     File file=resource.getFile();
170                     if (file!=null && file.exists())
171                     {
172                         if (classpath.length()>0)
173                             classpath.append(File.pathSeparatorChar);
174                         classpath.append(file.getAbsolutePath());
175                     }
176                 }
177             }
178             loader = loader.getParent();
179         }
180         return classpath.toString();
181     }
182 }
183