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

Feature/config extension #21

Merged
merged 13 commits into from
May 22, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
package com.kumuluz.ee.configuration;

import com.kumuluz.ee.configuration.utils.ConfigurationDispatcher;

import java.util.Optional;

/**
Expand All @@ -42,6 +44,8 @@ public interface ConfigurationSource {

Optional<Integer> getListSize(String key);

void watch(String key);

void set(String key, String value);

void set(String key, Boolean value);
Expand All @@ -51,4 +55,7 @@ public interface ConfigurationSource {
void set(String key, Double value);

void set(String key, Float value);

void setConfigurationDispatcher(ConfigurationDispatcher configurationDispatcher);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.kumuluz.ee.configuration.sources;

import com.kumuluz.ee.configuration.ConfigurationSource;
import com.kumuluz.ee.configuration.utils.ConfigurationDispatcher;

import java.util.Optional;
import java.util.logging.Logger;
Expand Down Expand Up @@ -133,6 +134,11 @@ public Optional<Integer> getListSize(String key) {
}
}

@Override
public void watch(String key) {

}

@Override
public void set(String key, String value) {

Expand All @@ -158,6 +164,11 @@ public void set(String key, Float value) {

}

@Override
public void setConfigurationDispatcher(ConfigurationDispatcher configurationDispatcher) {

}

private String parseKeyNameForEnvironmentVariables(String key) {

return key.toUpperCase().replaceAll("\\.", "_");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.kumuluz.ee.configuration.sources;

import com.kumuluz.ee.configuration.ConfigurationSource;
import com.kumuluz.ee.configuration.utils.ConfigurationDispatcher;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
Expand Down Expand Up @@ -213,6 +214,11 @@ public Optional<Integer> getListSize(String key) {

}

@Override
public void watch(String key) {

}

@Override
public void set(String key, String value) {

Expand All @@ -238,6 +244,11 @@ public void set(String key, Float value) {

}

@Override
public void setConfigurationDispatcher(ConfigurationDispatcher configurationDispatcher) {

}

/**
* Returns true, if key represents an array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ public class ConfigurationDispatcher {

public void notifyChange(String key, String value) {

for (ConfigurationListener subscription : subscriptions) {
subscription.onChange(key, value);
}

}

public void subscribe(ConfigurationListener listener) {

subscriptions.add(listener);
}

public void unsubscribe(ConfigurationListener listener) {

subscriptions.remove(listener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@

import com.kumuluz.ee.configuration.ConfigurationListener;
import com.kumuluz.ee.configuration.ConfigurationSource;
import com.kumuluz.ee.configuration.sources.EnvironmentConfigurationSource;
import com.kumuluz.ee.configuration.sources.FileConfigurationSource;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;

/**
* @author Tilen Faganel
Expand Down Expand Up @@ -133,6 +128,14 @@ public Optional<Integer> getListSize(String key) {
}
}

public void watch(String key) {

for (ConfigurationSource configurationSource : config.getConfigurationSources()) {
configurationSource.watch(key);
}

}

public void set(String key, String value) {
config.getConfigurationSources().get(0).set(key, value);
}
Expand All @@ -153,11 +156,16 @@ public void set(String key, Float value) {
config.getConfigurationSources().get(0).set(key, value);
}

public void subscribe(ConfigurationListener listener) {
public void subscribe(String key, ConfigurationListener listener) {

config.getDispatcher().subscribe(listener);

watch(key);

}

public void unsubscribe(ConfigurationListener listener) {
config.getDispatcher().unsubscribe(listener);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import java.lang.annotation.Target;

/**
* Annotation specifies key name for automatic initialisation of a field from configuration.
* Annotation specifies key name for automatic initialisation of a field from configuration. If attribute watch is
* set to true, key is subscribed to dynamic configuration events.
*
* @author Tilen Faganel
* @since 2.1.0
Expand All @@ -36,4 +37,6 @@
@Target({ElementType.FIELD})
public @interface ConfigValue {
@Nonbinding String value() default "";

@Nonbinding boolean watch() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@

import com.kumuluz.ee.configuration.cdi.ConfigBundle;
import com.kumuluz.ee.configuration.cdi.ConfigValue;
import com.kumuluz.ee.configuration.utils.ConfigurationDispatcher;
import com.kumuluz.ee.configuration.utils.ConfigurationUtil;

import javax.annotation.PostConstruct;
import javax.annotation.Priority;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.logging.Logger;

/**
* Interceptor class for ConfigBundle annotation.
Expand All @@ -44,6 +45,8 @@
@Priority(Interceptor.Priority.LIBRARY_BEFORE)
public class ConfigBundleInterceptor {

private static final Logger log = Logger.getLogger(ConfigBundleInterceptor.class.getName());

/**
* Method initialises class fields from configuration.
*/
Expand Down Expand Up @@ -72,6 +75,8 @@ public Object loadConfiguration(InvocationContext ic) throws Exception {
m.invoke(target, value.get());
}

deployWatcher(target, targetClass, m, getKeyName(targetClass, m.getName()));

} else if (m.getParameters()[0].getType().equals(Boolean.class)) {

Optional<Boolean> value = configurationUtil.getBoolean(getKeyName(targetClass, m.getName()));
Expand All @@ -80,6 +85,8 @@ public Object loadConfiguration(InvocationContext ic) throws Exception {
m.invoke(target, value.get());
}

deployWatcher(target, targetClass, m, getKeyName(targetClass, m.getName()));

} else if (m.getParameters()[0].getType().equals(Float.class)) {

Optional<Float> value = configurationUtil.getFloat(getKeyName(targetClass, m.getName()));
Expand All @@ -88,6 +95,8 @@ public Object loadConfiguration(InvocationContext ic) throws Exception {
m.invoke(target, value.get());
}

deployWatcher(target, targetClass, m, getKeyName(targetClass, m.getName()));

} else if (m.getParameters()[0].getType().equals(Double.class)) {

Optional<Double> value = configurationUtil.getDouble(getKeyName(targetClass, m.getName()));
Expand All @@ -96,6 +105,8 @@ public Object loadConfiguration(InvocationContext ic) throws Exception {
m.invoke(target, value.get());
}

deployWatcher(target, targetClass, m, getKeyName(targetClass, m.getName()));

} else if (m.getParameters()[0].getType().equals(Integer.class)) {

Optional<Integer> value = configurationUtil.getInteger(getKeyName(targetClass, m.getName()));
Expand All @@ -104,6 +115,8 @@ public Object loadConfiguration(InvocationContext ic) throws Exception {
m.invoke(target, value.get());
}

deployWatcher(target, targetClass, m, getKeyName(targetClass, m.getName()));

}
}
}
Expand Down Expand Up @@ -189,6 +202,70 @@ private String camelCaseToHyphenCase(String s) {
private boolean targetClassIsProxied(Class targetClass) {
return targetClass.getCanonicalName().contains("$Proxy");
}

/**
* Subscribes to an event dispatcher and starts a watch for a given key.
*
* @param target target instance
* @param targetClass target class
* @param method method to invoke
* @param watchedKey watched key
* @throws Exception
*/
private void deployWatcher(Object target, Class targetClass, Method method, String watchedKey) throws Exception {

String setter = method.getName();

Field field = targetClass.getDeclaredField(setterToField(setter));
ConfigValue fieldAnnotation = null;
if (field != null) {
fieldAnnotation = field.getAnnotation(ConfigValue.class);
}

if (fieldAnnotation != null && fieldAnnotation.watch() == true) {

ConfigurationUtil.getInstance().subscribe(watchedKey, (key, value) -> {

if (watchedKey == key) {
try {
if (String.class.equals(method.getParameters()[0].getType())) {
method.invoke(target, value);
} else if (Boolean.class.equals(method.getParameters()[0].getType())) {
method.invoke(target, Boolean.parseBoolean(value));
} else if (Float.class.equals(method.getParameters()[0].getType())) {
try {
method.invoke(target, Float.parseFloat(value));
} catch (NumberFormatException e) {
log.severe("Exception while storing new value: Number format exception. " +
"Expected: Float. Value: " + value);
}
} else if (Double.class.equals(method.getParameters()[0].getType())) {
try {
method.invoke(target, Double.parseDouble(value));
} catch (NumberFormatException e) {
log.severe("Exception while storing new value: Number format exception. Expected:" +
" Double. Value: " + value);
}
} else if (Integer.class.equals(method.getParameters()[0].getType())) {
try {
method.invoke(target, Integer.parseInt(value));
} catch (NumberFormatException e) {
log.severe("Exception while storing new value: Number format exception. Expected:" +
" Integer. Value: " + value);
}
}
} catch (IllegalAccessException e) {
log.severe("Illegal access exception: " + e.toString());
} catch (InvocationTargetException e) {
log.severe("Invocation target exception: " + e.toString());
}

}
});

}

}
}


Loading