1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33
34
35
36
37
38
39
40
41
42
43
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
85
86
87
88
89
90
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
164
165
166
167
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