SpringBootVFS.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2011-2023, baomidou (jobob@qq.com).
  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 com.baomidou.mybatisplus.autoconfigure;
  17. import org.apache.ibatis.io.VFS;
  18. import org.springframework.core.io.Resource;
  19. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  20. import org.springframework.core.io.support.ResourcePatternResolver;
  21. import org.springframework.util.ClassUtils;
  22. import java.io.IOException;
  23. import java.io.UncheckedIOException;
  24. import java.net.URL;
  25. import java.net.URLDecoder;
  26. import java.nio.charset.Charset;
  27. import java.text.Normalizer;
  28. import java.util.List;
  29. import java.util.function.Supplier;
  30. import java.util.stream.Collectors;
  31. import java.util.stream.Stream;
  32. /**
  33. * @author Hans Westerbeek
  34. * @author Eddú Meléndez
  35. * @author Kazuki Shimizu
  36. */
  37. public class SpringBootVFS extends VFS {
  38. private static Charset urlDecodingCharset;
  39. private static Supplier<ClassLoader> classLoaderSupplier;
  40. private final ResourcePatternResolver resourceResolver;
  41. static {
  42. setUrlDecodingCharset(Charset.defaultCharset());
  43. setClassLoaderSupplier(ClassUtils::getDefaultClassLoader);
  44. }
  45. public SpringBootVFS() {
  46. this.resourceResolver = new PathMatchingResourcePatternResolver(classLoaderSupplier.get());
  47. }
  48. @Override
  49. public boolean isValid() {
  50. return true;
  51. }
  52. @Override
  53. protected List<String> list(URL url, String path) throws IOException {
  54. String urlString = URLDecoder.decode(url.toString(), urlDecodingCharset.name());
  55. String baseUrlString = urlString.endsWith("/") ? urlString : urlString.concat("/");
  56. Resource[] resources = resourceResolver.getResources(baseUrlString + "**/*.class");
  57. return Stream.of(resources).map(resource -> preserveSubpackageName(baseUrlString, resource, path))
  58. .collect(Collectors.toList());
  59. }
  60. /**
  61. * Set the charset for decoding an encoded URL string.
  62. * <p>
  63. * Default is system default charset.
  64. * </p>
  65. *
  66. * @param charset the charset for decoding an encoded URL string
  67. * @since 2.3.0
  68. */
  69. public static void setUrlDecodingCharset(Charset charset) {
  70. urlDecodingCharset = charset;
  71. }
  72. /**
  73. * Set the supplier for providing {@link ClassLoader} to used.
  74. * <p>
  75. * Default is a returned instance from {@link ClassUtils#getDefaultClassLoader()}.
  76. * </p>
  77. *
  78. * @param supplier the supplier for providing {@link ClassLoader} to used
  79. * @since 3.0.2
  80. */
  81. public static void setClassLoaderSupplier(Supplier<ClassLoader> supplier) {
  82. classLoaderSupplier = supplier;
  83. }
  84. private static String preserveSubpackageName(final String baseUrlString, final Resource resource,
  85. final String rootPath) {
  86. try {
  87. return rootPath + (rootPath.endsWith("/") ? "" : "/") + Normalizer
  88. .normalize(URLDecoder.decode(resource.getURL().toString(), urlDecodingCharset.name()), Normalizer.Form.NFC)
  89. .substring(baseUrlString.length());
  90. } catch (IOException e) {
  91. throw new UncheckedIOException(e);
  92. }
  93. }
  94. }