CAMEL-8109: Allow to plugin custom functions to property placeholder
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/869f1b52 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/869f1b52 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/869f1b52 Branch: refs/heads/camel-2.14.x Commit: 869f1b52ff7c4b7d07028457af3e851ed9baedd1 Parents: 7be7d57 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Dec 2 22:13:16 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Dec 3 08:54:10 2014 +0100 ---------------------------------------------------------------------- .../properties/PropertiesComponent.java | 10 +++- .../PropertiesComponentFunctionTest.java | 2 +- .../blueprint/BlueprintPropertiesParser.java | 1 + .../handler/CamelNamespaceHandler.java | 6 +++ .../xml/AbstractCamelContextFactoryBean.java | 10 +++- .../xml/CamelPropertyPlaceholderDefinition.java | 13 +++++ ...elPropertyPlaceholderFunctionDefinition.java | 37 ++++++++++++++ .../SpringPropertiesComponentFunctionTest.java | 54 ++++++++++++++++++++ .../SpringPropertiesComponentFunctionTest.xml | 40 +++++++++++++++ .../PropertiesComponentFunctionTest.java | 53 +++++++++++++++++++ .../PropertiesComponentFunctionTest.xml | 41 +++++++++++++++ 11 files changed, 264 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/869f1b52/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java b/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java index 30692b0..e30fb03 100644 --- a/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java +++ b/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java @@ -325,15 +325,23 @@ public class PropertiesComponent extends DefaultComponent { } /** - * Add the function + * Registers the {@link org.apache.camel.component.properties.PropertiesFunction} as a function to this component. */ public void addFunction(PropertiesFunction function) { this.functions.put(function.getName(), function); } + /** + * Is there a {@link org.apache.camel.component.properties.PropertiesFunction} with the given name? + */ + public boolean hasFunction(String name) { + return functions.containsKey(name); + } + @Override protected void doStop() throws Exception { cacheMap.clear(); + functions.clear(); super.doStop(); } http://git-wip-us.apache.org/repos/asf/camel/blob/869f1b52/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentFunctionTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentFunctionTest.java b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentFunctionTest.java index 4213cd0..f960f6d 100644 --- a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentFunctionTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentFunctionTest.java @@ -21,7 +21,7 @@ import org.apache.camel.builder.RouteBuilder; public class PropertiesComponentFunctionTest extends ContextTestSupport { - private class MyFunction implements PropertiesFunction { + public static final class MyFunction implements PropertiesFunction { @Override public String getName() { http://git-wip-us.apache.org/repos/asf/camel/blob/869f1b52/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesParser.java ---------------------------------------------------------------------- diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesParser.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesParser.java index 9c5a3f0..efc5347 100644 --- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesParser.java +++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesParser.java @@ -51,6 +51,7 @@ public class BlueprintPropertiesParser extends DefaultPropertiesParser { private Method method; public BlueprintPropertiesParser(PropertiesComponent propertiesComponent, BlueprintContainer container, PropertiesParser delegate) { + super(propertiesComponent); this.propertiesComponent = propertiesComponent; this.container = container; this.delegate = delegate; http://git-wip-us.apache.org/repos/asf/camel/blob/869f1b52/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java ---------------------------------------------------------------------- diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java index 4985abc..eab8798 100644 --- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java +++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java @@ -33,6 +33,7 @@ import javax.xml.bind.Binder; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; +import org.apache.camel.component.properties.PropertiesComponent; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -1041,6 +1042,11 @@ public class CamelNamespaceHandler implements NamespaceHandler { } private void findUriComponent(String uri, Set<String> components) { + // if the uri is a placeholder then skip it + if (uri != null && uri.startsWith(PropertiesComponent.DEFAULT_PREFIX_TOKEN)) { + return; + } + if (uri != null) { String splitURI[] = ObjectHelper.splitOnCharacter(uri, ":", 2); if (splitURI[1] != null) { http://git-wip-us.apache.org/repos/asf/camel/blob/869f1b52/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java ---------------------------------------------------------------------- diff --git a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java index f960c3a..32b84a8 100644 --- a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java +++ b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java @@ -24,7 +24,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; - import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlTransient; @@ -38,6 +37,7 @@ import org.apache.camel.ShutdownRunningTask; import org.apache.camel.builder.ErrorHandlerBuilderRef; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.properties.PropertiesComponent; +import org.apache.camel.component.properties.PropertiesFunction; import org.apache.camel.component.properties.PropertiesParser; import org.apache.camel.component.properties.PropertiesResolver; import org.apache.camel.management.DefaultManagementAgent; @@ -535,6 +535,14 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex pc.setPrefixToken(def.getPrefixToken()); pc.setSuffixToken(def.getSuffixToken()); + if (def.getFunctions() != null && !def.getFunctions().isEmpty()) { + for (CamelPropertyPlaceholderFunctionDefinition function : def.getFunctions()) { + String ref = function.getRef(); + PropertiesFunction pf = CamelContextHelper.mandatoryLookup(getContext(), ref, PropertiesFunction.class); + pc.addFunction(pf); + } + } + // register the properties component getContext().addComponent("properties", pc); } http://git-wip-us.apache.org/repos/asf/camel/blob/869f1b52/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java ---------------------------------------------------------------------- diff --git a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java index 17d2276..cc61c2f 100644 --- a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java +++ b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java @@ -16,9 +16,11 @@ */ package org.apache.camel.core.xml; +import java.util.List; import javax.xml.bind.annotation.XmlAccessType; 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 org.apache.camel.model.IdentifiedType; @@ -62,6 +64,9 @@ public class CamelPropertyPlaceholderDefinition extends IdentifiedType { @XmlAttribute private String suffixToken; + @XmlElement(name = "propertiesFunction") + private List<CamelPropertyPlaceholderFunctionDefinition> functions; + public String getLocation() { return location; } @@ -141,4 +146,12 @@ public class CamelPropertyPlaceholderDefinition extends IdentifiedType { public void setSuffixToken(String suffixToken) { this.suffixToken = suffixToken; } + + public List<CamelPropertyPlaceholderFunctionDefinition> getFunctions() { + return functions; + } + + public void setFunctions(List<CamelPropertyPlaceholderFunctionDefinition> functions) { + this.functions = functions; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/869f1b52/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderFunctionDefinition.java ---------------------------------------------------------------------- diff --git a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderFunctionDefinition.java b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderFunctionDefinition.java new file mode 100644 index 0000000..a7e328d --- /dev/null +++ b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderFunctionDefinition.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.core.xml; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +import org.apache.camel.model.IdentifiedType; + +@XmlRootElement(name = "propertiesFunction") +public class CamelPropertyPlaceholderFunctionDefinition extends IdentifiedType { + + @XmlAttribute(required = true) + private String ref; + + public String getRef() { + return ref; + } + + public void setRef(String ref) { + this.ref = ref; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/869f1b52/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentFunctionTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentFunctionTest.java b/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentFunctionTest.java new file mode 100644 index 0000000..c5e36a8 --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentFunctionTest.java @@ -0,0 +1,54 @@ +/** + * 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 SpringPropertiesComponentFunctionTest extends SpringTestSupport { + + public static final class MyFunction implements PropertiesFunction { + + @Override + public String getName() { + return "beer"; + } + + @Override + public String apply(String remainder) { + return "mock:" + remainder.toLowerCase(); + } + } + + @Override + protected AbstractXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/properties/SpringPropertiesComponentFunctionTest.xml"); + } + + @Test + public void testFunction() throws Exception { + getMockEndpoint("mock:foo").expectedMessageCount(1); + getMockEndpoint("mock:bar").expectedMessageCount(1); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/869f1b52/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentFunctionTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentFunctionTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentFunctionTest.xml new file mode 100644 index 0000000..8d6e865 --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentFunctionTest.xml @@ -0,0 +1,40 @@ +<?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="beerFunction" class="org.apache.camel.component.properties.PropertiesComponentFunctionTest.MyFunction"/> + + <camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring"> + + <propertyPlaceholder id="properties" location="none" ignoreMissingLocation="true"> + <propertiesFunction ref="beerFunction"/> + </propertyPlaceholder> + + <route> + <from uri="direct:start"/> + <to uri="{{beer:FOO}}"/> + <to uri="{{beer:BAR}}"/> + </route> + </camelContext> + +</beans> http://git-wip-us.apache.org/repos/asf/camel/blob/869f1b52/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/PropertiesComponentFunctionTest.java ---------------------------------------------------------------------- diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/PropertiesComponentFunctionTest.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/PropertiesComponentFunctionTest.java new file mode 100644 index 0000000..ee39d10 --- /dev/null +++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/PropertiesComponentFunctionTest.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.test.blueprint; + +import org.apache.camel.component.properties.PropertiesFunction; +import org.junit.Test; + +public class PropertiesComponentFunctionTest extends CamelBlueprintTestSupport { + + public static final class MyFunction implements PropertiesFunction { + + @Override + public String getName() { + return "beer"; + } + + @Override + public String apply(String remainder) { + return "mock:" + remainder.toLowerCase(); + } + } + + @Override + protected String getBlueprintDescriptor() { + return "org/apache/camel/test/blueprint/PropertiesComponentFunctionTest.xml"; + } + + @Test + public void testFunction() throws Exception { + getMockEndpoint("mock:foo").expectedMessageCount(1); + getMockEndpoint("mock:bar").expectedMessageCount(1); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + +} + http://git-wip-us.apache.org/repos/asf/camel/blob/869f1b52/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/PropertiesComponentFunctionTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/PropertiesComponentFunctionTest.xml b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/PropertiesComponentFunctionTest.xml new file mode 100644 index 0000000..d2cba08 --- /dev/null +++ b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/PropertiesComponentFunctionTest.xml @@ -0,0 +1,41 @@ +<?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. +--> +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> + + <bean id="beerFunction" class="org.apache.camel.test.blueprint.PropertiesComponentFunctionTest.MyFunction"/> + + <bean id="vmProperties" class="org.apache.camel.test.blueprint.MyProperties" /> + + <camelContext xmlns="http://camel.apache.org/schema/blueprint"> + + <propertyPlaceholder id="properties" location="ref:vmProperties" ignoreMissingLocation="true"> + <propertiesFunction ref="beerFunction"/> + </propertyPlaceholder> + + <route> + <from uri="direct:start"/> + <to uri="{{beer:FOO}}"/> + <to uri="{{beer:BAR}}"/> + </route> + </camelContext> + +</blueprint> +