camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r707021 - in /activemq/camel/trunk/components/camel-guice/src: main/java/org/apache/camel/guice/CamelModule.java main/java/org/apache/camel/guice/impl/EndpointInjector.java test/java/org/apache/camel/guice/EndpointInjectionTest.java
Date Wed, 22 Oct 2008 10:20:57 GMT
Author: jstrachan
Date: Wed Oct 22 03:20:55 2008
New Revision: 707021

URL: http://svn.apache.org/viewvc?rev=707021&view=rev
Log:
first cut at implementing injection for CAMEL-1015

Added:
    activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java
  (with props)
    activemq/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/EndpointInjectionTest.java
  (with props)
Modified:
    activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModule.java

Modified: activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModule.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModule.java?rev=707021&r1=707020&r2=707021&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModule.java
(original)
+++ activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/CamelModule.java
Wed Oct 22 03:20:55 2008
@@ -21,6 +21,7 @@
 import com.google.inject.jsr250.Jsr250Module;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Routes;
+import org.apache.camel.guice.impl.EndpointInjector;
 
 /**
  * A base Guice module for creating a {@link CamelContext} leaving it up to the users module
@@ -47,6 +48,8 @@
         super.configure();
         
         bind(CamelContext.class).to(GuiceCamelContext.class).asEagerSingleton();
+
+        bind(EndpointInjector.class);
     }
 
 }

Added: activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java?rev=707021&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java
(added)
+++ activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java
Wed Oct 22 03:20:55 2008
@@ -0,0 +1,83 @@
+/**
+ *
+ * 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.guice.impl;
+
+import com.google.inject.spi.AnnotationProviderFactory;
+import com.google.inject.spi.InjectionAnnotation;
+import com.google.inject.Provider;
+import com.google.inject.Inject;
+import com.google.common.base.Objects;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.CamelPostProcessorSupport;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+@InjectionAnnotation(EndpointInject.class)
+public class EndpointInjector extends CamelPostProcessorSupport implements AnnotationProviderFactory
{
+
+    @Inject
+    public EndpointInjector(CamelContext camelContext) {
+        super(camelContext);
+    }
+
+    public Provider createProvider(final AnnotatedElement member) {
+        final EndpointInject inject = member.getAnnotation(EndpointInject.class);
+        Objects.nonNull(inject, "@EndpointInject is not present!");
+
+
+        final Class<?> type;
+        final String injectionPointName;
+        final String endpointRef = inject.name();
+        final String uri = inject.uri();
+
+        if (member instanceof Field) {
+            Field field = (Field) member;
+            type = field.getType();
+            injectionPointName = field.getName();
+        }
+        else if (member instanceof Method) {
+            Method method = (Method) member;
+            Class<?>[] parameterTypes = method.getParameterTypes();
+            if (parameterTypes.length == 1) {
+                type = parameterTypes[0];
+
+                // TODO remove setter method name?
+                injectionPointName = method.getName();
+            }
+            else {
+                throw new UnsupportedOperationException("Only a single method parameter value
supported for @EndpointInject on " + method);
+            }
+        }
+        else {
+            throw new UnsupportedOperationException("Annotated element " + member + " not
supported");
+        }
+
+        return new Provider() {
+            public Object get() {
+                return getInjectionValue(type, uri, endpointRef, injectionPointName);
+            }
+        };
+    }
+}

Propchange: activemq/camel/trunk/components/camel-guice/src/main/java/org/apache/camel/guice/impl/EndpointInjector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/EndpointInjectionTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/EndpointInjectionTest.java?rev=707021&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/EndpointInjectionTest.java
(added)
+++ activemq/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/EndpointInjectionTest.java
Wed Oct 22 03:20:55 2008
@@ -0,0 +1,76 @@
+/**
+ * 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.guice;
+
+import com.google.common.collect.Lists;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Provides;
+import com.google.inject.name.Named;
+import junit.framework.TestCase;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Route;
+import org.apache.camel.Routes;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.component.mock.MockEndpoint;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Create a collection of routes via a provider method
+ *
+ * @version $Revision: 705742 $
+ */
+public class EndpointInjectionTest extends TestCase {
+
+    public static class MyModule extends CamelModuleWithMatchingRoutes {
+
+        @Override
+        protected void configure() {
+            noResourceInjection();
+
+            super.configure();
+
+            bind(MyBean.class);
+        }
+
+        @Provides
+        @Named("foo")
+        protected Collection<? extends Routes> myRoutes() {
+            return Lists.newArrayList(new MyConfigurableRoute2("direct:a", "direct:b"), new
MyConfigurableRoute2("direct:c", "direct:d"));
+        }
+    }
+
+    public static class MyBean {
+        @EndpointInject(uri="mock:foo")
+        MockEndpoint endpoint;
+    }
+
+    public void testGuice() throws Exception {
+        Injector injector = Guice.createInjector(new MyModule());
+
+        MyBean bean = injector.getInstance(MyBean.class);
+        assertNotNull("bean.endpoint", bean.endpoint);
+        assertEquals("bean.endpoint.uri", "mock:foo", bean.endpoint.getEndpointUri());
+
+        GuiceTest.assertCamelContextRunningThenCloseInjector(injector);
+
+    }
+
+
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/components/camel-guice/src/test/java/org/apache/camel/guice/EndpointInjectionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message