You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2015/09/16 09:10:34 UTC

[5/6] incubator-tamaya git commit: Added support for multiple classloaders (draft). Started with CDI integration module.

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/66c2ef64/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/ConfigurationProducer.java
----------------------------------------------------------------------
diff --git a/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/ConfigurationProducer.java b/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/ConfigurationProducer.java
new file mode 100644
index 0000000..0cbfa66
--- /dev/null
+++ b/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/ConfigurationProducer.java
@@ -0,0 +1,52 @@
+/*
+ * 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.ConfigurationProvider;
+import org.apache.tamaya.integration.cdi.annot.System;
+import org.apache.tamaya.spi.ConfigurationContext;
+import org.apache.tamaya.spi.ConfigurationContextBuilder;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.ProcessAnnotatedType;
+
+/**
+ * CDI Extension that can be used to veto on beans by configuring the fully qualified class names (as reges expression)
+ * to {@code javax.enterprise.inject.vetoed}.
+ */
+public class ConfigurationProducer {
+
+    @Produces @System
+    public Configuration getSystemConfig(){
+        return ConfigurationProvider.getConfiguration();
+    }
+
+    @Produces @System
+    public ConfigurationContext getSystemConfigContext(){
+        return ConfigurationProvider.getConfigurationContext();
+    }
+
+    @Produces @System
+    public ConfigurationContextBuilder getSystemConfigContextBuilder(){
+        return ConfigurationProvider.getConfigurationContextBuilder();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/66c2ef64/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/ConfiguredVetoExtension.java
----------------------------------------------------------------------
diff --git a/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/ConfiguredVetoExtension.java b/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/ConfiguredVetoExtension.java
new file mode 100644
index 0000000..32821c4
--- /dev/null
+++ b/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/ConfiguredVetoExtension.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.integration.cdi.internal;
+
+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 reges expression)
+ * to {@code javax.enterprise.inject.vetoed}.
+ */
+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/blob/66c2ef64/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/SEInjectorCDIExtension.java
----------------------------------------------------------------------
diff --git a/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/SEInjectorCDIExtension.java b/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/SEInjectorCDIExtension.java
new file mode 100644
index 0000000..1d3b945
--- /dev/null
+++ b/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/SEInjectorCDIExtension.java
@@ -0,0 +1,80 @@
+/*
+ * 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.internal.ConfiguredType;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Vetoed;
+import javax.enterprise.inject.spi.*;
+import java.util.*;
+
+/**
+ * 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 {
+
+    public <T> void initializeConfiguredFields(final @Observes ProcessInjectionTarget<T> pit) {
+        final AnnotatedType<T> at = pit.getAnnotatedType();
+        if (!ConfiguredType.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);
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/66c2ef64/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/TamayaCDIIntegrationExtension.java
----------------------------------------------------------------------
diff --git a/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/TamayaCDIIntegrationExtension.java b/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/TamayaCDIIntegrationExtension.java
new file mode 100644
index 0000000..8a948e1
--- /dev/null
+++ b/sandbox/integration/cdi/src/main/java/org/apache/tamaya/integration/cdi/internal/TamayaCDIIntegrationExtension.java
@@ -0,0 +1,34 @@
+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 integreation with CDI, especially storing the BeanManager reference for implementation, where no
+ * JNDI is available or {@code java:comp/env/BeanManager} is not set correctly.
+ */
+public class TamayaCDIIntegrationExtension implements Extension{
+    /** The BeanManager references stored. */
+    private static BeanManager beanManager;
+
+    /**
+     * Initializes the current BeanMaanager with the instance passed.
+     * @param validation the event
+     * @param beanManager the BeanManager instance
+     */
+    public void initBeanManager(@Observes AfterDeploymentValidation validation, BeanManager beanManager){
+        TamayaCDIIntegrationExtension.beanManager = beanManager;
+    }
+
+    /**
+     * Get the current {@link  BeanManager} instance.
+     * @return
+     */
+    public static BeanManager getBeanManager(){
+        return beanManager;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/66c2ef64/sandbox/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
----------------------------------------------------------------------
diff --git a/sandbox/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/sandbox/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
index 3b909e7..696626b 100644
--- a/sandbox/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
+++ b/sandbox/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
@@ -16,4 +16,4 @@
 # specific language governing permissions and limitations
 # under the License.
 #
-org.apache.tamaya.integration.cdi.internal.ConfigurationExtension
+org.apache.tamaya.integration.cdi.internal.SEInjectorCDIExtension