Merge pull request #4971 from aloubyansky/4959

Platform Descriptor: load resources from a classloader instead of FileSystem
This commit is contained in:
Guillaume Smet
2019-10-29 15:44:44 +01:00
committed by GitHub
5 changed files with 88 additions and 34 deletions

View File

@@ -0,0 +1,22 @@
package io.quarkus.platform.descriptor.loader.json.impl;
import java.io.IOException;
import java.io.InputStream;
public class ClassPathResourceLoader implements ResourceLoader {
private final ClassLoader cl;
public ClassPathResourceLoader(ClassLoader cl) {
this.cl = cl;
}
@Override
public InputStream getResourceAsStream(String name) throws IOException {
final InputStream stream = cl.getResourceAsStream(name);
if (stream == null) {
throw new IOException("Failed to locate " + name + " on the classpath");
}
return stream;
}
}

View File

@@ -0,0 +1,30 @@
package io.quarkus.platform.descriptor.loader.json.impl;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
public class DirectoryResourceLoader implements ResourceLoader {
private final Path dir;
public DirectoryResourceLoader(Path dir) {
if (!Files.isDirectory(dir)) {
throw new IllegalStateException("Failed to locate directory " + dir);
}
this.dir = dir;
}
@Override
public InputStream getResourceAsStream(String name) throws IOException {
Path resource = dir.resolve(name);
if (!Files.exists(resource)) {
throw new IOException("Failed to locate " + resource);
}
if (Files.isDirectory(resource)) {
throw new IOException("Can't open a stream for path pointing to directory " + resource);
}
return Files.newInputStream(resource);
}
}

View File

@@ -3,17 +3,13 @@ package io.quarkus.platform.descriptor.loader.json.impl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.maven.model.Dependency;
import io.quarkus.bootstrap.util.ZipUtils;
import io.quarkus.dependencies.Category;
import io.quarkus.dependencies.Extension;
import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;
@@ -31,7 +27,7 @@ public class QuarkusJsonPlatformDescriptor implements QuarkusPlatformDescriptor
private List<Extension> extensions = Collections.emptyList();
private List<Dependency> managedDeps = Collections.emptyList();
private List<Category> categories = Collections.emptyList();
private Path templatesJar;
private ResourceLoader resourceLoader;
private MessageWriter log;
public QuarkusJsonPlatformDescriptor() {
@@ -51,8 +47,8 @@ public class QuarkusJsonPlatformDescriptor implements QuarkusPlatformDescriptor
this.managedDeps = managedDeps;
}
void setTemplatesJar(Path templatesJar) {
this.templatesJar = templatesJar;
void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
void setMessageWriter(MessageWriter log) {
@@ -100,41 +96,26 @@ public class QuarkusJsonPlatformDescriptor implements QuarkusPlatformDescriptor
@Override
public String getTemplate(String name) {
getLog().debug("Loading Quarkus project template %s", name);
if (templatesJar == null) {
return null;
if (resourceLoader == null) {
throw new IllegalStateException("Resource loader has not been provided");
}
try {
if (Files.isDirectory(templatesJar)) {
return readTemplate(name, templatesJar.resolve(name));
}
try (FileSystem fs = ZipUtils.newFileSystem(templatesJar)) {
return readTemplate(name, fs.getPath(name));
try (InputStream is = resourceLoader.getResourceAsStream(name)) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
return reader.lines().collect(Collectors.joining("\n"));
}
} catch (IOException e) {
throw new RuntimeException("Failed to resolve template " + name, e);
}
}
private String readTemplate(String name, final Path p) throws IOException {
if (!Files.exists(p)) {
throw new RuntimeException("Failed to locate template " + name + " in " + templatesJar);
}
try (BufferedReader reader = Files.newBufferedReader(p)) {
return reader.lines().collect(Collectors.joining("\n"));
}
}
@Override
public <T> T loadResource(String name, ResourceInputStreamConsumer<T> consumer) throws IOException {
getLog().debug("Loading Quarkus platform resource %s", name);
try (FileSystem fs = FileSystems.newFileSystem(templatesJar, null)) {
final Path p = fs.getPath(name);
if (!Files.exists(p)) {
throw new IOException("Failed to locate resource " + name);
}
try (InputStream is = Files.newInputStream(p)) {
return consumer.handle(is);
}
if (resourceLoader == null) {
throw new IllegalStateException("Resource loader has not been provided");
}
try (InputStream is = resourceLoader.getResourceAsStream(name)) {
return consumer.handle(is);
}
}

View File

@@ -3,6 +3,7 @@ package io.quarkus.platform.descriptor.loader.json.impl;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
@@ -75,11 +76,22 @@ public class QuarkusJsonPlatformDescriptorLoaderImpl
throw new RuntimeException("Failed to determine the Quarkus version for the platform " + platformBom);
}
final Path classOrigin;
try {
platform.setTemplatesJar(MojoUtils.getClassOrigin(getClass()));
classOrigin = MojoUtils.getClassOrigin(getClass());
} catch (Exception e) {
throw new IllegalStateException("Failed to determine the origin of " + getClass().getName(), e);
}
final ResourceLoader resourceLoader;
if (Files.isDirectory(classOrigin)) {
resourceLoader = new DirectoryResourceLoader(classOrigin);
} else {
// this means the class belongs to a JAR which is on the classpath
resourceLoader = new ClassPathResourceLoader(getClass().getClassLoader());
}
platform.setResourceLoader(resourceLoader);
platform.setQuarkusVersion(quarkusVersion);
platform.setMessageWriter(context.getMessageWriter());

View File

@@ -0,0 +1,9 @@
package io.quarkus.platform.descriptor.loader.json.impl;
import java.io.IOException;
import java.io.InputStream;
public interface ResourceLoader {
InputStream getResourceAsStream(String name) throws IOException;
}