FEAT YAML ResourceManager Loader
parent
9d55ce7392
commit
6b4c1b02da
11
pom.xml
11
pom.xml
|
@ -13,12 +13,23 @@
|
|||
<artifactId>resource-manager</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<snakeyml.version>1.27</snakeyml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>de.nclazz</groupId>
|
||||
<artifactId>commons</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyml.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package de.nclazz.resources;
|
|||
import de.nclazz.resources.protocol.ClasspathResourceHandler;
|
||||
import de.nclazz.resources.protocol.CustomStreamHandlerFactory;
|
||||
import lombok.NonNull;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
@ -42,11 +43,25 @@ public class Resource {
|
|||
}
|
||||
}
|
||||
|
||||
public static Resource fromFile(@NonNull File file) { return new Resource(checked(() -> new FileInputStream(file))); }
|
||||
public static Resource fromFile(@NonNull File file) {
|
||||
return new Resource(checked(() -> new FileInputStream(file)));
|
||||
}
|
||||
|
||||
public static Resource fromStream(@NonNull InputStream stream) {
|
||||
return new Resource(() -> stream);
|
||||
}
|
||||
public static Resource fromClasspath(String resourceName) { return fromStream(Resource.class.getResourceAsStream(resourceName)); }
|
||||
public static Resource fromURL(@NonNull URL url) { return new Resource(checked(url::openStream)); }
|
||||
|
||||
public static Resource fromClasspath(String resourceName) {
|
||||
return fromStream(Resource.class.getResourceAsStream(resourceName));
|
||||
}
|
||||
|
||||
public static Resource fromURL(@NonNull URL url) {
|
||||
return new Resource(checked(url::openStream));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static Resource load(@NonNull String url) {
|
||||
return fromURL(new URL(url));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,12 +8,10 @@ public class ResourceManager {
|
|||
|
||||
private Map<String, ResourceMapping> mappings = new HashMap<>();
|
||||
|
||||
|
||||
public Optional<ResourceMapping> getMapping(String key) {
|
||||
return Optional.ofNullable(this.mappings.get(key));
|
||||
}
|
||||
|
||||
|
||||
public ResourceManager addMapping(String key, ResourceMapping mapping) {
|
||||
this.mappings.put(key, mapping);
|
||||
return this;
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package de.nclazz.resources.loader;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
@Data
|
||||
public class YamlResource {
|
||||
|
||||
private String key;
|
||||
private String label;
|
||||
private String description;
|
||||
private String defaultLocation;
|
||||
private Set<String> types;
|
||||
private Set<String> options = Collections.emptySet();
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package de.nclazz.resources.loader;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class YamlResourceManager {
|
||||
|
||||
private List<YamlResource> resources;
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package de.nclazz.resources.loader;
|
||||
|
||||
import de.nclazz.resources.Resource;
|
||||
import de.nclazz.resources.ResourceManager;
|
||||
import de.nclazz.resources.ResourceMapping;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.constructor.Constructor;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.util.stream.Collectors.toSet;
|
||||
|
||||
public class YamlResourceManagerLoader {
|
||||
|
||||
public ResourceManager load(InputStream stream) {
|
||||
Yaml yaml = new Yaml(new Constructor(YamlResourceManager.class));
|
||||
|
||||
YamlResourceManager yamlResourceManager = yaml.load(stream);
|
||||
|
||||
return mapYamlResourceManager(yamlResourceManager);
|
||||
}
|
||||
|
||||
private ResourceManager mapYamlResourceManager(YamlResourceManager yaml) {
|
||||
|
||||
ResourceManager manager = new ResourceManager();
|
||||
|
||||
for (YamlResource yamlResource : yaml.getResources()) {
|
||||
manager.addMapping(yamlResource.getKey(), mapYamlResourceMapping(yamlResource));
|
||||
}
|
||||
|
||||
return manager;
|
||||
}
|
||||
|
||||
private ResourceMapping mapYamlResourceMapping(YamlResource resource) {
|
||||
Set<String> options = resource.getOptions() != null ? resource.getOptions() : Collections.emptySet();
|
||||
return ResourceMapping.builder()
|
||||
.defaultResource(Resource.load(resource.getDefaultLocation()))
|
||||
.label(resource.getLabel())
|
||||
.description(resource.getDescription())
|
||||
.options(options.stream()
|
||||
.map(ResourceMapping.Option::valueOf)
|
||||
.collect(toSet()))
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
|
@ -15,13 +15,13 @@ class ResourceManagerTest {
|
|||
|
||||
ResourceManager manager = new ResourceManager();
|
||||
manager.addMapping("test.resource.exists", ResourceMapping.builder()
|
||||
.defaultResource(Resource.fromClasspath("/airplane.png"))
|
||||
.defaultResource(Resource.fromClasspath("/data/airplane.png"))
|
||||
.build())
|
||||
.addMapping("test.resource.helloworld.default", ResourceMapping.builder()
|
||||
.defaultResource(Resource.fromClasspath("/helloworld.txt"))
|
||||
.defaultResource(Resource.fromClasspath("/data/helloworld.txt"))
|
||||
.build())
|
||||
.addMapping("test.resource.helloworld", ResourceMapping.builder()
|
||||
.definedResource(Resource.fromClasspath("/hellospecific.txt"))
|
||||
.definedResource(Resource.fromClasspath("/data/hellospecific.txt"))
|
||||
.build());
|
||||
|
||||
return manager;
|
||||
|
|
|
@ -10,8 +10,8 @@ class ResourceMappingTest {
|
|||
@Test
|
||||
void checkResourceExists() {
|
||||
ResourceMapping mapping = ResourceMapping.builder()
|
||||
.defaultResource(Resource.fromClasspath("/airplane.png"))
|
||||
.definedResource(Resource.fromClasspath("/airplane.png"))
|
||||
.defaultResource(Resource.fromClasspath("/data/airplane.png"))
|
||||
.definedResource(Resource.fromClasspath("/data/airplane.png"))
|
||||
.build();
|
||||
|
||||
assertTrue(mapping.exists());
|
||||
|
|
|
@ -14,26 +14,26 @@ class ResourceTest {
|
|||
|
||||
@Test
|
||||
void testResourceFromFile() {
|
||||
Resource resource = Resource.fromFile(new File("src/test/resources/airplane.png"));
|
||||
Resource resource = Resource.fromFile(new File("src/test/resources/data/airplane.png"));
|
||||
assertResource(resource, "from File");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testResourceFromResourceStream() {
|
||||
Resource resource = Resource.fromStream(ResourceTest.class.getResourceAsStream("/airplane.png"));
|
||||
Resource resource = Resource.fromStream(ResourceTest.class.getResourceAsStream("/data/airplane.png"));
|
||||
assertResource(resource, "from Stream");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testResourceFromResource() {
|
||||
Resource resource = Resource.fromClasspath("/airplane.png");
|
||||
Resource resource = Resource.fromClasspath("/data/airplane.png");
|
||||
assertResource(resource, "from Resource");
|
||||
}
|
||||
|
||||
static List<String> urls = Arrays.asList("https://homepages.cae.wisc.edu/~ece533/images/airplane.png",
|
||||
"file:///" + System.getProperty("user.dir") + "/src/test/resources/airplane.png",
|
||||
"classpath:/airplane.png");
|
||||
"file:///" + System.getProperty("user.dir") + "/src/test/resources/data/airplane.png",
|
||||
"classpath:/data/airplane.png");
|
||||
|
||||
@Test
|
||||
void testResourceFromURL() throws Exception {
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package de.nclazz.resources.loader;
|
||||
|
||||
import de.nclazz.resources.ResourceManager;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
class YamlResourceManagerLoaderTest {
|
||||
|
||||
@Test
|
||||
void loadResourceManagerFromYamlFile() {
|
||||
|
||||
String path = "/resources-example.yml";
|
||||
try (InputStream stream = YamlResourceManagerLoaderTest.class.getResourceAsStream(path)) {
|
||||
|
||||
YamlResourceManagerLoader yamlResourceManagerLoader = new YamlResourceManagerLoader();
|
||||
ResourceManager manager = yamlResourceManagerLoader.load(stream);
|
||||
|
||||
Assertions.assertTrue(manager.getMapping("images.airplane").isPresent());
|
||||
Assertions.assertTrue(manager.getMapping("texts.helloworld").isPresent());
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Before Width: | Height: | Size: 440 KiB After Width: | Height: | Size: 440 KiB |
|
@ -0,0 +1,17 @@
|
|||
resources:
|
||||
- key: images.airplane
|
||||
label: Airplane Image
|
||||
description: An image of an airplane
|
||||
options:
|
||||
defaultLocation: classpath:/data/airplane.png
|
||||
types:
|
||||
- image/png
|
||||
- image/jpeg
|
||||
- image/bmp
|
||||
|
||||
- key: texts.helloworld
|
||||
label: Hello World Text
|
||||
description: An Text file
|
||||
defaultLocation: classpath:/data/hellworld.txt
|
||||
types:
|
||||
- text/plain
|
Loading…
Reference in New Issue