1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.util;
15
16 import java.net.URL;
17 import java.util.Locale;
18 import java.util.MissingResourceException;
19 import java.util.ResourceBundle;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class Loader
38 {
39
40 public static URL getResource(Class<?> loadClass,String name, boolean checkParents)
41 throws ClassNotFoundException
42 {
43 URL url =null;
44 ClassLoader loader=Thread.currentThread().getContextClassLoader();
45 while (url==null && loader!=null )
46 {
47 url=loader.getResource(name);
48 loader=(url==null&&checkParents)?loader.getParent():null;
49 }
50
51 loader=loadClass==null?null:loadClass.getClassLoader();
52 while (url==null && loader!=null )
53 {
54 url=loader.getResource(name);
55 loader=(url==null&&checkParents)?loader.getParent():null;
56 }
57
58 if (url==null)
59 {
60 url=ClassLoader.getSystemResource(name);
61 }
62
63 return url;
64 }
65
66
67 @SuppressWarnings("rawtypes")
68 public static Class loadClass(Class loadClass,String name)
69 throws ClassNotFoundException
70 {
71 return loadClass(loadClass,name,false);
72 }
73
74
75
76
77
78
79
80
81
82
83 @SuppressWarnings("rawtypes")
84 public static Class loadClass(Class loadClass,String name,boolean checkParents)
85 throws ClassNotFoundException
86 {
87 ClassNotFoundException ex=null;
88 Class<?> c =null;
89 ClassLoader loader=Thread.currentThread().getContextClassLoader();
90 while (c==null && loader!=null )
91 {
92 try { c=loader.loadClass(name); }
93 catch (ClassNotFoundException e) {if(ex==null)ex=e;}
94 loader=(c==null&&checkParents)?loader.getParent():null;
95 }
96
97 loader=loadClass==null?null:loadClass.getClassLoader();
98 while (c==null && loader!=null )
99 {
100 try { c=loader.loadClass(name); }
101 catch (ClassNotFoundException e) {if(ex==null)ex=e;}
102 loader=(c==null&&checkParents)?loader.getParent():null;
103 }
104
105 if (c==null)
106 {
107 try { c=Class.forName(name); }
108 catch (ClassNotFoundException e) {if(ex==null)ex=e;}
109 }
110
111 if (c!=null)
112 return c;
113 throw ex;
114 }
115
116 public static ResourceBundle getResourceBundle(Class<?> loadClass,String name,boolean checkParents, Locale locale)
117 throws MissingResourceException
118 {
119 MissingResourceException ex=null;
120 ResourceBundle bundle =null;
121 ClassLoader loader=Thread.currentThread().getContextClassLoader();
122 while (bundle==null && loader!=null )
123 {
124 try { bundle=ResourceBundle.getBundle(name, locale, loader); }
125 catch (MissingResourceException e) {if(ex==null)ex=e;}
126 loader=(bundle==null&&checkParents)?loader.getParent():null;
127 }
128
129 loader=loadClass==null?null:loadClass.getClassLoader();
130 while (bundle==null && loader!=null )
131 {
132 try { bundle=ResourceBundle.getBundle(name, locale, loader); }
133 catch (MissingResourceException e) {if(ex==null)ex=e;}
134 loader=(bundle==null&&checkParents)?loader.getParent():null;
135 }
136
137 if (bundle==null)
138 {
139 try { bundle=ResourceBundle.getBundle(name, locale); }
140 catch (MissingResourceException e) {if(ex==null)ex=e;}
141 }
142
143 if (bundle!=null)
144 return bundle;
145 throw ex;
146 }
147
148
149 }
150