You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/11/14 18:38:33 UTC
[1/2] git commit: CAMEL-6769: Fixed the find methods in JndiRegistry
to return the instance and not its class
Updated Branches:
refs/heads/master 5d5b7e71d -> 610d1ebce
CAMEL-6769: Fixed the find methods in JndiRegistry to return the instance and not its class
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2bfcbd39
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2bfcbd39
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2bfcbd39
Branch: refs/heads/master
Commit: 2bfcbd39e8ab7bbbb11ebac43b79ed947427b88c
Parents: 5d5b7e7
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Nov 14 17:06:23 2013 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Nov 14 17:06:23 2013 +0100
----------------------------------------------------------------------
.../src/main/java/org/apache/camel/impl/JndiRegistry.java | 10 ++++++----
.../src/main/java/org/apache/camel/spi/Registry.java | 6 ------
2 files changed, 6 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/2bfcbd39/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java b/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java
index 173587d..dc663e3 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/JndiRegistry.java
@@ -78,8 +78,9 @@ public class JndiRegistry implements Registry {
NamingEnumeration<NameClassPair> list = getContext().list("");
while (list.hasMore()) {
NameClassPair pair = list.next();
- if (type.isInstance(pair.getClass())) {
- answer.put(pair.getName(), type.cast(pair.getClass()));
+ if (type.isInstance(pair.getClass()) || type.getName().equals(pair.getClassName())) {
+ Object instance = context.lookup(pair.getName());
+ answer.put(pair.getName(), type.cast(instance));
}
}
} catch (NamingException e) {
@@ -95,8 +96,9 @@ public class JndiRegistry implements Registry {
NamingEnumeration<NameClassPair> list = getContext().list("");
while (list.hasMore()) {
NameClassPair pair = list.next();
- if (type.isInstance(pair.getClass())) {
- answer.add(type.cast(pair.getClass()));
+ if (type.isInstance(pair.getClass()) || type.getName().equals(pair.getClassName())) {
+ Object instance = context.lookup(pair.getName());
+ answer.add(type.cast(instance));
}
}
} catch (NamingException e) {
http://git-wip-us.apache.org/repos/asf/camel/blob/2bfcbd39/camel-core/src/main/java/org/apache/camel/spi/Registry.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/spi/Registry.java b/camel-core/src/main/java/org/apache/camel/spi/Registry.java
index aac763d..1af6daa 100644
--- a/camel-core/src/main/java/org/apache/camel/spi/Registry.java
+++ b/camel-core/src/main/java/org/apache/camel/spi/Registry.java
@@ -47,9 +47,6 @@ public interface Registry {
/**
* Finds services in the registry by their type.
- * <p/>
- * <b>Note:</b> Not all registry implementations support this feature,
- * such as the {@link org.apache.camel.impl.JndiRegistry}.
*
* @param type the type of the registered services
* @return the types found, with their ids as the key. Returns an empty Map if none found.
@@ -58,9 +55,6 @@ public interface Registry {
/**
* Finds services in the registry by their type.
- * <p/>
- * <b>Note:</b> Not all registry implementations support this feature,
- * such as the {@link org.apache.camel.impl.JndiRegistry}.
*
* @param type the type of the registered services
* @return the types found. Returns an empty Set if none found.
[2/2] git commit: CAMEL-6965: Added @BeanInject to inject a bean from
the registry in other beans such as RouteBuilder classes.
Posted by da...@apache.org.
CAMEL-6965: Added @BeanInject to inject a bean from the registry in other beans such as RouteBuilder classes.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/610d1ebc
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/610d1ebc
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/610d1ebc
Branch: refs/heads/master
Commit: 610d1ebce965cc526ee92e1b39f4c81e0dcdbbb4
Parents: 2bfcbd3
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Nov 14 18:17:55 2013 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Nov 14 18:39:57 2013 +0100
----------------------------------------------------------------------
.../main/java/org/apache/camel/BeanInject.java | 38 ++++++++++
.../camel/impl/CamelPostProcessorHelper.java | 21 +++++-
.../impl/DefaultCamelBeanPostProcessor.java | 28 ++++++++
.../camel/impl/BeanInjectRouteBuilderTest.java | 73 ++++++++++++++++++++
.../impl/CamelPostProcessorHelperTest.java | 70 +++++++++++++++++++
.../test/java/org/apache/camel/impl/FooBar.java | 26 +++++++
.../handler/CamelNamespaceHandler.java | 27 ++++++++
.../SpringBuilderRefBeanInjectTest.java | 41 +++++++++++
.../route2/SpringBeanInjectRoute.java | 46 ++++++++++++
.../SpringBuilderRefBeanInjectTest.xml | 33 +++++++++
.../camel/test/blueprint/BeanInjectTest.java | 37 ++++++++++
.../blueprint/BlueprintBeanInjectRoute.java | 43 ++++++++++++
.../org/apache/camel/test/blueprint/FooBar.java | 27 ++++++++
.../test/blueprint/PropertyInjectTest.java | 1 -
.../camel/test/blueprint/beanInjectTest.xml | 33 +++++++++
15 files changed, 542 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/camel-core/src/main/java/org/apache/camel/BeanInject.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/BeanInject.java b/camel-core/src/main/java/org/apache/camel/BeanInject.java
new file mode 100644
index 0000000..09e70d0
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/BeanInject.java
@@ -0,0 +1,38 @@
+/**
+ * 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;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Used to indicate an injection point of a bean obtained from the {@link org.apache.camel.spi.Registry}, into a POJO.
+ *
+ * If no name is specified then the lookup is anonymous and based on lookup up by the type.
+ *
+ * @version
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Target({ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR})
+public @interface BeanInject {
+ String value() default "";
+ String context() default "";
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java b/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
index e13a91d..88df2f4 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java
@@ -17,6 +17,7 @@
package org.apache.camel.impl;
import java.lang.reflect.Method;
+import java.util.Set;
import javax.xml.bind.annotation.XmlTransient;
import org.apache.camel.CamelContext;
@@ -26,6 +27,7 @@ import org.apache.camel.Consumer;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Endpoint;
import org.apache.camel.IsSingleton;
+import org.apache.camel.NoSuchBeanException;
import org.apache.camel.PollingConsumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
@@ -221,7 +223,8 @@ public class CamelPostProcessorHelper implements CamelContextAware {
}
}
- public Object getInjectionPropertyValue(Class<?> type, String propertyName, String propertyDefaultValue, String injectionPointName, Object bean, String beanName) {
+ public Object getInjectionPropertyValue(Class<?> type, String propertyName, String propertyDefaultValue,
+ String injectionPointName, Object bean, String beanName) {
try {
String key;
String prefix = getCamelContext().getPropertyPrefixToken();
@@ -251,6 +254,22 @@ public class CamelPostProcessorHelper implements CamelContextAware {
}
}
+ public Object getInjectionBeanValue(Class<?> type, String name) {
+ if (ObjectHelper.isEmpty(name)) {
+ Set<?> found = getCamelContext().getRegistry().findByType(type);
+ if (found == null || found.isEmpty()) {
+ throw new NoSuchBeanException(name, type.getName());
+ } else if (found.size() > 1) {
+ throw new NoSuchBeanException("Found " + found.size() + " beans of type: " + type + ". Only one bean expected.");
+ } else {
+ // we found only one
+ return found.iterator().next();
+ }
+ } else {
+ return CamelContextHelper.mandatoryLookup(getCamelContext(), name, type);
+ }
+ }
+
/**
* Factory method to create a {@link org.apache.camel.ProducerTemplate} to be injected into a POJO
*/
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
index be44e3c..3d1e9f9 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelBeanPostProcessor.java
@@ -19,6 +19,7 @@ package org.apache.camel.impl;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import org.apache.camel.BeanInject;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.EndpointInject;
@@ -171,6 +172,11 @@ public class DefaultCamelBeanPostProcessor {
injectFieldProperty(field, propertyInject.value(), propertyInject.defaultValue(), bean, beanName);
}
+ BeanInject beanInject = field.getAnnotation(BeanInject.class);
+ if (beanInject != null && getPostProcessorHelper().matchContext(beanInject.context())) {
+ injectFieldBean(field, beanInject.value(), bean, beanName);
+ }
+
EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
if (endpointInject != null && getPostProcessorHelper().matchContext(endpointInject.context())) {
injectField(field, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), bean, beanName);
@@ -191,6 +197,11 @@ public class DefaultCamelBeanPostProcessor {
field.getName(), bean, beanName));
}
+ public void injectFieldBean(Field field, String name, Object bean, String beanName) {
+ ReflectionHelper.setField(field, bean,
+ getPostProcessorHelper().getInjectionBeanValue(field.getType(), name));
+ }
+
public void injectFieldProperty(Field field, String propertyName, String propertyDefaultValue, Object bean, String beanName) {
ReflectionHelper.setField(field, bean,
getPostProcessorHelper().getInjectionPropertyValue(field.getType(), propertyName, propertyDefaultValue,
@@ -212,6 +223,11 @@ public class DefaultCamelBeanPostProcessor {
setterPropertyInjection(method, propertyInject.value(), propertyInject.defaultValue(), bean, beanName);
}
+ BeanInject beanInject = method.getAnnotation(BeanInject.class);
+ if (beanInject != null && getPostProcessorHelper().matchContext(beanInject.context())) {
+ setterBeanInjection(method, beanInject.value(), bean, beanName);
+ }
+
EndpointInject endpointInject = method.getAnnotation(EndpointInject.class);
if (endpointInject != null && getPostProcessorHelper().matchContext(endpointInject.context())) {
setterInjection(method, bean, beanName, endpointInject.uri(), endpointInject.ref(), endpointInject.property());
@@ -251,4 +267,16 @@ public class DefaultCamelBeanPostProcessor {
}
}
+ public void setterBeanInjection(Method method, String name, Object bean, String beanName) {
+ Class<?>[] parameterTypes = method.getParameterTypes();
+ if (parameterTypes != null) {
+ if (parameterTypes.length != 1) {
+ LOG.warn("Ignoring badly annotated method for injection due to incorrect number of parameters: " + method);
+ } else {
+ Object value = getPostProcessorHelper().getInjectionBeanValue(parameterTypes[0], name);
+ ObjectHelper.invokeMethod(method, bean, value);
+ }
+ }
+ }
+
}
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/camel-core/src/test/java/org/apache/camel/impl/BeanInjectRouteBuilderTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/BeanInjectRouteBuilderTest.java b/camel-core/src/test/java/org/apache/camel/impl/BeanInjectRouteBuilderTest.java
new file mode 100644
index 0000000..b45685f
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/BeanInjectRouteBuilderTest.java
@@ -0,0 +1,73 @@
+/**
+ * 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.impl;
+
+import org.apache.camel.BeanInject;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+
+public class BeanInjectRouteBuilderTest extends ContextTestSupport {
+
+ @BeanInject
+ private FooBar foo;
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry jndi = super.createRegistry();
+ jndi.bind("foo", new FooBar());
+ return jndi;
+ }
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+
+ // manual post process us as ContextTestSupport in camel-core doesn't do that out of the box
+ DefaultCamelBeanPostProcessor post = new DefaultCamelBeanPostProcessor(context);
+ post.postProcessBeforeInitialization(this, "MyRoute");
+ post.postProcessAfterInitialization(this, "MyRoute");
+ return context;
+ }
+
+ public void testBeanInject() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+
+ template.sendBody("direct:start", "World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .process(new Processor() {
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ String out = foo.hello(exchange.getIn().getBody(String.class));
+ exchange.getIn().setBody(out);
+ }
+ }).to("mock:result");
+ }
+ };
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java b/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java
index 0edb046..f4fff4e 100644
--- a/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java
+++ b/camel-core/src/test/java/org/apache/camel/impl/CamelPostProcessorHelperTest.java
@@ -20,6 +20,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;
+import org.apache.camel.BeanInject;
import org.apache.camel.CamelContext;
import org.apache.camel.Consume;
import org.apache.camel.ContextTestSupport;
@@ -50,6 +51,7 @@ public class CamelPostProcessorHelperTest extends ContextTestSupport {
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry jndi = super.createRegistry();
jndi.bind("myProp", myProp);
+ jndi.bind("foo", new FooBar());
return jndi;
}
@@ -393,6 +395,52 @@ public class CamelPostProcessorHelperTest extends ContextTestSupport {
assertEquals("Hello Camel", value);
}
+ public void testBeanInject() throws Exception {
+ CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
+
+ MyBeanInjectBean bean = new MyBeanInjectBean();
+ Field field = bean.getClass().getField("foo");
+
+ BeanInject beanInject = field.getAnnotation(BeanInject.class);
+ Class<?> type = field.getType();
+ Object value = helper.getInjectionBeanValue(type, beanInject.value());
+ field.set(bean, value);
+
+ String out = bean.doSomething("World");
+ assertEquals("Hello World", out);
+ }
+
+ public void testBeanInjectNotFound() throws Exception {
+ CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
+
+ MyBeanInjectBean bean = new MyBeanInjectBean();
+ Field field = bean.getClass().getField("foo");
+
+ Class<?> type = field.getType();
+ try {
+ helper.getInjectionBeanValue(type, "bar");
+ fail("Should have thrown exception");
+ } catch (NoSuchBeanException e) {
+ assertEquals("No bean could be found in the registry for: bar of type: org.apache.camel.impl.FooBar", e.getMessage());
+ assertEquals("bar", e.getName());
+ }
+ }
+
+ public void testBeanInjectByType() throws Exception {
+ CamelPostProcessorHelper helper = new CamelPostProcessorHelper(context);
+
+ MyBeanInjectByTypeBean bean = new MyBeanInjectByTypeBean();
+ Field field = bean.getClass().getField("foo");
+
+ BeanInject beanInject = field.getAnnotation(BeanInject.class);
+ Class<?> type = field.getType();
+ Object value = helper.getInjectionBeanValue(type, beanInject.value());
+ field.set(bean, value);
+
+ String out = bean.doSomething("Camel");
+ assertEquals("Hello Camel", out);
+ }
+
public class MyConsumeBean {
@Consume(uri = "seda:foo")
@@ -619,4 +667,26 @@ public class CamelPostProcessorHelperTest extends ContextTestSupport {
}
}
+ public class MyBeanInjectBean {
+
+ @BeanInject("foo")
+ public FooBar foo;
+
+ public String doSomething(String body) {
+ return foo.hello(body);
+ }
+ }
+
+ public class MyBeanInjectByTypeBean {
+
+ @BeanInject
+ public FooBar foo;
+
+ public String doSomething(String body) {
+ return foo.hello(body);
+ }
+ }
+
+
+
}
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/camel-core/src/test/java/org/apache/camel/impl/FooBar.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/FooBar.java b/camel-core/src/test/java/org/apache/camel/impl/FooBar.java
new file mode 100644
index 0000000..24c9431
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/FooBar.java
@@ -0,0 +1,26 @@
+/**
+ * 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.impl;
+
+public class FooBar {
+
+ private String greeting = "Hello";
+
+ public String hello(String name) {
+ return greeting + " " + name;
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
index 2f0e44b..e25621d 100644
--- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
@@ -45,6 +45,7 @@ import org.apache.aries.blueprint.mutable.MutableBeanMetadata;
import org.apache.aries.blueprint.mutable.MutablePassThroughMetadata;
import org.apache.aries.blueprint.mutable.MutableRefMetadata;
import org.apache.aries.blueprint.mutable.MutableReferenceMetadata;
+import org.apache.camel.BeanInject;
import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
@@ -692,6 +693,11 @@ public class CamelNamespaceHandler implements NamespaceHandler {
injectFieldProperty(field, propertyInject.value(), propertyInject.defaultValue(), bean, beanName);
}
+ BeanInject beanInject = field.getAnnotation(BeanInject.class);
+ if (beanInject != null && matchContext(beanInject.context())) {
+ injectFieldBean(field, beanInject.value(), bean, beanName);
+ }
+
EndpointInject endpointInject = field.getAnnotation(EndpointInject.class);
if (endpointInject != null && matchContext(endpointInject.context())) {
injectField(field, endpointInject.uri(), endpointInject.ref(), endpointInject.property(), bean, beanName);
@@ -714,6 +720,10 @@ public class CamelNamespaceHandler implements NamespaceHandler {
setField(field, bean, getInjectionPropertyValue(field.getType(), propertyName, propertyDefaultValue, field.getName(), bean, beanName));
}
+ public void injectFieldBean(Field field, String name, Object bean, String beanName) {
+ setField(field, bean, getInjectionBeanValue(field.getType(), name));
+ }
+
protected static void setField(Field field, Object instance, Object value) {
try {
boolean oldAccessible = field.isAccessible();
@@ -750,6 +760,11 @@ public class CamelNamespaceHandler implements NamespaceHandler {
setterPropertyInjection(method, propertyInject.value(), propertyInject.defaultValue(), bean, beanName);
}
+ BeanInject beanInject = method.getAnnotation(BeanInject.class);
+ if (beanInject != null && matchContext(beanInject.context())) {
+ setterBeanInjection(method, beanInject.value(), bean, beanName);
+ }
+
EndpointInject endpointInject = method.getAnnotation(EndpointInject.class);
if (endpointInject != null && matchContext(endpointInject.context())) {
setterInjection(method, bean, beanName, endpointInject.uri(), endpointInject.ref(), endpointInject.property());
@@ -774,6 +789,18 @@ public class CamelNamespaceHandler implements NamespaceHandler {
}
}
+ protected void setterBeanInjection(Method method, String name, Object bean, String beanName) {
+ Class<?>[] parameterTypes = method.getParameterTypes();
+ if (parameterTypes != null) {
+ if (parameterTypes.length != 1) {
+ LOG.warn("Ignoring badly annotated method for injection due to incorrect number of parameters: " + method);
+ } else {
+ Object value = getInjectionBeanValue(parameterTypes[0], name);
+ ObjectHelper.invokeMethod(method, bean, value);
+ }
+ }
+ }
+
protected void setterInjection(Method method, Object bean, String beanName, String endpointUri, String endpointRef, String endpointProperty) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes != null) {
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringBuilderRefBeanInjectTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringBuilderRefBeanInjectTest.java b/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringBuilderRefBeanInjectTest.java
new file mode 100644
index 0000000..1302293
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringBuilderRefBeanInjectTest.java
@@ -0,0 +1,41 @@
+/**
+ * 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.properties;
+
+import org.apache.camel.spring.SpringTestSupport;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version
+ */
+public class SpringBuilderRefBeanInjectTest extends SpringTestSupport {
+
+ @Override
+ protected AbstractXmlApplicationContext createApplicationContext() {
+ return new ClassPathXmlApplicationContext("org/apache/camel/component/properties/SpringBuilderRefBeanInjectTest.xml");
+ }
+
+ public void testBeanInject() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+
+ template.sendBody("direct:start", "World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/components/camel-spring/src/test/java/org/apache/camel/component/properties/route2/SpringBeanInjectRoute.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/properties/route2/SpringBeanInjectRoute.java b/components/camel-spring/src/test/java/org/apache/camel/component/properties/route2/SpringBeanInjectRoute.java
new file mode 100644
index 0000000..ed8ee48
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/component/properties/route2/SpringBeanInjectRoute.java
@@ -0,0 +1,46 @@
+/**
+ * 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.properties.route2;
+
+import org.apache.camel.BeanInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.impl.FooBar;
+import org.apache.camel.spring.SpringRouteBuilder;
+
+/**
+ * @version
+ */
+public class SpringBeanInjectRoute extends SpringRouteBuilder {
+
+ @BeanInject("foo")
+ private FooBar greeting;
+
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .process(new Processor() {
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ String out = greeting.hello(exchange.getIn().getBody(String.class));
+ exchange.getIn().setBody(out);
+ }
+ })
+ .to("mock:result");
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringBuilderRefBeanInjectTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringBuilderRefBeanInjectTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringBuilderRefBeanInjectTest.xml
new file mode 100644
index 0000000..ec5b636
--- /dev/null
+++ b/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringBuilderRefBeanInjectTest.xml
@@ -0,0 +1,33 @@
+<?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 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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+ ">
+
+ <bean id="foo" class="org.apache.camel.impl.FooBar"/>
+
+ <bean id="simpleRoute" class="org.apache.camel.component.properties.route2.SpringBeanInjectRoute"/>
+
+ <camelContext xmlns="http://camel.apache.org/schema/spring">
+ <routeBuilder ref="simpleRoute"/>
+ </camelContext>
+
+</beans>
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BeanInjectTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BeanInjectTest.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BeanInjectTest.java
new file mode 100644
index 0000000..e00ec39
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BeanInjectTest.java
@@ -0,0 +1,37 @@
+/**
+ * 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.test.blueprint;
+
+import org.junit.Test;
+
+public class BeanInjectTest extends CamelBlueprintTestSupport {
+
+ @Override
+ protected String getBlueprintDescriptor() {
+ return "org/apache/camel/test/blueprint/beanInjectTest.xml";
+ }
+
+ @Test
+ public void testBeanInject() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+
+ template.sendBody("direct:start", "World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintBeanInjectRoute.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintBeanInjectRoute.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintBeanInjectRoute.java
new file mode 100644
index 0000000..6517970
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintBeanInjectRoute.java
@@ -0,0 +1,43 @@
+/**
+ * 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.test.blueprint;
+
+import org.apache.camel.BeanInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.PropertyInject;
+import org.apache.camel.builder.RouteBuilder;
+
+public class BlueprintBeanInjectRoute extends RouteBuilder {
+
+ @BeanInject("foo")
+ private FooBar greeting;
+
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .process(new Processor() {
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ String out = greeting.hello(exchange.getIn().getBody(String.class));
+ exchange.getIn().setBody(out);
+ }
+ })
+ .to("mock:result");
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/FooBar.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/FooBar.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/FooBar.java
new file mode 100644
index 0000000..98d9804
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/FooBar.java
@@ -0,0 +1,27 @@
+/**
+ * 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.test.blueprint;
+
+public class FooBar {
+
+ private String greeting = "Hello";
+
+ public String hello(String name) {
+ return greeting + " " + name;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/PropertyInjectTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/PropertyInjectTest.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/PropertyInjectTest.java
index 615edd1..589380d 100644
--- a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/PropertyInjectTest.java
+++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/PropertyInjectTest.java
@@ -34,5 +34,4 @@ public class PropertyInjectTest extends CamelBlueprintTestSupport {
assertMockEndpointsSatisfied();
}
-
}
http://git-wip-us.apache.org/repos/asf/camel/blob/610d1ebc/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/beanInjectTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/beanInjectTest.xml b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/beanInjectTest.xml
new file mode 100644
index 0000000..f8e874b
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/beanInjectTest.xml
@@ -0,0 +1,33 @@
+<?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 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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
+ xsi:schemaLocation="
+ http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.0.0.xsd
+ http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
+
+
+ <bean id="foo" class="org.apache.camel.test.blueprint.FooBar" />
+ <bean id="myRoute" class="org.apache.camel.test.blueprint.BlueprintBeanInjectRoute" />
+
+ <camelContext xmlns="http://camel.apache.org/schema/blueprint">
+ <routeBuilder ref="myRoute"/>
+ </camelContext>
+
+</blueprint>