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)
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
71
72
73
74
75
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
153
154
155
156
157
158
159
160 public static String getClassPath(ClassLoader loader) throws Exception
161 {
162 StringBuilder classpath=new StringBuilder();
163 while (loader != null && (loader instanceof URLClassLoader))
164 {
165 URL[] urls = ((URLClassLoader)loader).getURLs();
166 if (urls != null)
167 {
168 for (int i=0;i<urls.length;i++)
169 {
170 Resource resource = Resource.newResource(urls[i]);
171 File file=resource.getFile();
172 if (file!=null && file.exists())
173 {
174 if (classpath.length()>0)
175 classpath.append(File.pathSeparatorChar);
176 classpath.append(file.getAbsolutePath());
177 }
178 }
179 }
180 loader = loader.getParent();
181 }
182 return classpath.toString();
183 }
184 }
185