1
2
3
4
5
6
7
8
9
10 package ch.qos.logback.core.util;
11
12 import java.io.IOException;
13 import java.net.URL;
14 import java.util.ArrayList;
15 import java.util.Enumeration;
16 import java.util.List;
17
18 import ch.qos.logback.core.Context;
19
20
21
22
23
24
25 public class Loader {
26 static final String TSTR = "Caught Exception while in Loader.getResource. This may be innocuous.";
27
28 private static boolean ignoreTCL = false;
29 public static final String IGNORE_TCL_PROPERTY_NAME = "logback.ignoreTCL";
30
31 static {
32
33 String ignoreTCLProp = OptionHelper.getSystemProperty(
34 IGNORE_TCL_PROPERTY_NAME, null);
35
36 if (ignoreTCLProp != null) {
37 ignoreTCL = OptionHelper.toBoolean(ignoreTCLProp, true);
38 }
39 }
40
41
42
43
44
45
46
47
48
49
50 public static List<URL> getResourceOccurenceCount(String resource,
51 ClassLoader classLoader) throws IOException {
52 List<URL> urlList = new ArrayList<URL>();
53 Enumeration<URL> urlEnum = classLoader.getResources(resource);
54 while (urlEnum.hasMoreElements()) {
55 URL url = urlEnum.nextElement();
56 urlList.add(url);
57 }
58
59 return urlList;
60 }
61
62
63
64
65
66
67
68
69
70 public static URL getResource(String resource, ClassLoader classLoader) {
71 try {
72 return classLoader.getResource(resource);
73 } catch (Throwable t) {
74 return null;
75 }
76 }
77
78
79
80
81
82
83
84
85 public static URL getResourceBySelfClassLoader(String resource) {
86 return getResource(resource, Loader.class.getClassLoader());
87 }
88
89
90
91
92
93
94
95
96
97
98
99 public static ClassLoader getTCL() {
100 return Thread.currentThread().getContextClassLoader();
101 }
102
103 @SuppressWarnings("unchecked")
104 public static Class loadClass(String clazz, Context context)
105 throws ClassNotFoundException {
106 ClassLoader cl = context.getClass().getClassLoader();
107 return cl.loadClass(clazz);
108 }
109
110
111
112
113
114
115
116 @SuppressWarnings("unchecked")
117 public static Class loadClass(String clazz) throws ClassNotFoundException {
118
119
120 if (ignoreTCL) {
121 return Class.forName(clazz);
122 } else {
123 try {
124 return getTCL().loadClass(clazz);
125 } catch (Throwable e) {
126
127
128
129 return Class.forName(clazz);
130 }
131 }
132 }
133 }