Skip to content

Commit

Permalink
Merge pull request #1 from satori-com/dev
Browse files Browse the repository at this point in the history
0.1.7
  • Loading branch information
Satori-SDK authored Jul 31, 2017
2 parents 144e771 + 7c985ca commit 19b6eaa
Show file tree
Hide file tree
Showing 29 changed files with 481 additions and 161 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ idea {

subprojects {
group 'com.satori'
version '0.1.3'
version '0.1.7'

apply plugin: 'java'

Expand Down
7 changes: 4 additions & 3 deletions composer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ dependencies {
compile project(':mods')

compile 'com.google.protobuf:protobuf-java:3.3.1'

compile 'org.freemarker:freemarker:2.3.26-incubating'
compile 'net.sf.saxon:Saxon-HE:9.6.0-10'
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.0.pr4'
compile 'net.sf.saxon:Saxon-HE:9.8.0-3'
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.0'

compile 'org.slf4j:slf4j-api:1.7.25'

compile 'io.vertx:vertx-core:3.4.2'
compile 'io.vertx:vertx-web:3.4.2'
compile 'org.javassist:javassist:3.21.0-GA'

testCompile 'junit:junit:4.+'
testCompile 'org.apache.commons:commons-jexl3:+'
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.satori.composer.config.loader;

import java.util.*;

import com.fasterxml.jackson.databind.*;

/**
* Load Mod configuration, looking at a few places.
*/
public class CompositeConfigLoader implements ConfigLoader {

private final Iterable<ConfigLoader> loaders;

public CompositeConfigLoader(Iterable<ConfigLoader> loaders) {
this.loaders = loaders;
}

public CompositeConfigLoader(ConfigLoader... loaders) {
this.loaders = Arrays.stream(loaders)::iterator;
}

@Override
public JsonNode tryLoad() throws ConfigParsingException {
return tryLoad(loaders);
}

/**
* Load configuration trying all supplied configuration sources in turn.
*/
public static JsonNode tryLoad(ConfigLoader... loaders) throws ConfigParsingException {
if(loaders == null){
return null;
}
return tryLoad(Arrays.stream(loaders)::iterator);
}

public static JsonNode tryLoad(Iterable<ConfigLoader> loaders) throws ConfigParsingException {
if(loaders == null){
return null;
}
for (ConfigLoader l : loaders) {
if (l == null) {
continue;
}
JsonNode result = l.tryLoad();
if (result != null) {
return result;
}
}
return null;
}

public static JsonNode load(ConfigLoader... loaders) throws Exception {
JsonNode result = tryLoad(loaders);
if(result != null){
return result;
}
throw new Exception("not found");
}

public static JsonNode load(Iterable<ConfigLoader> loaders) throws Exception {
JsonNode result = tryLoad(loaders);
if(result != null){
return result;
}
throw new Exception("not found");
}
}
38 changes: 38 additions & 0 deletions composer/src/com/satori/composer/config/loader/ConfigLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.satori.composer.config.loader;

import com.satori.mods.core.config.*;

import java.io.*;
import java.nio.file.*;

import com.fasterxml.jackson.databind.*;

public interface ConfigLoader {

JsonNode tryLoad() throws ConfigParsingException;

default JsonNode load() throws Exception{
JsonNode result = tryLoad();
if(result != null){
return result;
}
throw new Exception("not found");
}

static ConfigLoader fromFile(Path path){
return ()->{
if (path == null) {
return null;
}
try (final Reader reader = Files.newBufferedReader(path)) {
return Config.mapper.readTree(reader);
} catch (FileNotFoundException e) {
return null;
} catch (Exception e) {
throw new ConfigParsingException(e);
}
};
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.satori.composer.config.loader;

import com.satori.mods.core.config.*;

import java.io.*;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;

public class ConfigLoaderUtils {

public static JsonNode parseConfigString(String config) throws ConfigParsingException {
try {
return Config.mapper.readTree(config);
} catch (JsonParseException e) {
throw new ConfigParsingException(
String.format("Can't parse configuration: '%s'", config), e
);
} catch (IOException e) {
throw new IllegalStateException("IOException while parsing JSON from a String");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.satori.composer.config.loader;

public class ConfigParsingException extends Exception {

ConfigParsingException() {
super(createErrorMessage(null));
}

ConfigParsingException(String config) {
super(createErrorMessage(config));
}

public ConfigParsingException(String config, Throwable cause) {
super(createErrorMessage(config), cause);
}

public ConfigParsingException(Throwable cause) {
super(createErrorMessage(null), cause);
}

private static String createErrorMessage(String config){
if(config != null){
return String.format("Can't parse configuration: '%s'", config);
}

return "Can't parse configuration";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.satori.composer.config.loader;

import com.satori.mods.core.config.*;

import java.io.*;
import java.nio.file.*;

import com.fasterxml.jackson.databind.*;

public class FileConfigLoader implements ConfigLoader {

private final Path path;

public FileConfigLoader(Path path) {
this.path = path;
}

@Override
public JsonNode tryLoad() throws ConfigParsingException {
if (path == null) {
return null;
}
File file = path.toFile();
if(!file.exists() || !file.isFile()){
return null;
}
try (final Reader reader = Files.newBufferedReader(path)) {
return Config.mapper.readTree(reader);
} catch (FileNotFoundException e) {
return null;
} catch (Exception e) {
throw new ConfigParsingException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.satori.composer.config.loader;

import com.satori.mods.resources.*;

import java.util.function.*;

import com.fasterxml.jackson.databind.*;

/**
* Get Mod configuration from a resource file bundled in the JAR.
*/
public class ModResourceConfigLoader implements ConfigLoader {

private final String resourceName;

public ModResourceConfigLoader(String resourceName) {
this.resourceName = resourceName;
}

public ModResourceConfigLoader(Supplier<String> resourceName) {
this.resourceName = resourceName != null ? resourceName.get() : null;
}

@Override
public JsonNode tryLoad() throws ConfigParsingException {
if(resourceName == null || resourceName.isEmpty()){
return null;
}
try {
return ModResourceLoader.tryLoadAsJson(resourceName);
} catch (Exception e) {
throw new ConfigParsingException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.satori.composer.config.loader;

import com.satori.mods.core.config.*;
import com.satori.mods.resources.*;

import java.io.*;
import java.util.function.*;

import com.fasterxml.jackson.databind.*;

/**
* Get Mod configuration from a resource file bundled in the JAR.
*/
public class ResourceConfigLoader implements ConfigLoader {

private final String resourceName;
private final ClassLoader classLoader;

public ResourceConfigLoader(String resourceName) {
this(ModResourceLoader.class.getClassLoader(), resourceName);
}

public ResourceConfigLoader(Supplier<String> resourceName) {
this(resourceName.get());
}

public ResourceConfigLoader(ClassLoader classLoader, String resourceName) {
this.resourceName = resourceName;
this.classLoader = classLoader;
}

public ResourceConfigLoader(ClassLoader classLoader, Supplier<String> resourceName) {
this(classLoader, resourceName.get());
}

@Override
public JsonNode tryLoad() throws ConfigParsingException {
if(resourceName == null || resourceName.isEmpty()){
return null;
}

try (final InputStream inputStream = classLoader.getResourceAsStream(resourceName)) {
if (inputStream == null) {
return null;
}
return Config.mapper.readTree(inputStream);
} catch (Exception e){
throw new ConfigParsingException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.satori.composer.config.loader;

import com.satori.mods.core.config.*;

import java.util.function.*;

import com.fasterxml.jackson.databind.*;

public class StringConfigLoader implements ConfigLoader {

private final String string;

public StringConfigLoader(String string) {
this.string = string;
}

public StringConfigLoader(Supplier<String> string) {
this.string = string != null ? string.get() : null;
}

@Override
public JsonNode tryLoad() throws ConfigParsingException {
return tryLoad(string);
}

public static JsonNode tryLoad(String string) throws ConfigParsingException {
if (string == null || string.isEmpty()) {
return null;
}
try {
return Config.mapper.readTree(string);
} catch (Exception e) {
throw new ConfigParsingException(e);
}
}

public static JsonNode load(String string) throws Exception {
JsonNode result = tryLoad(string);
if (result != null) {
return result;
}
throw new Exception("not found");
}

}
Loading

0 comments on commit 19b6eaa

Please sign in to comment.