1 /* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2  *
3  * This program and the accompanying materials are made available under
4  * the terms of the Common Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/cpl-v10.html
6  *
7  * $Id: ResourceLoader.java,v 1.1.1.1.2.1 2004/06/20 18:24:05 vlad_r Exp $
8  */
9 package com.vladium.util;
10 
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.net.URL;
14 import java.util.Enumeration;
15 
16 // ----------------------------------------------------------------------------
17 /**
18  * A static API that can be used as a drop-in replacement for
19  * java.lang.ClassLoader API (the class/resource loading part). This
20  * implementation is merely a wrapper around ClassLoaderResolverget.ClassLoader()
21  * method.
22  *
23  * @author Vlad Roubtsov, (C) 2003
24  */
25 public
26 abstract class ResourceLoader
27 {
28     // public: ................................................................
29 
30     /**
31      * @see java.lang.ClassLoader#loadClass(java.lang.String)
32      */
loadClass(final String name)33     public static Class loadClass (final String name)
34         throws ClassNotFoundException
35     {
36         final Class caller = ClassLoaderResolver.getCallerClass (1);
37         final ClassLoader loader = ClassLoaderResolver.getClassLoader (caller);
38 
39         return Class.forName (name, false, loader);
40     }
41 
42     /**
43      * @see java.lang.ClassLoader#getResource(java.lang.String)
44      */
getResource(final String name)45     public static URL getResource (final String name)
46     {
47         final Class caller = ClassLoaderResolver.getCallerClass (1);
48         final ClassLoader loader = ClassLoaderResolver.getClassLoader (caller);
49 
50         if (loader != null)
51             return loader.getResource (name);
52         else
53             return ClassLoader.getSystemResource (name);
54     }
55 
56     /**
57      * @see java.lang.ClassLoader#getResourceAsStream(java.lang.String)
58      */
getResourceAsStream(final String name)59     public static InputStream getResourceAsStream (final String name)
60     {
61         final Class caller = ClassLoaderResolver.getCallerClass (1);
62         final ClassLoader loader = ClassLoaderResolver.getClassLoader (caller);
63 
64         if (loader != null)
65             return loader.getResourceAsStream (name);
66         else
67             return ClassLoader.getSystemResourceAsStream (name);
68     }
69 
70     /**
71      * @see java.lang.ClassLoader#getResources(java.lang.String)
72      */
getResources(final String name)73     public static Enumeration getResources (final String name)
74         throws IOException
75     {
76         final Class caller = ClassLoaderResolver.getCallerClass (1);
77         final ClassLoader loader = ClassLoaderResolver.getClassLoader (caller);
78 
79         if (loader != null)
80             return loader.getResources (name);
81         else
82             return ClassLoader.getSystemResources (name);
83     }
84 
85 
loadClass(final String name, final ClassLoader loader)86     public static Class loadClass (final String name, final ClassLoader loader)
87         throws ClassNotFoundException
88     {
89         return Class.forName (name, false, loader != null ? loader : ClassLoader.getSystemClassLoader ());
90     }
91 
getResource(final String name, final ClassLoader loader)92     public static URL getResource (final String name, final ClassLoader loader)
93     {
94         if (loader != null)
95             return loader.getResource (name);
96         else
97             return ClassLoader.getSystemResource (name);
98     }
99 
getResourceAsStream(final String name, final ClassLoader loader)100     public static InputStream getResourceAsStream (final String name, final ClassLoader loader)
101     {
102         if (loader != null)
103             return loader.getResourceAsStream (name);
104         else
105             return ClassLoader.getSystemResourceAsStream (name);
106     }
107 
getResources(final String name, final ClassLoader loader)108     public static Enumeration getResources (final String name, final ClassLoader loader)
109         throws IOException
110     {
111         if (loader != null)
112             return loader.getResources (name);
113         else
114             return ClassLoader.getSystemResources (name);
115     }
116 
117     // protected: .............................................................
118 
119     // package: ...............................................................
120 
121     // private: ...............................................................
122 
123 
ResourceLoader()124     private ResourceLoader () {} // prevent subclassing
125 
126 } // end of class
127 // ----------------------------------------------------------------------------