This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch properties in repository https://gitbox.apache.org/repos/asf/camel.git
commit 59738b245ec717789059e1216e2115a1568f9fec Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Jul 4 06:27:38 2019 +0200 CAMEL-13708: Properties component should have API to make it easier to lookup a property on-demand or from all pre-loaded properties. --- .../camel/spring/CamelContextFactoryBean.java | 10 +-- .../spi/BridgePropertyPlaceholderConfigurer.java | 90 +-------------------- ...elSpringPropertyPlaceholderConfigurer3Test.java | 93 ---------------------- .../properties/SpringPropertiesResolver2Test.java | 29 ------- .../properties/SpringPropertiesResolver3Test.java | 29 ------- .../properties/SpringPropertiesResolverTest.java | 40 ---------- .../properties/SpringPropertiesResolver2Test.xml | 39 --------- .../properties/SpringPropertiesResolver3Test.xml | 38 --------- .../properties/SpringPropertiesResolverTest.xml | 41 ---------- 9 files changed, 6 insertions(+), 403 deletions(-) diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java index 0cbc647..856f240 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java @@ -334,20 +334,14 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Spr } // use the spring system properties mode which has a different value than Camel may have pc.setSystemPropertiesMode(configurer.getSystemPropertiesMode()); + // replace existing resolver with us - configurer.setResolver(pc.getPropertiesResolver()); configurer.setParser(pc.getPropertiesParser()); // use the bridge to handle the resolve and parsing - pc.setPropertiesResolver(configurer); pc.setPropertiesParser(configurer); - + // use the bridge as property source pc.addPropertiesSource(configurer); - // and update locations to have our as ref first -// List<PropertiesLocation> locations = new ArrayList<>(pc.getLocations()); -// locations.add(0, new PropertiesLocation("ref", id)); - - //pc.setLocations(locations); } else if (beans.size() > 1) { LOG.warn("Cannot bridge Camel and Spring property placeholders, as exact only 1 bean of type BridgePropertyPlaceholderConfigurer" + " must be defined, was {} beans defined.", beans.size()); diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java index b5e2f1d..6e74905 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java @@ -16,16 +16,10 @@ */ package org.apache.camel.spring.spi; -import java.util.Collections; -import java.util.List; import java.util.Properties; -import org.apache.camel.CamelContext; -import org.apache.camel.component.properties.DefaultPropertiesLookup; -import org.apache.camel.component.properties.PropertiesLocation; import org.apache.camel.component.properties.PropertiesLookup; import org.apache.camel.component.properties.PropertiesParser; -import org.apache.camel.component.properties.PropertiesResolver; import org.apache.camel.component.properties.PropertiesSource; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; @@ -37,28 +31,15 @@ import org.springframework.util.PropertyPlaceholderHelper; * A {@link PropertyPlaceholderConfigurer} that bridges Camel's <a href="http://camel.apache.org/using-propertyplaceholder.html"> * property placeholder</a> with the Spring property placeholder mechanism. */ -public class BridgePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer implements PropertiesResolver, PropertiesParser, PropertiesSource { +public class BridgePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer implements PropertiesParser, PropertiesSource { // NOTE: this class must be in the spi package as if its in the root package, then Spring fails to parse the XML // files due some weird spring issue. But that is okay as having this class in the spi package is fine anyway. private final Properties properties = new Properties(); - private PropertiesResolver resolver; private PropertiesParser parser; - private String id; private PropertyPlaceholderHelper helper; - - // to support both Spring 3.0 / 3.1+ we need to keep track of these as they have private modified in Spring 3.0 - private String configuredPlaceholderPrefix; - private String configuredPlaceholderSuffix; - private String configuredValueSeparator; - private Boolean configuredIgnoreUnresolvablePlaceholders; private int systemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK; - private Boolean ignoreResourceNotFound; - - public int getSystemPropertiesMode() { - return systemPropertiesMode; - } @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { @@ -66,17 +47,11 @@ public class BridgePropertyPlaceholderConfigurer extends PropertyPlaceholderConf // store all the spring properties so we can refer to them later properties.putAll(props); // create helper - helper = new PropertyPlaceholderHelper( - configuredPlaceholderPrefix != null ? configuredPlaceholderPrefix : DEFAULT_PLACEHOLDER_PREFIX, - configuredPlaceholderSuffix != null ? configuredPlaceholderSuffix : DEFAULT_PLACEHOLDER_SUFFIX, - configuredValueSeparator != null ? configuredValueSeparator : DEFAULT_VALUE_SEPARATOR, - configuredIgnoreUnresolvablePlaceholders != null ? configuredIgnoreUnresolvablePlaceholders : false); + helper = new PropertyPlaceholderHelper(placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders); } - @Override - public void setBeanName(String beanName) { - this.id = beanName; - super.setBeanName(beanName); + public int getSystemPropertiesMode() { + return systemPropertiesMode; } @Override @@ -93,36 +68,6 @@ public class BridgePropertyPlaceholderConfigurer extends PropertyPlaceholderConf } @Override - public void setPlaceholderPrefix(String placeholderPrefix) { - super.setPlaceholderPrefix(placeholderPrefix); - this.configuredPlaceholderPrefix = placeholderPrefix; - } - - @Override - public void setPlaceholderSuffix(String placeholderSuffix) { - super.setPlaceholderSuffix(placeholderSuffix); - this.configuredPlaceholderSuffix = placeholderSuffix; - } - - @Override - public void setValueSeparator(String valueSeparator) { - super.setValueSeparator(valueSeparator); - this.configuredValueSeparator = valueSeparator; - } - - @Override - public void setIgnoreUnresolvablePlaceholders(boolean ignoreUnresolvablePlaceholders) { - super.setIgnoreUnresolvablePlaceholders(ignoreUnresolvablePlaceholders); - this.configuredIgnoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders; - } - - @Override - public void setIgnoreResourceNotFound(boolean ignoreResourceNotFound) { - super.setIgnoreResourceNotFound(ignoreResourceNotFound); - this.ignoreResourceNotFound = ignoreResourceNotFound; - } - - @Override protected String resolvePlaceholder(String placeholder, Properties props) { String value = props.getProperty(placeholder); if (parser != null) { @@ -134,29 +79,6 @@ public class BridgePropertyPlaceholderConfigurer extends PropertyPlaceholderConf } @Override - public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) { - // return the spring properties, if it - Properties answer = new Properties(); - for (PropertiesLocation location : locations) { - if ("ref".equals(location.getResolver()) && id.equals(location.getPath())) { - answer.putAll(properties); - } else if (resolver != null) { - boolean flag = ignoreMissingLocation; - // Override the setting by using ignoreResourceNotFound - if (ignoreResourceNotFound != null) { - flag = ignoreResourceNotFound; - } - Properties p = resolver.resolveProperties(context, flag, Collections.singletonList(location)); - if (p != null) { - answer.putAll(p); - } - } - } - // must not return null - return answer; - } - - @Override public String parseUri(String text, PropertiesLookup properties, String prefixToken, String suffixToken, boolean fallback) throws IllegalArgumentException { // first let Camel parse the text as it may contain Camel placeholders String answer = parser.parseUri(text, properties, prefixToken, suffixToken, fallback); @@ -192,10 +114,6 @@ public class BridgePropertyPlaceholderConfigurer extends PropertyPlaceholderConf return helper.replacePlaceholders(text, new BridgePropertyPlaceholderResolver(properties)); } - public void setResolver(PropertiesResolver resolver) { - this.resolver = resolver; - } - public void setParser(PropertiesParser parser) { if (this.parser != null) { // use a bridge if there is already a parser configured diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer3Test.java b/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer3Test.java deleted file mode 100644 index 71e6dbf..0000000 --- a/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer3Test.java +++ /dev/null @@ -1,93 +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.component.properties; -import java.util.List; -import java.util.Properties; - -import org.apache.camel.CamelContext; -import org.apache.camel.spring.SpringTestSupport; -import org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.springframework.context.support.AbstractXmlApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -/** - * - */ -public class CamelSpringPropertyPlaceholderConfigurer3Test extends SpringTestSupport { - - @Override - @Before - public void setUp() throws Exception { - // inside the used properties file (cheese.properties) we've defined the following key/value mapping: - // hi2=Guten Tag - // however as we make use of the PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE mode - // (which is NOT the default mode) we expect that setting the system property below should override - // the mapping being defined above. that's we expect the following key/value mapping taking effect: - // hi2=Gute Nacht - System.setProperty("hi2", "Gute Nacht"); - - super.setUp(); - } - - @Override - @After - public void tearDown() throws Exception { - // clear the property to avoid any side effect by the other tests - System.clearProperty("hi2"); - - super.tearDown(); - } - - @Override - protected AbstractXmlApplicationContext createApplicationContext() { - return new ClassPathXmlApplicationContext("org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer3Test.xml"); - } - - @Test - public void testCamelSpringPropertyPlaceholderConfigurerTest() throws Exception { - getMockEndpoint("mock:result").expectedBodiesReceived("Gute Nacht Camel"); - - template.sendBody("direct:bar", "Camel"); - - assertMockEndpointsSatisfied(); - } - - @SuppressWarnings("unused") - private static class MyBridgePropertyPlaceholderConfigurer extends BridgePropertyPlaceholderConfigurer { - - @Override - public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) { - Properties answer = super.resolveProperties(context, ignoreMissingLocation, locations); - - // define the additional properties we need to provide so that the uri "direct:{{foo}}" by the "from" clause - // as well as "{{scheme}}{{separator}}{{context-path}}" by the "to" clause can be properly resolved. please - // note that in this simple test we just add these properties hard-coded below but of course the mechanism to - // retrieve these extra properties can be anything else, e.g. through the entries inside a database table etc. - answer.put("foo", "bar"); - answer.put("scheme", "mock"); - answer.put("separator", ":"); - answer.put("context-path", "result"); - - return answer; - } - - } - -} diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesResolver2Test.java b/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesResolver2Test.java deleted file mode 100644 index 3ab1e9b..0000000 --- a/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesResolver2Test.java +++ /dev/null @@ -1,29 +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.component.properties; - -import org.springframework.context.support.AbstractXmlApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class SpringPropertiesResolver2Test extends SpringPropertiesResolverTest { - - @Override - protected AbstractXmlApplicationContext createApplicationContext() { - return new ClassPathXmlApplicationContext("org/apache/camel/component/properties/SpringPropertiesResolver2Test.xml"); - } - -} \ No newline at end of file diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesResolver3Test.java b/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesResolver3Test.java deleted file mode 100644 index 35a1a52..0000000 --- a/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesResolver3Test.java +++ /dev/null @@ -1,29 +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.component.properties; - -import org.springframework.context.support.AbstractXmlApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class SpringPropertiesResolver3Test extends SpringPropertiesResolverTest { - - @Override - protected AbstractXmlApplicationContext createApplicationContext() { - return new ClassPathXmlApplicationContext("org/apache/camel/component/properties/SpringPropertiesResolver3Test.xml"); - } - -} \ No newline at end of file diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesResolverTest.java b/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesResolverTest.java deleted file mode 100644 index 41853dc20..0000000 --- a/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesResolverTest.java +++ /dev/null @@ -1,40 +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.component.properties; - -import org.apache.camel.spring.SpringTestSupport; -import org.junit.Test; -import org.springframework.context.support.AbstractXmlApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class SpringPropertiesResolverTest extends SpringTestSupport { - - @Override - protected AbstractXmlApplicationContext createApplicationContext() { - return new ClassPathXmlApplicationContext("org/apache/camel/component/properties/SpringPropertiesResolverTest.xml"); - } - - @Test - public void testSpringPropertiesResolver() throws Exception { - getMockEndpoint("mock:result").expectedMessageCount(1); - - template.sendBody("direct:start", "Hello World"); - - assertMockEndpointsSatisfied(); - } - -} \ No newline at end of file diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesResolver2Test.xml b/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesResolver2Test.xml deleted file mode 100644 index 23499fd..0000000 --- a/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesResolver2Test.xml +++ /dev/null @@ -1,39 +0,0 @@ -<?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="myResolver" class="org.apache.camel.component.properties.PropertiesResolverTest$MyCustomResolver"/> - - <camelContext xmlns="http://camel.apache.org/schema/spring"> - <propertyPlaceholder id="properties" - location="foo" - propertiesResolverRef="myResolver"/> - <route> - <from uri="direct:start"/> - <to uri="properties:foo"/> - </route> - </camelContext> - -</beans> diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesResolver3Test.xml b/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesResolver3Test.xml deleted file mode 100644 index 1b70c8a..0000000 --- a/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesResolver3Test.xml +++ /dev/null @@ -1,38 +0,0 @@ -<?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="myResolver" class="org.apache.camel.component.properties.PropertiesResolverTest$MyCustomResolver"/> - - <camelContext xmlns="http://camel.apache.org/schema/spring"> - <propertyPlaceholder id="properties" location="foo" propertiesResolverRef="myResolver"/> - - <route> - <from uri="direct:start"/> - <to uri="properties:foo"/> - </route> - </camelContext> - -</beans> diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesResolverTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesResolverTest.xml deleted file mode 100644 index cb46695..0000000 --- a/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesResolverTest.xml +++ /dev/null @@ -1,41 +0,0 @@ -<?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="myResolver" class="org.apache.camel.component.properties.PropertiesResolverTest$MyCustomResolver"/> - - <bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent"> - <property name="location" value="foo"/> - <property name="propertiesResolver" ref="myResolver"/> - </bean> - - <camelContext xmlns="http://camel.apache.org/schema/spring"> - <route> - <from uri="direct:start"/> - <to uri="properties:foo"/> - </route> - </camelContext> - -</beans>