Repository: knox Updated Branches: refs/heads/master 654f31d63 -> 0d9fa0dac
http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptor.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptor.java new file mode 100755 index 0000000..92a6cca --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptor.java @@ -0,0 +1,30 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.spi.AbstractConfigurationAdapterDescriptor; + +import java.util.Map; + +public class BeanConfigurationAdapterDescriptor extends AbstractConfigurationAdapterDescriptor { + + public BeanConfigurationAdapterDescriptor() { + add( Object.class, BeanConfigurationAdapter.class ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/ConfigurationAdapterFactory.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/ConfigurationAdapterFactory.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/ConfigurationAdapterFactory.java new file mode 100755 index 0000000..73cd97b --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/ConfigurationAdapterFactory.java @@ -0,0 +1,132 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.impl; + + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; +import org.apache.hadoop.gateway.config.ConfigurationException; +import org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor; + +import java.lang.reflect.Constructor; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.ServiceLoader; + +public class ConfigurationAdapterFactory { + + private static Map<Class<?>, Class<? extends ConfigurationAdapter>> ADAPTERS = null; + + private static synchronized Map<Class<?>, Class<? extends ConfigurationAdapter>> getAdapters() { + if( ADAPTERS == null ) { + loadAdapters(); + } + return ADAPTERS; + } + + private static void loadAdapters() { + Map<Class<?>, Class<? extends ConfigurationAdapter>> all = + new HashMap<Class<?>, Class<? extends ConfigurationAdapter>>(); + ServiceLoader<ConfigurationAdapterDescriptor> loader = ServiceLoader.load( ConfigurationAdapterDescriptor.class ); + if( loader != null ) { + Iterator<ConfigurationAdapterDescriptor> i = loader.iterator(); + if( i != null ) { + while( i.hasNext() ) { + ConfigurationAdapterDescriptor descriptor = i.next(); + Map<Class<?>, Class<? extends ConfigurationAdapter>> add = descriptor.providedConfigurationAdapters(); + if( add != null ) { + all.putAll( add ); + } + } + } + } + ADAPTERS = Collections.unmodifiableMap( all ); + } + + public static ConfigurationAdapter get( Object config ) throws ConfigurationException { + if( config == null ) { + throw new NullPointerException( "Configuration adapter instantiation impossible for null config object." ); + } + try { + Map<Class<?>, Class<? extends ConfigurationAdapter>> adapters = getAdapters(); + Class configType = config.getClass(); + Class adapterType = findAdapterTypeForConfigTypeOrParent( adapters, configType ); + if( adapterType == null ) { + throw new ConfigurationException( "No configuration adapter found for config type " + configType.getName() ); + } + Constructor c = findConstructorForConfigType( adapterType, configType ); + if( !c.isAccessible() ) { + c.setAccessible( true ); + } + Object adapter = c.newInstance( config ); + return ConfigurationAdapter.class.cast( adapter ); + } catch( ConfigurationException e ) { + throw e; + } catch( Exception e ) { + throw new ConfigurationException( "Configuration adapter instantiation failed.", e ); + } + } + + public static Constructor findConstructorForConfigType( Class<?> adapterType, Class<?> configType ) throws NoSuchMethodException { + Constructor constructor = null; + Constructor[] constructors = adapterType.getConstructors(); + for( Constructor candidate : constructors ) { + Class<?>[] paramTypes = candidate.getParameterTypes(); + if( paramTypes.length == 1 ) { + Class<?> paramType = paramTypes[0]; + if( paramType.isAssignableFrom( configType ) ) { + constructor = candidate; + break; + } + } + } + if( constructor == null ) { + throw new NoSuchMethodException( "No constructor for " + adapterType.getName() + " that will accept " + configType.getName() ); + } + return constructor; + } + + public static Class<? extends ConfigurationAdapter> findAdapterTypeForConfigTypeOrParent( + Map<Class<?>, Class<? extends ConfigurationAdapter>> adapters, Class<?> configType ) { + Class<? extends ConfigurationAdapter> adapterType = null; + while( configType != null ) { + adapterType = findAdapterTypeForConfigType( adapters, configType ); + if( adapterType != null ) { + break; + } + configType = configType.getSuperclass(); + } + return adapterType; + } + + public static Class<? extends ConfigurationAdapter> findAdapterTypeForConfigType( + Map<Class<?>, Class<? extends ConfigurationAdapter>> adapters, Class<?> configType ) { + Class<? extends ConfigurationAdapter> adapterType = adapters.get( configType ); + if( adapterType == null ) { + for( Class interfaceType : configType.getInterfaces() ) { + adapterType = findAdapterTypeForConfigTypeOrParent( adapters, interfaceType ); + if( adapterType != null ) { + break; + } + } + } + return adapterType; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationBinding.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationBinding.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationBinding.java new file mode 100755 index 0000000..5629bc1 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationBinding.java @@ -0,0 +1,29 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.ConfigurationBinding; + +public class DefaultConfigurationBinding implements ConfigurationBinding { + + @Override + public String getConfigurationName( String name ) { + return name; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationInjector.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationInjector.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationInjector.java new file mode 100755 index 0000000..8b86ba1 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/DefaultConfigurationInjector.java @@ -0,0 +1,224 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.impl; + +import org.apache.commons.beanutils.ConvertUtilsBean2; +import org.apache.hadoop.gateway.config.*; +import org.apache.hadoop.gateway.config.spi.ConfigurationInjector; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +public class DefaultConfigurationInjector implements ConfigurationInjector { + + private static ConvertUtilsBean2 DEFAULT_CONVERTER = new ConvertUtilsBean2(); + + @Override + public void configure( Object target, ConfigurationAdapter adapter, ConfigurationBinding binding ) + throws ConfigurationException { + Class type = target.getClass(); + while( type != null ) { + injectClass( type, target, adapter, binding ); + type = type.getSuperclass(); + } + } + + private void injectClass( Class type, Object target, ConfigurationAdapter config, ConfigurationBinding binding ) + throws ConfigurationException { + Field[] fields = type.getDeclaredFields(); + for( Field field : fields ) { + injectFieldValue( field, target, config, binding ); + } + Method[] methods = type.getDeclaredMethods(); + for( Method method : methods ) { + injectMethodValue( method, target, config, binding ); + } + } + + private void injectFieldValue( Field field, Object target, ConfigurationAdapter adapter, ConfigurationBinding binding ) + throws ConfigurationException { + Configure annotation = field.getAnnotation( Configure.class ); + if( annotation != null ) { + Alias alias = field.getAnnotation( Alias.class ); + String name = getConfigName( field, alias ); + String bind = getBindName( target, name, binding ); + Object value = retrieveValue( target, bind, name, field.getType(), adapter, binding ); + if( value == null ) { + Optional optional = field.getAnnotation( Optional.class ); + if( optional == null ) { + throw new ConfigurationException( String.format( + "Failed to find configuration for %s bound to %s of %s via %s", + bind, name, target.getClass().getName(), adapter.getClass().getName() ) ); + } + } else { + try { + if( !field.isAccessible() ) { + field.setAccessible( true ); + } + field.set( target, value ); + } catch( Exception e ) { + throw new ConfigurationException( String.format( + "Failed to inject field configuration property %s of %s", + name, target.getClass().getName() ), e ); + } + } + } + } + + private void injectMethodValue( Method method, Object target, ConfigurationAdapter adapter, ConfigurationBinding binding ) + throws ConfigurationException { + Configure methodTag = method.getAnnotation( Configure.class ); + if( methodTag != null ) { + Alias aliasTag = method.getAnnotation( Alias.class ); + String methodName = getConfigName( method, aliasTag ); + Class[] argTypes = method.getParameterTypes(); + Object[] args = new Object[ argTypes.length ]; + Annotation[][] argTags = method.getParameterAnnotations(); + for( int i=0; i<argTypes.length; i++ ) { + String argName = getConfigName( methodName, argTags[i] ); + String bndName = getBindName( target, argName, binding ); + Object argValue = retrieveValue( target, bndName, argName, argTypes[i], adapter, binding ); + if( argValue == null ) { + Default defTag = findAnnotation( argTags[i], Default.class ); + if( defTag != null ) { + String strValue = defTag.value(); + argValue = convertValue( target, argName, strValue, argTypes[i] ); + } else { + throw new ConfigurationException( String.format( + "Failed to find configuration for %s of %s via %s", + bndName, argName, target.getClass().getName(), adapter.getClass().getName() ) ); + } + } + args[ i ] = argValue; + } + if( !method.isAccessible() ) { + method.setAccessible( true ); + } + try { + method.invoke( target, args ); + } catch( Exception e ) { + throw new ConfigurationException( String.format( + "Failed to inject method configuration via %s of %s", + methodName, target.getClass().getName() ), e ); + } + } + } + + private Object convertValue( Object target, String name, Object strValue, Class<?> type ) { + Object objValue = null; + try { + objValue = DEFAULT_CONVERTER.convert( strValue, type ); + } catch( Exception e ) { + throw new ConfigurationException( String.format( + "Failed to convert configuration for %s of %s to %s", + name, target.getClass().getName(), type.getName() ), e ); + } + return objValue; + } + + private Object retrieveValue( Object target, String bind, String name, Class<?> type, ConfigurationAdapter adapter, ConfigurationBinding binding ) { + Object value; + try { + value = adapter.getConfigurationValue( bind ); + } catch( Exception e ) { + throw new ConfigurationException( String.format( + "Failed to retrieve configuration for %s bound to %s of %s via %s", + bind, name, target.getClass().getName(), adapter.getClass().getName() ), e ); + } + value = convertValue( target, name, value, type ); + return value; + } + + private <T extends Annotation> T findAnnotation( Annotation[] annotations, Class<T> type ) { + T found = null; + for( Annotation current : annotations ) { + if( type.isAssignableFrom( current.getClass() ) ) { + found = (T)current; + break; + } + } + return found; + } + + private static String pickName( String implied, Alias explicit ) { + String name = implied; + if( explicit != null ) { + String tagValue = explicit.value().trim(); + if( tagValue.length() > 0 ) { + name = tagValue; + } + } + return name; + } + + private static String getBindName( Object target, String name, ConfigurationBinding binding ) { + String bind = null; + try { + bind = binding.getConfigurationName( name ); + } catch( Exception e ) { + throw new ConfigurationException( String.format( + "Failed to bind configuration for %s of %s via %s", + name, target.getClass().getName(), binding.getClass().getName() ), e ); + } + if( bind == null ) { + bind = name; + } + return bind; + } + + private static String getConfigName( Field field, Alias tag ) { + return pickName( field.getName(), tag ); + } + + private static String getConfigName( String name, Annotation[] tags ) { + if( tags != null ) { + for( Annotation tag : tags ) { + if( tag != null && tag instanceof Alias ) { + Alias aliasTag = Alias.class.cast( tag ); + String aliasValue = aliasTag.value().trim(); + if( aliasValue.length() > 0 ) { + name = aliasValue; + break; + } + } + } + } + return name; + } + + private static String getConfigName( Method method, Alias tag ) { + return pickName( getConfigName( method ), tag ); + } + + private static String getConfigName( Method method ) { + String methodName = method.getName(); + StringBuilder name = new StringBuilder( methodName.length() ); + if( methodName != null && + methodName.length() > 3 && + methodName.startsWith( "set" ) && + Character.isUpperCase( methodName.charAt( 3 ) ) ) { + name.append( methodName.substring( 3 ) ); + name.setCharAt( 0, Character.toLowerCase( name.charAt( 0 ) ) ); + } else { + name.append( name ); + } + return name.toString(); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapter.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapter.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapter.java new file mode 100755 index 0000000..d0556f4 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapter.java @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; + +import java.util.Map; + +public class MapConfigurationAdapter implements ConfigurationAdapter { + + private Map config; + + public MapConfigurationAdapter( Map map ) { + this.config = map; + } + + @Override + public Object getConfigurationValue( String name ) { + return config.get( name ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapterDescriptor.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapterDescriptor.java new file mode 100755 index 0000000..4099483 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MapConfigurationAdapterDescriptor.java @@ -0,0 +1,30 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.spi.AbstractConfigurationAdapterDescriptor; + +import java.util.Map; + +public class MapConfigurationAdapterDescriptor extends AbstractConfigurationAdapterDescriptor { + + public MapConfigurationAdapterDescriptor() { + add( Map.class, MapConfigurationAdapter.class ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MappedConfigurationBinding.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MappedConfigurationBinding.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MappedConfigurationBinding.java new file mode 100755 index 0000000..161869f --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/MappedConfigurationBinding.java @@ -0,0 +1,38 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.ConfigurationBinding; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class MappedConfigurationBinding implements ConfigurationBinding { + + private Map<String,String> map = new ConcurrentHashMap<String, String>(); + + public void bind( String targetName, String sourceName ) { + map.put( targetName, sourceName ); + } + + @Override + public String getConfigurationName( String name ) { + return map.get( name ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapter.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapter.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapter.java new file mode 100755 index 0000000..6240d44 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapter.java @@ -0,0 +1,38 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; +import org.apache.hadoop.gateway.config.ConfigurationException; + +import java.util.Properties; + +public class PropertiesConfigurationAdapter implements ConfigurationAdapter { + + private Properties properties; + + public PropertiesConfigurationAdapter( Properties properties ) { + this.properties = properties; + } + + @Override + public Object getConfigurationValue( String name ) throws ConfigurationException { + return properties.getProperty( name ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapterDescriptor.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapterDescriptor.java new file mode 100755 index 0000000..b93e49a --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/impl/PropertiesConfigurationAdapterDescriptor.java @@ -0,0 +1,30 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.spi.AbstractConfigurationAdapterDescriptor; + +import java.util.Properties; + +public class PropertiesConfigurationAdapterDescriptor extends AbstractConfigurationAdapterDescriptor { + + public PropertiesConfigurationAdapterDescriptor() { + add( Properties.class, PropertiesConfigurationAdapter.class ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/AbstractConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/AbstractConfigurationAdapterDescriptor.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/AbstractConfigurationAdapterDescriptor.java new file mode 100755 index 0000000..5372911 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/AbstractConfigurationAdapterDescriptor.java @@ -0,0 +1,42 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.spi; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; + +import java.util.HashMap; +import java.util.Map; + +public abstract class AbstractConfigurationAdapterDescriptor implements ConfigurationAdapterDescriptor { + + private static Map<Class<?>, Class<? extends ConfigurationAdapter>> ADAPTERS = + new HashMap<Class<?>, Class<? extends ConfigurationAdapter>>(); + + protected AbstractConfigurationAdapterDescriptor() { + } + + protected void add( Class<?> configType, Class<? extends ConfigurationAdapter> adapterType ) { + ADAPTERS.put( configType, adapterType ); + } + + @Override + public Map<Class<?>, Class<? extends ConfigurationAdapter>> providedConfigurationAdapters() { + return ADAPTERS; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationAdapterDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationAdapterDescriptor.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationAdapterDescriptor.java new file mode 100755 index 0000000..3e304d4 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationAdapterDescriptor.java @@ -0,0 +1,28 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.spi; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; + +import java.util.Map; + +public interface ConfigurationAdapterDescriptor { + + Map<Class<?>,Class<? extends ConfigurationAdapter>> providedConfigurationAdapters(); + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationInjector.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationInjector.java b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationInjector.java new file mode 100755 index 0000000..0bbd1d9 --- /dev/null +++ b/gateway-util-configinjector/src/main/java/org/apache/hadoop/gateway/config/spi/ConfigurationInjector.java @@ -0,0 +1,27 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.spi; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; +import org.apache.hadoop.gateway.config.ConfigurationBinding; + +public interface ConfigurationInjector { + + void configure(Object target, ConfigurationAdapter adapter, ConfigurationBinding binding); + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor b/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor new file mode 100755 index 0000000..5a240e0 --- /dev/null +++ b/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor @@ -0,0 +1,20 @@ +########################################################################## +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +########################################################################## +org.apache.hadoop.gateway.config.impl.MapConfigurationAdapterDescriptor +org.apache.hadoop.gateway.config.impl.PropertiesConfigurationAdapterDescriptor +org.apache.hadoop.gateway.config.impl.BeanConfigurationAdapterDescriptor \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationInjector ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationInjector b/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationInjector new file mode 100755 index 0000000..e3a0d65 --- /dev/null +++ b/gateway-util-configinjector/src/main/resources/META-INF/services/org.apache.hadoop.gateway.config.spi.ConfigurationInjector @@ -0,0 +1,18 @@ +########################################################################## +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +########################################################################## +org.apache.hadoop.gateway.config.impl.DefaultConfigurationInjector \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/AdapterSampleTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/AdapterSampleTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/AdapterSampleTest.java new file mode 100755 index 0000000..f3e392e --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/AdapterSampleTest.java @@ -0,0 +1,57 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config; + +import org.junit.Test; + +import java.util.Hashtable; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class AdapterSampleTest { + + public static class Target { + @Configure + private String username = null; + } + + public static class Adapter implements ConfigurationAdapter { + private Hashtable config; + public Adapter( Hashtable config ) { + this.config = config; + } + @Override + public Object getConfigurationValue( String name ) throws ConfigurationException { + Object value = config.get( name.toUpperCase() ); + return value == null ? null : value.toString(); + } + } + + static Hashtable config = new Hashtable(); + static{ config.put( "USERNAME", "somebody" ); } + + @Test + public void sample() { + Target target = new Target(); + Adapter adapter = new Adapter( config ); + ConfigurationInjectorBuilder.configuration().target( target ).source( adapter ).inject(); + assertThat( target.username, is( "somebody" ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/FuncTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/FuncTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/FuncTest.java new file mode 100755 index 0000000..f4898ac --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/FuncTest.java @@ -0,0 +1,379 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config; + +import org.apache.hadoop.gateway.config.impl.MappedConfigurationBinding; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.core.AllOf.allOf; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.fail; + +import static org.apache.hadoop.gateway.config.ConfigurationInjectorBuilder.*; + +public class FuncTest { + + public static class TestBean { + @Configure + String stringMember = "stringDefault"; + + @Configure + int intMember = 1; + + @Configure + Integer integerMember = Integer.valueOf( 1 ); + + @Configure + public void setStringProp( String s ) { + stringPropField = s; + } + protected String stringPropField = "stringDefault"; + + @Configure + @Alias("altStringProp") + public void setNamedStringProp( String s ) { + stringPropFieldAlt = s; + } + protected String stringPropFieldAlt = "stringDefault"; + + @Configure + public void setNamedArgMethod( @Configure @Alias("altArgStringProp") String s ) { + stringPropFieldAltArg = s; + } + protected String stringPropFieldAltArg = "stringDefault"; + + @Configure + public void setMultiArgs( + @Configure @Alias("multiArg1") String s, + @Configure @Alias("multiArg2") Integer i, + @Configure @Alias("multiArg3") int n ) { + multiArgStringField = s; + multiArgIntegerField = i; + multiArgIntField = n; + } + String multiArgStringField = "default"; + Integer multiArgIntegerField = 0; + int multiArgIntField = 0; + + } + + @Test + public void testMapOfStrings() { + + Map<String,String> testConfig = new HashMap<String,String>(); + testConfig.put( "stringMember", "stringValue" ); + testConfig.put( "intMember", "2" ); + testConfig.put( "integerMember", "2" ); + testConfig.put( "stringProp", "stringValue" ); + testConfig.put( "altStringProp", "stringValue" ); + testConfig.put( "altArgStringProp", "stringValue" ); + testConfig.put( "multiArg1", "stringValue" ); + testConfig.put( "multiArg2", "42" ); + testConfig.put( "multiArg3", "42" ); + + TestBean testBean = new TestBean(); + + configuration().target( testBean ).source( testConfig ).inject(); + + assertThat( testBean.stringMember, is( "stringValue" ) ); + assertThat( testBean.intMember, is( 2 ) ); + assertThat( testBean.integerMember, is( new Integer(2) ) ); + assertThat( testBean.stringPropField, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAlt, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAltArg, is( "stringValue" ) ); + assertThat( testBean.multiArgStringField, is( "stringValue" ) ); + assertThat( testBean.multiArgIntegerField, is( 42 ) ); + assertThat( testBean.multiArgIntField, is( 42 ) ); + } + + @Test + public void testProperties() { + + Properties testConfig = new Properties(); + testConfig.put( "stringMember", "stringValue" ); + testConfig.put( "intMember", "2" ); + testConfig.put( "integerMember", "2" ); + testConfig.put( "stringProp", "stringValue" ); + testConfig.put( "altStringProp", "stringValue" ); + testConfig.put( "altArgStringProp", "stringValue" ); + testConfig.put( "multiArg1", "stringValue" ); + testConfig.put( "multiArg2", "42" ); + testConfig.put( "multiArg3", "42" ); + + TestBean testBean = new TestBean(); + + configuration().target( testBean ).source( testConfig ).inject(); + + assertThat( testBean.stringMember, is( "stringValue" ) ); + assertThat( testBean.intMember, is( 2 ) ); + assertThat( testBean.integerMember, is( new Integer(2) ) ); + assertThat( testBean.stringPropField, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAlt, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAltArg, is( "stringValue" ) ); + assertThat( testBean.multiArgStringField, is( "stringValue" ) ); + assertThat( testBean.multiArgIntegerField, is( 42 ) ); + assertThat( testBean.multiArgIntField, is( 42 ) ); + } + + public static class TestAdapter implements ConfigurationAdapter { + + private Map<String,String> config; + + public TestAdapter( Map<String,String> config ) { + this.config = config; + } + + @Override + public String getConfigurationValue( String name ) { + return config.get( name ); + } + + } + + @Test + public void testExplicitProvider() { + + Map<String,String> testConfig = new HashMap<String,String>(); + testConfig.put( "stringMember", "stringValue" ); + testConfig.put( "intMember", "2" ); + testConfig.put( "integerMember", "2" ); + testConfig.put( "stringProp", "stringValue" ); + testConfig.put( "altStringProp", "stringValue" ); + testConfig.put( "altArgStringProp", "stringValue" ); + testConfig.put( "multiArg1", "stringValue" ); + testConfig.put( "multiArg2", "42" ); + testConfig.put( "multiArg3", "42" ); + + TestBean testBean = new TestBean(); + + configuration().target( testBean ).source( new TestAdapter( testConfig ) ).inject(); + + assertThat( testBean.stringMember, is( "stringValue" ) ); + assertThat( testBean.intMember, is( 2 ) ); + assertThat( testBean.integerMember, is( new Integer(2) ) ); + assertThat( testBean.stringPropField, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAlt, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAltArg, is( "stringValue" ) ); + assertThat( testBean.multiArgStringField, is( "stringValue" ) ); + assertThat( testBean.multiArgIntegerField, is( 42 ) ); + assertThat( testBean.multiArgIntField, is( 42 ) ); + } + + @Test + public void testMapOfObjects() { + + Map<Object,Object> testConfig = new HashMap<Object,Object>(); + testConfig.put( "stringMember", "stringValue" ); + testConfig.put( "intMember", 42 ); + testConfig.put( "integerMember", new Integer(42) ); + testConfig.put( "stringProp", "stringValue" ); + testConfig.put( "altStringProp", "stringValue" ); + testConfig.put( "altArgStringProp", "stringValue" ); + testConfig.put( "multiArg1", "stringValue" ); + testConfig.put( "multiArg2", new Integer(42) ); + testConfig.put( "multiArg3", "42" ); + + TestBean testBean = new TestBean(); + + configuration().target( testBean ).source( testConfig ).inject(); + + assertThat( testBean.stringMember, is( "stringValue" ) ); + assertThat( testBean.intMember, is( 42 ) ); + assertThat( testBean.integerMember, is( new Integer(42) ) ); + assertThat( testBean.stringPropField, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAlt, is( "stringValue" ) ); + assertThat( testBean.stringPropFieldAltArg, is( "stringValue" ) ); + assertThat( testBean.multiArgStringField, is( "stringValue" ) ); + assertThat( testBean.multiArgIntegerField, is( 42 ) ); + assertThat( testBean.multiArgIntField, is( 42 ) ); + } + + public class Target { + @Configure @Alias("user.name") + private String user; + } + + public class Adapter implements ConfigurationAdapter { + @Override + public Object getConfigurationValue( String name ) throws ConfigurationException { + return System.getProperty( name ); + } + } + + @Test + public void testFactoryConfigurationDirect() { + Target target = new Target(); + configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.user, is( System.getProperty( "user.name" ) ) ); + } + + @Test + public void testFactoryConfigurationAdapter() { + Target target = new Target(); + configuration().target( target ).source( new Adapter() ).inject(); + assertThat( target.user, is( System.getProperty( "user.name" ) ) ); + } + + @Test + public void testMissingRequiredFieldConfiguration() { + class RequiredFieldTarget { + @Configure + private String required; + } + RequiredFieldTarget target = new RequiredFieldTarget(); + try { + configuration().target( target ).source( System.getProperties() ).inject(); + fail( "Expected an exception because the configuration values could not be populated." ); + } catch ( ConfigurationException e ) { + assertThat( e.getMessage(), allOf(containsString("Failed"),containsString( "find" ),containsString( "required" )) ); + } + } + + @Test + public void testMissingOptionalFieldConfiguration() { + class OptionalFieldTarget { + @Configure + @Optional + private String optional = "default"; + } + OptionalFieldTarget target = new OptionalFieldTarget(); + configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.optional, is("default") ); + } + + @Test + public void testMissingRequiredConfigurationParameter() { + class Target { + private String field; + @Configure + public void setRequired(String value) { + field = value; + } + } + Target target = new Target(); + try { + configuration().target( target ).source( System.getProperties() ).inject(); + fail( "Expected an exception because the configuration values could not be populated." ); + } catch ( ConfigurationException e ) { + assertThat( e.getMessage(), allOf(containsString("Failed"),containsString( "find" ),containsString( "required" )) ); + } + } + + @Test + public void testMissingRequiredConfigurationParameterWithDefault() { + class Target { + private String field; + @Configure + public void setRequired(@Default("default")String value) { + field = value; + } + } + Target target = new Target(); + configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.field, is( "default" ) ); + } + + @Test + public void testTwoMissingRequiredConfigurationParameterWithDefault() { + class Target { + private String field1; + private String field2; + @Configure + public void setRequired(@Default("default1")String value1, @Default("default2")String value2) { + field1 = value1; + field2 = value2; + } + } + Target target = new Target(); + configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.field1, is( "default1" ) ); + assertThat( target.field2, is("default2") ); + } + + @Test + public void testFieldBinding() { + class Target { + @Configure + private String user; + } + class Binding extends MappedConfigurationBinding { + Binding() { + bind("user","user.name"); + } + } + Target target = new Target(); + Properties source = System.getProperties(); + ConfigurationBinding binding = new Binding(); + configuration().target( target ).source( source ).binding( binding ).inject(); + assertThat( target.user, is(System.getProperty("user.name"))); + + } + + @Test + public void testFieldBindingUsingBuilderBinding() { + class Target { + @Configure + private String user; + } + Target target = new Target(); + Properties source = System.getProperties(); + configuration().target( target ).source( source ).bind( "user", "user.name" ).inject(); + assertThat( target.user, is(System.getProperty("user.name"))); + + } + + @Test + public void testFieldBindingUsingBuilderBindingFactory() { + class Target { + @Configure + private String user; + } + Target target = new Target(); + Properties source = System.getProperties(); + ConfigurationBinding binding = configuration().bind( "user", "user.name" ).binding(); + configuration().target( target ).source( source ).binding( binding ).inject(); + assertThat( target.user, is( System.getProperty( "user.name" ) ) ); + + } + + public static class UserBean { + public String getPrincipal() { + return "test-user"; + } + } + + @Test + public void testBeanAdapter() { + Target target = new Target(); + UserBean bean = new UserBean(); + configuration() + .target( target ) + .source( bean ) + .bind( "user.name", "principal" ) + .inject(); + assertThat( target.user, is( "test-user" ) ); + + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapFieldSampleTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapFieldSampleTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapFieldSampleTest.java new file mode 100755 index 0000000..b9336b5 --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapFieldSampleTest.java @@ -0,0 +1,45 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class MapFieldSampleTest { + + public static class Target { + @Configure + private int retryLimit = 3; + } + + static Map<String,String> config = new HashMap<String,String>(); + static { config.put( "retryLimit", "5" ); } + + @Test + public void sample() { + Target target = new Target(); + ConfigurationInjectorBuilder.configuration().target( target ).source( config ).inject(); + assertThat( target.retryLimit, is(5) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapMethodSampleTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapMethodSampleTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapMethodSampleTest.java new file mode 100755 index 0000000..96f46ec --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/MapMethodSampleTest.java @@ -0,0 +1,49 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class MapMethodSampleTest { + + public static class Target { + private int limit = 3; + + @Configure + public void setRetryLimit( int value ) { + limit = value; + } + } + + static Map<String,String> config = new HashMap<String,String>(); + static { config.put( "retryLimit", "5" ); } + + @Test + public void sample() { + Target target = new Target(); + ConfigurationInjectorBuilder.configuration().target( target ).source( config ).inject(); + assertThat( target.limit, is( 5 ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFactorySampleTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFactorySampleTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFactorySampleTest.java new file mode 100755 index 0000000..2adbe72 --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFactorySampleTest.java @@ -0,0 +1,40 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config; + +import org.junit.Test; + +import static org.apache.hadoop.gateway.config.ConfigurationInjectorBuilder.*; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class PropertiesFactorySampleTest { + + public static class Target { + @Configure @Alias("user.name") + private String user = "nobody"; + } + + @Test + public void sampleDirect() { + Target target = new Target(); + configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.user, is( System.getProperty( "user.name" ) ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFieldSampleTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFieldSampleTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFieldSampleTest.java new file mode 100755 index 0000000..db0af61 --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesFieldSampleTest.java @@ -0,0 +1,39 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class PropertiesFieldSampleTest { + + public static class Target { + @Configure @Alias("user.name") + private String user = "nobody"; + } + + @Test + public void sample() { + Target target = new Target(); + ConfigurationInjectorBuilder.configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.user, is( System.getProperty( "user.name" ) ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesMethodSampleTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesMethodSampleTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesMethodSampleTest.java new file mode 100755 index 0000000..4b72e3b --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/PropertiesMethodSampleTest.java @@ -0,0 +1,57 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class PropertiesMethodSampleTest { + + public static class Target { + + private String user = "nobody"; + private String home = "nowhere"; + private String temp = "nowhere"; + + @Configure + @Alias("user.name") + public void setUser( String value ) { + user = value; + } + + @Configure + public void setDirs( + @Alias("user.dir") String home, + @Alias("java.io.tmpdir") String temp ) { + this.home = home; + this.temp = temp; + } + } + + @Test + public void sample() { + Target target = new Target(); + ConfigurationInjectorBuilder.configuration().target( target ).source( System.getProperties() ).inject(); + assertThat( target.user, is( System.getProperty( "user.name" ) ) ); + assertThat( target.home, is( System.getProperty( "user.dir" ) ) ); + assertThat( target.temp, is( System.getProperty( "java.io.tmpdir" ) ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/UsageTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/UsageTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/UsageTest.java new file mode 100755 index 0000000..2fe963a --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/UsageTest.java @@ -0,0 +1,43 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config; + +import org.junit.Test; +import static org.apache.hadoop.gateway.config.ConfigurationInjectorBuilder.configuration; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +public class UsageTest { + + class Target { + @Configure + private String user; + } + + @Test + public void usage() { + Target target = new Target(); + configuration() + .target( target ) + .source( System.getProperties() ) + .bind( "user", "user.name" ) + .inject(); + assertThat( target.user, is(System.getProperty("user.name"))); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptorTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptorTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptorTest.java new file mode 100755 index 0000000..80913e2 --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterDescriptorTest.java @@ -0,0 +1,57 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.impl; + +import org.apache.hadoop.gateway.config.ConfigurationAdapter; +import org.apache.hadoop.gateway.config.spi.ConfigurationAdapterDescriptor; +import org.junit.Test; + +import java.util.Iterator; +import java.util.Map; +import java.util.ServiceLoader; + +import static junit.framework.TestCase.fail; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasKey; + +public class BeanConfigurationAdapterDescriptorTest { + + @Test + public void testServiceLoader() { + ServiceLoader<ConfigurationAdapterDescriptor> loader = ServiceLoader.load( ConfigurationAdapterDescriptor.class ); + Iterator<ConfigurationAdapterDescriptor> i = loader.iterator(); + while( i.hasNext() ) { + if( i.next() instanceof BeanConfigurationAdapterDescriptor ) { + return; + } + } + fail( "Failed to load BeanConfigurationAdapterDescriptor" ); + } + + @Test + public void testDescriptor() { + ConfigurationAdapterDescriptor descriptor = new BeanConfigurationAdapterDescriptor(); + Map<Class<?>,Class<? extends ConfigurationAdapter>> map = descriptor.providedConfigurationAdapters(); + assertThat( map, hasKey( (Class)Object.class ) ); + Class<? extends ConfigurationAdapter> type = map.get( Object.class ); + assertThat( + "Descriptor didn't return " + BeanConfigurationAdapter.class.getName(), + type == BeanConfigurationAdapter.class ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterTest.java ---------------------------------------------------------------------- diff --git a/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterTest.java b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterTest.java new file mode 100755 index 0000000..0d36eb6 --- /dev/null +++ b/gateway-util-configinjector/src/test/java/org/apache/hadoop/gateway/config/impl/BeanConfigurationAdapterTest.java @@ -0,0 +1,40 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.gateway.config.impl; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class BeanConfigurationAdapterTest { + + public static class Bean { + public String getValue() { + return "beanValue"; + }; + } + + @Test + public void test() { + Bean bean = new Bean(); + BeanConfigurationAdapter adapter = new BeanConfigurationAdapter( bean ); + assertThat( adapter.getConfigurationValue( "value" ).toString(), is( "beanValue" ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/hsso-release/pom.xml ---------------------------------------------------------------------- diff --git a/hsso-release/pom.xml b/hsso-release/pom.xml index df85c52..c5abf8e 100644 --- a/hsso-release/pom.xml +++ b/hsso-release/pom.xml @@ -119,21 +119,13 @@ </dependency> <dependency> <groupId>${gateway-group}</groupId> - <artifactId>gateway-service-oozie</artifactId> - </dependency> - <dependency> - <groupId>${gateway-group}</groupId> - <artifactId>gateway-service-webhcat</artifactId> + <artifactId>gateway-service-definitions</artifactId> </dependency> <dependency> <groupId>${gateway-group}</groupId> <artifactId>gateway-service-tgs</artifactId> </dependency> <dependency> - <groupId>${gateway-group}</groupId> - <artifactId>gateway-service-yarn-rm</artifactId> - </dependency> - <dependency> <groupId>${gateway-group}</groupId> <artifactId>gateway-provider-rewrite</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/knox/blob/0d9fa0da/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index e121942..f980c97 100644 --- a/pom.xml +++ b/pom.xml @@ -36,6 +36,7 @@ <modules> <module>gateway-test-utils</module> <module>gateway-util-common</module> + <module>gateway-util-configinjector</module> <module>gateway-util-launcher</module> <module>gateway-util-urltemplate</module> <module>gateway-test-ldap</module> @@ -65,10 +66,8 @@ <module>gateway-service-hbase</module> <module>gateway-service-hive</module> <module>gateway-service-webhdfs</module> - <module>gateway-service-oozie</module> - <module>gateway-service-webhcat</module> <module>gateway-service-tgs</module> - <module>gateway-service-yarn-rm</module> + <module>gateway-service-definitions</module> <module>gateway-shell</module> <module>gateway-shell-launcher</module> <module>knox-cli-launcher</module> @@ -351,6 +350,11 @@ </dependency> <dependency> <groupId>${gateway-group}</groupId> + <artifactId>gateway-util-configinjector</artifactId> + <version>${gateway-version}</version> + </dependency> + <dependency> + <groupId>${gateway-group}</groupId> <artifactId>gateway-util-launcher</artifactId> <version>${gateway-version}</version> </dependency> @@ -486,17 +490,7 @@ </dependency> <dependency> <groupId>${gateway-group}</groupId> - <artifactId>gateway-service-webhcat</artifactId> - <version>${gateway-version}</version> - </dependency> - <dependency> - <groupId>${gateway-group}</groupId> - <artifactId>gateway-service-yarn-rm</artifactId> - <version>${gateway-version}</version> - </dependency> - <dependency> - <groupId>${gateway-group}</groupId> - <artifactId>gateway-service-oozie</artifactId> + <artifactId>gateway-service-definitions</artifactId> <version>${gateway-version}</version> </dependency> <dependency> @@ -608,7 +602,7 @@ <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> - <version>4.2.5</version> + <version>4.3.6</version> </dependency> <!-- @@ -818,7 +812,11 @@ <artifactId>commons-net</artifactId> <version>1.4.1</version> </dependency> - + <dependency> + <groupId>commons-collections</groupId> + <artifactId>commons-collections</artifactId> + <version>3.2.1</version> + </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-digester3</artifactId>