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

mybatis / mybatis-3 / 2826

27 May 2025 02:19AM UTC coverage: 87.299% (-0.009%) from 87.308%
2826

push

github

web-flow
Merge pull request #3475 from hazendaz/nio-work

Modernize move to NIO

3839 of 4659 branches covered (82.4%)

6 of 13 new or added lines in 3 files covered. (46.15%)

2 existing lines in 2 files now uncovered.

9925 of 11369 relevant lines covered (87.3%)

0.87 hits per line

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

39.71
/src/main/java/org/apache/ibatis/io/DefaultVFS.java
1
/*
2
 *    Copyright 2009-2025 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.BufferedReader;
19
import java.io.File;
20
import java.io.FileNotFoundException;
21
import java.io.IOException;
22
import java.io.InputStream;
23
import java.io.InputStreamReader;
24
import java.net.MalformedURLException;
25
import java.net.URL;
26
import java.net.URLEncoder;
27
import java.nio.charset.StandardCharsets;
28
import java.nio.file.FileSystemException;
29
import java.nio.file.Files;
30
import java.nio.file.InvalidPathException;
31
import java.nio.file.Path;
32
import java.util.ArrayList;
33
import java.util.Arrays;
34
import java.util.List;
35
import java.util.jar.JarEntry;
36
import java.util.jar.JarInputStream;
37

38
import org.apache.ibatis.logging.Log;
39
import org.apache.ibatis.logging.LogFactory;
40

41
/**
42
 * A default implementation of {@link VFS} that works for most application servers.
43
 *
44
 * @author Ben Gunter
45
 */
46
public class DefaultVFS extends VFS {
1✔
47
  private static final Log log = LogFactory.getLog(DefaultVFS.class);
1✔
48

49
  /** The magic header that indicates a JAR (ZIP) file. */
50
  private static final byte[] JAR_MAGIC = { 'P', 'K', 3, 4 };
1✔
51

52
  @Override
53
  public boolean isValid() {
54
    return true;
1✔
55
  }
56

57
  @Override
58
  public List<String> list(URL url, String path) throws IOException {
59
    InputStream is = null;
1✔
60
    try {
61
      List<String> resources = new ArrayList<>();
1✔
62

63
      // First, try to find the URL of a JAR file containing the requested resource. If a JAR
64
      // file is found, then we'll list child resources by reading the JAR.
65
      URL jarUrl = findJarForResource(url);
1✔
66
      if (jarUrl != null) {
1!
67
        is = jarUrl.openStream();
×
68
        if (log.isDebugEnabled()) {
×
69
          log.debug("Listing " + url);
×
70
        }
71
        resources = listResources(new JarInputStream(is), path);
×
72
      } else {
73
        List<String> children = new ArrayList<>();
1✔
74
        try {
75
          if (isJar(url)) {
1!
76
            // Some versions of JBoss VFS might give a JAR stream even if the resource
77
            // referenced by the URL isn't actually a JAR
78
            is = url.openStream();
×
79
            try (JarInputStream jarInput = new JarInputStream(is)) {
×
80
              if (log.isDebugEnabled()) {
×
81
                log.debug("Listing " + url);
×
82
              }
NEW
83
              Path destinationDir = Path.of(path);
×
84
              for (JarEntry entry; (entry = jarInput.getNextJarEntry()) != null;) {
×
85
                if (log.isDebugEnabled()) {
×
86
                  log.debug("Jar entry: " + entry.getName());
×
87
                }
NEW
88
                File entryFile = destinationDir.resolve(entry.getName()).toFile().getCanonicalFile();
×
NEW
89
                if (!entryFile.getPath().startsWith(destinationDir.toFile().getCanonicalPath())) {
×
UNCOV
90
                  throw new IOException("Bad zip entry: " + entry.getName());
×
91
                }
92
                children.add(entry.getName());
×
93
              }
×
94
            }
95
          } else {
96
            /*
97
             * Some servlet containers allow reading from directory resources like a text file, listing the child
98
             * resources one per line. However, there is no way to differentiate between directory and file resources
99
             * just by reading them. To work around that, as each line is read, try to look it up via the class loader
100
             * as a child of the current resource. If any line fails then we assume the current resource is not a
101
             * directory.
102
             */
103
            is = url.openStream();
1✔
104
            List<String> lines = new ArrayList<>();
1✔
105
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
1✔
106
              for (String line; (line = reader.readLine()) != null;) {
1✔
107
                if (log.isDebugEnabled()) {
1!
108
                  log.debug("Reader entry: " + line);
×
109
                }
110
                lines.add(line);
1✔
111
                if (getResources(path + "/" + line).isEmpty()) {
1✔
112
                  lines.clear();
1✔
113
                  break;
1✔
114
                }
115
              }
116
            } catch (InvalidPathException | FileSystemException e) {
×
117
              // #1974 #2598
118
              lines.clear();
×
119
            }
1✔
120
            if (!lines.isEmpty()) {
1✔
121
              if (log.isDebugEnabled()) {
1!
122
                log.debug("Listing " + url);
×
123
              }
124
              children.addAll(lines);
1✔
125
            }
126
          }
127
        } catch (FileNotFoundException e) {
×
128
          /*
129
           * For file URLs the openStream() call might fail, depending on the servlet container, because directories
130
           * can't be opened for reading. If that happens, then list the directory directly instead.
131
           */
132
          if (!"file".equals(url.getProtocol())) {
×
133
            // No idea where the exception came from so rethrow it
134
            throw e;
×
135
          }
NEW
136
          File file = Path.of(url.getFile()).toFile();
×
137
          if (log.isDebugEnabled()) {
×
138
            log.debug("Listing directory " + file.getAbsolutePath());
×
139
          }
NEW
140
          if (Files.isDirectory(file.toPath())) {
×
141
            if (log.isDebugEnabled()) {
×
142
              log.debug("Listing " + url);
×
143
            }
144
            children = Arrays.asList(file.list());
×
145
          }
146
        }
1✔
147

148
        // The URL prefix to use when recursively listing child resources
149
        String prefix = url.toExternalForm();
1✔
150
        if (!prefix.endsWith("/")) {
1!
151
          prefix = prefix + "/";
1✔
152
        }
153

154
        // Iterate over immediate children, adding files and recurring into directories
155
        for (String child : children) {
1✔
156
          String resourcePath = path + "/" + child;
1✔
157
          resources.add(resourcePath);
1✔
158
          URL childUrl = new URL(prefix + child);
1✔
159
          resources.addAll(list(childUrl, resourcePath));
1✔
160
        }
1✔
161
      }
162

163
      return resources;
1✔
164
    } finally {
165
      if (is != null) {
1!
166
        try {
167
          is.close();
1✔
168
        } catch (Exception e) {
×
169
          // Ignore
170
        }
1✔
171
      }
172
    }
173
  }
174

175
  /**
176
   * List the names of the entries in the given {@link JarInputStream} that begin with the specified {@code path}.
177
   * Entries will match with or without a leading slash.
178
   *
179
   * @param jar
180
   *          The JAR input stream
181
   * @param path
182
   *          The leading path to match
183
   *
184
   * @return The names of all the matching entries
185
   *
186
   * @throws IOException
187
   *           If I/O errors occur
188
   */
189
  protected List<String> listResources(JarInputStream jar, String path) throws IOException {
190
    // Include the leading and trailing slash when matching names
191
    if (!path.startsWith("/")) {
×
192
      path = "/" + path;
×
193
    }
194
    if (!path.endsWith("/")) {
×
195
      path = path + "/";
×
196
    }
197

198
    // Iterate over the entries and collect those that begin with the requested path
199
    List<String> resources = new ArrayList<>();
×
200
    for (JarEntry entry; (entry = jar.getNextJarEntry()) != null;) {
×
201
      if (!entry.isDirectory()) {
×
202
        // Add leading slash if it's missing
203
        StringBuilder name = new StringBuilder(entry.getName());
×
204
        if (name.charAt(0) != '/') {
×
205
          name.insert(0, '/');
×
206
        }
207

208
        // Check file name
209
        if (name.indexOf(path) == 0) {
×
210
          if (log.isDebugEnabled()) {
×
211
            log.debug("Found resource: " + name);
×
212
          }
213
          // Trim leading slash
214
          resources.add(name.substring(1));
×
215
        }
216
      }
×
217
    }
218
    return resources;
×
219
  }
220

221
  /**
222
   * Attempts to deconstruct the given URL to find a JAR file containing the resource referenced by the URL. That is,
223
   * assuming the URL references a JAR entry, this method will return a URL that references the JAR file containing the
224
   * entry. If the JAR cannot be located, then this method returns null.
225
   *
226
   * @param url
227
   *          The URL of the JAR entry.
228
   *
229
   * @return The URL of the JAR file, if one is found. Null if not.
230
   *
231
   * @throws MalformedURLException
232
   *           the malformed URL exception
233
   */
234
  protected URL findJarForResource(URL url) throws MalformedURLException {
235
    if (log.isDebugEnabled()) {
1!
236
      log.debug("Find JAR URL: " + url);
×
237
    }
238

239
    // If the file part of the URL is itself a URL, then that URL probably points to the JAR
240
    boolean continueLoop = true;
1✔
241
    while (continueLoop) {
1✔
242
      try {
243
        url = new URL(url.getFile());
×
244
        if (log.isDebugEnabled()) {
×
245
          log.debug("Inner URL: " + url);
×
246
        }
247
      } catch (MalformedURLException e) {
1✔
248
        // This will happen at some point and serves as a break in the loop
249
        continueLoop = false;
1✔
250
      }
1✔
251
    }
252

253
    // Look for the .jar extension and chop off everything after that
254
    StringBuilder jarUrl = new StringBuilder(url.toExternalForm());
1✔
255
    int index = jarUrl.lastIndexOf(".jar");
1✔
256
    if (index < 0) {
1!
257
      if (log.isDebugEnabled()) {
1!
258
        log.debug("Not a JAR: " + jarUrl);
×
259
      }
260
      return null;
1✔
261
    }
262
    jarUrl.setLength(index + 4);
×
263
    if (log.isDebugEnabled()) {
×
264
      log.debug("Extracted JAR URL: " + jarUrl);
×
265
    }
266

267
    // Try to open and test it
268
    try {
269
      URL testUrl = new URL(jarUrl.toString());
×
270
      if (isJar(testUrl)) {
×
271
        return testUrl;
×
272
      }
273
      // WebLogic fix: check if the URL's file exists in the filesystem.
274
      if (log.isDebugEnabled()) {
×
275
        log.debug("Not a JAR: " + jarUrl);
×
276
      }
277
      jarUrl.replace(0, jarUrl.length(), testUrl.getFile());
×
NEW
278
      File file = Path.of(jarUrl.toString()).toFile();
×
279

280
      // File name might be URL-encoded
281
      if (!file.exists()) {
×
NEW
282
        file = Path.of(URLEncoder.encode(jarUrl.toString(), StandardCharsets.UTF_8)).toFile();
×
283
      }
284

285
      if (file.exists()) {
×
286
        if (log.isDebugEnabled()) {
×
287
          log.debug("Trying real file: " + file.getAbsolutePath());
×
288
        }
289
        testUrl = file.toURI().toURL();
×
290
        if (isJar(testUrl)) {
×
291
          return testUrl;
×
292
        }
293
      }
294
    } catch (MalformedURLException e) {
×
295
      log.warn("Invalid JAR URL: " + jarUrl);
×
296
    }
×
297

298
    if (log.isDebugEnabled()) {
×
299
      log.debug("Not a JAR: " + jarUrl);
×
300
    }
301
    return null;
×
302
  }
303

304
  /**
305
   * Converts a Java package name to a path that can be looked up with a call to
306
   * {@link ClassLoader#getResources(String)}.
307
   *
308
   * @param packageName
309
   *          The Java package name to convert to a path
310
   *
311
   * @return the package path
312
   */
313
  protected String getPackagePath(String packageName) {
314
    return packageName == null ? null : packageName.replace('.', '/');
×
315
  }
316

317
  /**
318
   * Returns true if the resource located at the given URL is a JAR file.
319
   *
320
   * @param url
321
   *          The URL of the resource to test.
322
   *
323
   * @return true, if is jar
324
   */
325
  protected boolean isJar(URL url) {
326
    return isJar(url, new byte[JAR_MAGIC.length]);
1✔
327
  }
328

329
  /**
330
   * Returns true if the resource located at the given URL is a JAR file.
331
   *
332
   * @param url
333
   *          The URL of the resource to test.
334
   * @param buffer
335
   *          A buffer into which the first few bytes of the resource are read. The buffer must be at least the size of
336
   *          {@link #JAR_MAGIC}. (The same buffer may be reused for multiple calls as an optimization.)
337
   *
338
   * @return true, if is jar
339
   */
340
  protected boolean isJar(URL url, byte[] buffer) {
341
    try (InputStream is = url.openStream()) {
1✔
342
      is.read(buffer, 0, JAR_MAGIC.length);
1✔
343
      if (Arrays.equals(buffer, JAR_MAGIC)) {
1!
344
        if (log.isDebugEnabled()) {
×
345
          log.debug("Found JAR: " + url);
×
346
        }
347
        return true;
×
348
      }
349
    } catch (Exception e) {
×
350
      // Failure to read the stream means this is not a JAR
351
    }
1✔
352

353
    return false;
1✔
354
  }
355
}
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

© 2026 Coveralls, Inc