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 public static Class loadClass(Class loadClass,String name)
68 throws ClassNotFoundException
69 {
70 return loadClass(loadClass,name,false);
71 }
72
73
74
75
76
77
78
79
80
81
82 public static Class loadClass(Class loadClass,String name,boolean checkParents)
83 throws ClassNotFoundException
84 {
85 ClassNotFoundException ex=null;
86 Class c =null;
87 ClassLoader loader=Thread.currentThread().getContextClassLoader();
88 while (c==null && loader!=null )
89 {
90 try { c=loader.loadClass(name); }
91 catch (ClassNotFoundException e) {if(ex==null)ex=e;}
92 loader=(c==null&&checkParents)?loader.getParent():null;
93 }
94
95 loader=loadClass==null?null:loadClass.getClassLoader();
96 while (c==null && loader!=null )
97 {
98 try { c=loader.loadClass(name); }
99 catch (ClassNotFoundException e) {if(ex==null)ex=e;}
100 loader=(c==null&&checkParents)?loader.getParent():null;
101 }
102
103 if (c==null)
104 {
105 try { c=Class.forName(name); }
106 catch (ClassNotFoundException e) {if(ex==null)ex=e;}
107 }
108
109 if (c!=null)
110 return c;
111 throw ex;
112 }
113
114 public static ResourceBundle getResourceBundle(Class loadClass,String name,boolean checkParents, Locale locale)
115 throws MissingResourceException
116 {
117 MissingResourceException ex=null;
118 ResourceBundle bundle =null;
119 ClassLoader loader=Thread.currentThread().getContextClassLoader();
120 while (bundle==null && loader!=null )
121 {
122 try { bundle=ResourceBundle.getBundle(name, locale, loader); }
123 catch (MissingResourceException e) {if(ex==null)ex=e;}
124 loader=(bundle==null&&checkParents)?loader.getParent():null;
125 }
126
127 loader=loadClass==null?null:loadClass.getClassLoader();
128 while (bundle==null && loader!=null )
129 {
130 try { bundle=ResourceBundle.getBundle(name, locale, loader); }
131 catch (MissingResourceException e) {if(ex==null)ex=e;}
132 loader=(bundle==null&&checkParents)?loader.getParent():null;
133 }
134
135 if (bundle==null)
136 {
137 try { bundle=ResourceBundle.getBundle(name, locale); }
138 catch (MissingResourceException e) {if(ex==null)ex=e;}
139 }
140
141 if (bundle!=null)
142 return bundle;
143 throw ex;
144 }
145
146
147 }
148