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