1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.io;
17
18 import java.io.IOException;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26
27 import org.apache.ibatis.logging.Log;
28 import org.apache.ibatis.logging.LogFactory;
29
30
31
32
33
34
35 public abstract class VFS {
36 private static final Log log = LogFactory.getLog(ResolverUtil.class);
37
38
39 public static final Class<?>[] IMPLEMENTATIONS = { JBoss6VFS.class, DefaultVFS.class };
40
41
42 public static final List<Class<? extends VFS>> USER_IMPLEMENTATIONS = new ArrayList<Class<? extends VFS>>();
43
44
45 private static VFS instance;
46
47
48
49
50
51 @SuppressWarnings("unchecked")
52 public static VFS getInstance() {
53 if (instance != null) {
54 return instance;
55 }
56
57
58 List<Class<? extends VFS>> impls = new ArrayList<Class<? extends VFS>>();
59 impls.addAll(USER_IMPLEMENTATIONS);
60 impls.addAll(Arrays.asList((Class<? extends VFS>[]) IMPLEMENTATIONS));
61
62
63 VFS vfs = null;
64 for (int i = 0; vfs == null || !vfs.isValid(); i++) {
65 Class<? extends VFS> impl = impls.get(i);
66 try {
67 vfs = impl.newInstance();
68 if (vfs == null || !vfs.isValid()) {
69 if (log.isDebugEnabled()) {
70 log.debug("VFS implementation " + impl.getName() +
71 " is not valid in this environment.");
72 }
73 }
74 } catch (InstantiationException e) {
75 log.error("Failed to instantiate " + impl, e);
76 return null;
77 } catch (IllegalAccessException e) {
78 log.error("Failed to instantiate " + impl, e);
79 return null;
80 }
81 }
82
83 if (log.isDebugEnabled()) {
84 log.debug("Using VFS adapter " + vfs.getClass().getName());
85 }
86 VFS.instance = vfs;
87 return VFS.instance;
88 }
89
90
91
92
93
94
95
96 public static void addImplClass(Class<? extends VFS> clazz) {
97 if (clazz != null) {
98 USER_IMPLEMENTATIONS.add(clazz);
99 }
100 }
101
102
103 protected static Class<?> getClass(String className) {
104 try {
105 return Thread.currentThread().getContextClassLoader().loadClass(className);
106
107 } catch (ClassNotFoundException e) {
108 if (log.isDebugEnabled()) {
109 log.debug("Class not found: " + className);
110 }
111 return null;
112 }
113 }
114
115
116
117
118
119
120
121
122 protected static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
123 if (clazz == null) {
124 return null;
125 }
126 try {
127 return clazz.getMethod(methodName, parameterTypes);
128 } catch (SecurityException e) {
129 log.error("Security exception looking for method " + clazz.getName() + "." + methodName + ". Cause: " + e);
130 return null;
131 } catch (NoSuchMethodException e) {
132 log.error("Method not found " + clazz.getName() + "." + methodName + "." + methodName + ". Cause: " + e);
133 return null;
134 }
135 }
136
137
138
139
140
141
142
143
144
145
146
147 @SuppressWarnings("unchecked")
148 protected static <T> T invoke(Method method, Object object, Object... parameters)
149 throws IOException, RuntimeException {
150 try {
151 return (T) method.invoke(object, parameters);
152 } catch (IllegalArgumentException e) {
153 throw new RuntimeException(e);
154 } catch (IllegalAccessException e) {
155 throw new RuntimeException(e);
156 } catch (InvocationTargetException e) {
157 if (e.getTargetException() instanceof IOException) {
158 throw (IOException) e.getTargetException();
159 } else {
160 throw new RuntimeException(e);
161 }
162 }
163 }
164
165
166
167
168
169
170
171
172
173 protected static List<URL> getResources(String path) throws IOException {
174 return Collections.list(Thread.currentThread().getContextClassLoader().getResources(path));
175 }
176
177
178 public abstract boolean isValid();
179
180
181
182
183
184
185
186
187
188
189
190 protected abstract List<String> list(URL url, String forPath) throws IOException;
191
192
193
194
195
196
197
198
199
200 public List<String> list(String path) throws IOException {
201 List<String> names = new ArrayList<String>();
202 for (URL url : getResources(path)) {
203 names.addAll(list(url, path));
204 }
205 return names;
206 }
207 }