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