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.Method;
20 import java.net.URL;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.locks.ReentrantLock;
24
25 import org.apache.ibatis.logging.Log;
26 import org.apache.ibatis.logging.LogFactory;
27
28
29
30
31
32
33 public class JBoss6VFS extends VFS {
34 private static final Log log = LogFactory.getLog(JBoss6VFS.class);
35 private static final ReentrantLock lock = new ReentrantLock();
36
37
38 static class VirtualFile {
39 static Class<?> VirtualFile;
40 static Method getPathNameRelativeTo;
41 static Method getChildrenRecursively;
42
43 Object virtualFile;
44
45 VirtualFile(Object virtualFile) {
46 this.virtualFile = virtualFile;
47 }
48
49 String getPathNameRelativeTo(VirtualFile parent) {
50 try {
51 return invoke(getPathNameRelativeTo, virtualFile, parent.virtualFile);
52 } catch (IOException e) {
53
54 log.error("This should not be possible. VirtualFile.getPathNameRelativeTo() threw IOException.");
55 return null;
56 }
57 }
58
59 List<VirtualFile> getChildren() throws IOException {
60 List<?> objects = invoke(getChildrenRecursively, virtualFile);
61 List<VirtualFile> children = new ArrayList<>(objects.size());
62 for (Object object : objects) {
63 children.add(new VirtualFile(object));
64 }
65 return children;
66 }
67 }
68
69
70 static class VFS {
71 static Class<?> VFS;
72 static Method getChild;
73
74 private VFS() {
75
76 }
77
78 static VirtualFile getChild(URL url) throws IOException {
79 Object o = invoke(getChild, VFS, url);
80 return o == null ? null : new VirtualFile(o);
81 }
82 }
83
84
85 private static Boolean valid;
86
87
88 protected static void initialize() {
89 lock.lock();
90 try {
91 if (valid == null) {
92
93 valid = Boolean.TRUE;
94
95
96 VFS.VFS = checkNotNull(getClass("org.jboss.vfs.VFS"));
97 VirtualFile.VirtualFile = checkNotNull(getClass("org.jboss.vfs.VirtualFile"));
98
99
100 VFS.getChild = checkNotNull(getMethod(VFS.VFS, "getChild", URL.class));
101 VirtualFile.getChildrenRecursively = checkNotNull(getMethod(VirtualFile.VirtualFile, "getChildrenRecursively"));
102 VirtualFile.getPathNameRelativeTo = checkNotNull(
103 getMethod(VirtualFile.VirtualFile, "getPathNameRelativeTo", VirtualFile.VirtualFile));
104
105
106 checkReturnType(VFS.getChild, VirtualFile.VirtualFile);
107 checkReturnType(VirtualFile.getChildrenRecursively, List.class);
108 checkReturnType(VirtualFile.getPathNameRelativeTo, String.class);
109 }
110 } finally {
111 lock.unlock();
112 }
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126 protected static <T> T checkNotNull(T object) {
127 if (object == null) {
128 setInvalid();
129 }
130 return object;
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144 protected static void checkReturnType(Method method, Class<?> expected) {
145 if (method != null && !expected.isAssignableFrom(method.getReturnType())) {
146 log.error("Method " + method.getClass().getName() + "." + method.getName() + "(..) should return "
147 + expected.getName() + " but returns " + method.getReturnType().getName() + " instead.");
148 setInvalid();
149 }
150 }
151
152
153
154
155 protected static void setInvalid() {
156 if (JBoss6VFS.valid.booleanValue()) {
157 log.debug("JBoss 6 VFS API is not available in this environment.");
158 JBoss6VFS.valid = Boolean.FALSE;
159 }
160 }
161
162 static {
163 initialize();
164 }
165
166 @Override
167 public boolean isValid() {
168 return valid;
169 }
170
171 @Override
172 public List<String> list(URL url, String path) throws IOException {
173 VirtualFile directory;
174 directory = VFS.getChild(url);
175 if (directory == null) {
176 return List.of();
177 }
178
179 if (!path.endsWith("/")) {
180 path += "/";
181 }
182
183 List<VirtualFile> children = directory.getChildren();
184 List<String> names = new ArrayList<>(children.size());
185 for (VirtualFile vf : children) {
186 names.add(path + vf.getPathNameRelativeTo(directory));
187 }
188
189 return names;
190 }
191 }