• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / mybatis-3 / #2968

pending completion
#2968

push

github

web-flow
Merge pull request #2792 from hazendaz/formatting

[ci] Formatting

84 of 84 new or added lines in 23 files covered. (100.0%)

9412 of 10781 relevant lines covered (87.3%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.77
/src/main/java/org/apache/ibatis/io/VFS.java
1
/*
2
 *    Copyright 2009-2023 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.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 {
1✔
36
  private static final Log log = LogFactory.getLog(VFS.class);
1✔
37

38
  /** The built-in implementations. */
39
  public static final Class<?>[] IMPLEMENTATIONS = { JBoss6VFS.class, DefaultVFS.class };
1✔
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<>();
1✔
45

46
  /** Singleton instance holder. */
47
  private static class VFSHolder {
48
    static final VFS INSTANCE = createVFS();
1✔
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<>();
1✔
54
      impls.addAll(USER_IMPLEMENTATIONS);
1✔
55
      impls.addAll(Arrays.asList((Class<? extends VFS>[]) IMPLEMENTATIONS));
1✔
56

57
      // Try each implementation class until a valid one is found
58
      VFS vfs = null;
1✔
59
      for (int i = 0; vfs == null || !vfs.isValid(); i++) {
1✔
60
        Class<? extends VFS> impl = impls.get(i);
1✔
61
        try {
62
          vfs = impl.getDeclaredConstructor().newInstance();
1✔
63
          if (!vfs.isValid() && log.isDebugEnabled()) {
1✔
64
            log.debug("VFS implementation " + impl.getName() + " is not valid in this environment.");
×
65
          }
66
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException
×
67
            | InvocationTargetException e) {
68
          log.error("Failed to instantiate " + impl, e);
×
69
          return null;
×
70
        }
1✔
71
      }
72

73
      if (log.isDebugEnabled()) {
1✔
74
        log.debug("Using VFS adapter " + vfs.getClass().getName());
×
75
      }
76

77
      return vfs;
1✔
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;
1✔
89
  }
90

91
  /**
92
   * Adds the specified class to the list of {@link VFS} implementations. Classes added in this manner are tried in the
93
   * order they are added and before any of the built-in implementations.
94
   *
95
   * @param clazz
96
   *          The {@link VFS} implementation class to add.
97
   */
98
  public static void addImplClass(Class<? extends VFS> clazz) {
99
    if (clazz != null) {
1✔
100
      USER_IMPLEMENTATIONS.add(clazz);
1✔
101
    }
102
  }
1✔
103

104
  /**
105
   * Get a class by name. If the class is not found then return null.
106
   *
107
   * @param className
108
   *          the class name
109
   *
110
   * @return the class
111
   */
112
  protected static Class<?> getClass(String className) {
113
    try {
114
      return Thread.currentThread().getContextClassLoader().loadClass(className);
×
115
      // return ReflectUtil.findClass(className);
116
    } catch (ClassNotFoundException e) {
1✔
117
      if (log.isDebugEnabled()) {
1✔
118
        log.debug("Class not found: " + className);
×
119
      }
120
      return null;
1✔
121
    }
122
  }
123

124
  /**
125
   * Get a method by name and parameter types. If the method is not found then return null.
126
   *
127
   * @param clazz
128
   *          The class to which the method belongs.
129
   * @param methodName
130
   *          The name of the method.
131
   * @param parameterTypes
132
   *          The types of the parameters accepted by the method.
133
   *
134
   * @return the method
135
   */
136
  protected static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
137
    if (clazz == null) {
1✔
138
      return null;
1✔
139
    }
140
    try {
141
      return clazz.getMethod(methodName, parameterTypes);
1✔
142
    } catch (SecurityException e) {
×
143
      log.error("Security exception looking for method " + clazz.getName() + "." + methodName + ".  Cause: " + e);
×
144
      return null;
×
145
    } catch (NoSuchMethodException e) {
1✔
146
      log.error("Method not found " + clazz.getName() + "." + methodName + "." + methodName + ".  Cause: " + e);
1✔
147
      return null;
1✔
148
    }
149
  }
150

151
  /**
152
   * Invoke a method on an object and return whatever it returns.
153
   *
154
   * @param <T>
155
   *          the generic type
156
   * @param method
157
   *          The method to invoke.
158
   * @param object
159
   *          The instance or class (for static methods) on which to invoke the method.
160
   * @param parameters
161
   *          The parameters to pass to the method.
162
   *
163
   * @return Whatever the method returns.
164
   *
165
   * @throws IOException
166
   *           If I/O errors occur
167
   * @throws RuntimeException
168
   *           If anything else goes wrong
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);
1✔
175
    } catch (IllegalArgumentException | IllegalAccessException e) {
1✔
176
      throw new RuntimeException(e);
1✔
177
    } catch (InvocationTargetException e) {
1✔
178
      if (e.getTargetException() instanceof IOException) {
1✔
179
        throw (IOException) e.getTargetException();
1✔
180
      } else {
181
        throw new RuntimeException(e);
1✔
182
      }
183
    }
184
  }
185

186
  /**
187
   * Get a list of {@link URL}s from the context classloader for all the resources found at the specified path.
188
   *
189
   * @param path
190
   *          The resource path.
191
   *
192
   * @return A list of {@link URL}s, as returned by {@link ClassLoader#getResources(String)}.
193
   *
194
   * @throws IOException
195
   *           If I/O errors occur
196
   */
197
  protected static List<URL> getResources(String path) throws IOException {
198
    return Collections.list(Thread.currentThread().getContextClassLoader().getResources(path));
1✔
199
  }
200

201
  /**
202
   * Return true if the {@link VFS} implementation is valid for the current environment.
203
   *
204
   * @return true, if is valid
205
   */
206
  public abstract boolean isValid();
207

208
  /**
209
   * Recursively list the full resource path of all the resources that are children of the resource identified by a URL.
210
   *
211
   * @param url
212
   *          The URL that identifies the resource to list.
213
   * @param forPath
214
   *          The path to the resource that is identified by the URL. Generally, this is the value passed to
215
   *          {@link #getResources(String)} to get the resource URL.
216
   *
217
   * @return A list containing the names of the child resources.
218
   *
219
   * @throws IOException
220
   *           If I/O errors occur
221
   */
222
  protected abstract List<String> list(URL url, String forPath) throws IOException;
223

224
  /**
225
   * Recursively list the full resource path of all the resources that are children of all the resources found at the
226
   * specified path.
227
   *
228
   * @param path
229
   *          The path of the resource(s) to list.
230
   *
231
   * @return A list containing the names of the child resources.
232
   *
233
   * @throws IOException
234
   *           If I/O errors occur
235
   */
236
  public List<String> list(String path) throws IOException {
237
    List<String> names = new ArrayList<>();
1✔
238
    for (URL url : getResources(path)) {
1✔
239
      names.addAll(list(url, path));
1✔
240
    }
1✔
241
    return names;
1✔
242
  }
243
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc