Skip to content

Instantly share code, notes, and snippets.

@hanswesterbeek
Last active February 9, 2018 04:01
Show Gist options
  • Save hanswesterbeek/94c359b22c6fed6dff1c to your computer and use it in GitHub Desktop.
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
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());
}
}
@hanswesterbeek
Copy link
Author

Yes, fine with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment