1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.migration.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 import java.util.logging.Level;
27 import java.util.logging.Logger;
28
29
30
31
32
33
34 public abstract class VFS {
35 private static final Logger log = Logger.getLogger(VFS.class.getName());
36
37
38 protected static final Class<?>[] IMPLEMENTATIONS = { JBoss6VFS.class, DefaultVFS.class };
39
40
41
42
43 protected static final List<Class<? extends VFS>> USER_IMPLEMENTATIONS = new ArrayList<>();
44
45
46 private static class VFSHolder {
47 static final VFS INSTANCE = createVFS();
48
49 @SuppressWarnings("unchecked")
50 static VFS createVFS() {
51
52 List<Class<? extends VFS>> impls = new ArrayList<>(USER_IMPLEMENTATIONS);
53 impls.addAll(Arrays.asList((Class<? extends VFS>[]) IMPLEMENTATIONS));
54
55
56 VFS vfs = null;
57 for (int i = 0; vfs == null || !vfs.isValid(); i++) {
58 Class<? extends VFS> impl = impls.get(i);
59 try {
60 vfs = impl.getDeclaredConstructor().newInstance();
61 if (!vfs.isValid() && log.isLoggable(Level.FINER)) {
62 log.log(Level.FINER, "VFS implementation " + impl.getName() + " is not valid in this environment.");
63 }
64 } catch (InstantiationException | IllegalAccessException | NoSuchMethodException
65 | InvocationTargetException e) {
66 log.log(Level.SEVERE, "Failed to instantiate " + impl, e);
67 return null;
68 }
69 }
70
71 if (log.isLoggable(Level.FINER)) {
72 log.log(Level.FINER, "Using VFS adapter " + vfs.getClass().getName());
73 }
74
75 return vfs;
76 }
77 }
78
79
80
81
82
83
84
85 public static VFS getInstance() {
86 return VFSHolder.INSTANCE;
87 }
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
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.isLoggable(Level.FINER)) {
116 log.log(Level.FINER, "Class not found: " + className);
117 }
118 return null;
119 }
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134 protected static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
135 if (clazz == null) {
136 return null;
137 }
138 try {
139 return clazz.getMethod(methodName, parameterTypes);
140 } catch (SecurityException e) {
141 log.log(Level.SEVERE,
142 "Security exception looking for method " + clazz.getName() + "." + methodName + ". Cause: " + e);
143 return null;
144 } catch (NoSuchMethodException e) {
145 log.log(Level.SEVERE,
146 "Method not found " + clazz.getName() + "." + methodName + "." + methodName + ". Cause: " + e);
147 return null;
148 }
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 @SuppressWarnings("unchecked")
171 protected static <T> T invoke(Method method, Object object, Object... parameters)
172 throws IOException, RuntimeException {
173 try {
174 return (T) method.invoke(object, parameters);
175 } catch (IllegalArgumentException | IllegalAccessException e) {
176 throw new RuntimeException(e);
177 } catch (InvocationTargetException e) {
178 if (e.getTargetException() instanceof IOException) {
179 throw (IOException) e.getTargetException();
180 }
181 throw new RuntimeException(e);
182 }
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196 protected static List<URL> getResources(String path) throws IOException {
197 return Collections.list(Thread.currentThread().getContextClassLoader().getResources(path));
198 }
199
200
201
202
203
204
205 public abstract boolean isValid();
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 protected abstract List<String> list(URL url, String forPath) throws IOException;
222
223
224
225
226
227
228
229
230
231
232
233
234
235 public List<String> list(String path) throws IOException {
236 List<String> names = new ArrayList<>();
237 for (URL url : getResources(path)) {
238 names.addAll(list(url, path));
239 }
240 return names;
241 }
242 }