This is an automated email from the ASF dual-hosted git repository. zregvart pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 30ac0f4 CAMEL-13893: refactor to remove ConfigurationOp... 30ac0f4 is described below commit 30ac0f41e2b26e8acab7746950c6a3d026e4833c Author: Zoran Regvart <zregv...@apache.org> AuthorDate: Fri Aug 23 13:37:08 2019 +0200 CAMEL-13893: refactor to remove ConfigurationOp... ...tions This refactors so we reuse PropertyDefinition instead of introducing ConfigurationOption and ConfigurationOptions. Also opts to use an XmlAdapter implementation for marshalling/unmarshalling Map<String, String>. --- .../any23/spring/SpringAny23DataFormatTest.xml | 2 +- components/camel-blueprint/pom.xml | 1 + components/camel-spring/pom.xml | 1 + .../apache/camel/model/ConfigurationOption.java | 70 ---------------------- .../apache/camel/model/ConfigurationOptions.java | 58 ------------------ .../org/apache/camel/model/PropertyDefinition.java | 5 ++ .../apache/camel/model/PropertyDefinitions.java | 43 +++++++++++++ .../camel/model/PropertyDescriptionsAdapter.java | 50 ++++++++++++++++ .../camel/model/dataformat/Any23DataFormat.java | 15 +++-- .../model/dataformat/Any23DataFormatTest.java | 52 ++++++++++++++++ 10 files changed, 162 insertions(+), 135 deletions(-) diff --git a/components/camel-any23/src/test/resources/org/apache/camel/dataformat/any23/spring/SpringAny23DataFormatTest.xml b/components/camel-any23/src/test/resources/org/apache/camel/dataformat/any23/spring/SpringAny23DataFormatTest.xml index 2820541..f9a4ff6 100644 --- a/components/camel-any23/src/test/resources/org/apache/camel/dataformat/any23/spring/SpringAny23DataFormatTest.xml +++ b/components/camel-any23/src/test/resources/org/apache/camel/dataformat/any23/spring/SpringAny23DataFormatTest.xml @@ -30,7 +30,7 @@ <dataFormats> <any23 id="any23" baseURI ="http://mock.foo/bar" outputFormat="TURTLE" > <configuration> - <option name="any23.extraction.metadata.nesting" value="off" /> + <property key="any23.extraction.metadata.nesting" value="off" /> </configuration> <extractors>html-head-title</extractors> </any23> diff --git a/components/camel-blueprint/pom.xml b/components/camel-blueprint/pom.xml index 567e9ed..9b6261b 100644 --- a/components/camel-blueprint/pom.xml +++ b/components/camel-blueprint/pom.xml @@ -270,6 +270,7 @@ <filter implementation="org.codehaus.mojo.jaxb2.shared.filters.pattern.PatternFileFilter"> <patterns> <pattern>Helper.java</pattern> + <pattern>Adapter.java</pattern> </patterns> </filter> </schemaSourceExcludeFilters> diff --git a/components/camel-spring/pom.xml b/components/camel-spring/pom.xml index 2849484..8d2e67b 100644 --- a/components/camel-spring/pom.xml +++ b/components/camel-spring/pom.xml @@ -301,6 +301,7 @@ <filter implementation="org.codehaus.mojo.jaxb2.shared.filters.pattern.PatternFileFilter"> <patterns> <pattern>Helper.java</pattern> + <pattern>Adapter.java</pattern> </patterns> </filter> </schemaSourceExcludeFilters> diff --git a/core/camel-core/src/main/java/org/apache/camel/model/ConfigurationOption.java b/core/camel-core/src/main/java/org/apache/camel/model/ConfigurationOption.java deleted file mode 100644 index e6f9d0c..0000000 --- a/core/camel-core/src/main/java/org/apache/camel/model/ConfigurationOption.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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.model; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlRootElement; - -import org.apache.camel.spi.Metadata; - -@Metadata(label = "configuration") -@XmlRootElement(name = "option") -@XmlAccessorType(XmlAccessType.FIELD) -public class ConfigurationOption { - - @XmlAttribute(required = true) - public String name; - - @XmlAttribute(required = true) - public String value; - - public ConfigurationOption() { - } - - public ConfigurationOption(final String name, final String value) { - this.name = name; - this.value = value; - } - - @Deprecated - String name() { - return name; - } - - @Deprecated - String value() { - return value; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } -} diff --git a/core/camel-core/src/main/java/org/apache/camel/model/ConfigurationOptions.java b/core/camel-core/src/main/java/org/apache/camel/model/ConfigurationOptions.java deleted file mode 100644 index b4662e7..0000000 --- a/core/camel-core/src/main/java/org/apache/camel/model/ConfigurationOptions.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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.model; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; - -import org.apache.camel.spi.Metadata; - -@Metadata(label = "configuration") -@XmlRootElement(name = "configuration") -@XmlAccessorType(XmlAccessType.FIELD) -public class ConfigurationOptions { - @XmlElement(name = "option") - public List<ConfigurationOption> globalOptions = new ArrayList<>(); - - public Map<String, String> asMap() { - return globalOptions.stream() - .collect(Collectors.toMap(ConfigurationOption::name, ConfigurationOption::value)); - } - - public static ConfigurationOptions from(Map<String, String> map) { - final ConfigurationOptions ret = new ConfigurationOptions(); - - map.forEach((k, v) -> ret.globalOptions.add(new ConfigurationOption(k, v))); - - return ret; - } - - public List<ConfigurationOption> getGlobalOptions() { - return globalOptions; - } - - public void setGlobalOptions(List<ConfigurationOption> globalOptions) { - this.globalOptions = globalOptions; - } -} diff --git a/core/camel-core/src/main/java/org/apache/camel/model/PropertyDefinition.java b/core/camel-core/src/main/java/org/apache/camel/model/PropertyDefinition.java index 349659f..4730894 100644 --- a/core/camel-core/src/main/java/org/apache/camel/model/PropertyDefinition.java +++ b/core/camel-core/src/main/java/org/apache/camel/model/PropertyDefinition.java @@ -38,6 +38,11 @@ public class PropertyDefinition { public PropertyDefinition() { } + public PropertyDefinition(String key, String value) { + this.key = key; + this.value = value; + } + /** * Property key */ diff --git a/core/camel-core/src/main/java/org/apache/camel/model/PropertyDefinitions.java b/core/camel-core/src/main/java/org/apache/camel/model/PropertyDefinitions.java new file mode 100644 index 0000000..b84c938 --- /dev/null +++ b/core/camel-core/src/main/java/org/apache/camel/model/PropertyDefinitions.java @@ -0,0 +1,43 @@ +/** + * 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.model; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlType; + +@XmlType +public class PropertyDefinitions { + + private List<PropertyDefinition> properties = new ArrayList<>(); + + @XmlElement(name = "property") + public List<PropertyDefinition> getProperties() { + return properties; + } + + public void setProperties(List<PropertyDefinition> properties) { + if (properties == null) { + this.properties = new ArrayList<>(); + } else { + this.properties = properties; + } + } + +} diff --git a/core/camel-core/src/main/java/org/apache/camel/model/PropertyDescriptionsAdapter.java b/core/camel-core/src/main/java/org/apache/camel/model/PropertyDescriptionsAdapter.java new file mode 100644 index 0000000..1e5f1db --- /dev/null +++ b/core/camel-core/src/main/java/org/apache/camel/model/PropertyDescriptionsAdapter.java @@ -0,0 +1,50 @@ +/** + * 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.model; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.bind.annotation.adapters.XmlAdapter; + +public class PropertyDescriptionsAdapter extends XmlAdapter<PropertyDefinitions, Map<String, String>> { + + @Override + public Map<String, String> unmarshal(PropertyDefinitions value) throws Exception { + Map<String, String> ret = new HashMap<>(); + + if (value != null) { + value.getProperties().forEach(p -> ret.put(p.getKey(), p.getValue())); + } + + return ret; + } + + @Override + public PropertyDefinitions marshal(Map<String, String> value) throws Exception { + PropertyDefinitions ret = new PropertyDefinitions(); + + if (value != null) { + final List<PropertyDefinition> properties = ret.getProperties(); + value.forEach((k, v) -> properties.add(new PropertyDefinition(k, v))); + } + + return ret; + } + +} diff --git a/core/camel-core/src/main/java/org/apache/camel/model/dataformat/Any23DataFormat.java b/core/camel-core/src/main/java/org/apache/camel/model/dataformat/Any23DataFormat.java index f0f8110..5678816 100644 --- a/core/camel-core/src/main/java/org/apache/camel/model/dataformat/Any23DataFormat.java +++ b/core/camel-core/src/main/java/org/apache/camel/model/dataformat/Any23DataFormat.java @@ -16,6 +16,7 @@ */ package org.apache.camel.model.dataformat; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -24,9 +25,10 @@ import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; -import org.apache.camel.model.ConfigurationOptions; import org.apache.camel.model.DataFormatDefinition; +import org.apache.camel.model.PropertyDescriptionsAdapter; import org.apache.camel.spi.Metadata; /** @@ -40,7 +42,8 @@ public class Any23DataFormat extends DataFormatDefinition { @XmlAttribute @Metadata(defaultValue = "RDF4JMODEL") private Any23Type outputFormat; - private ConfigurationOptions configuration = new ConfigurationOptions(); + @XmlJavaTypeAdapter(PropertyDescriptionsAdapter.class) + private Map<String, String> configuration = new HashMap<String, String>(); @XmlElement private List<String> extractors; @XmlAttribute @@ -63,13 +66,13 @@ public class Any23DataFormat extends DataFormatDefinition { public Any23DataFormat(String baseuri, Any23Type outputFormat, Map<String, String> configurations) { this(baseuri, outputFormat); this.outputFormat = outputFormat; - this.configuration = ConfigurationOptions.from(configurations); + this.configuration = configurations; } public Any23DataFormat(String baseuri, Any23Type outputFormat, Map<String, String> configurations, List<String> extractors) { this(baseuri, outputFormat, configurations); this.outputFormat = outputFormat; - this.configuration = ConfigurationOptions.from(configurations); + this.configuration = configurations; this.extractors = extractors; } @@ -86,7 +89,7 @@ public class Any23DataFormat extends DataFormatDefinition { } public Map<String, String> getConfiguration() { - return configuration.asMap(); + return configuration; } /** @@ -97,7 +100,7 @@ public class Any23DataFormat extends DataFormatDefinition { * If not provided, a default configuration is used. */ public void setConfiguration(Map<String, String> configurations) { - this.configuration = ConfigurationOptions.from(configurations); + this.configuration = configurations; } public List<String> getExtractors() { diff --git a/core/camel-core/src/test/java/org/apache/camel/model/dataformat/Any23DataFormatTest.java b/core/camel-core/src/test/java/org/apache/camel/model/dataformat/Any23DataFormatTest.java new file mode 100644 index 0000000..59dcb52 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/model/dataformat/Any23DataFormatTest.java @@ -0,0 +1,52 @@ +/** + * 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.model.dataformat; + +import java.io.IOException; +import java.io.StringReader; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +public class Any23DataFormatTest { + + private final String xml = "<?xml version=\"1.0\"?>" + + "<any23 xmlns=\"http://camel.apache.org/schema/spring\">" + + "<configuration>" + + "<property key=\"k1\" value=\"v1\" />" + + "<property key=\"k2\" value=\"v2\" />" + + "</configuration>" + + "</any23>"; + + @Test + public void shouldDeserializeConfigurationPropertiesFromXml() throws JAXBException, IOException { + final JAXBContext context = JAXBContext.newInstance(Any23DataFormat.class); + + final Unmarshaller unmarshaller = context.createUnmarshaller(); + + final StringReader reader = new StringReader(xml); + final Any23DataFormat any23DataFormat = (Any23DataFormat) unmarshaller.unmarshal(reader); + + assertThat(any23DataFormat.getConfiguration()).containsOnly(entry("k1", "v1"), entry("k2", "v2")); + } +}