-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from satori-com/dev
0.1.7
- Loading branch information
Showing
29 changed files
with
481 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ idea { | |
|
||
subprojects { | ||
group 'com.satori' | ||
version '0.1.3' | ||
version '0.1.7' | ||
|
||
apply plugin: 'java' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
composer/src/com/satori/composer/config/loader/CompositeConfigLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
composer/src/com/satori/composer/config/loader/ConfigLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
} | ||
|
||
|
||
} |
23 changes: 23 additions & 0 deletions
23
composer/src/com/satori/composer/config/loader/ConfigLoaderUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
composer/src/com/satori/composer/config/loader/ConfigParsingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
composer/src/com/satori/composer/config/loader/FileConfigLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
composer/src/com/satori/composer/config/loader/ModResourceConfigLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
composer/src/com/satori/composer/config/loader/ResourceConfigLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
composer/src/com/satori/composer/config/loader/StringConfigLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
Oops, something went wrong.