View Javadoc
1   /*
2    *    Copyright 2009-2026 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * A {@link VFS} implementation that works with the VFS API provided by JBoss 6.
30   *
31   * @author Ben Gunter
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    /** A class that mimics a tiny subset of the JBoss VirtualFile class. */
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          // This exception is not thrown by the called method
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    /** A class that mimics a tiny subset of the JBoss VFS class. */
70    static class VFS {
71      static Class<?> VFS;
72      static Method getChild;
73  
74      private VFS() {
75        // Prevent Instantiation
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    /** Flag that indicates if this VFS is valid for the current environment. */
85    private static Boolean valid;
86  
87    /** Find all the classes and methods that are required to access the JBoss 6 VFS. */
88    protected static void initialize() {
89      lock.lock();
90      try {
91        if (valid == null) {
92          // Assume valid. It will get flipped later if something goes wrong.
93          valid = Boolean.TRUE;
94  
95          // Look up and verify required classes
96          VFS.VFS = checkNotNull(getClass("org.jboss.vfs.VFS"));
97          VirtualFile.VirtualFile = checkNotNull(getClass("org.jboss.vfs.VirtualFile"));
98  
99          // Look up and verify required methods
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         // Verify that the API has not changed
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    * Verifies that the provided object reference is null. If it is null, then this VFS is marked as invalid for the
117    * current environment.
118    *
119    * @param <T>
120    *          the generic type
121    * @param object
122    *          The object reference to check for null.
123    *
124    * @return the t
125    */
126   protected static <T> T checkNotNull(T object) {
127     if (object == null) {
128       setInvalid();
129     }
130     return object;
131   }
132 
133   /**
134    * Verifies that the return type of method is what it is expected to be. If it is not, then this VFS is marked as
135    * invalid for the current environment.
136    *
137    * @param method
138    *          The method whose return type is to be checked.
139    * @param expected
140    *          A type to which the method's return type must be assignable.
141    *
142    * @see Class#isAssignableFrom(Class)
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    * Mark this {@link VFS} as invalid for the current environment.
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 }