Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dc47989ea7120a7c4e75b515c69831b38fde8cef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.util;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.jetty.util.resource.Resource;

/* ------------------------------------------------------------ */
/** ClassLoader Helper.
 * This helper class allows classes to be loaded either from the
 * Thread's ContextClassLoader, the classloader of the derived class
 * or the system ClassLoader.
 *
 * <B>Usage:</B><PRE>
 * public class MyClass {
 *     void myMethod() {
 *          ...
 *          Class c=Loader.loadClass(this.getClass(),classname);
 *          ...
 *     }
 * </PRE>          
 * 
 */
public class Loader
{
    /* ------------------------------------------------------------ */
    public static URL getResource(Class<?> loadClass,String name)
    {
        URL url =null;
        ClassLoader context_loader=Thread.currentThread().getContextClassLoader();
        if (context_loader!=null)
            url=context_loader.getResource(name); 
        
        if (url==null && loadClass!=null)
        {
            ClassLoader load_loader=loadClass.getClassLoader();
            if (load_loader!=null && load_loader!=context_loader)
                url=load_loader.getResource(name);
        }

        if (url==null)
            url=ClassLoader.getSystemResource(name);

        return url;
    }

    /* ------------------------------------------------------------ */
    /** Load a class.
     * 
     * @param loadClass a similar class, belong in the same classloader of the desired class to load
     * @param name the name of the new class to load, using the same ClassLoader as the <code>loadClass</code> 
     * @return Class
     * @throws ClassNotFoundException if not able to find the class
     */
    @SuppressWarnings("rawtypes")
    public static Class loadClass(Class loadClass,String name)
        throws ClassNotFoundException
    {
        ClassNotFoundException ex=null;
        Class<?> c =null;
        ClassLoader context_loader=Thread.currentThread().getContextClassLoader();
        if (context_loader!=null )
        {
            try { c=context_loader.loadClass(name); }
            catch (ClassNotFoundException e) {ex=e;}
        }    
        
        if (c==null && loadClass!=null)
        {
            ClassLoader load_loader=loadClass.getClassLoader();
            if (load_loader!=null && load_loader!=context_loader)
            {
                try { c=load_loader.loadClass(name); }
                catch (ClassNotFoundException e) {if(ex==null)ex=e;}
            }
        }

        if (c==null)
        {
            try { c=Class.forName(name); }
            catch (ClassNotFoundException e) 
            {
                if(ex!=null)
                    throw ex;
                throw e;
            }
        }   

        return c;
    }
    
    
    
    /* ------------------------------------------------------------ */
    public static ResourceBundle getResourceBundle(Class<?> loadClass,String name,boolean checkParents, Locale locale)
        throws MissingResourceException
    {
        MissingResourceException ex=null;
        ResourceBundle bundle =null;
        ClassLoader loader=Thread.currentThread().getContextClassLoader();
        while (bundle==null && loader!=null )
        {
            try { bundle=ResourceBundle.getBundle(name, locale, loader); }
            catch (MissingResourceException e) {if(ex==null)ex=e;}
            loader=(bundle==null&&checkParents)?loader.getParent():null;
        }      
        
        loader=loadClass==null?null:loadClass.getClassLoader();
        while (bundle==null && loader!=null )
        {
            try { bundle=ResourceBundle.getBundle(name, locale, loader); }
            catch (MissingResourceException e) {if(ex==null)ex=e;}
            loader=(bundle==null&&checkParents)?loader.getParent():null;
        }       

        if (bundle==null)
        {
            try { bundle=ResourceBundle.getBundle(name, locale); }
            catch (MissingResourceException e) {if(ex==null)ex=e;}
        }   

        if (bundle!=null)
            return bundle;
        throw ex;
    }
    
    
    /* ------------------------------------------------------------ */
    /**
     * Generate the classpath (as a string) of all classloaders
     * above the given classloader.
     * 
     * This is primarily used for jasper.
     * @param loader the classloader to use
     * @return the system class path
     * @throws Exception if unable to generate the classpath from the resource references
     */
    public static String getClassPath(ClassLoader loader) throws Exception
    {
        StringBuilder classpath=new StringBuilder();
        while (loader != null && (loader instanceof URLClassLoader))
        {
            URL[] urls = ((URLClassLoader)loader).getURLs();
            if (urls != null)
            {     
                for (int i=0;i<urls.length;i++)
                {
                    Resource resource = Resource.newResource(urls[i]);
                    File file=resource.getFile();
                    if (file!=null && file.exists())
                    {
                        if (classpath.length()>0)
                            classpath.append(File.pathSeparatorChar);
                        classpath.append(file.getAbsolutePath());
                    }
                }
            }
            loader = loader.getParent();
        }
        return classpath.toString();
    }
}

Back to the top