Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration building #28

Merged
merged 6 commits into from
May 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava-string</artifactId>
<version>1.0.1</version>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.atteo</groupId>
Expand Down Expand Up @@ -91,7 +91,7 @@
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
<version>1.0.12</version>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>io.reactivex</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package org.chodavarapu.datamill.configuration;

import org.chodavarapu.datamill.reflection.Bean;
import org.chodavarapu.datamill.values.StringValue;
import org.chodavarapu.datamill.values.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.functions.*;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.function.Consumer;

/**
* @author Ravi Chodavarapu (rchodava@gmail.com)
*/
public class ConfigurationBuilder<T> {
private static final Logger logger = LoggerFactory.getLogger(ConfigurationBuilder.class);

private final Bean<T> bean;

public ConfigurationBuilder(Bean<T> bean) {
this.bean = bean;
}

public ConfigurationBuilder<T> configure(Consumer<T> configuration) {
configuration.accept(get());
return this;
}

public ConfigurationBuilder<T> configure(Action2<ConfigurationBuilder<T>, T> configuration) {
configuration.call(this, get());
return this;
}

public T get() {
return bean.unwrap();
}

public ConfigurationBuilder<T> ifSystemPropertyExists(String name, Consumer<ConfigurationBuilder<T>> configuration) {
return ifSystemPropertyExists(name, configuration, null);
}

public ConfigurationBuilder<T> ifSystemPropertyExists(String name,
Consumer<ConfigurationBuilder<T>> configuration,
Consumer<ConfigurationBuilder<T>> elseConfiguration) {
String value = System.getProperty(name);
if (value != null) {
if (configuration != null) {
configuration.accept(this);
}
} else {
if (elseConfiguration != null) {
elseConfiguration.accept(this);
}
}

return this;
}

private String getSystemProperty(String name, boolean required) {
String value = System.getProperty(name);
if (value == null && required) {
throw new IllegalStateException("Expected " + name + " to be found in the system properties list!");
}

return value;
}

public Value getRequiredSystemProperty(String name) {
return new StringValue(getSystemProperty(name, true));
}

private <P> ConfigurationBuilder<T> fromSystemProperty(Consumer<T> propertyInvoker, String name,
Func1<String, P> derivation, boolean required) {
String value = getSystemProperty(name, required);
bean.set(propertyInvoker, derivation != null ? derivation.call(value) : value);

return this;
}

public <P> ConfigurationBuilder<T> fromRequiredSystemProperty(Consumer<T> propertyInvoker, String name,
Func1<String, P> derivation) {
return fromSystemProperty(propertyInvoker, name, derivation, true);
}

private <P> ConfigurationBuilder<T> fromSystemProperties(Consumer<T> propertyInvoker,
String name1, String name2,
Func2<String, String, P> derivation,
boolean required) {
String value1 = getSystemProperty(name1, required);
String value2 = getSystemProperty(name2, required);
bean.set(propertyInvoker, derivation.call(value1, value2));

return this;
}

public <P> ConfigurationBuilder<T> fromRequiredSystemProperties(Consumer<T> propertyInvoker,
String name1, String name2,
Func2<String, String, P> derivation) {
return fromSystemProperties(propertyInvoker, name1, name2, derivation, true);
}

private <P> ConfigurationBuilder<T> fromSystemProperties(Consumer<T> propertyInvoker,
String name1, String name2, String name3,
Func3<String, String, String, P> derivation,
boolean required) {
String value1 = getSystemProperty(name1, required);
String value2 = getSystemProperty(name2, required);
String value3 = getSystemProperty(name3, required);
bean.set(propertyInvoker, derivation.call(value1, value2, value3));

return this;
}

public <P> ConfigurationBuilder<T> fromRequiredSystemProperties(Consumer<T> propertyInvoker,
String name1, String name2, String name3,
Func3<String, String, String, P> derivation) {
return fromSystemProperties(propertyInvoker, name1, name2, name3, derivation, true);
}

private <P> ConfigurationBuilder<T> fromSystemProperties(Consumer<T> propertyInvoker,
String name1, String name2, String name3, String name4,
Func4<String, String, String, String, P> derivation,
boolean required) {
String value1 = getSystemProperty(name1, required);
String value2 = getSystemProperty(name2, required);
String value3 = getSystemProperty(name3, required);
String value4 = getSystemProperty(name4, required);
bean.set(propertyInvoker, derivation.call(value1, value2, value3, value4));

return this;
}

public <P> ConfigurationBuilder<T> fromRequiredSystemProperties(Consumer<T> propertyInvoker,
String name1, String name2, String name3, String name4,
Func4<String, String, String, String, P> derivation) {
return fromSystemProperties(propertyInvoker, name1, name2, name3, name4, derivation, true);
}

private <P> ConfigurationBuilder<T> fromSystemProperties(Consumer<T> propertyInvoker,
String name1, String name2, String name3, String name4, String name5,
Func5<String, String, String, String, String, P> derivation,
boolean required) {
String value1 = getSystemProperty(name1, required);
String value2 = getSystemProperty(name2, required);
String value3 = getSystemProperty(name3, required);
String value4 = getSystemProperty(name4, required);
String value5 = getSystemProperty(name5, required);
bean.set(propertyInvoker, derivation.call(value1, value2, value3, value4, value5));

return this;
}

public <P> ConfigurationBuilder<T> fromRequiredSystemProperties(Consumer<T> propertyInvoker,
String name1, String name2, String name3, String name4, String name5,
Func5<String, String, String, String, String, P> derivation) {
return fromSystemProperties(propertyInvoker, name1, name2, name3, name4, name5, derivation, true);
}

private <V> ConfigurationBuilder<T> fromSystemProperty(Consumer<T> propertyInvoker, String name,
V defaultValue, boolean required) {
String value = getSystemProperty(name, required);
if (value != null) {
bean.set(propertyInvoker, new StringValue(value));
} else {
bean.set(propertyInvoker, defaultValue);
}

return this;
}

public <V> ConfigurationBuilder<T> fromOptionalSystemProperty(Consumer<T> propertyInvoker, String name,
V defaultValue) {
return fromSystemProperty(propertyInvoker, name, defaultValue, false);
}

public ConfigurationBuilder<T> fromRequiredSystemProperty(Consumer<T> propertyInvoker, String name) {
return fromRequiredSystemProperty(propertyInvoker, name, null);
}

public ConfigurationBuilder<T> fromOptionalSystemProperty(Consumer<T> propertyInvoker, String name) {
return fromSystemProperty(propertyInvoker, name, null, false);
}

public ConfigurationBuilder<T> fromLocalAddress(Consumer<T> propertyInvoker) {
try {
bean.set(propertyInvoker, InetAddress.getLocalHost().getHostAddress());
} catch (UnknownHostException e) {
logger.debug("Error retrieving local host name, using localhost", e);
bean.set(propertyInvoker, "localhost");
}

return this;
}

public <P> ConfigurationBuilder<T> fromLocalAddress(Consumer<T> propertyInvoker, Func1<String, P> derivation) {
String localAddress;
try {
localAddress = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
logger.debug("Error retrieving local host name, using localhost", e);
localAddress = "localhost";
}

bean.set(propertyInvoker, derivation.call(localAddress));
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.CaseFormat;
import com.google.common.base.Defaults;
import com.sun.beans.TypeResolver;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.Proxy;
import org.atteo.evo.inflector.English;
Expand All @@ -15,6 +16,8 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.time.LocalDateTime;
Expand All @@ -30,6 +33,62 @@
*/
public class OutlineImpl<T> implements Outline<T> {
private static Method OBJECT_GET_CLASS_METHOD;

private static String capitalize(String string) {
if (string != null && string.length() > 0) {
return Character.toUpperCase(string.charAt(0)) + string.substring(1);
}

return string;
}

private static Method findMethod(Class<?> start, String methodName, int numberOfArguments, Class<?> arguments[]) {
for (Class<?> clazz = start; clazz != null; clazz = clazz.getSuperclass()) {
Method methods[] = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];

if (method == null || !Modifier.isPublic(method.getModifiers())) {
continue;
}

if (method.getName().equals(methodName)) {
Type[] parameters = method.getGenericParameterTypes();
if (parameters.length == numberOfArguments) {
if (arguments != null) {
boolean differentParameterType = false;
if (numberOfArguments > 0) {
for (int j = 0; j < numberOfArguments; j++) {
if (TypeResolver.erase(TypeResolver.resolveInClass(start, parameters[j])) !=
arguments[j]) {
differentParameterType = true;
continue;
}
}

if (differentParameterType) {
continue;
}
}
}

return method;
}
}
}
}

Class interfaces[] = start.getInterfaces();
for (int i = 0 ; i < interfaces.length; i++) {
Method method = findMethod(interfaces[i], methodName, numberOfArguments, null);
if (method != null) {
return method;
}
}

return null;
}

private static Method getObjectGetClassMethod() {
if (OBJECT_GET_CLASS_METHOD == null) {
try {
Expand Down Expand Up @@ -274,16 +333,35 @@ public T unwrap() {
}

private class PropertyImpl<T> extends MemberImpl implements Property<T> {

private final PropertyDescriptor descriptor;
private final Method writeMethod;

public PropertyImpl(PropertyDescriptor descriptor) {
super(descriptor.getName());

this.descriptor = descriptor;
this.writeMethod = introspectWriteMethod(descriptor);
}

private Method introspectWriteMethod(PropertyDescriptor descriptor) {
Method method = descriptor.getWriteMethod();
if (method == null) {
Class<?> cls = descriptor.getReadMethod().getDeclaringClass();

Class<?> type = descriptor.getPropertyType();

String writeMethodName = "set" + capitalize(descriptor.getName());

Class<?>[] args = (type == null) ? null : new Class<?>[] { type };
method = findMethod(cls, writeMethodName, 1, args);
}

return method;
}

public boolean isReadOnly() {
return descriptor.getWriteMethod() == null;
return writeMethod == null;
}

public boolean isSimple() {
Expand Down Expand Up @@ -347,7 +425,6 @@ public <P> P get(T instance) {
}

public <P> void set(T instance, P value) {
java.lang.reflect.Method writeMethod = descriptor.getWriteMethod();
if (writeMethod != null) {
if (!writeMethod.isAccessible()) {
performSecure(() -> writeMethod.setAccessible(true));
Expand Down
Loading