Skip to content

Commit

Permalink
Merge pull request #4971 from aloubyansky/4959
Browse files Browse the repository at this point in the history
Platform Descriptor: load resources from a classloader instead of FileSystem
  • Loading branch information
gsmet authored Oct 29, 2019
2 parents 696cc58 + f37ed16 commit 1819ca4
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
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;
Expand All @@ -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() {
Expand All @@ -51,8 +47,8 @@ void setManagedDependencies(List<Dependency> managedDeps) {
this.managedDeps = managedDeps;
}

void setTemplatesJar(Path templatesJar) {
this.templatesJar = templatesJar;
void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}

void setMessageWriter(MessageWriter log) {
Expand Down Expand Up @@ -100,41 +96,26 @@ public List<Extension> getExtensions() {
@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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;

Expand Down Expand Up @@ -75,11 +76,22 @@ public QuarkusJsonPlatformDescriptor load(final QuarkusJsonPlatformDescriptorLoa
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());

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 1819ca4

Please sign in to comment.