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(VFS.class);
37
38
39 private static final Class<?>[] IMPLEMENTATIONS = { JBoss6VFS.class, DefaultVFS.class };
40
41
42
43
44 private static final List<Class<? extends VFS>> USER_IMPLEMENTATIONS = new ArrayList<>();
45
46
47 private static class VFSHolder {
48 static final VFS INSTANCE = createVFS();
49
50 @SuppressWarnings("unchecked")
51 static VFS createVFS() {
52
53 List<Class<? extends VFS>> impls = new ArrayList<>(USER_IMPLEMENTATIONS);
54 impls.addAll(Arrays.asList((Class<? extends VFS>[]) IMPLEMENTATIONS));
55
56
57 VFS vfs = null;
58 for (int i = 0; vfs == null || !vfs.isValid(); i++) {
59 Class<? extends VFS> impl = impls.get(i);
60 try {
61 vfs = impl.getDeclaredConstructor().newInstance();
62 if (!vfs.isValid() && log.isDebugEnabled()) {
63 log.debug("VFS implementation " + impl.getName() + " is not valid in this environment.");
64 }
65 } catch (InstantiationException | IllegalAccessException | NoSuchMethodException
66 | InvocationTargetException e) {
67 log.error("Failed to instantiate " + impl, e);
68 return null;
69 }
70 }
71
72 if (log.isDebugEnabled()) {
73 log.debug("Using VFS adapter " + vfs.getClass().getName());
74 }
75
76 return vfs;
77 }
78
79 private VFSHolder() {
80 }
81 }
82
83
84
85
86
87
88
89 public static VFS getInstance() {
90 return VFSHolder.INSTANCE;
91 }
92
93
94
95
96
97
98
99
100 public static void addImplClass(Class<? extends VFS> clazz) {
101 if (clazz != null) {
102 USER_IMPLEMENTATIONS.add(clazz);
103 }
104 }
105
106
107
108
109
110
111
112
113
114 protected static Class<?> getClass(String className) {
115 try {
116 return Thread.currentThread().getContextClassLoader().loadClass(className);
117
118 } catch (ClassNotFoundException e) {
119 if (log.isDebugEnabled()) {
120 log.debug("Class not found: " + className);
121 }
122 return null;
123 }
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138 protected static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
139 if (clazz == null) {
140 return null;
141 }
142 try {
143 return clazz.getMethod(methodName, parameterTypes);
144 } catch (SecurityException e) {
145 log.error("Security exception looking for method " + clazz.getName() + "." + methodName + ". Cause: " + e);
146 return null;
147 } catch (NoSuchMethodException e) {
148 log.error("Method not found " + clazz.getName() + "." + methodName + ". Cause: " + e);
149 return null;
150 }
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 @SuppressWarnings("unchecked")
173 protected static <T> T invoke(Method method, Object object, Object... parameters)
174 throws IOException, RuntimeException {
175 try {
176 return (T) method.invoke(object, parameters);
177 } catch (IllegalArgumentException | IllegalAccessException e) {
178 throw new RuntimeException(e);
179 } catch (InvocationTargetException e) {
180 if (e.getTargetException() instanceof IOException) {
181 throw (IOException) e.getTargetException();
182 }
183 throw new RuntimeException(e);
184 }
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198 protected static List<URL> getResources(String path) throws IOException {
199 return Collections.list(Thread.currentThread().getContextClassLoader().getResources(path));
200 }
201
202
203
204
205
206
207 public abstract boolean isValid();
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223 protected abstract List<String> list(URL url, String forPath) throws IOException;
224
225
226
227
228
229
230
231
232
233
234
235
236
237 public List<String> list(String path) throws IOException {
238 List<String> names = new ArrayList<>();
239 for (URL url : getResources(path)) {
240 names.addAll(list(url, path));
241 }
242 return names;
243 }
244 }