SpringBootVFS.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2011-2021, 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 com.baomidou.mybatisplus.core.toolkit.StringPool;
  18. import org.apache.ibatis.io.VFS;
  19. import org.springframework.core.io.Resource;
  20. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  21. import org.springframework.core.io.support.ResourcePatternResolver;
  22. import java.io.IOException;
  23. import java.io.UncheckedIOException;
  24. import java.net.URL;
  25. import java.util.List;
  26. import java.util.stream.Collectors;
  27. import java.util.stream.Stream;
  28. /**
  29. * @author Hans Westerbeek
  30. * @author Eddú Meléndez
  31. * @author Kazuki Shimizu
  32. */
  33. public class SpringBootVFS extends VFS {
  34. private final ResourcePatternResolver resourceResolver;
  35. public SpringBootVFS() {
  36. this.resourceResolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader());
  37. }
  38. @Override
  39. public boolean isValid() {
  40. return true;
  41. }
  42. private static String preserveSubpackageName(final String baseUrlString, final Resource resource,
  43. final String rootPath) {
  44. try {
  45. return rootPath + (rootPath.endsWith(StringPool.SLASH) ? StringPool.EMPTY : StringPool.SLASH)
  46. + resource.getURL().toString().substring(baseUrlString.length());
  47. } catch (IOException e) {
  48. throw new UncheckedIOException(e);
  49. }
  50. }
  51. @Override
  52. protected List<String> list(URL url, String path) throws IOException {
  53. String urlString = url.toString();
  54. String baseUrlString = urlString.endsWith(StringPool.SLASH) ? urlString : urlString.concat(StringPool.SLASH);
  55. Resource[] resources = resourceResolver.getResources(baseUrlString + "**/*.class");
  56. return Stream.of(resources).map(resource -> preserveSubpackageName(baseUrlString, resource, path))
  57. .collect(Collectors.toList());
  58. }
  59. }