de.tsl2.nano.replication.util.Util Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tsl2.nano.replication Show documentation
Show all versions of tsl2.nano.replication Show documentation
Provides Database/XML Replication through JPA
package de.tsl2.nano.replication.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodType;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
public class Util {
static Map consumedProps = new HashMap<>(); //for system.out prints...as help
public static String getProperty(String name, String defaultOrMandatory, String info) {
return getProperty(name, String.class, defaultOrMandatory, info);
}
public static T getProperty(String name, Class type) {
return getProperty(name, type, null, null);
}
@SuppressWarnings("unchecked")
public static T getProperty(String name, Class type, T defaultOrMandatory, String info) {
assert defaultOrMandatory == null || type.isAssignableFrom(defaultOrMandatory.getClass());
T value = null;
if (type.equals(String.class))
value = (T) System.getProperty(name, (String)defaultOrMandatory);
else if (type.equals(Boolean.class))
value = type.cast(Boolean.getBoolean(name)); //TODO: involve defaultOrMandatory
else if (type.equals(Integer.class))
value = type.cast(Integer.getInteger(name)); //TODO: involve defaultOrMandatory
assert info == null || value != null : "Please define -D" + name + " " + info;
if (value != null)
ULog.log(" " + name + "=" + value, !String.class.equals(type));
consumedProps.put(name, value);
return value;
}
public static ClassLoader linkResourcePath(String origin, String link) {
ClassLoader originCL = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(new URLClassLoader(((URLClassLoader)originCL).getURLs()) {
@Override
public Enumeration getResources(String name) throws IOException {
if (name.equals(origin)) {
ULog.log("..loading " + link + "...", false);
return super.getResources(link);
}
return super.getResources(name);
}
});
return originCL;
}
public static Map getConsumedProperties() {
return consumedProps;
}
public static Properties loadPropertiesToSystem(String file) {
Properties props = new Properties();
try {
ULog.log("loading optional properties from " + file + "...", false, new Object[0]);
if (Files.exists(Paths.get(file))) {
props.load(Files.newInputStream(Paths.get(file)));
System.getProperties().putAll(props);
ULog.log("done");
} else {
ULog.log("file does not exist!");
}
return props;
} catch (IOException e) {
ULog.log(e.toString()); //optional
return null;
}
}
public static String[] mergeArgsAndProps(String[] args, int minargs, Properties props) {
if (props != null) {
ArrayList