This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch bean in repository https://gitbox.apache.org/repos/asf/camel.git
commit 9ce71a5c67f3d5ca5bd7822f9bb007563e54187b Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Apr 25 12:08:04 2019 +0200 CAMEL-13449: camel3 - Move bean component out of camel-core --- .../bean/DefaultBeanProcessorFactory.java | 35 +++++++++++ .../org/apache/camel/bean-processor-factory | 18 ++++++ .../main/java/org/apache/camel/CamelContext.java | 6 ++ .../org/apache/camel/spi/BeanProcessorFactory.java | 42 +++++++++++++ .../apache/camel/impl/AbstractCamelContext.java | 16 +++++ .../camel/impl/BeanProcessorFactoryResolver.java | 71 ++++++++++++++++++++++ .../org/apache/camel/impl/DefaultCamelContext.java | 5 ++ .../camel/impl/SubscribeMethodProcessor.java | 9 +-- 8 files changed, 195 insertions(+), 7 deletions(-) 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 new file mode 100644 index 0000000..9c7b184 --- /dev/null +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java @@ -0,0 +1,35 @@ +/* + * 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 java.lang.reflect.Method; + +import org.apache.camel.CamelContext; +import org.apache.camel.Processor; +import org.apache.camel.spi.BeanProcessorFactory; + +public final class DefaultBeanProcessorFactory implements BeanProcessorFactory { + + public DefaultBeanProcessorFactory() { + } + + @Override + public Processor createBeanProcessor(CamelContext camelContext, Object pojo, Method method) throws Exception { + BeanInfo info = new BeanInfo(camelContext, method); + return new BeanProcessor(pojo, info); + } +} diff --git a/components/camel-bean/src/main/resources/META-INF/services/org/apache/camel/bean-processor-factory b/components/camel-bean/src/main/resources/META-INF/services/org/apache/camel/bean-processor-factory new file mode 100644 index 0000000..289a548 --- /dev/null +++ b/components/camel-bean/src/main/resources/META-INF/services/org/apache/camel/bean-processor-factory @@ -0,0 +1,18 @@ +# +# 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. +# + +class=org.apache.camel.component.bean.DefaultBeanProcessorFactory diff --git a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java index 80aaa76..355ff69 100644 --- a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java +++ b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java @@ -25,6 +25,7 @@ import java.util.concurrent.ScheduledExecutorService; import org.apache.camel.spi.AnnotationBasedProcessorFactory; import org.apache.camel.spi.AsyncProcessorAwaitManager; +import org.apache.camel.spi.BeanProcessorFactory; import org.apache.camel.spi.BeanProxyFactory; import org.apache.camel.spi.CamelBeanPostProcessor; import org.apache.camel.spi.CamelContextNameStrategy; @@ -1491,4 +1492,9 @@ public interface CamelContext extends SuspendableService, RuntimeConfiguration { */ BeanProxyFactory getBeanProxyFactory(); + /** + * Gets the {@link BeanProcessorFactory} to use. + */ + BeanProcessorFactory getBeanProcessorFactory(); + } diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/BeanProcessorFactory.java b/core/camel-api/src/main/java/org/apache/camel/spi/BeanProcessorFactory.java new file mode 100644 index 0000000..08efe7d --- /dev/null +++ b/core/camel-api/src/main/java/org/apache/camel/spi/BeanProcessorFactory.java @@ -0,0 +1,42 @@ +/* + * 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.spi; + +import java.lang.reflect.Method; + +import org.apache.camel.CamelContext; +import org.apache.camel.Processor; + +/** + * Factory for creating a {@link Processor} that can invoke a method on a bean and supporting using Camel + * bean parameter bindings. + * <p/> + * This requires to have camel-bean on the classpath. + */ +public interface BeanProcessorFactory { + + /** + * Creates the bean processor + * + * @param camelContext the camel context + * @param pojo the bean + * @param method the method to invoke + * @return the created processor + * @throws Exception is thrown if error creating the processor + */ + Processor createBeanProcessor(CamelContext camelContext, Object pojo, Method method) throws Exception; +} diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/AbstractCamelContext.java b/core/camel-core/src/main/java/org/apache/camel/impl/AbstractCamelContext.java index 748554d..4b8eaa0 100644 --- a/core/camel-core/src/main/java/org/apache/camel/impl/AbstractCamelContext.java +++ b/core/camel-core/src/main/java/org/apache/camel/impl/AbstractCamelContext.java @@ -105,6 +105,7 @@ import org.apache.camel.reifier.RouteReifier; import org.apache.camel.runtimecatalog.RuntimeCamelCatalog; import org.apache.camel.spi.AnnotationBasedProcessorFactory; import org.apache.camel.spi.AsyncProcessorAwaitManager; +import org.apache.camel.spi.BeanProcessorFactory; import org.apache.camel.spi.BeanProxyFactory; import org.apache.camel.spi.CamelBeanPostProcessor; import org.apache.camel.spi.CamelContextNameStrategy; @@ -256,6 +257,7 @@ public abstract class AbstractCamelContext extends ServiceSupport implements Mod private volatile RestRegistry restRegistry; private volatile HeadersMapFactory headersMapFactory; private volatile BeanProxyFactory beanProxyFactory; + private volatile BeanProcessorFactory beanProcessorFactory; private volatile ClassResolver classResolver; private volatile PackageScanClassResolver packageScanClassResolver; private volatile ServicePool<Producer> producerServicePool; @@ -3956,6 +3958,18 @@ public abstract class AbstractCamelContext extends ServiceSupport implements Mod return beanProxyFactory; } + @Override + public BeanProcessorFactory getBeanProcessorFactory() { + if (beanProcessorFactory == null) { + synchronized (lock) { + if (beanProcessorFactory == null) { + beanProcessorFactory = createBeanProcessorFactory(); + } + } + } + return beanProcessorFactory; + } + protected Map<String, RouteService> getRouteServices() { return routeServices; } @@ -4059,6 +4073,8 @@ public abstract class AbstractCamelContext extends ServiceSupport implements Mod protected abstract BeanProxyFactory createBeanProxyFactory(); + protected abstract BeanProcessorFactory createBeanProcessorFactory(); + protected abstract LanguageResolver createLanguageResolver(); protected abstract RestRegistry createRestRegistry(); diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/BeanProcessorFactoryResolver.java b/core/camel-core/src/main/java/org/apache/camel/impl/BeanProcessorFactoryResolver.java new file mode 100644 index 0000000..94a9d41 --- /dev/null +++ b/core/camel-core/src/main/java/org/apache/camel/impl/BeanProcessorFactoryResolver.java @@ -0,0 +1,71 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 java.io.IOException; + +import org.apache.camel.CamelContext; +import org.apache.camel.spi.BeanProcessorFactory; +import org.apache.camel.spi.FactoryFinder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Factory resolver to find the {@link org.apache.camel.spi.BeanProcessorFactory} from the classpath in camel-bean. + */ +public class BeanProcessorFactoryResolver { + + public static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/"; + + private static final Logger LOG = LoggerFactory.getLogger(BeanProcessorFactoryResolver.class); + + private FactoryFinder factoryFinder; + + public BeanProcessorFactory resolve(CamelContext context) { + // use factory finder to find a custom implementations + Class<?> type = null; + try { + type = findFactory("bean-processor-factory", context); + } catch (Exception e) { + // ignore + } + + if (type != null) { + if (LOG.isDebugEnabled()) { + LOG.debug("Found BeanProxyFactory: {} via: {}{}", type.getName(), factoryFinder.getResourcePath(), "bean-processor-factory"); + } + if (BeanProcessorFactory.class.isAssignableFrom(type)) { + BeanProcessorFactory answer = (BeanProcessorFactory) context.getInjector().newInstance(type); + LOG.info("Detected and using custom BeanProcessorFactory: {}", answer); + return answer; + } else { + throw new IllegalArgumentException("Type is not a BeanProcessorFactory implementation. Found: " + type.getName()); + } + } + + LOG.debug("Cannot find BeanProcessorFactory. Make sure camel-bean is on the classpath."); + return null; + } + + private Class<?> findFactory(String name, CamelContext context) throws ClassNotFoundException, IOException { + if (factoryFinder == null) { + factoryFinder = context.getFactoryFinder(RESOURCE_PATH); + } + return factoryFinder.findClass(name); + } + +} diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java index f635af3..7909feb 100644 --- a/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java +++ b/core/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java @@ -36,6 +36,7 @@ import org.apache.camel.model.validator.ValidatorDefinition; import org.apache.camel.runtimecatalog.RuntimeCamelCatalog; import org.apache.camel.runtimecatalog.impl.DefaultRuntimeCamelCatalog; import org.apache.camel.spi.AsyncProcessorAwaitManager; +import org.apache.camel.spi.BeanProcessorFactory; import org.apache.camel.spi.BeanProxyFactory; import org.apache.camel.spi.BeanRepository; import org.apache.camel.spi.CamelBeanPostProcessor; @@ -281,6 +282,10 @@ public class DefaultCamelContext extends AbstractCamelContext { return new BeanProxyFactoryResolver().resolve(this); } + protected BeanProcessorFactory createBeanProcessorFactory() { + return new BeanProcessorFactoryResolver().resolve(this); + } + protected LanguageResolver createLanguageResolver() { return new DefaultLanguageResolver(); } diff --git a/core/camel-core/src/main/java/org/apache/camel/impl/SubscribeMethodProcessor.java b/core/camel-core/src/main/java/org/apache/camel/impl/SubscribeMethodProcessor.java index a9cf35e..51e13c7 100644 --- a/core/camel-core/src/main/java/org/apache/camel/impl/SubscribeMethodProcessor.java +++ b/core/camel-core/src/main/java/org/apache/camel/impl/SubscribeMethodProcessor.java @@ -30,8 +30,6 @@ import org.apache.camel.Navigate; import org.apache.camel.Predicate; import org.apache.camel.Processor; import org.apache.camel.builder.PredicateBuilder; -import org.apache.camel.component.bean.BeanInfo; -import org.apache.camel.component.bean.BeanProcessor; import org.apache.camel.processor.CamelInternalProcessor; import org.apache.camel.support.AsyncProcessorSupport; import org.apache.camel.support.service.ServiceHelper; @@ -43,8 +41,6 @@ import org.apache.camel.util.ObjectHelper; */ public final class SubscribeMethodProcessor extends AsyncProcessorSupport implements Navigate<Processor> { - // TODO: requires camel-bean - private final Endpoint endpoint; private final Map<AsyncProcessor, Predicate> methods = new LinkedHashMap<>(); @@ -56,9 +52,8 @@ public final class SubscribeMethodProcessor extends AsyncProcessorSupport implem return endpoint; } - protected void addMethod(final Object pojo, final Method method, final Endpoint endpoint, String predicate) { - BeanInfo info = new BeanInfo(endpoint.getCamelContext(), method); - BeanProcessor answer = new BeanProcessor(pojo, info); + protected void addMethod(final Object pojo, final Method method, final Endpoint endpoint, String predicate) throws Exception { + Processor answer = endpoint.getCamelContext().getBeanProcessorFactory().createBeanProcessor(endpoint.getCamelContext(), pojo, method); // must ensure the consumer is being executed in an unit of work so synchronization callbacks etc is invoked CamelInternalProcessor internal = new CamelInternalProcessor(answer); internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(null));