Last active
February 9, 2018 04:01
-
-
Save hanswesterbeek/94c359b22c6fed6dff1c to your computer and use it in GitHub Desktop.
Version of the VFS that also works when you run your Spring Boot as java -jar yourapp.jar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package foo.stripesutls; | |
import static java.util.stream.Collectors.toList; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.net.URL; | |
import java.util.Arrays; | |
import java.util.List; | |
import org.springframework.core.io.Resource; | |
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; | |
import org.springframework.core.io.support.ResourcePatternResolver; | |
import com.google.common.annotations.VisibleForTesting; | |
import net.sourceforge.stripes.vfs.VFS; | |
public class SpringBootExecutableJarVFS extends VFS { | |
@Override | |
public boolean isValid() { | |
return true; | |
} | |
@Override | |
public List<String> list(final URL url, final String path) throws IOException { | |
ClassLoader cl = this.getClass().getClassLoader(); | |
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl); | |
Resource[] resources = resolver.getResources(path + "/**/*.class"); | |
final List<String> resourcePaths = Arrays.asList(resources).stream() | |
.map(resource -> { | |
try { | |
return preserveSubpackageName(resource.getURI(), path); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
}) | |
.collect(toList()); | |
return resourcePaths; | |
} | |
@VisibleForTesting | |
protected static String preserveSubpackageName(final URI uri, final String rootPath) { | |
final String uriStr = uri.toString(); | |
// we must return the uri with everything before the rootpath stripped off | |
final int start = uriStr.indexOf(rootPath); | |
return uriStr.substring(start, uriStr.length()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for your work!
May I assume this is licensed under Apache License Version 2.0?
We consider porting it to mybatis-spring-boot](https://github.com/mybatis/mybatis-spring-boot) project.
Regards,
Iwao