CAMEL-10542: DataFormat from registry is used for every dataformat operation (marshal/unmarshal)
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c0a9f5cd Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c0a9f5cd Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c0a9f5cd Branch: refs/heads/master Commit: c0a9f5cd494a361c081ded85c5b23f4c903a78a3 Parents: d4c4a84 Author: lburgazzoli <lburgazz...@gmail.com> Authored: Fri Dec 2 17:38:55 2016 +0100 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Wed Feb 1 11:38:35 2017 +0100 ---------------------------------------------------------------------- .../java/org/apache/camel/CamelContext.java | 8 +++ .../dataformat/DataFormatComponent.java | 6 ++ .../dataformat/DataFormatEndpoint.java | 22 +++--- .../apache/camel/impl/DefaultCamelContext.java | 11 +++ .../camel/impl/DefaultDataFormatResolver.java | 70 ++++++++++++++------ .../org/apache/camel/impl/osgi/Activator.java | 12 ++++ .../camel/model/DataFormatDefinition.java | 2 +- .../org/apache/camel/spi/DataFormatFactory.java | 31 +++++++++ .../apache/camel/spi/DataFormatResolver.java | 9 ++- .../org/apache/camel/util/ResolverHelper.java | 19 ++++++ .../camel/processor/DataFormatFactoryTest.java | 53 +++++++++++++++ .../blueprint/BlueprintDataFormatResolver.java | 33 ++++++--- .../camel/core/osgi/OsgiDataFormatResolver.java | 45 +++++++++---- .../csv/CsvDataFormatCustomFactoryTest.java | 58 ++++++++++++++++ .../csv/CsvDataFormatResourceFactoryTest.java | 57 ++++++++++++++++ .../dataformat/csv/MyCsvDataFormatFactory.java | 37 +++++++++++ .../CsvDataFormatCustomFactoryTest-context.xml | 46 +++++++++++++ ...CsvDataFormatResourceFactoryTest-context.xml | 44 ++++++++++++ .../AvroDataFormatAutoConfiguration.java | 48 +++++++++----- .../BarcodeDataFormatAutoConfiguration.java | 49 ++++++++------ .../Base64DataFormatAutoConfiguration.java | 49 ++++++++------ .../BeanIODataFormatAutoConfiguration.java | 49 ++++++++------ .../BindyCsvDataFormatAutoConfiguration.java | 49 ++++++++------ ...yFixedLengthDataFormatAutoConfiguration.java | 51 ++++++++------ ...KeyValuePairDataFormatAutoConfiguration.java | 51 ++++++++------ .../BoonDataFormatAutoConfiguration.java | 48 +++++++++----- .../CastorDataFormatAutoConfiguration.java | 49 ++++++++------ .../GzipDataFormatAutoConfiguration.java | 48 +++++++++----- ...erializationDataFormatAutoConfiguration.java | 51 ++++++++------ .../StringDataFormatAutoConfiguration.java | 49 ++++++++------ .../ZipDataFormatAutoConfiguration.java | 48 +++++++++----- .../CryptoDataFormatAutoConfiguration.java | 49 ++++++++------ .../PGPDataFormatAutoConfiguration.java | 48 +++++++++----- .../CsvDataFormatAutoConfiguration.java | 48 +++++++++----- .../FlatpackDataFormatAutoConfiguration.java | 49 ++++++++------ .../GsonDataFormatAutoConfiguration.java | 48 +++++++++----- .../HessianDataFormatAutoConfiguration.java | 49 ++++++++------ .../HL7DataFormatAutoConfiguration.java | 48 +++++++++----- .../ICalDataFormatAutoConfiguration.java | 48 +++++++++----- .../JacksonDataFormatAutoConfiguration.java | 49 ++++++++------ .../JacksonXMLDataFormatAutoConfiguration.java | 50 ++++++++------ .../JaxbDataFormatAutoConfiguration.java | 48 +++++++++----- .../JibxDataFormatAutoConfiguration.java | 48 +++++++++----- .../JohnzonDataFormatAutoConfiguration.java | 49 ++++++++------ .../LZFDataFormatAutoConfiguration.java | 48 +++++++++----- ...imeMultipartDataFormatAutoConfiguration.java | 51 ++++++++------ .../ProtobufDataFormatAutoConfiguration.java | 49 ++++++++------ .../RssDataFormatAutoConfiguration.java | 48 +++++++++----- .../SnakeYAMLDataFormatAutoConfiguration.java | 49 ++++++++------ .../SoapJaxbDataFormatAutoConfiguration.java | 49 ++++++++------ .../SyslogDataFormatAutoConfiguration.java | 49 ++++++++------ .../TidyMarkupDataFormatAutoConfiguration.java | 50 ++++++++------ .../TarFileDataFormatAutoConfiguration.java | 49 ++++++++------ ...UniVocityCsvDataFormatAutoConfiguration.java | 50 ++++++++------ ...tyFixedWidthDataFormatAutoConfiguration.java | 51 ++++++++------ ...UniVocityTsvDataFormatAutoConfiguration.java | 50 ++++++++------ .../XmlBeansDataFormatAutoConfiguration.java | 49 ++++++++------ .../XmlJsonDataFormatAutoConfiguration.java | 49 ++++++++------ .../XmlRpcDataFormatAutoConfiguration.java | 49 ++++++++------ .../XMLSecurityDataFormatAutoConfiguration.java | 50 ++++++++------ .../JsonDataFormatAutoConfiguration.java | 48 +++++++++----- .../XStreamDataFormatAutoConfiguration.java | 49 ++++++++------ .../ZipFileDataFormatAutoConfiguration.java | 49 ++++++++------ .../SpringBootAutoConfigurationMojo.java | 46 +++++++------ 64 files changed, 1884 insertions(+), 931 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/camel-core/src/main/java/org/apache/camel/CamelContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/CamelContext.java b/camel-core/src/main/java/org/apache/camel/CamelContext.java index 597b685..b0ea66b 100644 --- a/camel-core/src/main/java/org/apache/camel/CamelContext.java +++ b/camel-core/src/main/java/org/apache/camel/CamelContext.java @@ -1206,6 +1206,14 @@ public interface CamelContext extends SuspendableService, RuntimeConfiguration { DataFormat resolveDataFormat(String name); /** + * Creates the given data format given its name. + * + * @param name the data format name or a reference to a data format factory in the {@link Registry} + * @return the resolved data format, or <tt>null</tt> if not found + */ + DataFormat createDataFormat(String name); + + /** * Resolve a data format definition given its name * * @param name the data format definition name or a reference to it in the {@link Registry} http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/camel-core/src/main/java/org/apache/camel/component/dataformat/DataFormatComponent.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/dataformat/DataFormatComponent.java b/camel-core/src/main/java/org/apache/camel/component/dataformat/DataFormatComponent.java index 692b831..9aee141 100644 --- a/camel-core/src/main/java/org/apache/camel/component/dataformat/DataFormatComponent.java +++ b/camel-core/src/main/java/org/apache/camel/component/dataformat/DataFormatComponent.java @@ -38,8 +38,14 @@ public class DataFormatComponent extends UriEndpointComponent { @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { String name = ObjectHelper.before(remaining, ":"); + + // try to lookup data format in the registry or create it from resource DataFormat df = getCamelContext().resolveDataFormat(name); if (df == null) { + // if not, try to find a factory in the registry + df = getCamelContext().createDataFormat(name); + } + if (df == null) { throw new IllegalArgumentException("Cannot find data format with name: " + name); } http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/camel-core/src/main/java/org/apache/camel/component/dataformat/DataFormatEndpoint.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/dataformat/DataFormatEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/dataformat/DataFormatEndpoint.java index 441d4ed..efbf89a 100644 --- a/camel-core/src/main/java/org/apache/camel/component/dataformat/DataFormatEndpoint.java +++ b/camel-core/src/main/java/org/apache/camel/component/dataformat/DataFormatEndpoint.java @@ -17,6 +17,7 @@ package org.apache.camel.component.dataformat; import org.apache.camel.AsyncCallback; +import org.apache.camel.AsyncProcessor; import org.apache.camel.Component; import org.apache.camel.Consumer; import org.apache.camel.Exchange; @@ -39,8 +40,7 @@ import org.apache.camel.util.ServiceHelper; label = "core,transformation", lenientProperties = true) public class DataFormatEndpoint extends DefaultEndpoint { - private MarshalProcessor marshal; - private UnmarshalProcessor unmarshal; + private AsyncProcessor processor; private DataFormat dataFormat; @UriPath(description = "Name of data format") @Metadata(required = "true") @@ -88,11 +88,7 @@ public class DataFormatEndpoint extends DefaultEndpoint { return new DefaultAsyncProducer(this) { @Override public boolean process(Exchange exchange, AsyncCallback callback) { - if (marshal != null) { - return marshal.process(exchange, callback); - } else { - return unmarshal.process(exchange, callback); - } + return processor.process(exchange, callback); } @Override @@ -123,20 +119,24 @@ public class DataFormatEndpoint extends DefaultEndpoint { dataFormat = getCamelContext().resolveDataFormat(name); } if (operation.equals("marshal")) { - marshal = new MarshalProcessor(dataFormat); + MarshalProcessor marshal = new MarshalProcessor(dataFormat); marshal.setCamelContext(getCamelContext()); + + processor = marshal; } else { - unmarshal = new UnmarshalProcessor(dataFormat); + UnmarshalProcessor unmarshal = new UnmarshalProcessor(dataFormat); unmarshal.setCamelContext(getCamelContext()); + + processor = unmarshal; } - ServiceHelper.startServices(dataFormat, marshal, unmarshal); + ServiceHelper.startServices(dataFormat, processor); super.doStart(); } @Override protected void doStop() throws Exception { - ServiceHelper.stopServices(marshal, unmarshal, dataFormat); + ServiceHelper.stopServices(processor, dataFormat); super.doStop(); } } http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java index 94f598a..67f77ea 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java @@ -4156,6 +4156,17 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon return answer; } + public DataFormat createDataFormat(String name) { + DataFormat answer = dataFormatResolver.createDataFormat(name, this); + + // inject CamelContext if aware + if (answer != null && answer instanceof CamelContextAware) { + ((CamelContextAware) answer).setCamelContext(this); + } + + return answer; + } + public DataFormatDefinition resolveDataFormatDefinition(String name) { // lookup type and create the data format from it DataFormatDefinition type = lookup(this, name, DataFormatDefinition.class); http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java index d777546..571799a 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultDataFormatResolver.java @@ -19,6 +19,7 @@ package org.apache.camel.impl; import org.apache.camel.CamelContext; import org.apache.camel.NoFactoryAvailableException; import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; import org.apache.camel.spi.DataFormatResolver; import org.apache.camel.spi.FactoryFinder; import org.apache.camel.util.ResolverHelper; @@ -29,38 +30,65 @@ import org.apache.camel.util.ResolverHelper; * @version */ public class DefaultDataFormatResolver implements DataFormatResolver { - public static final String DATAFORMAT_RESOURCE_PATH = "META-INF/services/org/apache/camel/dataformat/"; - protected FactoryFinder dataformatFactory; + private FactoryFinder dataformatFactory; + @Override public DataFormat resolveDataFormat(String name, CamelContext context) { // lookup in registry first DataFormat dataFormat = ResolverHelper.lookupDataFormatInRegistryWithFallback(context, name); if (dataFormat == null) { - Class<?> type = null; - try { - if (dataformatFactory == null) { - dataformatFactory = context.getFactoryFinder(DATAFORMAT_RESOURCE_PATH); - } - type = dataformatFactory.findClass(name); - } catch (NoFactoryAvailableException e) { - // ignore - } catch (Exception e) { - throw new IllegalArgumentException("Invalid URI, no DataFormat registered for scheme: " + name, e); - } + // If not found in the registry, try to create a new instance using + // a DataFormatFactory or from resources + dataFormat = createDataFormat(name, context); + } + + return dataFormat; + } + + @Override + public DataFormat createDataFormat(String name, CamelContext context) { + DataFormat dataFormat = null; + + // lookup in registry first + DataFormatFactory dataFormatFactory = ResolverHelper.lookupDataFormatFactoryInRegistryWithFallback(context, name); + if (dataFormatFactory != null) { + dataFormat = dataFormatFactory.newInstance(); + } - if (type == null) { - type = context.getClassResolver().resolveClass(name); + if (dataFormat == null) { + dataFormat = createDataFormatFromResource(name, context); + } + + return dataFormat; + } + + private DataFormat createDataFormatFromResource(String name, CamelContext context) { + DataFormat dataFormat = null; + + Class<?> type = null; + try { + if (dataformatFactory == null) { + dataformatFactory = context.getFactoryFinder(DATAFORMAT_RESOURCE_PATH); } + type = dataformatFactory.findClass(name); + } catch (NoFactoryAvailableException e) { + // ignore + } catch (Exception e) { + throw new IllegalArgumentException("Invalid URI, no DataFormat registered for scheme: " + name, e); + } + + if (type == null) { + type = context.getClassResolver().resolveClass(name); + } - if (type != null) { - if (DataFormat.class.isAssignableFrom(type)) { - dataFormat = (DataFormat) context.getInjector().newInstance(type); - } else { - throw new IllegalArgumentException("Resolving dataformat: " + name + " detected type conflict: Not a DataFormat implementation. Found: " + type.getName()); - } + if (type != null) { + if (DataFormat.class.isAssignableFrom(type)) { + dataFormat = (DataFormat) context.getInjector().newInstance(type); + } else { + throw new IllegalArgumentException("Resolving dataformat: " + name + " detected type conflict: Not a DataFormat implementation. Found: " + type.getName()); } } http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java b/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java index 5eb3ced..917d83a 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java +++ b/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java @@ -345,7 +345,18 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { this.dataformats = dataformats; } + @Override public DataFormat resolveDataFormat(String name, CamelContext context) { + DataFormat dataFormat = createInstance(name, dataformats.get(name), context); + if (dataFormat == null) { + dataFormat = createDataFormat(name, context); + } + + return dataFormat; + } + + @Override + public DataFormat createDataFormat(String name, CamelContext context) { return createInstance(name, dataformats.get(name), context); } @@ -353,6 +364,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { return null; } + @Override public void register() { doRegister(DataFormatResolver.class, "dataformat", dataformats.keySet()); } http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java b/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java index 06eeb19..d07b454 100644 --- a/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/DataFormatDefinition.java @@ -133,7 +133,7 @@ public class DataFormatDefinition extends IdentifiedType implements OtherAttribu protected DataFormat createDataFormat(RouteContext routeContext) { // must use getDataFormatName() as we need special logic in json dataformat if (getDataFormatName() != null) { - return routeContext.getCamelContext().resolveDataFormat(getDataFormatName()); + return routeContext.getCamelContext().createDataFormat(getDataFormatName()); } return null; } http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/camel-core/src/main/java/org/apache/camel/spi/DataFormatFactory.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/spi/DataFormatFactory.java b/camel-core/src/main/java/org/apache/camel/spi/DataFormatFactory.java new file mode 100644 index 0000000..557cfad --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/spi/DataFormatFactory.java @@ -0,0 +1,31 @@ +/** + * 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; + +/** + * A factory to create {@link org.apache.camel.spi.DataFormat}. + */ +@FunctionalInterface +public interface DataFormatFactory { + /** + * Creates a new DataFormat instance. + * + * @return the instance + */ + DataFormat newInstance(); +} http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java b/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java index b3de2cc..2f846aa 100644 --- a/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java +++ b/camel-core/src/main/java/org/apache/camel/spi/DataFormatResolver.java @@ -24,7 +24,6 @@ import org.apache.camel.CamelContext; * @version */ public interface DataFormatResolver { - /** * Resolves the given data format given its name. * @@ -34,4 +33,12 @@ public interface DataFormatResolver { */ DataFormat resolveDataFormat(String name, CamelContext context); + /** + * Creates the given data format given its name. + * + * @param name the name of the data format factory to lookup in {@link org.apache.camel.spi.Registry} or create + * @param context the camel context + * @return the data format or <tt>null</tt> if not possible to resolve + */ + DataFormat createDataFormat(String name, CamelContext context); } http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/camel-core/src/main/java/org/apache/camel/util/ResolverHelper.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/ResolverHelper.java b/camel-core/src/main/java/org/apache/camel/util/ResolverHelper.java index a972c4d..a551d73 100644 --- a/camel-core/src/main/java/org/apache/camel/util/ResolverHelper.java +++ b/camel-core/src/main/java/org/apache/camel/util/ResolverHelper.java @@ -19,6 +19,7 @@ package org.apache.camel.util; import org.apache.camel.CamelContext; import org.apache.camel.Component; import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; import org.apache.camel.spi.Language; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,6 +35,8 @@ public final class ResolverHelper { public static final String DATA_FORMAT_FALLBACK_SUFFIX = "-dataformat"; + public static final String DATA_FORMAT_FACTORY_FALLBACK_SUFFIX = "-dataformat-factory"; + public static final String LANGUAGE_FALLBACK_SUFFIX = "-language"; private static final Logger LOG = LoggerFactory.getLogger(ResolverHelper.class); @@ -87,6 +90,22 @@ public final class ResolverHelper { return null; } + public static DataFormatFactory lookupDataFormatFactoryInRegistryWithFallback(CamelContext context, String name) { + return lookupDataFormatFactoryInRegistryWithFallback(context, name, EXCEPTION_HANDLER); + } + + public static DataFormatFactory lookupDataFormatFactoryInRegistryWithFallback(CamelContext context, String name, LookupExceptionHandler exceptionHandler) { + Object bean = lookupInRegistry(context, DataFormatFactory.class, false, exceptionHandler, name, name + DATA_FORMAT_FACTORY_FALLBACK_SUFFIX); + if (bean instanceof DataFormatFactory) { + return (DataFormatFactory) bean; + } + + if (bean != null) { + LOG.debug("Found DataFormatFactory with incompatible class: {}", bean.getClass().getName()); + } + return null; + } + public static Language lookupLanguageInRegistryWithFallback(CamelContext context, String name) { return lookupLanguageInRegistryWithFallback(context, name, EXCEPTION_HANDLER); } http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/camel-core/src/test/java/org/apache/camel/processor/DataFormatFactoryTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/processor/DataFormatFactoryTest.java b/camel-core/src/test/java/org/apache/camel/processor/DataFormatFactoryTest.java new file mode 100644 index 0000000..802bbc2 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/processor/DataFormatFactoryTest.java @@ -0,0 +1,53 @@ +/** + * 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.processor; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.impl.SerializationDataFormat; +import org.apache.camel.impl.StringDataFormat; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; + +public class DataFormatFactoryTest extends ContextTestSupport { + private static final DataFormat STRING_DF = new StringDataFormat("US-ASCII"); + private static final DataFormatFactory STRING_DFF = () -> new StringDataFormat("UTF-8"); + private static final DataFormat SERIALIZATION_DF = new SerializationDataFormat(); + + public void testDataFormatResolveOrCreate() throws Exception { + assertSame(STRING_DF, context.resolveDataFormat("string")); + assertNotSame(STRING_DF, context.createDataFormat("string")); + assertNotSame(context.createDataFormat("string"), context.createDataFormat("string")); + + assertSame(SERIALIZATION_DF, context.resolveDataFormat("serialization")); + assertNotSame(SERIALIZATION_DF, context.createDataFormat("serialization")); + assertNotSame(context.createDataFormat("serialization"), context.createDataFormat("serialization")); + + assertEquals("US-ASCII", ((StringDataFormat)context.resolveDataFormat("string")).getCharset()); + assertEquals("UTF-8", ((StringDataFormat)context.createDataFormat("string")).getCharset()); + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry registry = super.createRegistry(); + registry.bind("string-dataformat", STRING_DF); + registry.bind("string-dataformat-factory", STRING_DFF); + registry.bind("serialization", SERIALIZATION_DF); + + return registry; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintDataFormatResolver.java ---------------------------------------------------------------------- diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintDataFormatResolver.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintDataFormatResolver.java index 1673c0e..7bec9b2 100644 --- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintDataFormatResolver.java +++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintDataFormatResolver.java @@ -19,13 +19,13 @@ package org.apache.camel.blueprint; import org.apache.camel.CamelContext; import org.apache.camel.core.osgi.OsgiDataFormatResolver; import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; import org.apache.camel.spi.DataFormatResolver; import org.osgi.framework.BundleContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class BlueprintDataFormatResolver extends OsgiDataFormatResolver { - private static final Logger LOG = LoggerFactory.getLogger(BlueprintDataFormatResolver.class); public BlueprintDataFormatResolver(BundleContext bundleContext) { @@ -34,16 +34,29 @@ public class BlueprintDataFormatResolver extends OsgiDataFormatResolver { @Override public DataFormat resolveDataFormat(String name, CamelContext context) { - try { - Object bean = context.getRegistry().lookupByName(".camelBlueprint.dataformatResolver." + name); - if (bean instanceof DataFormatResolver) { - LOG.debug("Found dataformat resolver: {} in registry: {}", name, bean); - return ((DataFormatResolver) bean).resolveDataFormat(name, context); - } - } catch (Exception e) { - LOG.trace("Ignored error looking up bean: " + name + " due: " + e.getMessage(), e); + DataFormat dataFormat = null; + + DataFormatResolver resolver = context.getRegistry().lookupByNameAndType(".camelBlueprint.dataformatResolver." + name, DataFormatResolver.class); + if (resolver != null) { + LOG.debug("Found dataformat resolver: {} in registry: {}", name, resolver); + dataFormat = resolver.resolveDataFormat(name, context); } - return super.resolveDataFormat(name, context); + + if (dataFormat == null) { + dataFormat = super.resolveDataFormat(name, context); + } + + return dataFormat; } + @Override + public DataFormat createDataFormat(String name, CamelContext context) { + DataFormatFactory factory = context.getRegistry().lookupByNameAndType(".camelBlueprint.dataformatFactory." + name, DataFormatFactory.class); + if (factory != null) { + LOG.debug("Found dataformat factory: {} in registry: {}", name, factory); + return factory.newInstance(); + } + + return super.createDataFormat(name, context); + } } http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiDataFormatResolver.java ---------------------------------------------------------------------- diff --git a/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiDataFormatResolver.java b/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiDataFormatResolver.java index c403632..af70de7 100644 --- a/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiDataFormatResolver.java +++ b/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiDataFormatResolver.java @@ -16,8 +16,11 @@ */ package org.apache.camel.core.osgi; +import java.util.Collection; + import org.apache.camel.CamelContext; import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; import org.apache.camel.spi.DataFormatResolver; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.ResolverHelper; @@ -38,25 +41,43 @@ public class OsgiDataFormatResolver implements DataFormatResolver { public DataFormat resolveDataFormat(String name, CamelContext context) { // lookup in registry first - DataFormat dataFormatReg = ResolverHelper.lookupDataFormatInRegistryWithFallback(context, name); - if (dataFormatReg != null) { - return dataFormatReg; + DataFormat dataFormat = ResolverHelper.lookupDataFormatInRegistryWithFallback(context, name); + if (dataFormat == null) { + dataFormat = getDataFormat(name, context, false); + } + + if (dataFormat == null) { + dataFormat = createDataFormat(name, context); + } + + return dataFormat; + } + + public DataFormat createDataFormat(String name, CamelContext context) { + DataFormat dataFormat = null; + + // lookup in registry first + DataFormatFactory dataFormatFactory = ResolverHelper.lookupDataFormatFactoryInRegistryWithFallback(context, name); + if (dataFormatFactory != null) { + dataFormat = dataFormatFactory.newInstance(); + } + + if (dataFormat == null) { + dataFormat = getDataFormat(name, context, true); } - return getDataFormat(name, context); + return dataFormat; } - protected DataFormat getDataFormat(String name, CamelContext context) { + private DataFormat getDataFormat(String name, CamelContext context, boolean create) { LOG.trace("Finding DataFormat: {}", name); try { - ServiceReference<?>[] refs = bundleContext.getServiceReferences(DataFormatResolver.class.getName(), "(dataformat=" + name + ")"); + Collection<ServiceReference<DataFormatResolver>> refs = bundleContext.getServiceReferences(DataFormatResolver.class, "(dataformat=" + name + ")"); if (refs != null) { - for (ServiceReference<?> ref : refs) { - Object service = bundleContext.getService(ref); - if (DataFormatResolver.class.isAssignableFrom(service.getClass())) { - DataFormatResolver resolver = (DataFormatResolver) service; - return resolver.resolveDataFormat(name, context); - } + for (ServiceReference<DataFormatResolver> ref : refs) { + return create + ? bundleContext.getService(ref).createDataFormat(name, context) + : bundleContext.getService(ref).resolveDataFormat(name, context); } } return null; http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatCustomFactoryTest.java ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatCustomFactoryTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatCustomFactoryTest.java new file mode 100644 index 0000000..7950545 --- /dev/null +++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatCustomFactoryTest.java @@ -0,0 +1,58 @@ +/** + * 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.dataformat.csv; + +import java.util.Arrays; +import java.util.List; + +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.spring.CamelSpringTestSupport; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class CsvDataFormatCustomFactoryTest extends CamelSpringTestSupport { + @Test + public void marshalTest() throws InterruptedException { + MockEndpoint mock = getMockEndpoint("mock:marshaled"); + mock.expectedMessageCount(1); + + template.sendBody("direct:marshal", getData()); + + mock.assertIsSatisfied(); + + String body = mock.getReceivedExchanges().get(0).getIn().getBody(String.class); + String[] lines = body.split("\r\n"); + + Assert.assertEquals(2, lines.length); + Assert.assertEquals("A1:B1:C1", lines[0].trim()); + Assert.assertEquals("A2:B2:C2", lines[1].trim()); + } + + private List<List<String>> getData() { + return Arrays.asList( + Arrays.asList("A1", "B1", "C1"), + Arrays.asList("A2", "B2", "C2") + ); + } + + @Override + protected ClassPathXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/dataformat/csv/CsvDataFormatCustomFactoryTest-context.xml"); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatResourceFactoryTest.java ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatResourceFactoryTest.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatResourceFactoryTest.java new file mode 100644 index 0000000..5714808 --- /dev/null +++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvDataFormatResourceFactoryTest.java @@ -0,0 +1,57 @@ +/** + * 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.dataformat.csv; + +import java.util.Arrays; +import java.util.List; + +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.spring.CamelSpringTestSupport; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class CsvDataFormatResourceFactoryTest extends CamelSpringTestSupport { + @Test + public void marshalTest() throws InterruptedException { + MockEndpoint mock = getMockEndpoint("mock:marshaled"); + mock.expectedMessageCount(1); + + template.sendBody("direct:marshal", getData()); + + mock.assertIsSatisfied(); + + String body = mock.getReceivedExchanges().get(0).getIn().getBody(String.class); + String[] lines = body.split("\r\n"); + + Assert.assertEquals(2, lines.length); + Assert.assertEquals("A1,B1,C1", lines[0].trim()); + Assert.assertEquals("A2,B2,C2", lines[1].trim()); + } + + private List<List<String>> getData() { + return Arrays.asList( + Arrays.asList("A1", "B1", "C1"), + Arrays.asList("A2", "B2", "C2") + ); + } + + @Override + protected ClassPathXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/dataformat/csv/CsvDataFormatResourceFactoryTest-context.xml"); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/MyCsvDataFormatFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/MyCsvDataFormatFactory.java b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/MyCsvDataFormatFactory.java new file mode 100644 index 0000000..28c7065 --- /dev/null +++ b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/MyCsvDataFormatFactory.java @@ -0,0 +1,37 @@ +/** + * 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.dataformat.csv; + +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; + +public class MyCsvDataFormatFactory implements DataFormatFactory { + private final char delimiter; + + public MyCsvDataFormatFactory(char delimiter) { + this.delimiter = delimiter; + } + + @Override + public DataFormat newInstance() { + CsvDataFormat dataFormat = new CsvDataFormat(); + dataFormat.setDelimiter(delimiter); + + return dataFormat; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvDataFormatCustomFactoryTest-context.xml ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvDataFormatCustomFactoryTest-context.xml b/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvDataFormatCustomFactoryTest-context.xml new file mode 100644 index 0000000..3668a73 --- /dev/null +++ b/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvDataFormatCustomFactoryTest-context.xml @@ -0,0 +1,46 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> + + <bean id="csv-dataformat-factory" class="org.apache.camel.dataformat.csv.MyCsvDataFormatFactory"> + <constructor-arg value=":"/> + </bean> + + <camelContext id="csvCamelContext" xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:marshal"/> + <marshal> + <csv headerDisabled="true" quoteDisabled="true"/> + </marshal> + <convertBodyTo type="java.lang.String"/> + <to uri="mock:marshaled"/> + </route> + <route> + <from uri="direct:unmarshal"/> + <unmarshal> + <csv delimiter=";" headerDisabled="true"/> + </unmarshal> + <to uri="mock:unmarshaled"/> + </route> + </camelContext> + +</beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvDataFormatResourceFactoryTest-context.xml ---------------------------------------------------------------------- diff --git a/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvDataFormatResourceFactoryTest-context.xml b/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvDataFormatResourceFactoryTest-context.xml new file mode 100644 index 0000000..f6b60f2 --- /dev/null +++ b/components/camel-csv/src/test/resources/org/apache/camel/dataformat/csv/CsvDataFormatResourceFactoryTest-context.xml @@ -0,0 +1,44 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> + + <bean id="csv-dataformat" class="org.apache.camel.dataformat.csv.CsvDataFormat"/> + + <camelContext id="csvCamelContext" xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:marshal"/> + <marshal> + <csv headerDisabled="true" quoteDisabled="true"/> + </marshal> + <convertBodyTo type="java.lang.String"/> + <to uri="mock:marshaled"/> + </route> + <route> + <from uri="direct:unmarshal"/> + <unmarshal> + <csv delimiter=";" headerDisabled="true"/> + </unmarshal> + <to uri="mock:unmarshaled"/> + </route> + </camelContext> + +</beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/platforms/spring-boot/components-starter/camel-avro-starter/src/main/java/org/apache/camel/dataformat/avro/springboot/AvroDataFormatAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-avro-starter/src/main/java/org/apache/camel/dataformat/avro/springboot/AvroDataFormatAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-avro-starter/src/main/java/org/apache/camel/dataformat/avro/springboot/AvroDataFormatAutoConfiguration.java index dee5a8f..f4e6093 100644 --- a/platforms/spring-boot/components-starter/camel-avro-starter/src/main/java/org/apache/camel/dataformat/avro/springboot/AvroDataFormatAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-avro-starter/src/main/java/org/apache/camel/dataformat/avro/springboot/AvroDataFormatAutoConfiguration.java @@ -20,7 +20,10 @@ import java.util.HashMap; import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.dataformat.avro.AvroDataFormat; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; import org.apache.camel.util.IntrospectionSupport; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionMessage; @@ -35,7 +38,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; import org.springframework.core.type.AnnotatedTypeMetadata; /** @@ -48,26 +50,36 @@ import org.springframework.core.type.AnnotatedTypeMetadata; @EnableConfigurationProperties(AvroDataFormatConfiguration.class) public class AvroDataFormatAutoConfiguration { - @Bean(name = "avro-dataformat") - @Scope("prototype") + @Bean(name = "avro-dataformat-factory") @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(AvroDataFormat.class) - public AvroDataFormat configureAvroDataFormat(CamelContext camelContext, - AvroDataFormatConfiguration configuration) throws Exception { - AvroDataFormat dataformat = new AvroDataFormat(); - if (CamelContextAware.class.isAssignableFrom(AvroDataFormat.class)) { - CamelContextAware contextAware = CamelContextAware.class - .cast(dataformat); - if (contextAware != null) { - contextAware.setCamelContext(camelContext); + public DataFormatFactory configureAvroDataFormatFactory( + final CamelContext camelContext, + final AvroDataFormatConfiguration configuration) { + return new DataFormatFactory() { + public DataFormat newInstance() { + AvroDataFormat dataformat = new AvroDataFormat(); + if (CamelContextAware.class + .isAssignableFrom(AvroDataFormat.class)) { + CamelContextAware contextAware = CamelContextAware.class + .cast(dataformat); + if (contextAware != null) { + contextAware.setCamelContext(camelContext); + } + } + try { + Map<String, Object> parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, + parameters, null, false); + IntrospectionSupport.setProperties(camelContext, + camelContext.getTypeConverter(), dataformat, + parameters); + } catch (Exception e) { + throw new RuntimeCamelException(e); + } + return dataformat; } - } - Map<String, Object> parameters = new HashMap<>(); - IntrospectionSupport.getProperties(configuration, parameters, null, - false); - IntrospectionSupport.setProperties(camelContext, - camelContext.getTypeConverter(), dataformat, parameters); - return dataformat; + }; } public static class Condition extends SpringBootCondition { http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/platforms/spring-boot/components-starter/camel-barcode-starter/src/main/java/org/apache/camel/dataformat/barcode/springboot/BarcodeDataFormatAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-barcode-starter/src/main/java/org/apache/camel/dataformat/barcode/springboot/BarcodeDataFormatAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-barcode-starter/src/main/java/org/apache/camel/dataformat/barcode/springboot/BarcodeDataFormatAutoConfiguration.java index cf84be8..ce76822 100644 --- a/platforms/spring-boot/components-starter/camel-barcode-starter/src/main/java/org/apache/camel/dataformat/barcode/springboot/BarcodeDataFormatAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-barcode-starter/src/main/java/org/apache/camel/dataformat/barcode/springboot/BarcodeDataFormatAutoConfiguration.java @@ -20,7 +20,10 @@ import java.util.HashMap; import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.dataformat.barcode.BarcodeDataFormat; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; import org.apache.camel.util.IntrospectionSupport; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionMessage; @@ -35,7 +38,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; import org.springframework.core.type.AnnotatedTypeMetadata; /** @@ -48,27 +50,36 @@ import org.springframework.core.type.AnnotatedTypeMetadata; @EnableConfigurationProperties(BarcodeDataFormatConfiguration.class) public class BarcodeDataFormatAutoConfiguration { - @Bean(name = "barcode-dataformat") - @Scope("prototype") + @Bean(name = "barcode-dataformat-factory") @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(BarcodeDataFormat.class) - public BarcodeDataFormat configureBarcodeDataFormat( - CamelContext camelContext, - BarcodeDataFormatConfiguration configuration) throws Exception { - BarcodeDataFormat dataformat = new BarcodeDataFormat(); - if (CamelContextAware.class.isAssignableFrom(BarcodeDataFormat.class)) { - CamelContextAware contextAware = CamelContextAware.class - .cast(dataformat); - if (contextAware != null) { - contextAware.setCamelContext(camelContext); + public DataFormatFactory configureBarcodeDataFormatFactory( + final CamelContext camelContext, + final BarcodeDataFormatConfiguration configuration) { + return new DataFormatFactory() { + public DataFormat newInstance() { + BarcodeDataFormat dataformat = new BarcodeDataFormat(); + if (CamelContextAware.class + .isAssignableFrom(BarcodeDataFormat.class)) { + CamelContextAware contextAware = CamelContextAware.class + .cast(dataformat); + if (contextAware != null) { + contextAware.setCamelContext(camelContext); + } + } + try { + Map<String, Object> parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, + parameters, null, false); + IntrospectionSupport.setProperties(camelContext, + camelContext.getTypeConverter(), dataformat, + parameters); + } catch (Exception e) { + throw new RuntimeCamelException(e); + } + return dataformat; } - } - Map<String, Object> parameters = new HashMap<>(); - IntrospectionSupport.getProperties(configuration, parameters, null, - false); - IntrospectionSupport.setProperties(camelContext, - camelContext.getTypeConverter(), dataformat, parameters); - return dataformat; + }; } public static class Condition extends SpringBootCondition { http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/platforms/spring-boot/components-starter/camel-base64-starter/src/main/java/org/apache/camel/dataformat/base64/springboot/Base64DataFormatAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-base64-starter/src/main/java/org/apache/camel/dataformat/base64/springboot/Base64DataFormatAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-base64-starter/src/main/java/org/apache/camel/dataformat/base64/springboot/Base64DataFormatAutoConfiguration.java index a84ad11..11b6c7a 100644 --- a/platforms/spring-boot/components-starter/camel-base64-starter/src/main/java/org/apache/camel/dataformat/base64/springboot/Base64DataFormatAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-base64-starter/src/main/java/org/apache/camel/dataformat/base64/springboot/Base64DataFormatAutoConfiguration.java @@ -20,7 +20,10 @@ import java.util.HashMap; import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.dataformat.base64.Base64DataFormat; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; import org.apache.camel.util.IntrospectionSupport; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionMessage; @@ -35,7 +38,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; import org.springframework.core.type.AnnotatedTypeMetadata; /** @@ -48,27 +50,36 @@ import org.springframework.core.type.AnnotatedTypeMetadata; @EnableConfigurationProperties(Base64DataFormatConfiguration.class) public class Base64DataFormatAutoConfiguration { - @Bean(name = "base64-dataformat") - @Scope("prototype") + @Bean(name = "base64-dataformat-factory") @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(Base64DataFormat.class) - public Base64DataFormat configureBase64DataFormat( - CamelContext camelContext, - Base64DataFormatConfiguration configuration) throws Exception { - Base64DataFormat dataformat = new Base64DataFormat(); - if (CamelContextAware.class.isAssignableFrom(Base64DataFormat.class)) { - CamelContextAware contextAware = CamelContextAware.class - .cast(dataformat); - if (contextAware != null) { - contextAware.setCamelContext(camelContext); + public DataFormatFactory configureBase64DataFormatFactory( + final CamelContext camelContext, + final Base64DataFormatConfiguration configuration) { + return new DataFormatFactory() { + public DataFormat newInstance() { + Base64DataFormat dataformat = new Base64DataFormat(); + if (CamelContextAware.class + .isAssignableFrom(Base64DataFormat.class)) { + CamelContextAware contextAware = CamelContextAware.class + .cast(dataformat); + if (contextAware != null) { + contextAware.setCamelContext(camelContext); + } + } + try { + Map<String, Object> parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, + parameters, null, false); + IntrospectionSupport.setProperties(camelContext, + camelContext.getTypeConverter(), dataformat, + parameters); + } catch (Exception e) { + throw new RuntimeCamelException(e); + } + return dataformat; } - } - Map<String, Object> parameters = new HashMap<>(); - IntrospectionSupport.getProperties(configuration, parameters, null, - false); - IntrospectionSupport.setProperties(camelContext, - camelContext.getTypeConverter(), dataformat, parameters); - return dataformat; + }; } public static class Condition extends SpringBootCondition { http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/platforms/spring-boot/components-starter/camel-beanio-starter/src/main/java/org/apache/camel/dataformat/beanio/springboot/BeanIODataFormatAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-beanio-starter/src/main/java/org/apache/camel/dataformat/beanio/springboot/BeanIODataFormatAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-beanio-starter/src/main/java/org/apache/camel/dataformat/beanio/springboot/BeanIODataFormatAutoConfiguration.java index f207b6f..dc93bab 100644 --- a/platforms/spring-boot/components-starter/camel-beanio-starter/src/main/java/org/apache/camel/dataformat/beanio/springboot/BeanIODataFormatAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-beanio-starter/src/main/java/org/apache/camel/dataformat/beanio/springboot/BeanIODataFormatAutoConfiguration.java @@ -20,7 +20,10 @@ import java.util.HashMap; import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.dataformat.beanio.BeanIODataFormat; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; import org.apache.camel.util.IntrospectionSupport; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionMessage; @@ -35,7 +38,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; import org.springframework.core.type.AnnotatedTypeMetadata; /** @@ -48,27 +50,36 @@ import org.springframework.core.type.AnnotatedTypeMetadata; @EnableConfigurationProperties(BeanIODataFormatConfiguration.class) public class BeanIODataFormatAutoConfiguration { - @Bean(name = "beanio-dataformat") - @Scope("prototype") + @Bean(name = "beanio-dataformat-factory") @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(BeanIODataFormat.class) - public BeanIODataFormat configureBeanIODataFormat( - CamelContext camelContext, - BeanIODataFormatConfiguration configuration) throws Exception { - BeanIODataFormat dataformat = new BeanIODataFormat(); - if (CamelContextAware.class.isAssignableFrom(BeanIODataFormat.class)) { - CamelContextAware contextAware = CamelContextAware.class - .cast(dataformat); - if (contextAware != null) { - contextAware.setCamelContext(camelContext); + public DataFormatFactory configureBeanIODataFormatFactory( + final CamelContext camelContext, + final BeanIODataFormatConfiguration configuration) { + return new DataFormatFactory() { + public DataFormat newInstance() { + BeanIODataFormat dataformat = new BeanIODataFormat(); + if (CamelContextAware.class + .isAssignableFrom(BeanIODataFormat.class)) { + CamelContextAware contextAware = CamelContextAware.class + .cast(dataformat); + if (contextAware != null) { + contextAware.setCamelContext(camelContext); + } + } + try { + Map<String, Object> parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, + parameters, null, false); + IntrospectionSupport.setProperties(camelContext, + camelContext.getTypeConverter(), dataformat, + parameters); + } catch (Exception e) { + throw new RuntimeCamelException(e); + } + return dataformat; } - } - Map<String, Object> parameters = new HashMap<>(); - IntrospectionSupport.getProperties(configuration, parameters, null, - false); - IntrospectionSupport.setProperties(camelContext, - camelContext.getTypeConverter(), dataformat, parameters); - return dataformat; + }; } public static class Condition extends SpringBootCondition { http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/csv/springboot/BindyCsvDataFormatAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/csv/springboot/BindyCsvDataFormatAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/csv/springboot/BindyCsvDataFormatAutoConfiguration.java index 9309645..e2583ee 100644 --- a/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/csv/springboot/BindyCsvDataFormatAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/csv/springboot/BindyCsvDataFormatAutoConfiguration.java @@ -20,7 +20,10 @@ import java.util.HashMap; import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; import org.apache.camel.util.IntrospectionSupport; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionMessage; @@ -35,7 +38,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; import org.springframework.core.type.AnnotatedTypeMetadata; /** @@ -48,27 +50,36 @@ import org.springframework.core.type.AnnotatedTypeMetadata; @EnableConfigurationProperties(BindyCsvDataFormatConfiguration.class) public class BindyCsvDataFormatAutoConfiguration { - @Bean(name = "bindy-csv-dataformat") - @Scope("prototype") + @Bean(name = "bindy-csv-dataformat-factory") @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(BindyCsvDataFormat.class) - public BindyCsvDataFormat configureBindyCsvDataFormat( - CamelContext camelContext, - BindyCsvDataFormatConfiguration configuration) throws Exception { - BindyCsvDataFormat dataformat = new BindyCsvDataFormat(); - if (CamelContextAware.class.isAssignableFrom(BindyCsvDataFormat.class)) { - CamelContextAware contextAware = CamelContextAware.class - .cast(dataformat); - if (contextAware != null) { - contextAware.setCamelContext(camelContext); + public DataFormatFactory configureBindyCsvDataFormatFactory( + final CamelContext camelContext, + final BindyCsvDataFormatConfiguration configuration) { + return new DataFormatFactory() { + public DataFormat newInstance() { + BindyCsvDataFormat dataformat = new BindyCsvDataFormat(); + if (CamelContextAware.class + .isAssignableFrom(BindyCsvDataFormat.class)) { + CamelContextAware contextAware = CamelContextAware.class + .cast(dataformat); + if (contextAware != null) { + contextAware.setCamelContext(camelContext); + } + } + try { + Map<String, Object> parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, + parameters, null, false); + IntrospectionSupport.setProperties(camelContext, + camelContext.getTypeConverter(), dataformat, + parameters); + } catch (Exception e) { + throw new RuntimeCamelException(e); + } + return dataformat; } - } - Map<String, Object> parameters = new HashMap<>(); - IntrospectionSupport.getProperties(configuration, parameters, null, - false); - IntrospectionSupport.setProperties(camelContext, - camelContext.getTypeConverter(), dataformat, parameters); - return dataformat; + }; } public static class Condition extends SpringBootCondition { http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/fixed/springboot/BindyFixedLengthDataFormatAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/fixed/springboot/BindyFixedLengthDataFormatAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/fixed/springboot/BindyFixedLengthDataFormatAutoConfiguration.java index b19ac42..64a547a 100644 --- a/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/fixed/springboot/BindyFixedLengthDataFormatAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/fixed/springboot/BindyFixedLengthDataFormatAutoConfiguration.java @@ -20,7 +20,10 @@ import java.util.HashMap; import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.dataformat.bindy.fixed.BindyFixedLengthDataFormat; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; import org.apache.camel.util.IntrospectionSupport; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionMessage; @@ -35,7 +38,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; import org.springframework.core.type.AnnotatedTypeMetadata; /** @@ -48,29 +50,36 @@ import org.springframework.core.type.AnnotatedTypeMetadata; @EnableConfigurationProperties(BindyFixedLengthDataFormatConfiguration.class) public class BindyFixedLengthDataFormatAutoConfiguration { - @Bean(name = "bindy-fixed-dataformat") - @Scope("prototype") + @Bean(name = "bindy-fixed-dataformat-factory") @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(BindyFixedLengthDataFormat.class) - public BindyFixedLengthDataFormat configureBindyFixedLengthDataFormat( - CamelContext camelContext, - BindyFixedLengthDataFormatConfiguration configuration) - throws Exception { - BindyFixedLengthDataFormat dataformat = new BindyFixedLengthDataFormat(); - if (CamelContextAware.class - .isAssignableFrom(BindyFixedLengthDataFormat.class)) { - CamelContextAware contextAware = CamelContextAware.class - .cast(dataformat); - if (contextAware != null) { - contextAware.setCamelContext(camelContext); + public DataFormatFactory configureBindyFixedLengthDataFormatFactory( + final CamelContext camelContext, + final BindyFixedLengthDataFormatConfiguration configuration) { + return new DataFormatFactory() { + public DataFormat newInstance() { + BindyFixedLengthDataFormat dataformat = new BindyFixedLengthDataFormat(); + if (CamelContextAware.class + .isAssignableFrom(BindyFixedLengthDataFormat.class)) { + CamelContextAware contextAware = CamelContextAware.class + .cast(dataformat); + if (contextAware != null) { + contextAware.setCamelContext(camelContext); + } + } + try { + Map<String, Object> parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, + parameters, null, false); + IntrospectionSupport.setProperties(camelContext, + camelContext.getTypeConverter(), dataformat, + parameters); + } catch (Exception e) { + throw new RuntimeCamelException(e); + } + return dataformat; } - } - Map<String, Object> parameters = new HashMap<>(); - IntrospectionSupport.getProperties(configuration, parameters, null, - false); - IntrospectionSupport.setProperties(camelContext, - camelContext.getTypeConverter(), dataformat, parameters); - return dataformat; + }; } public static class Condition extends SpringBootCondition { http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/kvp/springboot/BindyKeyValuePairDataFormatAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/kvp/springboot/BindyKeyValuePairDataFormatAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/kvp/springboot/BindyKeyValuePairDataFormatAutoConfiguration.java index 27b0cbd..733e14c 100644 --- a/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/kvp/springboot/BindyKeyValuePairDataFormatAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/kvp/springboot/BindyKeyValuePairDataFormatAutoConfiguration.java @@ -20,7 +20,10 @@ import java.util.HashMap; import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.dataformat.bindy.kvp.BindyKeyValuePairDataFormat; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; import org.apache.camel.util.IntrospectionSupport; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionMessage; @@ -35,7 +38,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; import org.springframework.core.type.AnnotatedTypeMetadata; /** @@ -48,29 +50,36 @@ import org.springframework.core.type.AnnotatedTypeMetadata; @EnableConfigurationProperties(BindyKeyValuePairDataFormatConfiguration.class) public class BindyKeyValuePairDataFormatAutoConfiguration { - @Bean(name = "bindy-kvp-dataformat") - @Scope("prototype") + @Bean(name = "bindy-kvp-dataformat-factory") @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(BindyKeyValuePairDataFormat.class) - public BindyKeyValuePairDataFormat configureBindyKeyValuePairDataFormat( - CamelContext camelContext, - BindyKeyValuePairDataFormatConfiguration configuration) - throws Exception { - BindyKeyValuePairDataFormat dataformat = new BindyKeyValuePairDataFormat(); - if (CamelContextAware.class - .isAssignableFrom(BindyKeyValuePairDataFormat.class)) { - CamelContextAware contextAware = CamelContextAware.class - .cast(dataformat); - if (contextAware != null) { - contextAware.setCamelContext(camelContext); + public DataFormatFactory configureBindyKeyValuePairDataFormatFactory( + final CamelContext camelContext, + final BindyKeyValuePairDataFormatConfiguration configuration) { + return new DataFormatFactory() { + public DataFormat newInstance() { + BindyKeyValuePairDataFormat dataformat = new BindyKeyValuePairDataFormat(); + if (CamelContextAware.class + .isAssignableFrom(BindyKeyValuePairDataFormat.class)) { + CamelContextAware contextAware = CamelContextAware.class + .cast(dataformat); + if (contextAware != null) { + contextAware.setCamelContext(camelContext); + } + } + try { + Map<String, Object> parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, + parameters, null, false); + IntrospectionSupport.setProperties(camelContext, + camelContext.getTypeConverter(), dataformat, + parameters); + } catch (Exception e) { + throw new RuntimeCamelException(e); + } + return dataformat; } - } - Map<String, Object> parameters = new HashMap<>(); - IntrospectionSupport.getProperties(configuration, parameters, null, - false); - IntrospectionSupport.setProperties(camelContext, - camelContext.getTypeConverter(), dataformat, parameters); - return dataformat; + }; } public static class Condition extends SpringBootCondition { http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/platforms/spring-boot/components-starter/camel-boon-starter/src/main/java/org/apache/camel/component/boon/springboot/BoonDataFormatAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-boon-starter/src/main/java/org/apache/camel/component/boon/springboot/BoonDataFormatAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-boon-starter/src/main/java/org/apache/camel/component/boon/springboot/BoonDataFormatAutoConfiguration.java index ff20711..5d6702f 100644 --- a/platforms/spring-boot/components-starter/camel-boon-starter/src/main/java/org/apache/camel/component/boon/springboot/BoonDataFormatAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-boon-starter/src/main/java/org/apache/camel/component/boon/springboot/BoonDataFormatAutoConfiguration.java @@ -20,7 +20,10 @@ import java.util.HashMap; import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.component.boon.BoonDataFormat; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; import org.apache.camel.util.IntrospectionSupport; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionMessage; @@ -35,7 +38,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; import org.springframework.core.type.AnnotatedTypeMetadata; /** @@ -48,26 +50,36 @@ import org.springframework.core.type.AnnotatedTypeMetadata; @EnableConfigurationProperties(BoonDataFormatConfiguration.class) public class BoonDataFormatAutoConfiguration { - @Bean(name = "boon-dataformat") - @Scope("prototype") + @Bean(name = "boon-dataformat-factory") @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(BoonDataFormat.class) - public BoonDataFormat configureBoonDataFormat(CamelContext camelContext, - BoonDataFormatConfiguration configuration) throws Exception { - BoonDataFormat dataformat = new BoonDataFormat(); - if (CamelContextAware.class.isAssignableFrom(BoonDataFormat.class)) { - CamelContextAware contextAware = CamelContextAware.class - .cast(dataformat); - if (contextAware != null) { - contextAware.setCamelContext(camelContext); + public DataFormatFactory configureBoonDataFormatFactory( + final CamelContext camelContext, + final BoonDataFormatConfiguration configuration) { + return new DataFormatFactory() { + public DataFormat newInstance() { + BoonDataFormat dataformat = new BoonDataFormat(); + if (CamelContextAware.class + .isAssignableFrom(BoonDataFormat.class)) { + CamelContextAware contextAware = CamelContextAware.class + .cast(dataformat); + if (contextAware != null) { + contextAware.setCamelContext(camelContext); + } + } + try { + Map<String, Object> parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, + parameters, null, false); + IntrospectionSupport.setProperties(camelContext, + camelContext.getTypeConverter(), dataformat, + parameters); + } catch (Exception e) { + throw new RuntimeCamelException(e); + } + return dataformat; } - } - Map<String, Object> parameters = new HashMap<>(); - IntrospectionSupport.getProperties(configuration, parameters, null, - false); - IntrospectionSupport.setProperties(camelContext, - camelContext.getTypeConverter(), dataformat, parameters); - return dataformat; + }; } public static class Condition extends SpringBootCondition { http://git-wip-us.apache.org/repos/asf/camel/blob/c0a9f5cd/platforms/spring-boot/components-starter/camel-castor-starter/src/main/java/org/apache/camel/dataformat/castor/springboot/CastorDataFormatAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-castor-starter/src/main/java/org/apache/camel/dataformat/castor/springboot/CastorDataFormatAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-castor-starter/src/main/java/org/apache/camel/dataformat/castor/springboot/CastorDataFormatAutoConfiguration.java index 07ec505..f4588e1 100644 --- a/platforms/spring-boot/components-starter/camel-castor-starter/src/main/java/org/apache/camel/dataformat/castor/springboot/CastorDataFormatAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-castor-starter/src/main/java/org/apache/camel/dataformat/castor/springboot/CastorDataFormatAutoConfiguration.java @@ -20,7 +20,10 @@ import java.util.HashMap; import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.dataformat.castor.CastorDataFormat; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatFactory; import org.apache.camel.util.IntrospectionSupport; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionMessage; @@ -35,7 +38,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; import org.springframework.core.type.AnnotatedTypeMetadata; /** @@ -48,27 +50,36 @@ import org.springframework.core.type.AnnotatedTypeMetadata; @EnableConfigurationProperties(CastorDataFormatConfiguration.class) public class CastorDataFormatAutoConfiguration { - @Bean(name = "castor-dataformat") - @Scope("prototype") + @Bean(name = "castor-dataformat-factory") @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(CastorDataFormat.class) - public CastorDataFormat configureCastorDataFormat( - CamelContext camelContext, - CastorDataFormatConfiguration configuration) throws Exception { - CastorDataFormat dataformat = new CastorDataFormat(); - if (CamelContextAware.class.isAssignableFrom(CastorDataFormat.class)) { - CamelContextAware contextAware = CamelContextAware.class - .cast(dataformat); - if (contextAware != null) { - contextAware.setCamelContext(camelContext); + public DataFormatFactory configureCastorDataFormatFactory( + final CamelContext camelContext, + final CastorDataFormatConfiguration configuration) { + return new DataFormatFactory() { + public DataFormat newInstance() { + CastorDataFormat dataformat = new CastorDataFormat(); + if (CamelContextAware.class + .isAssignableFrom(CastorDataFormat.class)) { + CamelContextAware contextAware = CamelContextAware.class + .cast(dataformat); + if (contextAware != null) { + contextAware.setCamelContext(camelContext); + } + } + try { + Map<String, Object> parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, + parameters, null, false); + IntrospectionSupport.setProperties(camelContext, + camelContext.getTypeConverter(), dataformat, + parameters); + } catch (Exception e) { + throw new RuntimeCamelException(e); + } + return dataformat; } - } - Map<String, Object> parameters = new HashMap<>(); - IntrospectionSupport.getProperties(configuration, parameters, null, - false); - IntrospectionSupport.setProperties(camelContext, - camelContext.getTypeConverter(), dataformat, parameters); - return dataformat; + }; } public static class Condition extends SpringBootCondition {