View Javadoc
1   /*
2    *    Copyright 2009-2022 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    *       http://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.Collections;
23  import java.util.List;
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  
36    /** A class that mimics a tiny subset of the JBoss VirtualFile class. */
37    static class VirtualFile {
38      static Class<?> VirtualFile;
39      static Method getPathNameRelativeTo;
40      static Method getChildrenRecursively;
41  
42      Object virtualFile;
43  
44      VirtualFile(Object virtualFile) {
45        this.virtualFile = virtualFile;
46      }
47  
48      String getPathNameRelativeTo(VirtualFile parent) {
49        try {
50          return invoke(getPathNameRelativeTo, virtualFile, parent.virtualFile);
51        } catch (IOException e) {
52          // This exception is not thrown by the called method
53          log.error("This should not be possible. VirtualFile.getPathNameRelativeTo() threw IOException.");
54          return null;
55        }
56      }
57  
58      List<VirtualFile> getChildren() throws IOException {
59        List<?> objects = invoke(getChildrenRecursively, virtualFile);
60        List<VirtualFile> children = new ArrayList<>(objects.size());
61        for (Object object : objects) {
62          children.add(new VirtualFile(object));
63        }
64        return children;
65      }
66    }
67  
68    /** A class that mimics a tiny subset of the JBoss VFS class. */
69    static class VFS {
70      static Class<?> VFS;
71      static Method getChild;
72  
73      private VFS() {
74        // Prevent Instantiation
75      }
76  
77      static VirtualFile getChild(URL url) throws IOException {
78        Object o = invoke(getChild, VFS, url);
79        return o == null ? null : new VirtualFile(o);
80      }
81    }
82  
83    /** Flag that indicates if this VFS is valid for the current environment. */
84    private static Boolean valid;
85  
86    /** Find all the classes and methods that are required to access the JBoss 6 VFS. */
87    protected static synchronized void initialize() {
88      if (valid == null) {
89        // Assume valid. It will get flipped later if something goes wrong.
90        valid = Boolean.TRUE;
91  
92        // Look up and verify required classes
93        VFS.VFS = checkNotNull(getClass("org.jboss.vfs.VFS"));
94        VirtualFile.VirtualFile = checkNotNull(getClass("org.jboss.vfs.VirtualFile"));
95  
96        // Look up and verify required methods
97        VFS.getChild = checkNotNull(getMethod(VFS.VFS, "getChild", URL.class));
98        VirtualFile.getChildrenRecursively = checkNotNull(getMethod(VirtualFile.VirtualFile,
99            "getChildrenRecursively"));
100       VirtualFile.getPathNameRelativeTo = checkNotNull(getMethod(VirtualFile.VirtualFile,
101           "getPathNameRelativeTo", VirtualFile.VirtualFile));
102 
103       // Verify that the API has not changed
104       checkReturnType(VFS.getChild, VirtualFile.VirtualFile);
105       checkReturnType(VirtualFile.getChildrenRecursively, List.class);
106       checkReturnType(VirtualFile.getPathNameRelativeTo, String.class);
107     }
108   }
109 
110   /**
111    * Verifies that the provided object reference is null. If it is null, then this VFS is marked
112    * as invalid for the current environment.
113    *
114    * @param <T>
115    *          the generic type
116    * @param object
117    *          The object reference to check for null.
118    * @return the t
119    */
120   protected static <T> T checkNotNull(T object) {
121     if (object == null) {
122       setInvalid();
123     }
124     return object;
125   }
126 
127   /**
128    * Verifies that the return type of method is what it is expected to be. If it is not, then
129    * this VFS is marked as invalid for the current environment.
130    *
131    * @param method The method whose return type is to be checked.
132    * @param expected A type to which the method's return type must be assignable.
133    * @see Class#isAssignableFrom(Class)
134    */
135   protected static void checkReturnType(Method method, Class<?> expected) {
136     if (method != null && !expected.isAssignableFrom(method.getReturnType())) {
137       log.error("Method " + method.getClass().getName() + "." + method.getName()
138           + "(..) should return " + expected.getName() + " but returns "
139           + method.getReturnType().getName() + " instead.");
140       setInvalid();
141     }
142   }
143 
144   /**
145    * Mark this {@link VFS} as invalid for the current environment.
146    */
147   protected static void setInvalid() {
148     if (JBoss6VFS.valid.booleanValue()) {
149       log.debug("JBoss 6 VFS API is not available in this environment.");
150       JBoss6VFS.valid = Boolean.FALSE;
151     }
152   }
153 
154   static {
155     initialize();
156   }
157 
158   @Override
159   public boolean isValid() {
160     return valid;
161   }
162 
163   @Override
164   public List<String> list(URL url, String path) throws IOException {
165     VirtualFile directory;
166     directory = VFS.getChild(url);
167     if (directory == null) {
168       return Collections.emptyList();
169     }
170 
171     if (!path.endsWith("/")) {
172       path += "/";
173     }
174 
175     List<VirtualFile> children = directory.getChildren();
176     List<String> names = new ArrayList<>(children.size());
177     for (VirtualFile vf : children) {
178       names.add(path + vf.getPathNameRelativeTo(directory));
179     }
180 
181     return names;
182   }
183 }