Skip to content

Commit

Permalink
PAYARA-2545 Tidy up of some of the imports and additional comments
Browse files Browse the repository at this point in the history
  • Loading branch information
smillidge committed Mar 30, 2018
1 parent 2fd1d8a commit be782cb
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

import fish.payara.nucleus.microprofile.config.spi.PayaraConfig;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
Expand All @@ -51,7 +50,6 @@
import javax.enterprise.inject.spi.*;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.inject.Provider;
Expand Down Expand Up @@ -137,29 +135,35 @@ public void addDynamicProducers(@Observes AfterBeanDiscovery event, BeanManager
}
}

// we have the method
if (beanAttr != null) {
HashSet<Type> types = new HashSet<>();
types.addAll(((PayaraConfig) config).getConverterTypes());
// add primitives explictly
// add String explictly
types.add(String.class);

// go through each type which has a converter either custom or built in
// create a bean with a Producer method using the bean factory and with custom bean attributes
// also override the set of types depending on the type of the converter
for (final Type converterType : types) {
// go through each type which has a converter
// create a bean with a Producer method using the bean factory and with custom bean attributes
Bean<?> bean = bm.createBean(new TypesBeanAttributes<Object>(beanAttr) {

// overrides the bean types to return the type registered for a Converter
// overrides the bean types to return the types registered for a Converter
@Override
public Set<Type> getTypes() {
HashSet<Type> result = new HashSet<>();
// add the type that the converter converts
result.add(converterType);

// also indicate support array of the type.
if (converterType instanceof Class) {
Object array = Array.newInstance((Class)converterType, 0);
result.add(array.getClass());
}

// ok we will have to do specific code for the primitive converters
// ok we will have to do specific code for wrappers
// for each wrapper indicate we can also convert the primitive
// and we can convert the primitive array
if (converterType == Long.class) {
result.add(long.class);
result.add((new long[0]).getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public Config getConfig() {
return new InjectedPayaraConfig(ConfigProvider.getConfig(), im.getCurrentInvocation().getAppName());
}

/**
* Producer method for Sets
* @param <T> Type
* @param ip Injection Point
* @return
*/
@Produces
@ConfigProperty
public <T> Set<T> getSetProperty(InjectionPoint ip) {
Expand All @@ -98,6 +104,12 @@ public <T> Set<T> getSetProperty(InjectionPoint ip) {
return result;
}

/**
* Producer method for Lists
* @param <T> Type
* @param ip Injection Point
* @return
*/
@Produces
@ConfigProperty
public <T> List<T> getListProperty(InjectionPoint ip) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public Boolean convert(String value) {
case "y":
case "on":
result = true;
break;
default:
result = false;
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.config.spi.Converter;

/**
Expand All @@ -57,14 +58,15 @@ public CommonSenseConverter(Class clazz) {
}

@Override
public Object convert(String string) {
public Object convert(String value) {
if (value == null || value.equals(ConfigProperty.UNCONFIGURED_VALUE)) return null;
Object result = null;
result = convertViaConstructor(string);
result = convertViaConstructor(value);
if (result == null) {
result = convertViaValueOf(string);
result = convertViaValueOf(value);
}
if (result == null) {
result = convertViaCharSequence(string);
result = convertViaCharSequence(value);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*/
package fish.payara.nucleus.microprofile.config.converters;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.config.spi.Converter;

/**
Expand All @@ -48,8 +49,9 @@
public class StringConverter implements Converter<String>{

@Override
public String convert(String string) {
return string;
public String convert(String value) {
if (value == null || value.equals(ConfigProperty.UNCONFIGURED_VALUE)) return null;
return value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,50 +72,6 @@ public PayaraConfig(List<ConfigSource> configSources, Map<Type,Converter> conver
this.converters.putAll(convertersMap);
Collections.sort(configSources, new ConfigSourceComparator());
}

public <T> List<T> getListValues(String propertyName, String defaultValue, Class<T> elementType) {
String value = getValue(propertyName);
if (value == null) {
value = defaultValue;
}
if (value == null) {
throw new NoSuchElementException("Unable to find property with name " + propertyName);
}
String keys[] = splitValue(value);
List<T> result = new ArrayList<>(keys.length);
for (String key : keys) {
result.add(convertString(key, elementType));
}
return result;
}

public <T> Set<T> getSetValues(String propertyName, String defaultValue, Class<T> elementType) {
String value = getValue(propertyName);
if (value == null) {
value = defaultValue;
}
if (value == null) {
throw new NoSuchElementException("Unable to find property with name " + propertyName);
}
String keys[] = splitValue(value);
Set<T> result = new HashSet<>(keys.length);
for (String key : keys) {
result.add(convertString(key, elementType));
}
return result;
}


public <T> T getValue(String propertyName, String defaultValue, Class<T> propertyType) {
String result = getValue(propertyName);
if (result == null) {
result = defaultValue;
}
if (result == null) {
throw new NoSuchElementException("Unable to find property with name " + propertyName);
}
return convertString(result, propertyType);
}

@Override
public <T> T getValue(String propertyName, Class<T> propertyType) {
Expand Down Expand Up @@ -158,9 +114,52 @@ public Iterable<ConfigSource> getConfigSources() {

public Set<Type> getConverterTypes() {
return converters.keySet();
}

public <T> List<T> getListValues(String propertyName, String defaultValue, Class<T> elementType) {
String value = getValue(propertyName);
if (value == null) {
value = defaultValue;
}
if (value == null) {
throw new NoSuchElementException("Unable to find property with name " + propertyName);
}
String keys[] = splitValue(value);
List<T> result = new ArrayList<>(keys.length);
for (String key : keys) {
result.add(convertString(key, elementType));
}
return result;
}

public <T> Set<T> getSetValues(String propertyName, String defaultValue, Class<T> elementType) {
String value = getValue(propertyName);
if (value == null) {
value = defaultValue;
}
if (value == null) {
throw new NoSuchElementException("Unable to find property with name " + propertyName);
}
String keys[] = splitValue(value);
Set<T> result = new HashSet<>(keys.length);
for (String key : keys) {
result.add(convertString(key, elementType));
}
return result;
}


public <T> T getValue(String propertyName, String defaultValue, Class<T> propertyType) {
String result = getValue(propertyName);
if (result == null) {
result = defaultValue;
}
if (result == null) {
throw new NoSuchElementException("Unable to find property with name " + propertyName);
}
return convertString(result, propertyType);
}

private String getValue(String propertyName) {
String result = null;
for (ConfigSource configSource : configSources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,6 @@ public <T> ConfigBuilder withConverter(Class<T> type, int i, Converter<T> cnvrtr
}
return this;
}

private void addConvertersToMap(List<Converter> convertersList) {
for (Converter converter : convertersList) {
Type cType = getTypeForConverter(converter);
if (cType != null) {
Converter old = converters.get(cType);
if (old != null) {
if (getPriority(converter) > getPriority(old)) {
this.converters.put(cType, converter);
}
}else {
this.converters.put(cType, converter);
}
}
}
}

public static Type getTypeForConverter(Converter converter) {
// add each converter to the map for later lookup
Expand All @@ -169,5 +153,22 @@ private int getPriority(Converter converter) {
}
return result;
}


private void addConvertersToMap(List<Converter> convertersList) {
for (Converter converter : convertersList) {
Type cType = getTypeForConverter(converter);
if (cType != null) {
Converter old = converters.get(cType);
if (old != null) {
if (getPriority(converter) > getPriority(old)) {
this.converters.put(cType, converter);
}
}else {
this.converters.put(cType, converter);
}
}
}
}

}

0 comments on commit be782cb

Please sign in to comment.