Author: jstrachan
Date: Mon Sep  3 09:30:51 2012
New Revision: 1380183

URL: http://svn.apache.org/viewvc?rev=1380183&view=rev
Log:
added support for @Consume for CAMEL-5553 along with supporting @Inject of 
ProducerTemplate

Added:
    
camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java
   (with props)
    
camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelFactory.java
   (contents, props changed)
      - copied, changed from r1380149, 
camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java
    
camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ConsumeTest.java
   (with props)
Removed:
    
camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java
Modified:
    
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/BeanAdapter.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java?rev=1380183&view=auto
==============================================================================
--- 
camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java
 (added)
+++ 
camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/BeanAdapter.java
 Mon Sep  3 09:30:51 2012
@@ -0,0 +1,47 @@
+/**
+ *
+ * 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.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import javax.enterprise.inject.spi.Bean;
+
+/**
+ * Contains the bean and the consume methods
+ */
+public class BeanAdapter {
+    private final Bean<?> bean;
+    private final List<Method> consumeMethods = new ArrayList<Method>();
+
+    public BeanAdapter(Bean<?> bean) {
+        this.bean = bean;
+    }
+
+    public Bean<?> getBean() {
+        return bean;
+    }
+
+    public List<Method> getConsumeMethods() {
+        return consumeMethods;
+    }
+
+    public void addConsumeMethod(Method method) {
+        consumeMethods.add(method);
+    }
+}

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

Modified: 
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=1380183&r1=1380182&r2=1380183&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java
 (original)
+++ 
camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelExtension.java
 Mon Sep  3 09:30:51 2012
@@ -17,19 +17,29 @@
 package org.apache.camel.component.cdi.internal;
 
 import java.lang.reflect.Method;
-
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.event.Observes;
 import javax.enterprise.inject.spi.AfterBeanDiscovery;
 import javax.enterprise.inject.spi.AfterDeploymentValidation;
 import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.Bean;
 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 javax.enterprise.inject.spi.ProcessBean;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
+import org.apache.camel.Consume;
 import org.apache.camel.component.cdi.CdiCamelContext;
+import org.apache.camel.impl.CamelPostProcessorHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ReflectionHelper;
 import org.apache.deltaspike.core.api.provider.BeanProvider;
 import org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder;
 
@@ -43,9 +53,11 @@ public class CamelExtension implements E
      */
     private CamelContext camelContext;
 
+    private Map<Bean<?>, BeanAdapter> beanAdapters = new HashMap<Bean<?>, 
BeanAdapter>();
+
     /**
      * Process camel context aware bean definitions.
-     * 
+     *
      * @param process Annotated type.
      * @throws Exception In case of exceptions.
      */
@@ -55,8 +67,8 @@ public class CamelExtension implements E
         if (CamelContextAware.class.isAssignableFrom(javaClass)) {
             Method method = javaClass.getMethod("setCamelContext", 
CamelContext.class);
             AnnotatedTypeBuilder<CamelContextAware> builder = new 
AnnotatedTypeBuilder<CamelContextAware>()
-                .readFromType(javaClass)
-                .addToMethod(method, new InjectLiteral());
+                    .readFromType(javaClass)
+                    .addToMethod(method, new InjectLiteral());
             process.setAnnotatedType(builder.create());
         }
     }
@@ -73,8 +85,8 @@ public class CamelExtension implements E
 
     /**
      * Registers managed camel bean.
-     * 
-     * @param abd After bean discovery event.
+     *
+     * @param abd     After bean discovery event.
      * @param manager Bean manager.
      */
     protected void registerManagedCamelContext(@Observes AfterBeanDiscovery 
abd, BeanManager manager) {
@@ -83,23 +95,71 @@ public class CamelExtension implements E
 
     /**
      * 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();
+        getCamelContext().start();
     }
 
     /**
      * Shutdown camel context.
-     * 
+     *
      * @param bsd Shutdown event.
      * @throws Exception In case of failures.
      */
     protected void shutdown(@Observes BeforeShutdown bsd) throws Exception {
-        camelContext.stop();
+        if (camelContext != null) {
+            camelContext.stop();
+        }
+    }
+
+    public void detectConsumeBeans(@Observes ProcessBean<?> event) {
+        final Bean<?> bean = event.getBean();
+        ReflectionHelper.doWithMethods(bean.getBeanClass(), new 
ReflectionHelper.MethodCallback() {
+            @Override
+            public void doWith(Method method) throws IllegalArgumentException, 
IllegalAccessException {
+                Consume consume = method.getAnnotation(Consume.class);
+                if (consume != null) {
+                    BeanAdapter beanAdapter = getBeanAdapter(bean);
+                    beanAdapter.addConsumeMethod(method);
+                }
+            }
+        });
     }
 
+    public void initializeBeans(@Observes AfterDeploymentValidation event, 
BeanManager beanManager) {
+        ObjectHelper.notNull(getCamelContext(), "camelContext");
+        CamelPostProcessorHelper postProcessor = new 
CamelPostProcessorHelper(getCamelContext());
+        Collection<BeanAdapter> adapters = beanAdapters.values();
+        for (BeanAdapter adapter : adapters) {
+            Bean<?> bean = adapter.getBean();
+            CreationalContext<?> creationalContext = 
beanManager.createCreationalContext(bean);
+
+            Object reference = beanManager.getReference(bean, Object.class, 
creationalContext);
+            String beanName = bean.getName();
+
+            List<Method> consumeMethods = adapter.getConsumeMethods();
+            for (Method method : consumeMethods) {
+                postProcessor.consumerInjection(method, reference, beanName);
+            }
+        }
+    }
+
+    protected BeanAdapter getBeanAdapter(Bean<?> bean) {
+        BeanAdapter beanAdapter = beanAdapters.get(bean);
+        if (beanAdapter == null) {
+            beanAdapter = new BeanAdapter(bean);
+            beanAdapters.put(bean, beanAdapter);
+        }
+        return beanAdapter;
+    }
+
+    public CamelContext getCamelContext() {
+        if (camelContext == null) {
+            camelContext = 
BeanProvider.getContextualReference(CamelContext.class);
+        }
+        return camelContext;
+    }
 }

Copied: 
camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelFactory.java
 (from r1380149, 
camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelFactory.java?p2=camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelFactory.java&p1=camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java&r1=1380149&r2=1380183&rev=1380183&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java
 (original)
+++ 
camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/CamelFactory.java
 Mon Sep  3 09:30:51 2012
@@ -17,7 +17,6 @@
 package org.apache.camel.component.cdi.internal;
 
 import java.lang.reflect.Type;
-
 import javax.enterprise.inject.Produces;
 import javax.enterprise.inject.spi.InjectionPoint;
 import javax.inject.Inject;
@@ -25,21 +24,22 @@ import javax.inject.Inject;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.EndpointInject;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
 import org.apache.camel.component.cdi.Mock;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.util.ObjectHelper;
 
 /**
- * Injects endpoints into beans
+ * produces {@link Endpoint} and {@link org.apache.camel.ProducerTemplate} 
instances for injection into beans
  */
-public class EndpointInjector {
+public class CamelFactory {
     @Inject
     private CamelContext camelContext;
 
-
     @Produces
     @Mock
-    protected MockEndpoint createMockEndpoint(InjectionPoint point) {
+    public MockEndpoint createMockEndpoint(InjectionPoint point) {
         String url = "";
         String name = "";
         EndpointInject annotation = 
point.getAnnotated().getAnnotation(EndpointInject.class);
@@ -56,24 +56,50 @@ public class EndpointInjector {
         return camelContext.getEndpoint(url, MockEndpoint.class);
     }
 
+    @SuppressWarnings("unchecked")
     @Produces
     public Endpoint createEndpoint(InjectionPoint point) {
         Class<? extends Endpoint> endpointType = Endpoint.class;
         Type pointType = point.getType();
         if (pointType instanceof Class<?>) {
-            endpointType = (Class<? extends Endpoint>) pointType;
+            endpointType = (Class<? extends Endpoint>)pointType;
         }
         EndpointInject annotation = 
point.getAnnotated().getAnnotation(EndpointInject.class);
         if (annotation != null) {
             String uri = annotation.uri();
-            if (ObjectHelper.isNotEmpty(uri)) {
-                return camelContext.getEndpoint(uri, endpointType);
+            if (ObjectHelper.isEmpty(uri)) {
+                String ref = annotation.ref();
+                if (ObjectHelper.isNotEmpty(ref)) {
+                    uri = "ref:" + ref;
+                } else {
+
+                }
             }
+            return camelContext.getEndpoint(uri, endpointType);
+        }
+        throw new IllegalArgumentException(
+                "Could not create instance of Endpoint for the given injection 
point " + point);
+    }
+
+    @Produces
+    public ProducerTemplate createProducerTemplate(InjectionPoint point) {
+        ProducerTemplate producerTemplate = 
camelContext.createProducerTemplate();
+        Produce annotation = point.getAnnotated().getAnnotation(Produce.class);
+        if (annotation != null) {
+            String uri = annotation.uri();
             String ref = annotation.ref();
-            if (ObjectHelper.isNotEmpty(ref)) {
-                return camelContext.getEndpoint("ref:" + ref, endpointType);
+            String property = annotation.property();
+            if (ObjectHelper.isEmpty(uri)) {
+                if (ObjectHelper.isNotEmpty(ref)) {
+                    uri = "ref:" + ref;
+                } else {
+                    ObjectHelper.notEmpty(property, "uri, ref or property", 
annotation);
+                    // now lets get the property value
+                    throw new UnsupportedOperationException("property not yet 
supported");
+                }
             }
+            producerTemplate.setDefaultEndpointUri(uri);
         }
-        throw new IllegalArgumentException("Could not create instance of 
Endpoint for the given injection point " + point);
+        return producerTemplate;
     }
 }

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

Added: 
camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ConsumeTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ConsumeTest.java?rev=1380183&view=auto
==============================================================================
--- 
camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ConsumeTest.java
 (added)
+++ 
camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/ConsumeTest.java
 Mon Sep  3 09:30:51 2012
@@ -0,0 +1,57 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.cdi;
+
+import javax.inject.Inject;
+
+import org.apache.camel.Consume;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.cdi.support.MockEndpointInjectedBean;
+import org.apache.camel.component.cdi.Mock;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+/**
+ * Test consume annotation
+ */
+public class ConsumeTest extends CdiTestSupport {
+
+    @Inject @Mock
+    private MockEndpoint result;
+
+    @Inject @Produce(uri = "seda:start")
+    private ProducerTemplate producer;
+
+    @Consume(uri = "seda:start")
+    public void onStart(String body) {
+        producer.sendBody("mock:result", "Hello " + body + "!");
+    }
+
+    @Test
+    public void consumeAnnotation() throws Exception {
+        assertNotNull("Could not inject producer", producer);
+        assertNotNull("Could not inject mock endpoint", result);
+
+        result.expectedBodiesReceived("Hello world!");
+
+        producer.sendBody("world");
+
+        result.assertIsSatisfied();
+    }
+
+}

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


Reply via email to