diff --git a/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/ClassPathResourceLoader.java b/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/ClassPathResourceLoader.java new file mode 100644 index 000000000..f78aed6e4 --- /dev/null +++ b/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/ClassPathResourceLoader.java @@ -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; + } +} diff --git a/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/DirectoryResourceLoader.java b/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/DirectoryResourceLoader.java new file mode 100644 index 000000000..d54e2fa87 --- /dev/null +++ b/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/DirectoryResourceLoader.java @@ -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); + } +} diff --git a/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/QuarkusJsonPlatformDescriptor.java b/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/QuarkusJsonPlatformDescriptor.java index 15e98e904..a3c6689bb 100644 --- a/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/QuarkusJsonPlatformDescriptor.java +++ b/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/QuarkusJsonPlatformDescriptor.java @@ -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 extensions = Collections.emptyList(); private List managedDeps = Collections.emptyList(); private List 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 loadResource(String name, ResourceInputStreamConsumer 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); } } diff --git a/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/QuarkusJsonPlatformDescriptorLoaderImpl.java b/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/QuarkusJsonPlatformDescriptorLoaderImpl.java index 93e79d6e5..1f61dbf25 100644 --- a/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/QuarkusJsonPlatformDescriptorLoaderImpl.java +++ b/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/QuarkusJsonPlatformDescriptorLoaderImpl.java @@ -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()); diff --git a/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/ResourceLoader.java b/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/ResourceLoader.java new file mode 100644 index 000000000..49d68cc76 --- /dev/null +++ b/devtools/platform-descriptor-json/src/main/java/io/quarkus/platform/descriptor/loader/json/impl/ResourceLoader.java @@ -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; +}