This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 9069a6774b2 CAMEL-20211: camel-jbang - Export should work when using idempotent consumer with repostiroy that connects to a database. Those need to be stubbed like we do for components. 9069a6774b2 is described below commit 9069a6774b2ebd7bf0aad638026f33de55a657d5 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Fri Dec 8 14:55:42 2023 +0100 CAMEL-20211: camel-jbang - Export should work when using idempotent consumer with repostiroy that connects to a database. Those need to be stubbed like we do for components. --- .../java/org/apache/camel/spi/StateRepository.java | 2 +- .../org/apache/camel/support/DefaultRegistry.java | 10 +++ .../java/org/apache/camel/main/KameletMain.java | 5 ++ .../camel/main/download/StubBeanRepository.java | 97 ++++++++++++++++++++++ 4 files changed, 113 insertions(+), 1 deletion(-) diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/StateRepository.java b/core/camel-api/src/main/java/org/apache/camel/spi/StateRepository.java index e86493f3b00..31f4cf66df9 100644 --- a/core/camel-api/src/main/java/org/apache/camel/spi/StateRepository.java +++ b/core/camel-api/src/main/java/org/apache/camel/spi/StateRepository.java @@ -20,7 +20,7 @@ import org.apache.camel.Service; /** * This {@link StateRepository} holds a set of key/value pairs for defining a particular <em>state</em> of a component. - * For instance it can be a set of indexes. + * For instance, it can be a set of indexes. * <p/> * An {@link IdempotentRepository} behaves more or less like a {@code Set} whereas this {@link StateRepository} behaves * like a {@code Map}. diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java index dd52d5a6b07..76a9aa869f0 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java @@ -163,6 +163,16 @@ public class DefaultRegistry extends ServiceSupport implements Registry, LocalBe } } + /** + * Adds a custom {@link BeanRepository}. + */ + public void addBeanRepository(BeanRepository repository) { + if (repository == null) { + repositories = new ArrayList<>(); + } + repositories.add(repository); + } + @Override public void bind(String id, Class<?> type, Object bean) throws RuntimeCamelException { if (bean != null) { diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java index 7effdfae500..2f87e6c2ae4 100644 --- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java @@ -59,6 +59,7 @@ import org.apache.camel.main.download.KnownDependenciesResolver; import org.apache.camel.main.download.KnownReposResolver; import org.apache.camel.main.download.MavenDependencyDownloader; import org.apache.camel.main.download.PackageNameSourceLoader; +import org.apache.camel.main.download.StubBeanRepository; import org.apache.camel.main.download.TypeConverterLoaderDownloadListener; import org.apache.camel.main.injection.AnnotationDependencyInjection; import org.apache.camel.main.util.ClipboardReloadStrategy; @@ -403,6 +404,10 @@ public class KameletMain extends MainCommandLineSupport { } answer.getCamelContextExtension().setRegistry(registry); + if (silent || "*".equals(stubPattern)) { + registry.addBeanRepository(new StubBeanRepository(stubPattern)); + } + // load camel component and custom health-checks answer.setLoadHealthChecks(true); // annotation based dependency injection for camel/spring/quarkus annotations in DSLs and Java beans diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/StubBeanRepository.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/StubBeanRepository.java new file mode 100644 index 00000000000..5dc962cd32c --- /dev/null +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/StubBeanRepository.java @@ -0,0 +1,97 @@ +/* + * 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.main.download; + +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import org.apache.camel.impl.engine.MemoryStateRepository; +import org.apache.camel.processor.DefaultClaimCheckRepository; +import org.apache.camel.processor.aggregate.MemoryAggregationRepository; +import org.apache.camel.spi.AggregationRepository; +import org.apache.camel.spi.BeanRepository; +import org.apache.camel.spi.ClaimCheckRepository; +import org.apache.camel.spi.IdempotentRepository; +import org.apache.camel.spi.StateRepository; +import org.apache.camel.support.PatternHelper; +import org.apache.camel.support.processor.idempotent.MemoryIdempotentRepository; + +public class StubBeanRepository implements BeanRepository { + + private final String stubPattern; + + public StubBeanRepository(String stubPattern) { + this.stubPattern = stubPattern; + } + + @Override + public Object lookupByName(String name) { + return null; + } + + @Override + public <T> T lookupByNameAndType(String name, Class<T> type) { + if (PatternHelper.matchPattern(name, stubPattern)) { + return stubType(type); + } + return null; + } + + @Override + public <T> Map<String, T> findByTypeWithName(Class<T> type) { + if (stubPattern != null) { + T answer = stubType(type); + if (answer != null) { + // generate dummy name + String name = UUID.randomUUID().toString(); + return Map.of(name, answer); + } + } + return Collections.EMPTY_MAP; + } + + @Override + public <T> Set<T> findByType(Class<T> type) { + if (stubPattern != null) { + T answer = stubType(type); + if (answer != null) { + return Set.of(answer); + } + } + return Collections.EMPTY_SET; + } + + private <T> T stubType(Class<T> type) { + // add repositories and other stuff we need to stub out, so they run noop/in-memory only + // and do not start live connections to databases or other services + if (IdempotentRepository.class.isAssignableFrom(type)) { + return (T) new MemoryIdempotentRepository(); + } + if (AggregationRepository.class.isAssignableFrom(type)) { + return (T) new MemoryAggregationRepository(); + } + if (ClaimCheckRepository.class.isAssignableFrom(type)) { + return (T) new DefaultClaimCheckRepository(); + } + if (StateRepository.class.isAssignableFrom(type)) { + return (T) new MemoryStateRepository(); + } + return null; + } +}