camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hadr...@apache.org
Subject svn commit: r1362746 - in /camel/trunk/components/camel-cdi: ./ src/main/java/org/apache/camel/component/cdi/ src/main/java/org/apache/camel/component/cdi/internal/ src/main/resources/META-INF/services/ src/test/java/org/apache/camel/cdi/
Date Wed, 18 Jul 2012 02:29:49 GMT
Author: hadrian
Date: Wed Jul 18 02:29:49 2012
New Revision: 1362746

URL: http://svn.apache.org/viewvc?rev=1362746&view=rev
Log:
CAMEL-5447. Camel context creation in CDI containers. Patch applied with thatnks to Lukasz

Added:
    camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/
    camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelContextBean.java
  (with props)
    camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java
  (with props)
    camel/trunk/components/camel-cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
    camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelExtensionTest.java
      - copied, changed from r1362634, camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java
    camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiContextTestSupport.java
      - copied, changed from r1362634, camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java
    camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiTestSupport.java
  (contents, props changed)
      - copied, changed from r1362634, camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java
Removed:
    camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java
Modified:
    camel/trunk/components/camel-cdi/pom.xml
    camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java
    camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiInjector.java
    camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/RegistryLookupAndInjectorTest.java

Modified: camel/trunk/components/camel-cdi/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/pom.xml?rev=1362746&r1=1362745&r2=1362746&view=diff
==============================================================================
--- camel/trunk/components/camel-cdi/pom.xml (original)
+++ camel/trunk/components/camel-cdi/pom.xml Wed Jul 18 02:29:49 2012
@@ -103,6 +103,17 @@
     </dependency>
   </dependencies>
 
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <forkMode>pertest</forkMode>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
   <profiles>
     <profile>
       <id>owb</id>

Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java?rev=1362746&r1=1362745&r2=1362746&view=diff
==============================================================================
--- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java
(original)
+++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java
Wed Jul 18 02:29:49 2012
@@ -16,7 +16,13 @@
  */
 package org.apache.camel.component.cdi;
 
+import javax.annotation.PostConstruct;
+import javax.enterprise.inject.Instance;
+import javax.inject.Inject;
+
 import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.spi.Injector;
+import org.apache.camel.spi.Registry;
 
 /**
  * CDI {@link org.apache.camel.CamelContext} class.
@@ -24,8 +30,26 @@ import org.apache.camel.impl.DefaultCame
 public class CdiCamelContext extends DefaultCamelContext {
 
     public CdiCamelContext() {
-        setRegistry(new CdiBeanRegistry());
+        super(new CdiBeanRegistry());
         setInjector(new CdiInjector(getInjector()));
     }
 
-}
\ No newline at end of file
+    @Inject
+    public void setRegistry(Instance<Registry> instance) {
+        if (isSingular(instance)) {
+            setRegistry(instance.get());
+        }
+    }
+
+    @Inject
+    public void setInjector(Instance<Injector> instance) {
+        if (isSingular(instance)) {
+            setInjector(instance.get());
+        }
+    }
+
+    private <T> boolean isSingular(Instance<T> instance) {
+        return !instance.isUnsatisfied() && !instance.isAmbiguous();
+    }
+
+}

Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiInjector.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiInjector.java?rev=1362746&r1=1362745&r2=1362746&view=diff
==============================================================================
--- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiInjector.java
(original)
+++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiInjector.java
Wed Jul 18 02:29:49 2012
@@ -18,6 +18,7 @@ package org.apache.camel.component.cdi;
 
 import org.apache.camel.IsSingleton;
 import org.apache.camel.spi.Injector;
+import org.apache.camel.util.ReflectionInjector;
 import org.apache.deltaspike.core.api.provider.BeanProvider;
 
 /**
@@ -30,6 +31,10 @@ public class CdiInjector implements Inje
      */
     private Injector injector;
 
+    public CdiInjector() {
+        this(new ReflectionInjector());
+    }
+
     public CdiInjector(Injector parent) {
         this.injector = parent;
     }

Added: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelContextBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelContextBean.java?rev=1362746&view=auto
==============================================================================
--- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelContextBean.java
(added)
+++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelContextBean.java
Wed Jul 18 02:29:49 2012
@@ -0,0 +1,108 @@
+/**
+ * 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.camel.component.cdi.internal;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.inject.spi.InjectionTarget;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.component.cdi.CdiCamelContext;
+import org.apache.deltaspike.core.api.literal.AnyLiteral;
+import org.apache.deltaspike.core.api.literal.DefaultLiteral;
+
+/**
+ * Description of camel context bean.
+ */
+@SuppressWarnings("unchecked")
+public class CamelContextBean implements Bean<CdiCamelContext> {
+
+    private final InjectionTarget<CdiCamelContext> target;
+
+    public CamelContextBean(InjectionTarget<CdiCamelContext> injectionTarget) {
+        this.target = injectionTarget;
+    }
+
+    @Override
+    public CdiCamelContext create(CreationalContext<CdiCamelContext> context) {
+        CdiCamelContext camelContext = target.produce(context);
+        target.postConstruct(camelContext);
+        context.push(camelContext);
+        return camelContext;
+    }
+
+    @Override
+    public void destroy(CdiCamelContext instance, CreationalContext<CdiCamelContext>
context) {
+        target.preDestroy(instance);
+        target.dispose(instance);
+        context.release();
+    }
+
+    @Override
+    public Set<Type> getTypes() {
+        return new HashSet<Type>(Arrays.asList(Object.class, CamelContext.class, CdiCamelContext.class));
+    }
+
+    @Override
+    public Set<Annotation> getQualifiers() {
+        return new HashSet<Annotation>(Arrays.asList(new DefaultLiteral(), new AnyLiteral()));
+    }
+
+    @Override
+    public Class<? extends Annotation> getScope() {
+        return ApplicationScoped.class;
+    }
+
+    @Override
+    public String getName() {
+        return "CamelContext";
+    }
+
+    @Override
+    public boolean isNullable() {
+        return false;
+    }
+
+    @Override
+    public Set<InjectionPoint> getInjectionPoints() {
+        return target.getInjectionPoints();
+    }
+
+    @Override
+    public Class<?> getBeanClass() {
+        return CdiCamelContext.class;
+    }
+
+    @Override
+    public Set<Class<? extends Annotation>> getStereotypes() {
+        return null;
+    }
+
+    @Override
+    public boolean isAlternative() {
+        return false;
+    }
+
+}

Propchange: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelContextBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java?rev=1362746&view=auto
==============================================================================
--- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java
(added)
+++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java
Wed Jul 18 02:29:49 2012
@@ -0,0 +1,82 @@
+/**
+ * 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.camel.component.cdi.internal;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.AfterDeploymentValidation;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.BeforeShutdown;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.ProcessAnnotatedType;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.component.cdi.CdiCamelContext;
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+
+/**
+ * Set of camel specific hooks for CDI.
+ */
+public class CamelExtension implements Extension {
+
+    /**
+     * Context instance.
+     */
+    private CamelContext camelContext;
+
+    /**
+     * Disable creation of default CamelContext bean and rely on context created
+     * and managed by extension.
+     *
+     * @param process Annotated type.
+     */
+    protected void disableDefaultContext(@Observes ProcessAnnotatedType<CamelContext>
process) {
+        process.veto();
+    }
+
+    /**
+     * Registers managed camel bean.
+     * 
+     * @param abd After bean discovery event.
+     * @param manager Bean manager.
+     */
+    protected void registerManagedCamelContext(@Observes AfterBeanDiscovery abd, BeanManager
manager) {
+        abd.addBean(new CamelContextBean(manager.createInjectionTarget(manager.createAnnotatedType(CdiCamelContext.class))));
+    }
+
+    /**
+     * Start up camel context.
+     * 
+     * @param adv After deployment validation event.
+     * @throws Exception In case of failures.
+     */
+    protected void validate(@Observes AfterDeploymentValidation adv) throws Exception {
+        camelContext = BeanProvider.getContextualReference(CamelContext.class);
+        camelContext.start();
+    }
+
+    /**
+     * Shutdown camel context.
+     * 
+     * @param bsd Shutdown event.
+     * @throws Exception In case of failures.
+     */
+    protected void shutdown(@Observes BeforeShutdown bsd) throws Exception {
+        camelContext.stop();
+    }
+
+}

Propchange: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: camel/trunk/components/camel-cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension?rev=1362746&view=auto
==============================================================================
--- camel/trunk/components/camel-cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
(added)
+++ camel/trunk/components/camel-cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
Wed Jul 18 02:29:49 2012
@@ -0,0 +1 @@
+org.apache.camel.component.cdi.internal.CamelExtension
\ No newline at end of file

Copied: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelExtensionTest.java
(from r1362634, camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelExtensionTest.java?p2=camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelExtensionTest.java&p1=camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java&r1=1362634&r2=1362746&rev=1362746&view=diff
==============================================================================
--- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/CdiCamelContext.java
(original)
+++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelExtensionTest.java
Wed Jul 18 02:29:49 2012
@@ -14,18 +14,26 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.component.cdi;
+package org.apache.camel.cdi;
 
-import org.apache.camel.impl.DefaultCamelContext;
+import javax.inject.Inject;
+
+import org.apache.camel.CamelContext;
+import org.junit.Test;
 
 /**
- * CDI {@link org.apache.camel.CamelContext} class.
+ * Test camel extension.
  */
-public class CdiCamelContext extends DefaultCamelContext {
+public class CamelExtensionTest extends CdiTestSupport {
+
+    @Inject
+    private CamelContext context;
 
-    public CdiCamelContext() {
-        setRegistry(new CdiBeanRegistry());
-        setInjector(new CdiInjector(getInjector()));
+    @Test
+    public void shouldBeStartedByTestFramework() {
+        assertTrue(context.getStatus().isStarted());
+        assertFalse(context.getStatus().isStopped());
+        assertFalse(context.getStatus().isStopping());
     }
 
-}
\ No newline at end of file
+}

Copied: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiContextTestSupport.java
(from r1362634, camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiContextTestSupport.java?p2=camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiContextTestSupport.java&p1=camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java&r1=1362634&r2=1362746&rev=1362746&view=diff
==============================================================================
--- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java
(original)
+++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiContextTestSupport.java
Wed Jul 18 02:29:49 2012
@@ -19,10 +19,10 @@ package org.apache.camel.cdi;
 import java.util.logging.LogManager;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.component.cdi.CdiCamelContext;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.apache.deltaspike.cdise.api.CdiContainer;
 import org.apache.deltaspike.cdise.api.CdiContainerLoader;
+import org.apache.deltaspike.core.api.provider.BeanProvider;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -30,7 +30,7 @@ import org.junit.BeforeClass;
 /**
  * Base class for cdi tests.
  */
-public abstract class CamelCdiTestContainer extends CamelTestSupport {
+public abstract class CdiContextTestSupport extends CamelTestSupport {
 
     private CdiContainer cdiContainer;
 
@@ -51,19 +51,21 @@ public abstract class CamelCdiTestContai
         cdiContainer = CdiContainerLoader.getCdiContainer();
         cdiContainer.boot();
 
+        // inject fields inside child classes
+        BeanProvider.injectFields(this);
+
         super.setUp();
     }
 
     @After
     public void tearDown() throws Exception {
         super.tearDown();
-        context.stop();
         cdiContainer.shutdown();
     }
 
     @Override
     protected CamelContext createCamelContext() throws Exception {
-        return new CdiCamelContext();
+        return BeanProvider.getContextualReference(CamelContext.class);
     }
 }
 

Copied: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiTestSupport.java
(from r1362634, camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiTestSupport.java?p2=camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiTestSupport.java&p1=camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java&r1=1362634&r2=1362746&rev=1362746&view=diff
==============================================================================
--- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CamelCdiTestContainer.java
(original)
+++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiTestSupport.java
Wed Jul 18 02:29:49 2012
@@ -18,11 +18,10 @@ package org.apache.camel.cdi;
 
 import java.util.logging.LogManager;
 
-import org.apache.camel.CamelContext;
-import org.apache.camel.component.cdi.CdiCamelContext;
-import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.camel.test.junit4.TestSupport;
 import org.apache.deltaspike.cdise.api.CdiContainer;
 import org.apache.deltaspike.cdise.api.CdiContainerLoader;
+import org.apache.deltaspike.core.api.provider.BeanProvider;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -30,7 +29,7 @@ import org.junit.BeforeClass;
 /**
  * Base class for cdi tests.
  */
-public abstract class CamelCdiTestContainer extends CamelTestSupport {
+public abstract class CdiTestSupport extends TestSupport {
 
     private CdiContainer cdiContainer;
 
@@ -51,19 +50,13 @@ public abstract class CamelCdiTestContai
         cdiContainer = CdiContainerLoader.getCdiContainer();
         cdiContainer.boot();
 
-        super.setUp();
+        BeanProvider.injectFields(this);
     }
 
     @After
     public void tearDown() throws Exception {
-        super.tearDown();
-        context.stop();
         cdiContainer.shutdown();
     }
 
-    @Override
-    protected CamelContext createCamelContext() throws Exception {
-        return new CdiCamelContext();
-    }
 }
 

Propchange: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/CdiTestSupport.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/RegistryLookupAndInjectorTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/RegistryLookupAndInjectorTest.java?rev=1362746&r1=1362745&r2=1362746&view=diff
==============================================================================
--- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/RegistryLookupAndInjectorTest.java
(original)
+++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/RegistryLookupAndInjectorTest.java
Wed Jul 18 02:29:49 2012
@@ -26,7 +26,7 @@ import org.apache.camel.cdi.store.Shoppi
 import org.apache.camel.component.mock.MockEndpoint;
 import org.junit.Test;
 
-public class RegistryLookupAndInjectorTest extends CamelCdiTestContainer {
+public class RegistryLookupAndInjectorTest extends CdiContextTestSupport {
 
     private MockEndpoint resultEndpoint;
 



Mime
View raw message