This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new 29026c6 CAMEL-15082: Fixed camel-bean to set additional bean
parameters which did not work correctly before.
29026c6 is described below
commit 29026c66acaff12c282ed5a59a2e8adb3cef5389
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon May 18 19:46:04 2020 +0200
CAMEL-15082: Fixed camel-bean to set additional bean parameters which did
not work correctly before.
---
.../apache/camel/component/bean/BeanEndpoint.java | 2 +-
.../apache/camel/component/bean/BeanHolder.java | 12 +++++
.../camel/component/bean/ConstantBeanHolder.java | 24 ++++++++++
.../component/bean/ConstantTypeBeanHolder.java | 25 ++++++++++-
.../apache/camel/component/bean/RegistryBean.java | 27 +++++++++++
.../camel/component/bean/RequestBeanHolder.java | 12 +++++
.../apache/camel/language/bean/BeanExpression.java | 2 +-
...entWithPropertiesLookupSetFromEndpointTest.java | 52 ++++++++++++++++++++++
8 files changed, 153 insertions(+), 3 deletions(-)
diff --git
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
index 8d0df45..89483f3 100644
---
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
+++
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
@@ -111,7 +111,7 @@ public class BeanEndpoint extends DefaultEndpoint {
}
processor.setScope(scope);
if (parameters != null) {
- setProperties(processor, parameters);
+ holder.setOptions(parameters);
}
}
}
diff --git
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHolder.java
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHolder.java
index af56127..ab73441 100644
---
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHolder.java
+++
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHolder.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.component.bean;
+import java.util.Map;
+
import org.apache.camel.Exchange;
import org.apache.camel.NoSuchBeanException;
import org.apache.camel.Processor;
@@ -26,6 +28,16 @@ import org.apache.camel.Processor;
public interface BeanHolder {
/**
+ * Additional options that should be configured on the bean
+ */
+ Map<String, Object> getOptions();
+
+ /**
+ * Sets additional options that should be configured on the bean
+ */
+ void setOptions(Map<String, Object> options);
+
+ /**
* Gets the bean.
*
* @throws NoSuchBeanException is thrown if the bean cannot be found.
diff --git
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java
index 9f42b16..83b1313 100644
---
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java
+++
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java
@@ -16,10 +16,13 @@
*/
package org.apache.camel.component.bean;
+import java.util.Map;
+
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.support.CamelContextHelper;
+import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.util.ObjectHelper;
/**
@@ -29,6 +32,7 @@ public class ConstantBeanHolder implements BeanHolder {
private final Object bean;
private final BeanInfo beanInfo;
private Processor processor;
+ private Map<String, Object> options;
public ConstantBeanHolder(Object bean, BeanInfo beanInfo) {
ObjectHelper.notNull(bean, "bean");
@@ -46,6 +50,26 @@ public class ConstantBeanHolder implements BeanHolder {
}
@Override
+ public Map<String, Object> getOptions() {
+ return options;
+ }
+
+ @Override
+ public void setOptions(Map<String, Object> options) {
+ this.options = options;
+
+ // since its a constant we can set the options immediately on the bean
+ if (options != null && !options.isEmpty()) {
+ PropertyBindingSupport.build()
+ .withRemoveParameters(false)
+ .withCamelContext(getBeanInfo().getCamelContext())
+ .withProperties(options)
+ .withTarget(bean)
+ .bind();
+ }
+ }
+
+ @Override
public String toString() {
// avoid invoke toString on bean as it may be a remote proxy
return ObjectHelper.className(bean) + "(" +
ObjectHelper.getIdentityHashCode(bean) + ")";
diff --git
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java
index 3802380..4191b32 100644
---
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java
+++
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java
@@ -16,9 +16,12 @@
*/
package org.apache.camel.component.bean;
+import java.util.Map;
+
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
+import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.util.ObjectHelper;
/**
@@ -27,6 +30,7 @@ import org.apache.camel.util.ObjectHelper;
public class ConstantTypeBeanHolder implements BeanTypeHolder {
private final Class<?> type;
private final BeanInfo beanInfo;
+ private Map<String, Object> options;
public ConstantTypeBeanHolder(Class<?> type, CamelContext context) {
this(type, new BeanInfo(context, type));
@@ -40,6 +44,16 @@ public class ConstantTypeBeanHolder implements
BeanTypeHolder {
this.beanInfo = beanInfo;
}
+ @Override
+ public Map<String, Object> getOptions() {
+ return options;
+ }
+
+ @Override
+ public void setOptions(Map<String, Object> options) {
+ this.options = options;
+ }
+
/**
* Creates a cached and constant {@link
org.apache.camel.component.bean.BeanHolder} from this holder.
*
@@ -59,7 +73,16 @@ public class ConstantTypeBeanHolder implements
BeanTypeHolder {
public Object getBean(Exchange exchange) {
// only create a bean if we have a default no-arg constructor
if (beanInfo.hasPublicNoArgConstructors()) {
- return
getBeanInfo().getCamelContext().getInjector().newInstance(type, false);
+ Object bean =
getBeanInfo().getCamelContext().getInjector().newInstance(type, false);
+ if (options != null && !options.isEmpty()) {
+ PropertyBindingSupport.build()
+ .withRemoveParameters(false)
+ .withCamelContext(getBeanInfo().getCamelContext())
+ .withProperties(options)
+ .withTarget(bean)
+ .bind();
+ }
+ return bean;
} else {
return null;
}
diff --git
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/RegistryBean.java
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/RegistryBean.java
index d8376fd..907477b 100644
---
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/RegistryBean.java
+++
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/RegistryBean.java
@@ -16,11 +16,14 @@
*/
package org.apache.camel.component.bean;
+import java.util.Map;
+
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.NoSuchBeanException;
import org.apache.camel.Processor;
import org.apache.camel.spi.Registry;
+import org.apache.camel.support.PropertyBindingSupport;
/**
* An implementation of a {@link BeanHolder} which will look up a bean from
the registry and act as a cache of its metadata
@@ -32,6 +35,7 @@ public class RegistryBean implements BeanHolder {
private volatile BeanInfo beanInfo;
private volatile Class<?> clazz;
private ParameterMappingStrategy parameterMappingStrategy;
+ private Map<String, Object> options;
public RegistryBean(CamelContext context, String name) {
this(context.getRegistry(), context, name);
@@ -59,6 +63,16 @@ public class RegistryBean implements BeanHolder {
return "bean: " + name;
}
+ @Override
+ public Map<String, Object> getOptions() {
+ return options;
+ }
+
+ @Override
+ public void setOptions(Map<String, Object> options) {
+ this.options = options;
+ }
+
/**
* Creates a singleton (cached and constant) {@link
org.apache.camel.component.bean.BeanHolder} from this holder.
*/
@@ -70,6 +84,19 @@ public class RegistryBean implements BeanHolder {
@Override
public Object getBean(Exchange exchange) throws NoSuchBeanException {
+ Object bean = doGetBean(exchange);
+ if (options != null && !options.isEmpty()) {
+ PropertyBindingSupport.build()
+ .withRemoveParameters(false)
+ .withCamelContext(getBeanInfo().getCamelContext())
+ .withProperties(options)
+ .withTarget(bean)
+ .bind();
+ }
+ return bean;
+ }
+
+ private Object doGetBean(Exchange exchange) throws NoSuchBeanException {
// must always lookup bean first
Object value = lookupBean();
diff --git
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/RequestBeanHolder.java
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/RequestBeanHolder.java
index ce4d789..df98660 100644
---
a/components/camel-bean/src/main/java/org/apache/camel/component/bean/RequestBeanHolder.java
+++
b/components/camel-bean/src/main/java/org/apache/camel/component/bean/RequestBeanHolder.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.component.bean;
+import java.util.Map;
+
import org.apache.camel.Exchange;
import org.apache.camel.NoSuchBeanException;
import org.apache.camel.Processor;
@@ -34,6 +36,16 @@ public class RequestBeanHolder implements BeanHolder {
}
@Override
+ public Map<String, Object> getOptions() {
+ return holder.getOptions();
+ }
+
+ @Override
+ public void setOptions(Map<String, Object> options) {
+ this.holder.setOptions(options);
+ }
+
+ @Override
public Object getBean(Exchange exchange) throws NoSuchBeanException {
Object bean = exchange.getProperty(key);
if (bean == null) {
diff --git
a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
index 3d48bf0..8bc87cf 100644
---
a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
+++
b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
@@ -281,7 +281,7 @@ public class BeanExpression implements Expression,
Predicate, AfterPropertiesCon
} else if (beanName != null) {
// it may refer to a type such as when used with bean language
if (context.getRegistry().lookupByName(beanName) == null) {
-
+ // TODO: ?????
}
holder = new RegistryBean(context, beanName);
} else if (type != null) {
diff --git
a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanComponentWithPropertiesLookupSetFromEndpointTest.java
b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanComponentWithPropertiesLookupSetFromEndpointTest.java
new file mode 100644
index 0000000..55f3392
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanComponentWithPropertiesLookupSetFromEndpointTest.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.camel.component.bean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.Registry;
+import org.junit.Test;
+
+public class BeanComponentWithPropertiesLookupSetFromEndpointTest extends
ContextTestSupport {
+
+ @Override
+ protected Registry createRegistry() throws Exception {
+ Registry jndi = super.createRegistry();
+ jndi.bind("foo", "Hi");
+ return jndi;
+ }
+
+ @Test
+ public void testBeanComponent() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hi 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").to("bean:org.apache.camel.component.bean.MyPrefixBean?bean.prefix=#foo").to("mock:result");
+ }
+ };
+ }
+
+}