1 /*
2 * Copyright 2009-2021 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.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 * Provides a very simple API for accessing resources within an application server.
32 *
33 * @author Ben Gunter
34 */
35 public abstract class VFS {
36 private static final Log log = LogFactory.getLog(VFS.class);
37
38 /** The built-in implementations. */
39 public static final Class<?>[] IMPLEMENTATIONS = { JBoss6VFS.class, DefaultVFS.class };
40
41 /**
42 * The list to which implementations are added by {@link #addImplClass(Class)}.
43 */
44 public static final List<Class<? extends VFS>> USER_IMPLEMENTATIONS = new ArrayList<>();
45
46 /** Singleton instance holder. */
47 private static class VFSHolder {
48 static final VFS INSTANCE = createVFS();
49
50 @SuppressWarnings("unchecked")
51 static VFS createVFS() {
52 // Try the user implementations first, then the built-ins
53 List<Class<? extends VFS>> impls = new ArrayList<>();
54 impls.addAll(USER_IMPLEMENTATIONS);
55 impls.addAll(Arrays.asList((Class<? extends VFS>[]) IMPLEMENTATIONS));
56
57 // Try each implementation class until a valid one is found
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 * Get the singleton {@link VFS} instance. If no {@link VFS} implementation can be found for the current environment,
83 * then this method returns null.
84 *
85 * @return single instance of VFS
86 */
87 public static VFS getInstance() {
88 return VFSHolder.INSTANCE;
89 }
90
91 /**
92 * Adds the specified class to the list of {@link VFS} implementations. Classes added in this
93 * manner are tried in the order they are added and before any of the built-in implementations.
94 *
95 * @param clazz The {@link VFS} implementation class to add.
96 */
97 public static void addImplClass(Class<? extends VFS> clazz) {
98 if (clazz != null) {
99 USER_IMPLEMENTATIONS.add(clazz);
100 }
101 }
102
103 /**
104 * Get a class by name. If the class is not found then return null.
105 *
106 * @param className
107 * the class name
108 * @return the class
109 */
110 protected static Class<?> getClass(String className) {
111 try {
112 return Thread.currentThread().getContextClassLoader().loadClass(className);
113 // return ReflectUtil.findClass(className);
114 } catch (ClassNotFoundException e) {
115 if (log.isDebugEnabled()) {
116 log.debug("Class not found: " + className);
117 }
118 return null;
119 }
120 }
121
122 /**
123 * Get a method by name and parameter types. If the method is not found then return null.
124 *
125 * @param clazz
126 * The class to which the method belongs.
127 * @param methodName
128 * The name of the method.
129 * @param parameterTypes
130 * The types of the parameters accepted by the method.
131 * @return the method
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 * Invoke a method on an object and return whatever it returns.
150 *
151 * @param <T>
152 * the generic type
153 * @param method
154 * The method to invoke.
155 * @param object
156 * The instance or class (for static methods) on which to invoke the method.
157 * @param parameters
158 * The parameters to pass to the method.
159 * @return Whatever the method returns.
160 * @throws IOException
161 * If I/O errors occur
162 * @throws RuntimeException
163 * If anything else goes wrong
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 * Get a list of {@link URL}s from the context classloader for all the resources found at the
183 * specified path.
184 *
185 * @param path The resource path.
186 * @return A list of {@link URL}s, as returned by {@link ClassLoader#getResources(String)}.
187 * @throws IOException If I/O errors occur
188 */
189 protected static List<URL> getResources(String path) throws IOException {
190 return Collections.list(Thread.currentThread().getContextClassLoader().getResources(path));
191 }
192
193 /**
194 * Return true if the {@link VFS} implementation is valid for the current environment.
195 *
196 * @return true, if is valid
197 */
198 public abstract boolean isValid();
199
200 /**
201 * Recursively list the full resource path of all the resources that are children of the
202 * resource identified by a URL.
203 *
204 * @param url The URL that identifies the resource to list.
205 * @param forPath The path to the resource that is identified by the URL. Generally, this is the
206 * value passed to {@link #getResources(String)} to get the resource URL.
207 * @return A list containing the names of the child resources.
208 * @throws IOException If I/O errors occur
209 */
210 protected abstract List<String> list(URL url, String forPath) throws IOException;
211
212 /**
213 * Recursively list the full resource path of all the resources that are children of all the
214 * resources found at the specified path.
215 *
216 * @param path The path of the resource(s) to list.
217 * @return A list containing the names of the child resources.
218 * @throws IOException If I/O errors occur
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 }