http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-ee/src/test/java/org/apache/tamaya/integration/cdi/EnvironmentsTest.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-ee/src/test/java/org/apache/tamaya/integration/cdi/EnvironmentsTest.java b/modules/injection/cdi-ee/src/test/java/org/apache/tamaya/integration/cdi/EnvironmentsTest.java deleted file mode 100644 index 6b73294..0000000 --- a/modules/injection/cdi-ee/src/test/java/org/apache/tamaya/integration/cdi/EnvironmentsTest.java +++ /dev/null @@ -1,83 +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; -// -//import org.junit.Test; -// -//import java.util.Map; -//import java.util.Properties; -// -//import static org.junit.Assert.assertEquals; -// -///** -// * Tests the related environment properties exist -// */ -//public class EnvironmentsTest { -// -// @Test -// public void testGetProperties() throws Exception { -// -// final Properties test = Environments.getProperties("test"); -// -// // loaded from test.properties -// assertEquals("classpath:/test-service-wsdl.xml", test.getProperty("remote.wsdl.location")); -// -// // loaded from base.properties -// assertEquals("joecool", test.getProperty("remote.username")); -// } -// -// @Test(expected = IllegalArgumentException.class) -// public void noEnvFound() { -// Environments.getProperties("does not exists"); -// } -// -// @Test -// public void dev() throws Exception { -// -// final Properties test = Environments.getProperties("dev"); -// -// assertEquals("org.apache.openejb.cipher.StaticDESPasswordCipher", test.getProperty("cipher")); -// assertEquals("NjAq6q2agYVnvSMz+eYUZg==", test.getProperty("remote.password")); -// assertEquals("1443", test.getProperty("remote.port")); -// assertEquals("https://srv1114.supertribe.org:1443/remote/service/url", test.getProperty("remote.target.url")); -// assertEquals("srv1114.supertribe.org:1443", test.getProperty("remote.address")); -// assertEquals("srv1114.supertribe.org", test.getProperty("remote.host")); -// assertEquals("classpath:/service-wsdl.xml", test.getProperty("remote.wsdl.location")); -// assertEquals("joecool", test.getProperty("remote.username")); -// } -// -// @Test -// public void cert() throws Exception { -// final Properties test = Environments.getProperties("cert"); -// assertEquals("srv1016.supertribe.org", test.getProperty("remote.host")); -// assertEquals("joecool", test.getProperty("remote.username")); -// } -// -// @Test -// public void prod() throws Exception { -// final Properties test = Environments.getProperties("prod"); -// assertEquals("remotedb001.supertribe.org", test.getProperty("remote.host")); -// assertEquals("joecool", test.getProperty("remote.username")); -// } -// -// -// private static void generateAsserts(Properties test) { -// for (Map.Entry<Object, Object> entry : test.entrySet()) { -// System.out.printf("assertEquals(\"%s\", test.getProperty(\"%s\"));%n", entry.getProperty(), entry.getKey()); -// } -// } -//} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-ee/src/test/java/org/apache/tamaya/integration/cdi/InterpolationTest.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-ee/src/test/java/org/apache/tamaya/integration/cdi/InterpolationTest.java b/modules/injection/cdi-ee/src/test/java/org/apache/tamaya/integration/cdi/InterpolationTest.java deleted file mode 100644 index 56cc8c9..0000000 --- a/modules/injection/cdi-ee/src/test/java/org/apache/tamaya/integration/cdi/InterpolationTest.java +++ /dev/null @@ -1,62 +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; -// -//import org.junit.Test; -//import org.tomitribe.util.IO; -// -//import java.net.URL; -//import java.util.Properties; -// -//import static org.junit.Assert.assertEquals; -// -//public class InterpolationTest { -// -// @Test -// public void testInterpolate() throws Exception { -// final Properties interpolated; -// { -// final Properties properties = new Properties(); -// properties.setProperty("foo.host", "localhost"); -// properties.setProperty("foo.port", "1234"); -// properties.setProperty("address", "http://${foo.host}:${foo.port}"); -// properties.setProperty("url", "${address}/webapp"); -// properties.setProperty("urlUnchanged", "${not an address}/webapp"); -// -// interpolated = Interpolation.interpolate(properties); -// } -// -// assertEquals("localhost", interpolated.getProperty("foo.host")); -// assertEquals("1234", interpolated.getProperty("foo.port")); -// assertEquals("http://localhost:1234", interpolated.getProperty("address")); -// assertEquals("http://localhost:1234/webapp", interpolated.getProperty("url")); -// assertEquals("${not an address}/webapp", interpolated.getProperty("urlUnchanged")); -// } -// -// @Test -// public void test() throws Exception { -// -// final ClassLoader loader = Thread.currentThread().getContextClassLoader(); -// -// final URL resource = loader.getResource("test.properties"); -// final Properties properties = Interpolation.interpolate(IO.readProperties(resource)); -// -// //remote.wsdl.location = classpath:/lx01116-zhr-active-partner-service-wsdl.xml -// assertEquals("classpath:/test-service-wsdl.xml", properties.getProperty("remote.wsdl.location")); -// } -// -//} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-ee/src/test/java/org/apache/tamaya/integration/cdi/NotFoundNoDefault.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-ee/src/test/java/org/apache/tamaya/integration/cdi/NotFoundNoDefault.java b/modules/injection/cdi-ee/src/test/java/org/apache/tamaya/integration/cdi/NotFoundNoDefault.java deleted file mode 100644 index afeeb84..0000000 --- a/modules/injection/cdi-ee/src/test/java/org/apache/tamaya/integration/cdi/NotFoundNoDefault.java +++ /dev/null @@ -1,78 +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; - -import org.apache.tamaya.inject.api.Config; -import org.tomitribe.util.Duration; - -import javax.inject.Inject; -import java.io.File; - -public class NotFoundNoDefault { - - @Inject - @Config("string.bla") - private String string; - - @Inject - @Config("file.bla") - private File file; - - @Inject - @Config("duration.bla") - private Duration duration; - - @Inject - @Config("boolean.bla") - private Boolean aBoolean; - - @Inject - @Config("integer.bla") - private Integer integer; - - public String getString() { - return string; - } - - public File getFile() { - return file; - } - - public Duration getDuration() { - return duration; - } - - public Boolean getaBoolean() { - return aBoolean; - } - - public Integer getInteger() { - return integer; - } - - @Override - public String toString() { - return "NotFoundNoDefault{" + - "string='" + string + '\'' + - ", file=" + file + - ", duration=" + duration + - ", aBoolean=" + aBoolean + - ", integer=" + integer + - '}'; - } - - } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-ee/src/test/resources/META-INF/javaconfiguration.properties ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-ee/src/test/resources/META-INF/javaconfiguration.properties b/modules/injection/cdi-ee/src/test/resources/META-INF/javaconfiguration.properties deleted file mode 100644 index 8d3c5c8..0000000 --- a/modules/injection/cdi-ee/src/test/resources/META-INF/javaconfiguration.properties +++ /dev/null @@ -1,32 +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 current 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. -# -remote.wsdl.location = classpath:/service-wsdl.xml -remote.port=1443 -remote.address=${remote.host}:${remote.port} -remote.target.url = https://${remote.address}/remote/service/url -remote.username = joecool -# ciphered using built in StaticDESPasswordCipher -remote.password = NjAq6q2agYVnvSMz+eYUZg== -cipher=org.apache.openejb.cipher.StaticDESPasswordCipher - -string.value = hello -file.value = ./conf -duration.value = 10 minutes and 57 seconds -boolean.value = true -integer.value = 123 http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-ee/src/test/resources/base.properties ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-ee/src/test/resources/base.properties b/modules/injection/cdi-ee/src/test/resources/base.properties deleted file mode 100644 index a4d9896..0000000 --- a/modules/injection/cdi-ee/src/test/resources/base.properties +++ /dev/null @@ -1,32 +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 current 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. -# -remote.wsdl.location = classpath:/service-wsdl.xml -remote.port=1443 -remote.address=${remote.host}:${remote.port} -remote.target.url = https://${remote.address}/remote/service/url -remote.username = joecool -# ciphered using built in StaticDESPasswordCipher -remote.password = NjAq6q2agYVnvSMz+eYUZg== -cipher=org.apache.openejb.cipher.StaticDESPasswordCipher - -string.value = hello -file.value = ./conf -duration.value = 10 minutes and 57 seconds -boolean.value = true -integer.value = 123 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-ee/src/test/resources/cert.properties ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-ee/src/test/resources/cert.properties b/modules/injection/cdi-ee/src/test/resources/cert.properties deleted file mode 100644 index c2c3712..0000000 --- a/modules/injection/cdi-ee/src/test/resources/cert.properties +++ /dev/null @@ -1,20 +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 current 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. -# -remote.host=srv1016.supertribe.org -remote.password=vm9oNWJpN8Y= http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-ee/src/test/resources/dev.properties ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-ee/src/test/resources/dev.properties b/modules/injection/cdi-ee/src/test/resources/dev.properties deleted file mode 100644 index abd3e03..0000000 --- a/modules/injection/cdi-ee/src/test/resources/dev.properties +++ /dev/null @@ -1,19 +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 current 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. -# -remote.host=srv1114.supertribe.org http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-ee/src/test/resources/prod.properties ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-ee/src/test/resources/prod.properties b/modules/injection/cdi-ee/src/test/resources/prod.properties deleted file mode 100644 index 9f1ad67..0000000 --- a/modules/injection/cdi-ee/src/test/resources/prod.properties +++ /dev/null @@ -1,20 +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 current 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. -# -remote.host=remotedb001.supertribe.org -remote.password=vm9oNWJpN8Y= http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-ee/src/test/resources/qa.properties ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-ee/src/test/resources/qa.properties b/modules/injection/cdi-ee/src/test/resources/qa.properties deleted file mode 100644 index 3f43795..0000000 --- a/modules/injection/cdi-ee/src/test/resources/qa.properties +++ /dev/null @@ -1,20 +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 current 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. -# -remote.host=srv1115.supertribe.org -remote.password=vm9oNWJpN8Y= http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-ee/src/test/resources/test.properties ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-ee/src/test/resources/test.properties b/modules/injection/cdi-ee/src/test/resources/test.properties deleted file mode 100644 index 8e08405..0000000 --- a/modules/injection/cdi-ee/src/test/resources/test.properties +++ /dev/null @@ -1,20 +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 current 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. -# -remote.wsdl.location = classpath:/test-service-wsdl.xml - http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/SEInjectorCDIExtension.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/SEInjectorCDIExtension.java b/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/SEInjectorCDIExtension.java deleted file mode 100644 index 86e95b0..0000000 --- a/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/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; - - -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/b3bcddcb/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/ServiceLoaderServiceContext.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/ServiceLoaderServiceContext.java b/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/ServiceLoaderServiceContext.java deleted file mode 100644 index 1de0655..0000000 --- a/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/ServiceLoaderServiceContext.java +++ /dev/null @@ -1,190 +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; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.spi.ServiceContext; -import org.apache.tamaya.spisupport.PriorityServiceComparator; - -import javax.annotation.Priority; -import java.io.IOException; -import java.net.URL; -import java.text.MessageFormat; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * This class implements the (default) {@link ServiceContext} interface and hereby uses the JDK - * {@link ServiceLoader} to load the services required. - */ -final class ServiceLoaderServiceContext implements ServiceContext { - private static final Logger LOG = Logger.getLogger(ServiceLoaderServiceContext.class.getName()); - /** - * List current services loaded, per class. - */ - private final ConcurrentHashMap<Class<?>, List<Object>> servicesLoaded = new ConcurrentHashMap<>(); - /** - * Singletons. - */ - private final Map<Class<?>, Object> singletons = new ConcurrentHashMap<>(); - private Map<Class, Class> factoryTypes = new ConcurrentHashMap<>(); - - @Override - public <T> T getService(Class<T> serviceType) { - Object cached = singletons.get(serviceType); - if (cached == null) { - cached = create(serviceType); - if(cached!=null) { - singletons.put(serviceType, cached); - } - } - return serviceType.cast(cached); - } - - @Override - public <T> T create(Class<T> serviceType) { - Class<? extends T> implType = factoryTypes.get(serviceType); - if(implType==null) { - Collection<T> services = getServices(serviceType); - if (services.isEmpty()) { - return null; - } else { - return getServiceWithHighestPriority(services, serviceType); - } - } - try { - return implType.newInstance(); - } catch (Exception e) { - LOG.log(Level.SEVERE, "Failed to create instabce of " + implType.getName(), e); - return null; - } - } - - /** - * Loads and registers services. - * - * @param <T> the concrete type. - * @param serviceType The service type. - * @return the items found, never {@code null}. - */ - @Override - public <T> List<T> getServices(final Class<T> serviceType) { - List<T> found = (List<T>) servicesLoaded.get(serviceType); - if (found != null) { - return found; - } - List<T> services = new ArrayList<>(); - try { - for (T t : ServiceLoader.load(serviceType)) { - services.add(t); - } - Collections.sort(services, PriorityServiceComparator.getInstance()); - services = Collections.unmodifiableList(services); - } catch (ServiceConfigurationError e) { - LOG.log(Level.WARNING, - "Error loading services current type " + serviceType, e); - if(services==null){ - services = Collections.emptyList(); - } - } - final List<T> previousServices = List.class.cast(servicesLoaded.putIfAbsent(serviceType, (List<Object>) services)); - return previousServices != null ? previousServices : services; - } - - /** - * Checks the given instance for a @Priority annotation. If present the annotation's value s evaluated. If no such - * annotation is present, a default priority is returned (1); - * @param o the instance, not null. - * @return a priority, by default 1. - */ - public static int getPriority(Object o){ - int prio = 1; //X TODO discuss default priority - Priority priority = o.getClass().getAnnotation(Priority.class); - if (priority != null) { - prio = priority.value(); - } - return prio; - } - - /** - * @param services to scan - * @param <T> type of the service - * - * @return the service with the highest {@link javax.annotation.Priority#value()} - * - * @throws ConfigException if there are multiple service implementations with the maximum priority - */ - private <T> T getServiceWithHighestPriority(Collection<T> services, Class<T> serviceType) { - T highestService = null; - // we do not need the priority stuff if the list contains only one element - if (services.size() == 1) { - highestService = services.iterator().next(); - this.factoryTypes.put(serviceType, highestService.getClass()); - return highestService; - } - - Integer highestPriority = null; - int highestPriorityServiceCount = 0; - - for (T service : services) { - int prio = getPriority(service); - if (highestPriority == null || highestPriority < prio) { - highestService = service; - highestPriorityServiceCount = 1; - highestPriority = prio; - } else if (highestPriority == prio) { - highestPriorityServiceCount++; - } - } - - if (highestPriorityServiceCount > 1) { - throw new ConfigException(MessageFormat.format("Found {0} implementations for Service {1} with Priority {2}: {3}", - highestPriorityServiceCount, - serviceType.getName(), - highestPriority, - services)); - } - this.factoryTypes.put(serviceType, highestService.getClass()); - return highestService; - } - - @Override - public int ordinal() { - return 1; - } - - @Override - public Enumeration<URL> getResources(String resource, ClassLoader cl) throws IOException{ - if(cl==null){ - cl = Thread.currentThread().getContextClassLoader(); - } - return cl.getResources(resource); - } - - @Override - public URL getResource(String resource, ClassLoader cl){ - if(cl==null){ - cl = Thread.currentThread().getContextClassLoader(); - } - return cl.getResource(resource); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/TamayaCDIIntegration.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/TamayaCDIIntegration.java b/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/TamayaCDIIntegration.java deleted file mode 100644 index 3f62039..0000000 --- a/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/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; - -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/b3bcddcb/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/config/ConfiguredVetoExtension.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/config/ConfiguredVetoExtension.java b/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/config/ConfiguredVetoExtension.java deleted file mode 100644 index f90998d..0000000 --- a/modules/injection/cdi-se/src/main/java/org/apache/tamaya/integration/cdi/config/ConfiguredVetoExtension.java +++ /dev/null @@ -1,42 +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.config; - -import org.apache.tamaya.ConfigurationProvider; - -import javax.enterprise.event.Observes; -import javax.enterprise.inject.spi.ProcessAnnotatedType; - -/** - * CDI Extension that can be used to veto on beans by configuring the fully qualified class names (as regex expression) - * under {@code javax.enterprise.inject.vetoed}. Multiple expression can be added as comma separated values. - */ -public class ConfiguredVetoExtension { - - public void observesBean(@Observes ProcessAnnotatedType<?> type){ - String vetoedTypesVal = ConfigurationProvider.getConfiguration().get("javax.enterprise.inject.vetoed"); - String[] vetoedTypes = vetoedTypesVal.split(","); - for(String typeExpr:vetoedTypes){ - if(type.getAnnotatedType().getJavaClass().getName().matches(typeExpr)){ - type.veto(); - } - } - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-se/src/main/resources/META-INF/beans.xml ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/main/resources/META-INF/beans.xml b/modules/injection/cdi-se/src/main/resources/META-INF/beans.xml deleted file mode 100644 index adee378..0000000 --- a/modules/injection/cdi-se/src/main/resources/META-INF/beans.xml +++ /dev/null @@ -1,24 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- -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 current 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. ---> -<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_0.xsd"> - -</beans> - http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-se/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext b/modules/injection/cdi-se/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext deleted file mode 100644 index 4fe7e01..0000000 --- a/modules/injection/cdi-se/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext +++ /dev/null @@ -1,19 +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 current 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.tamaya.integration.cdi.CDIAwareServiceContext \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/ConfiguredClass.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/ConfiguredClass.java b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/ConfiguredClass.java deleted file mode 100644 index 9d920bc..0000000 --- a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/ConfiguredClass.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 current 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.tamaya.inject.api.Config; - -import java.math.BigDecimal; - -import javax.inject.Singleton; - -/** - * Class to be loaded from CDI to ensure fields are correctly configured using CDI injection mechanisms. - */ -@Singleton -public class ConfiguredClass{ - - @Config - private String testProperty; - - @Config(value = {"a.b.c.key1","a.b.c.key2","a.b.c.key3"}, defaultValue = "The current \\${JAVA_HOME} env property is ${env:JAVA_HOME}.") - String value1; - - @Config({"foo","a.b.c.key2"}) - private String value2; - - @Config(defaultValue = "N/A") - private String runtimeVersion; - - @Config(defaultValue = "${sys:java.version}") - private String javaVersion2; - - @Config(defaultValue = "5") - private Integer int1; - - @Config - private int int2; - - @Config - private boolean booleanT; - - @Config("BD") - private BigDecimal bigNumber; - - @Config("double1") - private double doubleValue; - - public String getTestProperty() { - return testProperty; - } - - public String getValue1() { - return value1; - } - - public String getValue2() { - return value2; - } - - public String getRuntimeVersion() { - return runtimeVersion; - } - - public String getJavaVersion2() { - return javaVersion2; - } - - public Integer getInt1() { - return int1; - } - - public int getInt2() { - return int2; - } - - public boolean isBooleanT() { - return booleanT; - } - - public BigDecimal getBigNumber() { - return bigNumber; - } - - public double getDoubleValue() { - return doubleValue; - } - - @Override - public String toString(){ - return super.toString() + ": testProperty="+testProperty+", value1="+value1+", value2="+value2 - +", int1="+int1+", int2="+int2+", booleanT="+booleanT+", bigNumber="+bigNumber - +", runtimeVersion="+runtimeVersion+", javaVersion2="+javaVersion2; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/ConfiguredTest.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/ConfiguredTest.java b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/ConfiguredTest.java deleted file mode 100644 index 69e8ef9..0000000 --- a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/ConfiguredTest.java +++ /dev/null @@ -1,83 +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 current 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.deltaspike.testcontrol.api.TestControl; -import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner; -import org.hamcrest.MatcherAssert; -import org.junit.Test; -import org.junit.runner.RunWith; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.spi.CDI; -import javax.inject.Singleton; - -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** - * Tests for CDI integration. - */ -@RunWith(CdiTestRunner.class) -@TestControl(startScopes = {ApplicationScoped.class, Singleton.class}) -public class ConfiguredTest{ - - @Test - public void test_Configuration_is_injected_correctly(){ - ConfiguredClass item = CDI.current().select(ConfiguredClass.class).get(); - System.out.println("********************************************"); - System.out.println(item); - System.out.println("********************************************"); - double actual = 1234.5678; - MatcherAssert.assertThat(item.getDoubleValue(), is(actual)); - } - - @Test - public void test_Default_injections_are_accessible(){ - InjectedClass injectedClass = CDI.current().select(InjectedClass.class).get(); - System.out.println("********************************************"); - System.out.println(injectedClass); - System.out.println("********************************************"); - assertNotNull(injectedClass.builder1); - assertNotNull(injectedClass.builder2); - assertNotNull(injectedClass.config); - assertNotNull(injectedClass.configContext); - } - - @Test - public void test_Injected_builders_are_notSame(){ - InjectedClass injectedClass = CDI.current().select(InjectedClass.class).get(); - assertTrue(injectedClass.builder1 != injectedClass.builder2); - } - - @Test - public void test_Injected_configs_are_same(){ - InjectedClass injectedClass = CDI.current().select(InjectedClass.class).get(); - assertTrue(injectedClass.config == injectedClass.config2); - } - - @Test - public void test_Injected_configContexts_are_same(){ - InjectedClass injectedClass = CDI.current().select(InjectedClass.class).get(); - assertTrue(injectedClass.configContext == injectedClass.configContext2); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/InjectedClass.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/InjectedClass.java b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/InjectedClass.java deleted file mode 100644 index 9b7bd23..0000000 --- a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/InjectedClass.java +++ /dev/null @@ -1,62 +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 current 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.tamaya.Configuration; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConfigurationContextBuilder; - -import javax.inject.Inject; -import javax.inject.Singleton; - -/** - * Class loaded by CDI to test correct injection of Configuration API artifacts. - */ -@Singleton -public class InjectedClass { - - @Inject - Configuration config; - - @Inject - Configuration config2; - - @Inject - ConfigurationContext configContext; - - @Inject - ConfigurationContext configContext2; - - @Inject - ConfigurationContextBuilder builder1; - - @Inject - ConfigurationContextBuilder builder2; - - @Override - public String toString() { - return "InjectedClass{" + - "config=" + config + - ", configContext=" + configContext + - ", builder1=" + builder1 + - ", builder2=" + builder2 + - '}'; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/ProvidedPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/ProvidedPropertySource.java b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/ProvidedPropertySource.java deleted file mode 100644 index 90692f3..0000000 --- a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/ProvidedPropertySource.java +++ /dev/null @@ -1,66 +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 current 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.cfg; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; - -import javax.enterprise.inject.Vetoed; -import java.util.HashMap; -import java.util.Map; - -/** - * Created by Anatole on 17.09.2015. - */ -@Vetoed -class ProvidedPropertySource implements PropertySource{ - - final Map<String,PropertyValue> config = new HashMap<>(); - - public ProvidedPropertySource(){ - config.put("a.b.c.key3", PropertyValue.of("a.b.c.key3","keys current a.b.c.key3",getName())); - config.put("a.b.c.key4", PropertyValue.of("a.b.c.key4","keys current a.b.c.key4", getName())); - } - - @Override - public int getOrdinal() { - return 10; - } - - @Override - public String getName() { - return getClass().getName(); - } - - @Override - public PropertyValue get(String key) { - return config.get(key); - } - - @Override - public Map<String, PropertyValue> getProperties() { - return config; - } - - @Override - public boolean isScannable() { - return true; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestConfigProvider.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestConfigProvider.java b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestConfigProvider.java deleted file mode 100644 index 7b89fba..0000000 --- a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestConfigProvider.java +++ /dev/null @@ -1,45 +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 current 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.cfg; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertySourceProvider; - -import javax.enterprise.context.ApplicationScoped; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -/** - * Created by Anatole on 29.09.2014. - */ -@ApplicationScoped -public class TestConfigProvider implements PropertySourceProvider { - - private List<PropertySource> configs = new ArrayList<>(); - - public TestConfigProvider(){ - configs.add(new ProvidedPropertySource()); - } - - @Override - public Collection<PropertySource> getPropertySources() { - return configs; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestPropertySource.java b/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestPropertySource.java deleted file mode 100644 index 6e86b8d..0000000 --- a/modules/injection/cdi-se/src/test/java/org/apache/tamaya/integration/cdi/cfg/TestPropertySource.java +++ /dev/null @@ -1,82 +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 current 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.cfg; - -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; - -import javax.inject.Singleton; -import java.util.HashMap; -import java.util.Map; - -/** - * Created by Anatole on 17.09.2015. - */ -@Singleton -public class TestPropertySource implements PropertySource{ - - final Map<String,String> config = new HashMap<>(); - - public TestPropertySource(){ - config.put("a.b.c.key1", "keys current a.b.c.key1"); - config.put("a.b.c.key2", "keys current a.b.c.key2"); - config.put("a.b.key3", "keys current a.b.key3"); - config.put("a.b.key4", "keys current a.b.key4"); - config.put("a.key5", "keys current a.key5"); - config.put("a.key6", "keys current a.key6"); - config.put("int1", "123456"); - config.put("int2", "111222"); - config.put("testProperty", "testPropertyValue!"); - config.put("booleanT", "true"); - config.put("double1", "1234.5678"); - config.put("BD", "123456789123456789123456789123456789.123456789123456789123456789123456789"); - config.put("testProperty", "keys current testProperty"); - config.put("runtimeVersion", "${java.version}"); - config.put("{meta}source.type:"+getClass().getName(), "PropertySource"); - } - - public int getOrdinal() { - return 10; - } - - @Override - public String getName() { - return getClass().getName(); - } - - @Override - public PropertyValue get(String key) { - String val = this.config.get(key); - if(val!=null) { - return PropertyValue.of(key, val, getName()); - } - return null; - } - - @Override - public Map<String, PropertyValue> getProperties() { - return PropertyValue.map(config ,getName()); - } - - @Override - public boolean isScannable() { - return true; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi-se/src/test/resources/META-INF/beans.xml ---------------------------------------------------------------------- diff --git a/modules/injection/cdi-se/src/test/resources/META-INF/beans.xml b/modules/injection/cdi-se/src/test/resources/META-INF/beans.xml deleted file mode 100644 index adee378..0000000 --- a/modules/injection/cdi-se/src/test/resources/META-INF/beans.xml +++ /dev/null @@ -1,24 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- -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 current 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. ---> -<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_0.xsd"> - -</beans> - http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi/bnd.bnd ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/bnd.bnd b/modules/injection/cdi/bnd.bnd new file mode 100644 index 0000000..a97720d --- /dev/null +++ b/modules/injection/cdi/bnd.bnd @@ -0,0 +1,3 @@ +Export-Package: \ + org.apache.tamaya.inject +Bundle-SymbolicName: org.apache.tamaya.inject-ee \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi/pom.xml ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/pom.xml b/modules/injection/cdi/pom.xml new file mode 100644 index 0000000..24e4ef0 --- /dev/null +++ b/modules/injection/cdi/pom.xml @@ -0,0 +1,213 @@ +<!-- +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 current 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-injection-all</artifactId> + <version>0.3-incubating-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + + <artifactId>tamaya-cdi</artifactId> + <name>Apache Tamaya Modules - Injection CDI</name> + <packaging>jar</packaging> + + <properties> + <owb.version>1.6.2</owb.version> + <weld.version>2.2.7.Final</weld.version> + <geronimo-jcdi-1.1-spec.version>1.0</geronimo-jcdi-1.1-spec.version> + <geronimo-interceptor-1.2-spec.version>1.0</geronimo-interceptor-1.2-spec.version> + <geronimo-atinject-1.0-spec.version>1.0</geronimo-atinject-1.0-spec.version> + <bval.version>0.5</bval.version> + <ds.version>1.1.0</ds.version> + <javaee-api.version>6.0-6</javaee-api.version> + <openejb.version>4.7.1</openejb.version> + <tomee.version>1.7.1</tomee.version> + </properties> + + <dependencies> + <dependency> + <groupId>javax.enterprise</groupId> + <artifactId>cdi-api</artifactId> + <version>1.2</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-injection-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-injection</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>java-hamcrest</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-core</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-api</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-spisupport</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.openejb</groupId> + <artifactId>javaee-api</artifactId> + <version>${javaee-api.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.tomitribe</groupId> + <artifactId>tomitribe-util</artifactId> + <version>1.1.0</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.openejb</groupId> + <artifactId>openejb-core</artifactId> + <version>${openejb.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-jcdi_1.1_spec</artifactId> + <version>${geronimo-jcdi-1.1-spec.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.deltaspike.modules</groupId> + <artifactId>deltaspike-test-control-module-api</artifactId> + <version>${ds.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.deltaspike.modules</groupId> + <artifactId>deltaspike-test-control-module-impl</artifactId> + <version>${ds.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + <profiles> + <!--<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.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> + <activeByDefault>true</activeByDefault> + </activation> + <dependencies> + <dependency> + <groupId>org.jboss.weld.se</groupId> + <artifactId>weld-se</artifactId> + <version>${weld.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.deltaspike.cdictrl</groupId> + <artifactId>deltaspike-cdictrl-weld</artifactId> + <version>${ds.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + </profile> + </profiles> +</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIAwareServiceContext.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIAwareServiceContext.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIAwareServiceContext.java new file mode 100644 index 0000000..5d65ac8 --- /dev/null +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIAwareServiceContext.java @@ -0,0 +1,206 @@ +/* + * 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.tamaya.ConfigException; +import org.apache.tamaya.spi.ServiceContext; + +import javax.annotation.Priority; +import javax.enterprise.inject.Instance; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import java.io.IOException; +import java.net.URL; +import java.text.MessageFormat; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + + +/** + * <p>This class implements a {@link ServiceContext}, which basically provides a similar loading mechanism as used + * by the {@link ServiceLoader}. Whereas the {@link ServiceLoader} only loads configurations + * and instances from one classloader, this loader manages configs found and the related instances for each + * classloader along the classloader hierarchies individually. It ensures instances are loaded on the classloader + * level, where they first are visible. Additionally it ensures the same configuration resource (and its + * declared services) are loaded multiple times, when going up the classloader hierarchy.</p> + * + * <p>Finally classloaders are not stored by reference by this class, to ensure they still can be garbage collected. + * Refer also the inherited parent class for further details.</p> + * + * <p>This class uses an ordinal of {@code 10}, so it overrides any default {@link ServiceContext} implementations + * provided with the Tamaya core modules.</p> + */ +public class CDIAwareServiceContext implements ServiceContext { + + /** + * Singletons. + */ + private final Map<Class<?>, Object> singletons = new ConcurrentHashMap<>(); + + private ServiceContext defaultServiceContext = new ServiceLoaderServiceContext(); + + + @Override + public <T> T getService(Class<T> serviceType) { + Object cached = singletons.get(serviceType); + if (cached == null) { + Collection<T> services = getServices(serviceType); + if (services.isEmpty()) { + cached = null; + } else { + cached = getServiceWithHighestPriority(services, serviceType); + } + if(cached!=null) { + singletons.put(serviceType, cached); + } + } + return serviceType.cast(cached); + } + + @Override + public <T> T create(Class<T> serviceType) { + T serv = getService(serviceType); + if(serv!=null){ + try { + return (T)serv.getClass().newInstance(); + } catch (Exception e) { + Logger.getLogger(getClass().getName()) + .log(Level.SEVERE, "Failed to create new instance of: " +serviceType.getName(), e); + } + } + return null; + } + + /** + * Loads and registers services. + * + * @param <T> the concrete type. + * @param serviceType The service type. + * @return the items found, never {@code null}. + */ + @Override + public <T> List<T> getServices(final Class<T> serviceType) { + List<T> found = defaultServiceContext.getServices(serviceType); + BeanManager beanManager = TamayaCDIAccessor.getBeanManager(); + Instance<T> cdiInstances = null; + if(beanManager!=null){ + Set<Bean<?>> instanceBeans = beanManager.getBeans(Instance.class); + Bean<?> bean = instanceBeans.iterator().next(); + cdiInstances = (Instance<T>)beanManager.getReference(bean, Instance.class, + beanManager.createCreationalContext(bean)); + } + if(cdiInstances!=null){ + for(T t:cdiInstances.select(serviceType)){ + found.add(t); + } + } + return found; + } + + @Override + public Enumeration<URL> getResources(String resource, ClassLoader cl) throws IOException { + if(cl==null){ + cl = Thread.currentThread().getContextClassLoader(); + } + if(cl==null){ + cl = getClass().getClassLoader(); + } + return cl.getResources(resource); + } + + @Override + public URL getResource(String resource, ClassLoader cl) { + if(cl==null){ + cl = Thread.currentThread().getContextClassLoader(); + } + if(cl==null){ + cl = getClass().getClassLoader(); + } + return cl.getResource(resource); + } + + /** + * Checks the given instance for a @Priority annotation. If present the annotation's value s evaluated. If no such + * annotation is present, a default priority is returned (1); + * @param o the instance, not null. + * @return a priority, by default 1. + */ + public static int getPriority(Object o){ + int prio = 1; //X TODO discuss default priority + Priority priority = o.getClass().getAnnotation(Priority.class); + if (priority != null) { + prio = priority.value(); + } + return prio; + } + + /** + * @param services to scan + * @param <T> type of the service + * + * @return the service with the highest {@link Priority#value()} + * + * @throws ConfigException if there are multiple service implementations with the maximum priority + */ + private <T> T getServiceWithHighestPriority(Collection<T> services, Class<T> serviceType) { + + // we do not need the priority stuff if the list contains only one element + if (services.size() == 1) { + return services.iterator().next(); + } + + Integer highestPriority = null; + int highestPriorityServiceCount = 0; + T highestService = null; + + for (T service : services) { + int prio = getPriority(service); + if (highestPriority == null || highestPriority < prio) { + highestService = service; + highestPriorityServiceCount = 1; + highestPriority = prio; + } else if (highestPriority == prio) { + highestPriorityServiceCount++; + } + } + + if (highestPriorityServiceCount > 1) { + throw new ConfigException(MessageFormat.format("Found {0} implementations for Service {1} with Priority {2}: {3}", + highestPriorityServiceCount, + serviceType.getName(), + highestPriority, + services)); + } + + return highestService; + } + + /** + * Returns ordinal of 20, overriding defaults as well as the inherited (internally used) CLAwareServiceContext + * instance. + * @return ordinal of 20. + */ + @Override + public int ordinal() { + return 20; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIConfiguredField.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIConfiguredField.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIConfiguredField.java new file mode 100644 index 0000000..fa826f5 --- /dev/null +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIConfiguredField.java @@ -0,0 +1,77 @@ +/* + * 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.tamaya.Configuration; +import org.apache.tamaya.inject.spi.ConfiguredField; + +import javax.enterprise.inject.spi.InjectionPoint; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * CDI implementation for event publishing of configured instances. + */ +class CDIConfiguredField implements ConfiguredField{ + + private final Field field; + private List<String> keys = new ArrayList<>(); + + CDIConfiguredField(InjectionPoint injectionPoint, List<String> keys){ + this.field = (Field)injectionPoint.getMember(); + this.keys.addAll(keys); + this.keys = Collections.unmodifiableList(this.keys); + } + + @Override + public Class<?> getType() { + return field.getType(); + } + + @Override + public Collection<String> getConfiguredKeys() { + return keys; + } + + @Override + public Field getAnnotatedField() { + return field; + } + + @Override + public String getName() { + return field.getName(); + } + + @Override + public String getSignature() { + return getName()+':'+field.getType().getName(); + } + + @Override + public void configure(Object instance, Configuration config) { + throw new UnsupportedOperationException("Use CDI annotations for configuration injection."); + } + + @Override + public String toString() { + return "CDIConfiguredField["+getSignature()+']'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIConfiguredMethod.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIConfiguredMethod.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIConfiguredMethod.java new file mode 100644 index 0000000..ed8019a --- /dev/null +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIConfiguredMethod.java @@ -0,0 +1,77 @@ +/* + * 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.tamaya.Configuration; +import org.apache.tamaya.inject.spi.ConfiguredMethod; + +import javax.enterprise.inject.spi.InjectionPoint; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * Implementation of a configured methods for CDI module. + */ +public class CDIConfiguredMethod implements ConfiguredMethod{ + + private final Method method; + private List<String> keys = new ArrayList<>(); + + CDIConfiguredMethod(InjectionPoint injectionPoint, List<String> keys){ + this.method = (Method)injectionPoint.getMember(); + this.keys.addAll(keys); + this.keys = Collections.unmodifiableList(this.keys); + } + + @Override + public Collection<String> getConfiguredKeys() { + return keys; + } + + @Override + public Class<?>[] getParameterTypes() { + return method.getParameterTypes(); + } + + @Override + public Method getAnnotatedMethod() { + return method; + } + + @Override + public String getName() { + return method.getName(); + } + + @Override + public String getSignature() { + return null; + } + + @Override + public void configure(Object instance, Configuration config) { + throw new UnsupportedOperationException("Use CDI annotations for configuration injection."); + } + + @Override + public String toString() { + return "CDIConfiguredMethod["+getSignature()+']'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b3bcddcb/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIConfiguredType.java ---------------------------------------------------------------------- diff --git a/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIConfiguredType.java b/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIConfiguredType.java new file mode 100644 index 0000000..c677065 --- /dev/null +++ b/modules/injection/cdi/src/main/java/org/apache/tamaya/integration/cdi/CDIConfiguredType.java @@ -0,0 +1,94 @@ +/* + * 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.tamaya.Configuration; +import org.apache.tamaya.inject.spi.ConfiguredField; +import org.apache.tamaya.inject.spi.ConfiguredMethod; +import org.apache.tamaya.inject.spi.ConfiguredType; + +import javax.enterprise.inject.spi.InjectionPoint; +import java.lang.reflect.Field; +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; + +/** + * Event published for items configured by CDI extensions. This is for example used by the documentation module + * to automatically track the configuration endpoints for documentation. + */ +class CDIConfiguredType implements ConfiguredType{ + + private final Class<?> type; + private final List<CDIConfiguredMethod> methods = new ArrayList<>(); + private final List<CDIConfiguredField> fields = new ArrayList<>(); + + public CDIConfiguredType(Class<?> type){ + this.type = Objects.requireNonNull(type); + } + + @Override + public Class getType() { + return type; + } + + @Override + public String getName() { + return type.getName(); + } + + @Override + public Collection<ConfiguredField> getConfiguredFields() { + return null; + } + + @Override + public Collection<ConfiguredMethod> getConfiguredMethods() { + return null; + } + + @Override + public void configure(Object instance, Configuration config) { + throw new UnsupportedOperationException("Use CDI annotations for configuration injection."); + } + + /** + * Used to build up during injection point processing. + * @param injectionPoint the CDI injection ppint, not null. + * @param keys the possible config keys, in order of precedence, not null. + */ + void addConfiguredMember(InjectionPoint injectionPoint, List<String> keys) { + Member member = injectionPoint.getMember(); + if(member instanceof Field){ + this.fields.add(new CDIConfiguredField(injectionPoint, keys)); + } else if(member instanceof Method){ + this.methods.add(new CDIConfiguredMethod(injectionPoint, keys)); + } + } + + @Override + public String toString() { + return "CDIConfiguredType{" + + "type=" + type + + ", methods=" + methods + + ", fields=" + fields + + '}'; + } +}