Commit 915aa663 authored by Filip Dreger's avatar Filip Dreger
Browse files

release 1.4

parent edeeeeda
Pipeline #9323 passed with stage
in 0 seconds
publishing {
publications {
configPublication(MavenPublication) {
from components.java
artifact sourceJar
artifact javadocJar
groupId rootProject.getGroup()
artifactId project.name
version rootProject.getVersion()
}
}
}
dependencies {
implementation project(':bento-core')
}
package net.snowyhollows.bento.config;
import net.snowyhollows.bento.Bento;
import net.snowyhollows.bento.BentoFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Properties;
import java.util.function.Consumer;
public class Configurer {
private final Bento bento;
private final WorkDir workDir;
public Configurer() {
this(new DefaultWorkDir());
}
public Configurer(WorkDir workDir) {
this.workDir = workDir;
this.bento = Bento.createRoot();
this.bento.register(WorkDirFactory.IT, workDir);
}
public Configurer loadConfigDir(String dirPath) throws IOException {
loadAllConfigFiles(dirPath, bento);
return this;
}
public Configurer loadConfigFile(String filename) throws IOException {
loadProperties(filename, bento);
return this;
}
public Configurer setParam(String key, Object value) {
bento.register(key, value);
return this;
}
public Configurer overrideParam(String key, Object value) {
assertKeyDefined(key);
setParam(key, value);
return this;
}
public Configurer initialize(BentoFactory<?>... factories) {
for (BentoFactory<?> factory : factories) {
bento.get(factory);
}
return this;
}
public <T> Configurer use(BentoFactory<T> factory, Consumer<T> user) {
user.accept(bento.get(factory, null));
return this;
}
public <T> Configurer useWithIo(BentoFactory<T> factory, IoConsumer<T> user) throws IOException {
user.accept(bento.get(factory, null));
return this;
}
public Bento getConfig() {
return bento;
}
private void assertKeyDefined(Object key) {
checkArgument(bento.get(key, null) != null,
"key `%s` is not undefined. Use 'param' instead of 'override'.",
key);
}
private void loadProperties(String configPath, Bento config) throws IOException {
loadProperties(new InputStreamReader(workDir.openForReading(new File(configPath)), StandardCharsets.UTF_8), config);
}
private void loadProperties(Reader reader, Bento config) throws IOException {
Properties properties = new Properties();
properties.load(reader);
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
config.register(entry.getKey(), entry.getValue());
}
}
private void loadAllConfigFiles(String configDir, Bento config) throws IOException {
for (File file : workDir.listFiles(new File(configDir), f -> !f.isDirectory())) {
loadProperties(file.getAbsolutePath(), config);
}
}
private static void checkArgument(boolean test, String errorMessageTemplate, Object... args) {
if (!test) {
throw new IllegalArgumentException(String.format(errorMessageTemplate, args));
}
}
public interface IoConsumer<T> {
void accept(T t) throws IOException;
}
}
package net.snowyhollows.bento.config;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class DefaultWorkDir implements WorkDir {
private final File root;
public DefaultWorkDir() {
this(new File("."));
}
public DefaultWorkDir(File root) {
if (!root.isDirectory()) {
throw new IllegalArgumentException(String.format("%s is not a directory", root.getAbsolutePath()));
}
this.root = root;
}
@Override
public OutputStream openForWriting(File file) {
try {
return new FileOutputStream(absolutize(file));
} catch (FileNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public InputStream openForReading(File file) {
try {
return new FileInputStream(absolutize(file));
} catch (FileNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public File[] listFiles(File dir, FileFilter fileFilter) {
return absolutize(dir).listFiles(fileFilter);
}
private File absolutize(File file) {
return file.isAbsolute() ? file : new File(root.getAbsolutePath(), file.getPath());
}
}
package net.snowyhollows.bento.config;
import java.io.File;
import java.io.FileFilter;
import java.io.InputStream;
import java.io.OutputStream;
public interface WorkDir {
OutputStream openForWriting(File file);
InputStream openForReading(File file);
File[] listFiles(File dir, FileFilter fileFilter);
}
package net.snowyhollows.bento.config;
import net.snowyhollows.bento.Bento;
import net.snowyhollows.bento.BentoFactory;
public enum WorkDirFactory implements BentoFactory<WorkDir> {
IT;
@Override
public WorkDir createInContext(Bento bento) {
throw new IllegalArgumentException("No WorkDir implementation registered");
}
}
package net.snowyhollows.bento2;
package net.snowyhollows.bento;
import net.snowyhollows.bento2.inspector.BentoInspector;
import net.snowyhollows.bento2.store.BentoStore;
import net.snowyhollows.bento2.store.MapStore;
import net.snowyhollows.bento.inspector.BentoInspector;
import net.snowyhollows.bento.store.BentoStore;
import net.snowyhollows.bento.store.MapStore;
public final class Bento {
private final Bento parent;
......
package net.snowyhollows.bento2;
package net.snowyhollows.bento;
public class BentoException extends RuntimeException {
public BentoException() {
......
package net.snowyhollows.bento2;
package net.snowyhollows.bento;
public interface BentoFactory<T> {
T createInContext(Bento bento);
......
package net.snowyhollows.bento2;
package net.snowyhollows.bento;
public interface BentoResettable<T> {
void reset(T t, Bento bento);
......
package net.snowyhollows.bento2.annotation;
package net.snowyhollows.bento.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
......
package net.snowyhollows.bento2.annotation;
package net.snowyhollows.bento.annotation;
import net.snowyhollows.bento2.BentoFactory;
import net.snowyhollows.bento.BentoFactory;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
......
package net.snowyhollows.bento2.annotation;
package net.snowyhollows.bento.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
......
package net.snowyhollows.bento2.annotation;
package net.snowyhollows.bento.annotation;
import net.snowyhollows.bento2.Bento;
import net.snowyhollows.bento2.BentoFactory;
import net.snowyhollows.bento.Bento;
import net.snowyhollows.bento.BentoFactory;
public final class DefaultFactory implements BentoFactory<Object> {
......
package net.snowyhollows.bento2.annotation;
package net.snowyhollows.bento.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
......
package net.snowyhollows.bento2.annotation;
package net.snowyhollows.bento.annotation;
import java.lang.annotation.ElementType;
......
package net.snowyhollows.bento2.inspector;
package net.snowyhollows.bento.inspector;
import net.snowyhollows.bento2.Bento;
import net.snowyhollows.bento.Bento;
public interface BentoInspector {
void createChild(Bento parent, Bento bento);
......
package net.snowyhollows.bento2.store;
package net.snowyhollows.bento.store;
public interface BentoStore {
Object get(Object key);
......
package net.snowyhollows.bento2.store;
package net.snowyhollows.bento.store;
import java.util.HashMap;
import java.util.Map;
......@@ -22,4 +22,4 @@ public class MapStore implements BentoStore {
}
}
\ No newline at end of file
}
/*
* Copyright (c) 2009-2018 Ericsson AB, Sweden. All rights reserved.
*
* The Copyright to the computer program(s) herein is the property of Ericsson AB, Sweden.
* The program(s) may be used and/or copied with the written permission from Ericsson AB
* or in accordance with the terms and conditions stipulated in the agreement/contract under
* which the program(s) have been supplied.
*
*/
package net.snowyhollows.bento2;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
/**
* @author efildre
*/
public class BentoRunner {
public static Bento runWithClasspathProperties(BentoFactory<?> factory, String classpathProperties)
throws IOException {
Properties properties = new Properties();
properties.load(BentoRunner.class.getResourceAsStream(classpathProperties));
Bento root = Bento.createRoot();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
root.register(entry.getKey(), entry.getValue());
}
root.get(factory);
return root;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit trunk//EN" "http://google-web-toolkit.googlecode.com/svn/trunk/distro-source/core/src/gwt-module.dtd">
<module>
<source path="net/snowyhollows/bento2" >
<exclude name="**/BentoRunner.java" />
<source path="net/snowyhollows/bento" >
</source>
</module>
\ No newline at end of file
</module>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment