This is an automated email from the ASF dual-hosted git repository. nferraro pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2a9c6099ef9cee84f1352f47b58553cb29e77987 Author: Nicola Ferraro <[email protected]> AuthorDate: Mon Jan 13 11:09:57 2020 +0100 CAMEL-14385: switch to factory finder --- components/camel-cron/pom.xml | 3 +-- .../camel-cron/src/main/docs/cron-component.adoc | 3 +-- .../apache/camel/component/cron/CronHelper.java | 28 ++++++++++------------ .../camel/component/cron/CronLoaderTest.java | 2 +- .../apache/camel/cron/cron-service} | 3 +-- .../apache/camel/cron/cron-service} | 3 +-- .../apache/camel/cron/cron-service} | 3 +-- 7 files changed, 19 insertions(+), 26 deletions(-) diff --git a/components/camel-cron/pom.xml b/components/camel-cron/pom.xml index 7881656..1ca57c2 100644 --- a/components/camel-cron/pom.xml +++ b/components/camel-cron/pom.xml @@ -36,10 +36,9 @@ <dependencies> - <!-- requires camel-core --> <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-core-engine</artifactId> + <artifactId>camel-support</artifactId> </dependency> <!-- test dependencies --> diff --git a/components/camel-cron/src/main/docs/cron-component.adoc b/components/camel-cron/src/main/docs/cron-component.adoc index b4d2288..6035f87 100644 --- a/components/camel-cron/src/main/docs/cron-component.adoc +++ b/components/camel-cron/src/main/docs/cron-component.adoc @@ -40,7 +40,7 @@ Additional libraries may be needed in order to plug a specific implementation. // component options: START -The Cron component supports 4 options, which are listed below. +The Cron component supports 3 options, which are listed below. @@ -49,7 +49,6 @@ The Cron component supports 4 options, which are listed below. | Name | Description | Default | Type | *cronService* (advanced) | The id of the CamelCronService to use when multiple implementations are provided | | String | *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean -| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...] | *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean |=== // component options: END diff --git a/components/camel-cron/src/main/java/org/apache/camel/component/cron/CronHelper.java b/components/camel-cron/src/main/java/org/apache/camel/component/cron/CronHelper.java index 208c0e1..d2d9deb 100644 --- a/components/camel-cron/src/main/java/org/apache/camel/component/cron/CronHelper.java +++ b/components/camel-cron/src/main/java/org/apache/camel/component/cron/CronHelper.java @@ -16,12 +16,12 @@ */ package org.apache.camel.component.cron; -import java.util.Map; -import java.util.ServiceLoader; -import java.util.TreeMap; - import org.apache.camel.CamelContext; +import org.apache.camel.ExtendedCamelContext; +import org.apache.camel.NoFactoryAvailableException; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.component.cron.api.CamelCronService; +import org.apache.camel.spi.FactoryFinder; import org.apache.camel.support.CamelContextHelper; import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; @@ -30,6 +30,9 @@ import org.slf4j.LoggerFactory; public final class CronHelper { private static final Logger LOGGER = LoggerFactory.getLogger(CronHelper.class); + private static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/cron/"; + private static final String FACTORY_KEY = "cron-service"; + private CronHelper() { } @@ -48,18 +51,13 @@ public final class CronHelper { return service; } - // Fallback to service loader - Map<String, CamelCronService> services = new TreeMap<>(); - ServiceLoader.load(CamelCronService.class).forEach(s -> services.put(s.getId(), s)); - if (name != null) { - return services.get(name); - } - if (services.size() == 1) { - return services.values().iterator().next(); - } else if (services.size() > 1) { - LOGGER.warn("Multiple implementations found for CamelCronService: {}", services.keySet()); + // Fallback to factory finder + try { + FactoryFinder finder = context.adapt(ExtendedCamelContext.class).getFactoryFinder(RESOURCE_PATH); + return finder.newInstance(FACTORY_KEY, CamelCronService.class).orElse(null); + } catch (NoFactoryAvailableException e) { + throw new RuntimeCamelException("Cannot find any CamelCronService implementation", e); } - return null; } } diff --git a/components/camel-cron/src/test/java/org/apache/camel/component/cron/CronLoaderTest.java b/components/camel-cron/src/test/java/org/apache/camel/component/cron/CronLoaderTest.java index ad11413..fe1a9ff 100644 --- a/components/camel-cron/src/test/java/org/apache/camel/component/cron/CronLoaderTest.java +++ b/components/camel-cron/src/test/java/org/apache/camel/component/cron/CronLoaderTest.java @@ -28,7 +28,7 @@ public class CronLoaderTest extends CamelTestSupport { public void testDummyCronServiceLoading() throws Exception { configureRoutes(); MockEndpoint mock = getMockEndpoint("mock:result"); - mock.expectedBodiesReceived("x"); + mock.expectedMinimumMessageCount(1); context.start(); mock.assertIsSatisfied(); diff --git a/components/camel-cron/src/test/resources/META-INF/services/org.apache.camel.component.cron.api.CamelCronService b/components/camel-cron/src/test/resources/META-INF/services/org/apache/camel/cron/cron-service similarity index 92% rename from components/camel-cron/src/test/resources/META-INF/services/org.apache.camel.component.cron.api.CamelCronService rename to components/camel-cron/src/test/resources/META-INF/services/org/apache/camel/cron/cron-service index bb79081..b5d3830 100644 --- a/components/camel-cron/src/test/resources/META-INF/services/org.apache.camel.component.cron.api.CamelCronService +++ b/components/camel-cron/src/test/resources/META-INF/services/org/apache/camel/cron/cron-service @@ -14,5 +14,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # - -org.apache.camel.component.cron.DummyCamelCronService +class=org.apache.camel.component.cron.DummyCamelCronService diff --git a/components/camel-quartz/src/main/resources/META-INF/services/org.apache.camel.component.cron.api.CamelCronService b/components/camel-quartz/src/main/resources/META-INF/services/org/apache/camel/cron/cron-service similarity index 91% rename from components/camel-quartz/src/main/resources/META-INF/services/org.apache.camel.component.cron.api.CamelCronService rename to components/camel-quartz/src/main/resources/META-INF/services/org/apache/camel/cron/cron-service index eb744e9..3ab9652 100644 --- a/components/camel-quartz/src/main/resources/META-INF/services/org.apache.camel.component.cron.api.CamelCronService +++ b/components/camel-quartz/src/main/resources/META-INF/services/org/apache/camel/cron/cron-service @@ -14,5 +14,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # - -org.apache.camel.component.quartz.cron.CamelQuartzCronService +class=org.apache.camel.component.quartz.cron.CamelQuartzCronService diff --git a/components/camel-spring/src/main/resources/META-INF/services/org.apache.camel.component.cron.api.CamelCronService b/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/cron/cron-service similarity index 92% rename from components/camel-spring/src/main/resources/META-INF/services/org.apache.camel.component.cron.api.CamelCronService rename to components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/cron/cron-service index b65cde4..5f64b35 100644 --- a/components/camel-spring/src/main/resources/META-INF/services/org.apache.camel.component.cron.api.CamelCronService +++ b/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/cron/cron-service @@ -14,5 +14,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # - -org.apache.camel.component.cron.CamelSpringCronService +class=org.apache.camel.component.cron.CamelSpringCronService
