http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/33d67501/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/DefaultConfigurationContextBuilder.java ---------------------------------------------------------------------- diff --git a/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/DefaultConfigurationContextBuilder.java b/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/DefaultConfigurationContextBuilder.java deleted file mode 100644 index f10038e..0000000 --- a/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/DefaultConfigurationContextBuilder.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * 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.tamaya.integration.cdi.internal; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConfigurationContextBuilder; -import org.apache.tamaya.spi.PropertyConverter; -import org.apache.tamaya.spi.PropertyFilter; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValueCombinationPolicy; - -import javax.enterprise.inject.Vetoed; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; - -/** - * Default implementation of {@link ConfigurationContextBuilder}. - */ -@Vetoed -public class DefaultConfigurationContextBuilder implements ConfigurationContextBuilder { - - final Map<String, PropertySource> propertySources = new HashMap<>(); - final List<PropertyFilter> propertyFilters = new ArrayList<>(); - final Map<TypeLiteral<?>, List<PropertyConverter<?>>> propertyConverters = new HashMap<>(); - PropertyValueCombinationPolicy combinationPolicy; - - @Override - public ConfigurationContextBuilder setContext(ConfigurationContext context) { - this.propertySources.clear(); - for(PropertySource ps:context.getPropertySources()) { - this.propertySources.put(ps.getName(), ps); - } - this.propertyFilters.clear(); - this.propertyFilters.addAll(context.getPropertyFilters()); - this.propertyConverters.clear(); - this.propertyConverters.putAll(context.getPropertyConverters()); - this.combinationPolicy = context.getPropertyValueCombinationPolicy(); - return this; - } - - @Override - public ConfigurationContextBuilder addPropertySources(Collection<PropertySource> propertySourcesToAdd) { - for(PropertySource ps:propertySourcesToAdd){ - if(this.propertySources.containsKey(ps.getName())){ - throw new ConfigException("Duplicate PropertySource: " + ps.getName()); - } - } - for(PropertySource ps:propertySourcesToAdd) { - this.propertySources.put(ps.getName(), ps); - } - return this; - } - - @Override - public ConfigurationContextBuilder addPropertySources(PropertySource... propertySourcesToAdd) { - return addPropertySources(Arrays.asList(propertySourcesToAdd)); - } - - @Override - public ConfigurationContextBuilder removePropertySources(Collection<String> propertySourcesToRemove) { - for(String key: propertySourcesToRemove){ - this.propertySources.remove(key); - } - return this; - } - - @Override - public ConfigurationContextBuilder removePropertySources(String... propertySourcesToRemove) { - return removePropertySources(Arrays.asList(propertySourcesToRemove)); - } - - @Override - public ConfigurationContextBuilder addPropertyFilters(Collection<PropertyFilter> filters) { - this.propertyFilters.addAll(filters); - return this; - } - - @Override - public ConfigurationContextBuilder addPropertyFilters(PropertyFilter... filters) { - return addPropertyFilters(Arrays.asList(filters)); - } - - @Override - public ConfigurationContextBuilder removePropertyFilters(Collection<PropertyFilter> filters) { - this.propertyFilters.removeAll(filters); - return this; - } - - @Override - public ConfigurationContextBuilder removePropertyFilters(PropertyFilter... filters) { - return removePropertyFilters(Arrays.asList(filters)); - } - - @Override - public <T> ConfigurationContextBuilder addPropertyConverter(TypeLiteral<T> typeToConvert, PropertyConverter<T> propertyConverter) { - List<PropertyConverter<?>> converters = this.propertyConverters.get(typeToConvert); - if(converters==null){ - converters = new ArrayList<>(); - this.propertyConverters.put(typeToConvert, converters); - } - return this; - } - - @Override - public ConfigurationContextBuilder removePropertyConverters(TypeLiteral<?> typeToConvert, PropertyConverter<?>... converters) { - return removePropertyConverters(typeToConvert, Arrays.asList(converters)); - } - - @Override - public ConfigurationContextBuilder removePropertyConverters(TypeLiteral<?> typeToConvert, Collection<PropertyConverter<?>> converters) { - List<PropertyConverter<?>> existing = this.propertyConverters.get(typeToConvert); - if(existing!=null) { - existing.removeAll(converters); - } - return this; - } - - @Override - public ConfigurationContextBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy) { - this.combinationPolicy = Objects.requireNonNull(policy); - return this; - } - - @Override - public ConfigurationContext build() { - return new DefaultConfigurationContext(this); - } - -}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/33d67501/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/EnumConverter.java ---------------------------------------------------------------------- diff --git a/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/EnumConverter.java b/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/EnumConverter.java deleted file mode 100644 index 90edbaf..0000000 --- a/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/EnumConverter.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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.tamaya.integration.cdi.internal; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.spi.ConversionContext; -import org.apache.tamaya.spi.PropertyConverter; - -import javax.enterprise.inject.Vetoed; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Locale; -import java.util.Objects; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Converter, converting from String to tge given enum type. - * @param <T> the enum type. - */ -@Vetoed -public class EnumConverter<T> implements PropertyConverter<T> { - private final Logger LOG = Logger.getLogger(EnumConverter.class.getName()); - private Class<T> enumType; - private Method factory; - - public EnumConverter(Class<T> enumType) { - if (!Enum.class.isAssignableFrom(enumType)) { - throw new IllegalArgumentException("Not an Enum: " + enumType.getName()); - } - this.enumType = Objects.requireNonNull(enumType); - try { - this.factory = enumType.getMethod("valueOf", String.class); - } catch (NoSuchMethodException e) { - throw new ConfigException("Uncovertible enum type without valueOf method found, please provide a custom " + - "PropertyConverter for: " + enumType.getName()); - } - } - - @Override - public T convert(String value, ConversionContext context) { - context.addSupportedFormats(getClass(), "<enumValue>"); - try { - return (T) factory.invoke(null, value); - } catch (InvocationTargetException | IllegalAccessException e) { - LOG.log(Level.FINEST, "Invalid enum value '" + value + "' for " + enumType.getName(), e); - } - try { - return (T) factory.invoke(null, value.toUpperCase(Locale.ENGLISH)); - } catch (InvocationTargetException | IllegalAccessException e) { - LOG.log(Level.FINEST, "Invalid enum value '" + value + "' for " + enumType.getName(), e); - } - return null; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/33d67501/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/PropertyConverterManager.java ---------------------------------------------------------------------- diff --git a/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/PropertyConverterManager.java b/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/PropertyConverterManager.java deleted file mode 100644 index 75e3004..0000000 --- a/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/PropertyConverterManager.java +++ /dev/null @@ -1,427 +0,0 @@ -/* - * 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.tamaya.integration.cdi.internal; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.ConversionContext; -import org.apache.tamaya.spi.PropertyConverter; -import org.apache.tamaya.spi.ServiceContextManager; - -import javax.enterprise.inject.Vetoed; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.lang.reflect.Type; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.logging.Logger; - -/** - * Manager that deals with {@link PropertyConverter} instances. - * This class is thread-safe. - */ -@Vetoed -public class PropertyConverterManager { - /** - * The logger used. - */ - private static final Logger LOG = Logger.getLogger(PropertyConverterManager.class.getName()); - /** - * The registered converters. - */ - private final Map<TypeLiteral<?>, List<PropertyConverter<?>>> converters = new ConcurrentHashMap<>(); - /** - * The transitive converters. - */ - private final Map<TypeLiteral<?>, List<PropertyConverter<?>>> transitiveConverters = new ConcurrentHashMap<>(); - /** - * The lock used. - */ - private final ReadWriteLock lock = new ReentrantReadWriteLock(); - - private static final Comparator<Object> PRIORITY_COMPARATOR = new Comparator<Object>() { - - @Override - public int compare(Object o1, Object o2) { - int prio = CDIAwareServiceContext.getPriority(o1) - CDIAwareServiceContext.getPriority(o2); - if (prio < 0) { - return 1; - } else if (prio > 0) { - return -1; - } else { - return o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName()); - } - } - }; - - /** - * Constructor. - */ - public PropertyConverterManager() { - initConverters(); - } - - /** - * Registers the default converters provided out of the box. - */ - protected void initConverters() { - for (PropertyConverter conv : ServiceContextManager.getServiceContext().getServices(PropertyConverter.class)) { - Type type = TypeLiteral.getGenericInterfaceTypeParameters(conv.getClass(), PropertyConverter.class)[0]; - register(TypeLiteral.of(type), conv); - } - } - - /** - * Registers a ew converter instance. - * - * @param targetType the target type, not null. - * @param converter the converter, not null. - * @param <T> the type. - */ - public <T> void register(TypeLiteral<T> targetType, PropertyConverter<T> converter) { - Objects.requireNonNull(converter); - Lock writeLock = lock.writeLock(); - try { - writeLock.lock(); - List converters = List.class.cast(this.converters.get(targetType)); - List<PropertyConverter<?>> newConverters = new ArrayList<>(); - if (converters != null) { - newConverters.addAll(converters); - } - newConverters.add(converter); - Collections.sort(newConverters, PRIORITY_COMPARATOR); - this.converters.put(targetType, Collections.unmodifiableList(newConverters)); - // evaluate transitive closure for all inherited supertypes and implemented interfaces - // direct implemented interfaces - for (Class<?> ifaceType : targetType.getRawType().getInterfaces()) { - converters = List.class.cast(this.transitiveConverters.get(TypeLiteral.of(ifaceType))); - newConverters = new ArrayList<>(); - if (converters != null) { - newConverters.addAll(converters); - } - newConverters.add(converter); - Collections.sort(newConverters, PRIORITY_COMPARATOR); - this.transitiveConverters.put(TypeLiteral.of(ifaceType), Collections.unmodifiableList(newConverters)); - } - Class<?> superClass = targetType.getRawType().getSuperclass(); - while (superClass != null && !superClass.equals(Object.class)) { - converters = List.class.cast(this.transitiveConverters.get(TypeLiteral.of(superClass))); - newConverters = new ArrayList<>(); - if (converters != null) { - newConverters.addAll(converters); - } - newConverters.add(converter); - Collections.sort(newConverters, PRIORITY_COMPARATOR); - this.transitiveConverters.put(TypeLiteral.of(superClass), Collections.unmodifiableList(newConverters)); - for (Class<?> ifaceType : superClass.getInterfaces()) { - converters = List.class.cast(this.transitiveConverters.get(TypeLiteral.of(ifaceType))); - newConverters = new ArrayList<>(); - if (converters != null) { - newConverters.addAll(converters); - } - newConverters.add(converter); - Collections.sort(newConverters, PRIORITY_COMPARATOR); - this.transitiveConverters.put(TypeLiteral.of(ifaceType), Collections.unmodifiableList(newConverters)); - } - superClass = superClass.getSuperclass(); - } - } finally { - writeLock.unlock(); - } - } - - /** - * Allows to evaluate if a given target type is supported. - * - * @param targetType the target type, not null. - * @return true, if a converter for the given type is registered, or a default one can be created. - */ - public boolean isTargetTypeSupported(TypeLiteral<?> targetType) { - if (converters.containsKey(targetType) || transitiveConverters.containsKey(targetType)) { - return true; - } - return createDefaultPropertyConverter(targetType) != null; - } - - /** - * Get a map of all property converters currently registered. This will not contain the converters that - * may be created, when an instance is adapted, which provides a String constructor or compatible - * factory methods taking a single String instance. - * - * @return the current map of instantiated and registered converters. - * @see #createDefaultPropertyConverter(TypeLiteral) - */ - public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() { - Lock readLock = lock.readLock(); - try { - readLock.lock(); - return new HashMap<>(this.converters); - } finally { - readLock.unlock(); - } - } - - /** - * <p>Get the list of all current registered converters for the given target type. - * If not converters are registered, they component tries to create and register a dynamic - * converter based on String costructor or static factory methods available.</p> - * - * <p>The converters provided are of the following type and returned in the following order:</p> - * <ul> - * <li>Converters mapped explicitly to the required target type are returned first, ordered - * by decreasing priority. This means, if explicit converters are registered these are used - * primarly for converting a value.</li> - * <li>The target type of each explicitly registered converter also can be transitively mapped to - * 1) all directly implemented interfaces, 2) all its superclasses (except Object), 3) all the interfaces - * implemented by its superclasses. These groups of transitive converters is returned similarly in the - * order as mentioned, whereas also here a priority based decreasing ordering is applied.</li> - * <li>java.lang wrapper classes and native types are automatically mapped.</li> - * <li>If no explicit converters are registered, for Enum types a default implementation is provided that - * compares the configuration values with the different enum members defined (cases sensitive mapping).</li> - * </ul> - * - * <p>So given that list above directly registered mappings always are tried first, before any transitive mapping - * should be used. Also in all cases @Priority annotations are honored for ordering of the converters in place. - * Transitive conversion is supported for all directly implemented interfaces (including inherited ones) and - * the inheritance hierarchy (exception Object). Super interfaces of implemented interfaces are ignored.</p> - * - * - * @param targetType the target type, not null. - * @param <T> the type class - * @return the ordered list of converters (may be empty for not convertible types). - * @see #createDefaultPropertyConverter(TypeLiteral) - */ - public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> targetType) { - Lock readLock = lock.readLock(); - List<PropertyConverter<T>> converterList = new ArrayList<>(); - List<PropertyConverter<T>> converters; - // direct mapped converters - try { - readLock.lock(); - converters = List.class.cast(this.converters.get(targetType)); - } finally { - readLock.unlock(); - } - if (converters != null) { - converterList.addAll(converters); - } - // transitive converter - try { - readLock.lock(); - converters = List.class.cast(this.transitiveConverters.get(targetType)); - } finally { - readLock.unlock(); - } - if (converters != null) { - converterList.addAll(converters); - } - // handling of java.lang wrapper classes - TypeLiteral<T> boxedType = mapBoxedType(targetType); - if (boxedType != null) { - try { - readLock.lock(); - converters = List.class.cast(this.converters.get(boxedType)); - } finally { - readLock.unlock(); - } - if (converters != null) { - converterList.addAll(converters); - } - } - if (converterList.isEmpty()) { - // adding any converters created on the fly, e.g. for enum types. - PropertyConverter<T> defaultConverter = createDefaultPropertyConverter(targetType); - if (defaultConverter != null) { - register(targetType, defaultConverter); - try { - readLock.lock(); - converters = List.class.cast(this.converters.get(targetType)); - } finally { - readLock.unlock(); - } - } - if (converters != null) { - converterList.addAll(converters); - } - } - return converterList; - } - - /** - * Maps native types to the corresponding boxed types. - * - * @param targetType the native type. - * @param <T> the type - * @return the boxed type, or null. - */ - private <T> TypeLiteral<T> mapBoxedType(TypeLiteral<T> targetType) { - Type parameterType = targetType.getType(); - if (parameterType == int.class) { - return TypeLiteral.class.cast(TypeLiteral.of(Integer.class)); - } - if (parameterType == short.class) { - return TypeLiteral.class.cast(TypeLiteral.of(Short.class)); - } - if (parameterType == byte.class) { - return TypeLiteral.class.cast(TypeLiteral.of(Byte.class)); - } - if (parameterType == long.class) { - return TypeLiteral.class.cast(TypeLiteral.of(Long.class)); - } - if (parameterType == boolean.class) { - return TypeLiteral.class.cast(TypeLiteral.of(Boolean.class)); - } - if (parameterType == char.class) { - return TypeLiteral.class.cast(TypeLiteral.of(Character.class)); - } - if (parameterType == float.class) { - return TypeLiteral.class.cast(TypeLiteral.of(Float.class)); - } - if (parameterType == double.class) { - return TypeLiteral.class.cast(TypeLiteral.of(Double.class)); - } - if (parameterType == int[].class) { - return TypeLiteral.class.cast(TypeLiteral.of(Integer[].class)); - } - if (parameterType == short[].class) { - return TypeLiteral.class.cast(TypeLiteral.of(Short[].class)); - } - if (parameterType == byte[].class) { - return TypeLiteral.class.cast(TypeLiteral.of(Byte[].class)); - } - if (parameterType == long[].class) { - return TypeLiteral.class.cast(TypeLiteral.of(Long[].class)); - } - if (parameterType == boolean.class) { - return TypeLiteral.class.cast(TypeLiteral.of(Boolean.class)); - } - if (parameterType == char[].class) { - return TypeLiteral.class.cast(TypeLiteral.of(Character[].class)); - } - if (parameterType == float[].class) { - return TypeLiteral.class.cast(TypeLiteral.of(Float[].class)); - } - if (parameterType == double[].class) { - return TypeLiteral.class.cast(TypeLiteral.of(Double[].class)); - } - return null; - } - - /** - * Creates a dynamic PropertyConverter for the given target type. - * - * @param targetType the target type - * @param <T> the type class - * @return a new converter, or null. - */ - protected <T> PropertyConverter<T> createDefaultPropertyConverter(final TypeLiteral<T> targetType) { - if (Enum.class.isAssignableFrom(targetType.getRawType())) { - return new EnumConverter<>(targetType.getRawType()); - } - PropertyConverter<T> converter = null; - final Method factoryMethod = getFactoryMethod(targetType.getRawType(), "of", "valueOf", "instanceOf", "getInstance", "from", "fromString", "parse"); - if (factoryMethod != null) { - converter = new PropertyConverter<T>() { - @Override - public T convert(String value, ConversionContext context) { - context.addSupportedFormats(PropertyConverter.class, "static T of(String)", "static T valueOf(String)", - "static T getInstance(String)", "static T from(String)", - "static T fromString(String)", "static T parse(String)"); - try { - if (!Modifier.isStatic(factoryMethod.getModifiers())) { - throw new ConfigException(factoryMethod.toGenericString() + - " is not a static method. Only static " + - "methods can be used as factory methods."); - } - AccessController.doPrivileged(new PrivilegedAction<Object>() { - @Override - public Object run() { - factoryMethod.setAccessible(true); - return null; - } - }); - Object invoke = factoryMethod.invoke(null, value); - return targetType.getRawType().cast(invoke); - } catch (Exception e) { - throw new ConfigException("Failed to decode '" + value + "'", e); - } - } - }; - } - if (converter == null) { - try { - final Constructor<T> constr = targetType.getRawType().getDeclaredConstructor(String.class); - converter = new PropertyConverter<T>() { - @Override - public T convert(String value, ConversionContext context) { - context.addSupportedFormats(PropertyConverter.class, "new T(String)"); - try { - AccessController.doPrivileged(new PrivilegedAction<Object>() { - @Override - public Object run() { - constr.setAccessible(true); - return null; - } - }); - return constr.newInstance(value); - } catch (Exception e) { - throw new ConfigException("Failed to decode '" + value + "'", e); - } - } - }; - } catch (Exception e) { - LOG.finest("Failed to construct instance of type: " + targetType.getRawType().getName() + ": " + e); - } - } - return converter; - } - - /** - * Tries to evaluate a factory method that can be used to create an instance based on a String. - * - * @param type the target type - * @param methodNames the possible static method names - * @return the first method found, or null. - */ - private Method getFactoryMethod(Class<?> type, String... methodNames) { - Method m; - for (String name : methodNames) { - try { - m = type.getDeclaredMethod(name, String.class); - return m; - } catch (NoSuchMethodException | RuntimeException e) { - LOG.finest("No such factory method found on type: " + type.getName() + ", methodName: " + name); - } - } - return null; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/33d67501/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/SEInjectorCDIExtension.java ---------------------------------------------------------------------- diff --git a/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/SEInjectorCDIExtension.java b/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/SEInjectorCDIExtension.java deleted file mode 100644 index c6d1b85..0000000 --- a/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/SEInjectorCDIExtension.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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.tamaya.integration.cdi.internal; - - -import org.apache.tamaya.inject.ConfigurationInjection; -import org.apache.tamaya.inject.api.Config; -import org.apache.tamaya.inject.api.ConfigDefaultSections; - -import javax.enterprise.context.spi.CreationalContext; -import javax.enterprise.event.Observes; -import javax.enterprise.inject.Vetoed; -import javax.enterprise.inject.spi.AnnotatedType; -import javax.enterprise.inject.spi.Extension; -import javax.enterprise.inject.spi.InjectionPoint; -import javax.enterprise.inject.spi.InjectionTarget; -import javax.enterprise.inject.spi.ProcessInjectionTarget; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.Set; - -/** - * CDI portable extension that integrates {@link org.apache.tamaya.inject.ConfigurationInjector} - * with CDI by adding configuration features to CDI (config enable CDI beans). - */ -@Vetoed -public final class SEInjectorCDIExtension implements Extension { - - /** - * Method that injects the values into any configured fields, by wrapping the given - * InjectionTarget. - * @param pit the injection target - * @param <T> the target type - */ - public <T> void initializeConfiguredFields(final @Observes ProcessInjectionTarget<T> pit) { - final AnnotatedType<T> at = pit.getAnnotatedType(); - if (!isConfigured(at.getJavaClass())) { - return; - } - final InjectionTarget<T> it = pit.getInjectionTarget(); - InjectionTarget<T> wrapped = new InjectionTarget<T>() { - @Override - public void inject(T instance, CreationalContext<T> ctx) { - it.inject(instance, ctx); - ConfigurationInjection.getConfigurationInjector().configure(instance); - } - - @Override - public void postConstruct(T instance) { - it.postConstruct(instance); - } - - @Override - public void preDestroy(T instance) { - it.dispose(instance); - } - - @Override - public void dispose(T instance) { - it.dispose(instance); - } - - @Override - public Set<InjectionPoint> getInjectionPoints() { - return it.getInjectionPoints(); - } - - @Override - public T produce(CreationalContext<T> ctx) { - return it.produce(ctx); - } - }; - pit.setInjectionTarget(wrapped); - } - - private boolean isConfigured(Class type) { - if (type.getAnnotation(ConfigDefaultSections.class) != null) { - return true; - } - // if no class level annotation is there we might have field level annotations only - for (Field field : type.getDeclaredFields()) { - if (field.isAnnotationPresent(Config.class)) { - return true; - } - } - // if no class level annotation is there we might have method level annotations only - for (Method method : type.getDeclaredMethods()) { - if(method.isAnnotationPresent(Config.class)) { - return true; - } - } - return false; - } - - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/33d67501/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/TamayaCDIIntegration.java ---------------------------------------------------------------------- diff --git a/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/TamayaCDIIntegration.java b/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/TamayaCDIIntegration.java deleted file mode 100644 index 43d0074..0000000 --- a/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/TamayaCDIIntegration.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.tamaya.integration.cdi.internal; - -import javax.enterprise.event.Observes; -import javax.enterprise.inject.spi.AfterDeploymentValidation; -import javax.enterprise.inject.spi.BeanManager; -import javax.enterprise.inject.spi.Extension; - -/** - * Tamaya main integration with CDI, storing the BeanManager reference for implementation, where no - * JNDI is available or {@code java:comp/env/BeanManager} is not set correctly. - */ -public class TamayaCDIIntegration implements Extension { - /** The BeanManager references stored. */ - private static BeanManager beanManager; - - /** - * Initializes the current BeanManager with the instance passed. - * @param validation the event - * @param beanManager the BeanManager instance - */ - @SuppressWarnings("all") - public void initBeanManager(@Observes AfterDeploymentValidation validation, BeanManager beanManager){ - TamayaCDIIntegration.beanManager = beanManager; - } - - /** - * Get the current {@link BeanManager} instance. - * @return the currently used bean manager. - */ - public static BeanManager getBeanManager(){ - return beanManager; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/33d67501/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/TamayaConfigProvider.java ---------------------------------------------------------------------- diff --git a/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/TamayaConfigProvider.java b/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/TamayaConfigProvider.java deleted file mode 100644 index 33251d7..0000000 --- a/integration/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/internal/TamayaConfigProvider.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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.tamaya.integration.cdi.internal; - - -import org.apache.tamaya.Configuration; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConfigurationContextBuilder; -import org.apache.tamaya.spisupport.DefaultConfiguration; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.Produces; -import javax.inject.Singleton; - -/** - * Tamaya main integreation with CDI (singleton) serving producers for Configuration, ConfigurationContext and - * ConfigurationContextBuilder. - */ -@Singleton -public class TamayaConfigProvider{ - - @Produces - @ApplicationScoped - public Configuration getConfiguration(ConfigurationContext context){ - return new DefaultConfiguration(context); - } - - @Produces @ApplicationScoped - public ConfigurationContext getConfigurationContext(){ - return new DefaultConfigurationContext(); - } - - @Produces - public ConfigurationContextBuilder getConfigurationContextBuilder(){ - return new DefaultConfigurationContextBuilder(); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/33d67501/integration/cdi-se/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension ---------------------------------------------------------------------- diff --git a/integration/cdi-se/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/integration/cdi-se/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension index db6641d..8580a42 100644 --- a/integration/cdi-se/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension +++ b/integration/cdi-se/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension @@ -16,5 +16,5 @@ # specific language governing permissions and limitations # under the License. # -org.apache.tamaya.integration.cdi.internal.TamayaCDIIntegration -org.apache.tamaya.integration.cdi.internal.SEInjectorCDIExtension +org.apache.tamaya.integration.cdi.TamayaCDIIntegration +org.apache.tamaya.integration.cdi.SEInjectorCDIExtension http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/33d67501/integration/cdi-se/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext ---------------------------------------------------------------------- diff --git a/integration/cdi-se/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext b/integration/cdi-se/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext index 5c501ee..4fe7e01 100644 --- a/integration/cdi-se/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext +++ b/integration/cdi-se/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext @@ -16,4 +16,4 @@ # specific language governing permissions and limitations # under the License. # -org.apache.tamaya.integration.cdi.internal.CDIAwareServiceContext \ No newline at end of file +org.apache.tamaya.integration.cdi.CDIAwareServiceContext \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/33d67501/integration/cdi/pom.xml ---------------------------------------------------------------------- diff --git a/integration/cdi/pom.xml b/integration/cdi/pom.xml index df7288a..34481f6 100644 --- a/integration/cdi/pom.xml +++ b/integration/cdi/pom.xml @@ -117,7 +117,6 @@ under the License. <version>${openejb.version}</version> <scope>provided</scope> </dependency> - <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-jcdi_1.1_spec</artifactId> @@ -138,66 +137,69 @@ under the License. </dependency> </dependencies> <profiles> - <profile> - <id>OWB</id> - <!-- there is an issue with this profile: - java.lang.NoClassDefFoundError: org/apache/webbeans/event/EventMetadata - --> - <dependencies> - <!-- OWB specific dependencies--> - <dependency> - <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-atinject_1.0_spec</artifactId> - <version>${geronimo-atinject-1.0-spec.version}</version> - </dependency> - <dependency> - <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-interceptor_1.2_spec</artifactId> - <version>${geronimo-interceptor-1.2-spec.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-annotation_1.2_spec</artifactId> - <version>1.0</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-el_2.2_spec</artifactId> - <version>1.0.2</version> - </dependency> + <!--<profile>--> + <!--<id>OWB</id>--> + <!--<!– there is an issue with this profile:--> + <!--java.lang.NoClassDefFoundError: org/apache/webbeans/event/EventMetadata--> + <!--–>--> + <!--<activation>--> + <!--<activeByDefault>false</activeByDefault>--> + <!--</activation>--> + <!--<dependencies>--> + <!--<!– OWB specific dependencies–>--> + <!--<dependency>--> + <!--<groupId>org.apache.geronimo.specs</groupId>--> + <!--<artifactId>geronimo-atinject_1.0_spec</artifactId>--> + <!--<version>${geronimo-atinject-1.0-spec.version}</version>--> + <!--</dependency>--> + <!--<dependency>--> + <!--<groupId>org.apache.geronimo.specs</groupId>--> + <!--<artifactId>geronimo-interceptor_1.2_spec</artifactId>--> + <!--<version>${geronimo-interceptor-1.2-spec.version}</version>--> + <!--<scope>test</scope>--> + <!--</dependency>--> + <!--<dependency>--> + <!--<groupId>org.apache.geronimo.specs</groupId>--> + <!--<artifactId>geronimo-annotation_1.2_spec</artifactId>--> + <!--<version>1.0</version>--> + <!--<scope>test</scope>--> + <!--</dependency>--> + <!--<dependency>--> + <!--<groupId>org.apache.geronimo.specs</groupId>--> + <!--<artifactId>geronimo-el_2.2_spec</artifactId>--> + <!--<version>1.0.2</version>--> + <!--</dependency>--> - <dependency> - <groupId>org.apache.openwebbeans</groupId> - <artifactId>openwebbeans-impl</artifactId> - <version>${owb.version}</version> - </dependency> - <dependency> - <groupId>org.apache.openwebbeans</groupId> - <artifactId>openwebbeans-spi</artifactId> - <version>${owb.version}</version> - </dependency> - <dependency> - <groupId>org.apache.openwebbeans</groupId> - <artifactId>openwebbeans-resource</artifactId> - <version>${owb.version}</version> - </dependency> + <!--<dependency>--> + <!--<groupId>org.apache.openwebbeans</groupId>--> + <!--<artifactId>openwebbeans-impl</artifactId>--> + <!--<version>${owb.version}</version>--> + <!--</dependency>--> + <!--<dependency>--> + <!--<groupId>org.apache.openwebbeans</groupId>--> + <!--<artifactId>openwebbeans-spi</artifactId>--> + <!--<version>${owb.version}</version>--> + <!--</dependency>--> + <!--<dependency>--> + <!--<groupId>org.apache.openwebbeans</groupId>--> + <!--<artifactId>openwebbeans-resource</artifactId>--> + <!--<version>${owb.version}</version>--> + <!--</dependency>--> - <dependency> - <groupId>org.apache.bval</groupId> - <artifactId>bval-jsr303</artifactId> - <version>${bval.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.deltaspike.cdictrl</groupId> - <artifactId>deltaspike-cdictrl-owb</artifactId> - <version>${ds.version}</version> - <scope>test</scope> - </dependency> - </dependencies> - </profile> + <!--<dependency>--> + <!--<groupId>org.apache.bval</groupId>--> + <!--<artifactId>bval-jsr303</artifactId>--> + <!--<version>${bval.version}</version>--> + <!--<scope>test</scope>--> + <!--</dependency>--> + <!--<dependency>--> + <!--<groupId>org.apache.deltaspike.cdictrl</groupId>--> + <!--<artifactId>deltaspike-cdictrl-owb</artifactId>--> + <!--<version>${ds.version}</version>--> + <!--<scope>test</scope>--> + <!--</dependency>--> + <!--</dependencies>--> + <!--</profile>--> <profile> <id>Weld</id> <activation> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/33d67501/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/ConfigurationExtension.java ---------------------------------------------------------------------- diff --git a/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/ConfigurationExtension.java b/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/ConfigurationExtension.java index 376836d..2a3d7db 100644 --- a/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/ConfigurationExtension.java +++ b/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/ConfigurationExtension.java @@ -46,6 +46,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Logger; + /** * CDI Extension module that adds injection mechanism for configuration. @@ -56,6 +58,8 @@ import java.util.concurrent.ConcurrentHashMap; */ public class ConfigurationExtension implements Extension { + private static final Logger LOG = Logger.getLogger(ConfigurationExtension.class.getName()); + static final Map<Class, ConfigOperator> CUSTOM_OPERATORS = new ConcurrentHashMap<>(); static final Map<Class, PropertyConverter> CUSTOM_CONVERTERS = new ConcurrentHashMap<>(); @@ -63,6 +67,13 @@ public class ConfigurationExtension implements Extension { private Bean<?> convBean; /** + * Constructor for loading logging its load. + */ + public ConfigurationExtension(){ + LOG.info("Enabling Tamaya CDI Configuration..."); + } + + /** * Method that checks the configuration injection points during deployment for available configuration. * @param pb the bean to process. * @param beanManager the bean manager to notify about new injections. @@ -112,6 +123,7 @@ public class ConfigurationExtension implements Extension { types.add(injectionPoint.getType()); if(annotation!=null){ configured = true; + LOG.info("Enabling Tamaya CDI Configuration on bean: " + configuredType.getName()); configuredType.addConfiguredMember(injectionPoint, keys); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/33d67501/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension ---------------------------------------------------------------------- diff --git a/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension index e82118a..4543268 100644 --- a/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension +++ b/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension @@ -16,4 +16,5 @@ # specific language governing permissions and limitations # under the License. # +org.apache.tamaya.integration.cdi.TamayaCDIIntegration org.apache.tamaya.integration.cdi.ConfigurationExtension \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/33d67501/integration/cdi/src/test/java/org/apache/tamaya/integration/cdi/ConfigurationResolverTest.java ---------------------------------------------------------------------- diff --git a/integration/cdi/src/test/java/org/apache/tamaya/integration/cdi/ConfigurationResolverTest.java b/integration/cdi/src/test/java/org/apache/tamaya/integration/cdi/ConfigurationResolverTest.java index 697580a..1c551b2 100644 --- a/integration/cdi/src/test/java/org/apache/tamaya/integration/cdi/ConfigurationResolverTest.java +++ b/integration/cdi/src/test/java/org/apache/tamaya/integration/cdi/ConfigurationResolverTest.java @@ -1,19 +1,19 @@ -/* - * 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. - */ +///* +// * 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.tamaya.integration.cdi; // //import org.apache.openejb.loader.SystemInstance;