This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch bean-cache in repository https://gitbox.apache.org/repos/asf/camel.git
commit 77454e06252b46ed1be87bac304b92313fec249f Author: Claus Ibsen <[email protected]> AuthorDate: Tue Aug 1 21:04:27 2023 +0200 CAMEL-19487: camel-bean - Fix concurrency issue in BeanInfo cache when EIPs are using an existing bean instance. --- .../java/org/apache/camel/component/bean/BeanInfo.java | 11 +++++++---- .../org/apache/camel/component/bean/BeanInfoCacheKey.java | 15 +++++++++------ .../apache/camel/component/bean/ConstantBeanHolder.java | 2 +- .../camel/component/bean/DefaultBeanProcessorFactory.java | 3 ++- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java index 03282944ff1..a7e190381ce 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java @@ -73,6 +73,7 @@ public class BeanInfo { private final CamelContext camelContext; private final BeanComponent component; private final Class<?> type; + private final Object instance; private final ParameterMappingStrategy strategy; private final MethodInfo defaultMethod; // shared state with details of operations introspected from the bean, created during the constructor @@ -92,22 +93,24 @@ public class BeanInfo { public BeanInfo(CamelContext camelContext, Method explicitMethod, ParameterMappingStrategy parameterMappingStrategy, BeanComponent beanComponent) { - this(camelContext, explicitMethod.getDeclaringClass(), explicitMethod, parameterMappingStrategy, beanComponent); + this(camelContext, explicitMethod.getDeclaringClass(), null, explicitMethod, parameterMappingStrategy, beanComponent); } public BeanInfo(CamelContext camelContext, Class<?> type, ParameterMappingStrategy strategy, BeanComponent beanComponent) { - this(camelContext, type, null, strategy, beanComponent); + this(camelContext, type, null, null, strategy, beanComponent); } - public BeanInfo(CamelContext camelContext, Class<?> type, Method explicitMethod, ParameterMappingStrategy strategy, + public BeanInfo(CamelContext camelContext, Class<?> type, Object instance, Method explicitMethod, + ParameterMappingStrategy strategy, BeanComponent beanComponent) { this.camelContext = camelContext; this.type = type; + this.instance = instance; this.strategy = strategy; this.component = beanComponent; - final BeanInfoCacheKey key = new BeanInfoCacheKey(type, explicitMethod); + final BeanInfoCacheKey key = new BeanInfoCacheKey(type, instance, explicitMethod); // lookup if we have a bean info cache BeanInfo beanInfo = component.getBeanInfoFromCache(key); diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfoCacheKey.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfoCacheKey.java index f29438189ff..3edabbdfb05 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfoCacheKey.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfoCacheKey.java @@ -17,6 +17,7 @@ package org.apache.camel.component.bean; import java.lang.reflect.Method; +import java.util.Objects; /** * A key used for caching {@link BeanInfo} by the {@link BeanComponent} @@ -24,10 +25,12 @@ import java.lang.reflect.Method; public final class BeanInfoCacheKey { private final Class<?> type; + private final Object instance; private final Method explicitMethod; - public BeanInfoCacheKey(Class<?> type, Method explicitMethod) { + public BeanInfoCacheKey(Class<?> type, Object instance, Method explicitMethod) { this.type = type; + this.instance = instance; this.explicitMethod = explicitMethod; } @@ -42,19 +45,19 @@ public final class BeanInfoCacheKey { BeanInfoCacheKey that = (BeanInfoCacheKey) o; - if (explicitMethod != null ? !explicitMethod.equals(that.explicitMethod) : that.explicitMethod != null) { + if (!Objects.equals(type, that.type)) { return false; } - if (!type.equals(that.type)) { + if (!Objects.equals(instance, that.instance)) { return false; } - - return true; + return Objects.equals(explicitMethod, that.explicitMethod); } @Override public int hashCode() { - int result = type.hashCode(); + int result = type != null ? type.hashCode() : 0; + result = 31 * result + (instance != null ? instance.hashCode() : 0); result = 31 * result + (explicitMethod != null ? explicitMethod.hashCode() : 0); return result; } 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 31b00171a17..dd9fd2ec615 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 @@ -47,7 +47,7 @@ public class ConstantBeanHolder implements BeanHolder { ObjectHelper.notNull(bean, "bean"); this.bean = bean; - this.beanInfo = new BeanInfo(context, bean.getClass(), parameterMappingStrategy, beanComponent); + this.beanInfo = new BeanInfo(context, bean.getClass(), bean, null, parameterMappingStrategy, beanComponent); } @Override diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java index e3affca6948..9ff2ee574b6 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java @@ -58,7 +58,8 @@ public final class DefaultBeanProcessorFactory extends ServiceSupport @Override public Processor createBeanProcessor(CamelContext camelContext, Object bean, Method method) throws Exception { - BeanInfo info = new BeanInfo(camelContext, method, parameterMappingStrategy, beanComponent); + BeanInfo info + = new BeanInfo(camelContext, method.getDeclaringClass(), bean, method, parameterMappingStrategy, beanComponent); return new BeanProcessor(bean, info); }
