You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2014/12/03 11:37:07 UTC
[2/2] camel git commit: CAMEL-8115: Properties component - Include
default functions to lookup from ENV / SYS etc
CAMEL-8115: Properties component - Include default functions to lookup from ENV / SYS etc
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c17ba8b4
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c17ba8b4
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c17ba8b4
Branch: refs/heads/camel-2.14.x
Commit: c17ba8b4375a74ff932bd222a76e03fabc022792
Parents: 48a75fa
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Dec 3 10:01:23 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Dec 3 11:36:54 2014 +0100
----------------------------------------------------------------------
camel-core/pom.xml | 5 ++
.../properties/EnvPropertiesFunction.java | 46 ++++++++++++
.../properties/PropertiesComponent.java | 5 ++
.../properties/ServicePropertiesFunction.java | 68 +++++++++++++++++
.../properties/SysPropertiesFunction.java | 46 ++++++++++++
...PropertiesComponentDefaultFunctionsTest.java | 78 ++++++++++++++++++++
.../PropertiesComponentServiceTest.java | 70 ++++++++++++++++++
7 files changed, 318 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/c17ba8b4/camel-core/pom.xml
----------------------------------------------------------------------
diff --git a/camel-core/pom.xml b/camel-core/pom.xml
index e20755b..4a52203 100644
--- a/camel-core/pom.xml
+++ b/camel-core/pom.xml
@@ -264,6 +264,11 @@
<exclude>${platform.skip.tests}</exclude>
</excludes>
<forkedProcessTimeoutInSeconds>3000</forkedProcessTimeoutInSeconds>
+ <!-- needed for testing the properties component -->
+ <environmentVariables>
+ <FOO_SERVICE_HOST>myserver</FOO_SERVICE_HOST>
+ <FOO_SERVICE_PORT>8081</FOO_SERVICE_PORT>
+ </environmentVariables>
</configuration>
</plugin>
http://git-wip-us.apache.org/repos/asf/camel/blob/c17ba8b4/camel-core/src/main/java/org/apache/camel/component/properties/EnvPropertiesFunction.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/properties/EnvPropertiesFunction.java b/camel-core/src/main/java/org/apache/camel/component/properties/EnvPropertiesFunction.java
new file mode 100644
index 0000000..5111ceb
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/component/properties/EnvPropertiesFunction.java
@@ -0,0 +1,46 @@
+/**
+ * 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.util.ObjectHelper;
+
+/**
+ * A {@link org.apache.camel.component.properties.PropertiesFunction} that lookup the property value from
+ * OS environment variables.
+ */
+public class EnvPropertiesFunction implements PropertiesFunction {
+
+ @Override
+ public String getName() {
+ return "env";
+ }
+
+ @Override
+ public String apply(String remainder) {
+ String key = remainder;
+ String defaultValue = null;
+
+ if (remainder.contains(":")) {
+ key = ObjectHelper.before(remainder, ":");
+ defaultValue = ObjectHelper.after(remainder, ":");
+ }
+
+ String value = System.getenv(key);
+ return value != null ? value : defaultValue;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/c17ba8b4/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 247c357..0c2f05f 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
@@ -346,6 +346,11 @@ public class PropertiesComponent extends DefaultComponent {
if (propertiesParser instanceof DefaultPropertiesParser) {
((DefaultPropertiesParser) propertiesParser).setPropertiesComponent(this);
}
+
+ // include out of the box functions
+ addFunction(new EnvPropertiesFunction());
+ addFunction(new SysPropertiesFunction());
+ addFunction(new ServicePropertiesFunction());
}
@Override
http://git-wip-us.apache.org/repos/asf/camel/blob/c17ba8b4/camel-core/src/main/java/org/apache/camel/component/properties/ServicePropertiesFunction.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/properties/ServicePropertiesFunction.java b/camel-core/src/main/java/org/apache/camel/component/properties/ServicePropertiesFunction.java
new file mode 100644
index 0000000..5acf7ea
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/component/properties/ServicePropertiesFunction.java
@@ -0,0 +1,68 @@
+/**
+ * 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.Locale;
+
+import org.apache.camel.util.ObjectHelper;
+
+/**
+ * A {@link org.apache.camel.component.properties.PropertiesFunction} that lookup the property value from
+ * OS environment variables using the service idiom.
+ * <p/>
+ * A service is defined using two environment variables where name is name of the service:
+ * <ul>
+ * <li><tt>NAME_SERVICE_HOST</tt></li>
+ * <li><tt>NAME_SERVICE_PORT</tt></li>
+ * </ul>
+ * in other words the service uses <tt>_SERVICE_HOST</tt> and <tt>_SERVICE_PORT</tt> as prefix.
+ */
+public class ServicePropertiesFunction implements PropertiesFunction {
+
+ private static final String HOST_PREFIX = "_SERVICE_HOST";
+ private static final String PORT_PREFIX = "_SERVICE_PORT";
+
+ @Override
+ public String getName() {
+ return "service";
+ }
+
+ @Override
+ public String apply(String remainder) {
+ String key = remainder;
+ String defaultValue = null;
+
+ if (remainder.contains(":")) {
+ key = ObjectHelper.before(remainder, ":");
+ defaultValue = ObjectHelper.after(remainder, ":");
+ }
+
+ // make sure to use upper case
+ key = key.toUpperCase(Locale.US);
+
+ // a service should have both the host and port defined
+ String host = System.getenv(key + HOST_PREFIX);
+ String port = System.getenv(key + PORT_PREFIX);
+
+ if (host != null && port != null) {
+ return host + ":" + port;
+ } else {
+ return defaultValue;
+ }
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/camel/blob/c17ba8b4/camel-core/src/main/java/org/apache/camel/component/properties/SysPropertiesFunction.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/properties/SysPropertiesFunction.java b/camel-core/src/main/java/org/apache/camel/component/properties/SysPropertiesFunction.java
new file mode 100644
index 0000000..7a91890
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/component/properties/SysPropertiesFunction.java
@@ -0,0 +1,46 @@
+/**
+ * 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.util.ObjectHelper;
+
+/**
+ * A {@link PropertiesFunction} that lookup the property value from
+ * JVM system property.
+ */
+public class SysPropertiesFunction implements PropertiesFunction {
+
+ @Override
+ public String getName() {
+ return "sys";
+ }
+
+ @Override
+ public String apply(String remainder) {
+ String key = remainder;
+ String defaultValue = null;
+
+ if (remainder.contains(":")) {
+ key = ObjectHelper.before(remainder, ":");
+ defaultValue = ObjectHelper.after(remainder, ":");
+ }
+
+ String value = System.getProperty(key);
+ return value != null ? value : defaultValue;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/c17ba8b4/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultFunctionsTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultFunctionsTest.java b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultFunctionsTest.java
new file mode 100644
index 0000000..6328c0e
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultFunctionsTest.java
@@ -0,0 +1,78 @@
+/**
+ * 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.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+public class PropertiesComponentDefaultFunctionsTest extends ContextTestSupport {
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ public void testFunction() throws Exception {
+ System.setProperty("FOO", "mock:foo");
+
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .to("{{sys:FOO}}")
+ .transform().constant("{{env:JAVA_HOME}}")
+ .to("mock:bar");
+ }
+ });
+ context.start();
+
+ String body = System.getenv("JAVA_HOME");
+
+ getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
+ getMockEndpoint("mock:bar").expectedBodiesReceived(body);
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+
+ System.getProperties().remove("FOO");
+ }
+
+ public void testFunctionGetOrElse() throws Exception {
+ System.setProperty("FOO2", "mock:foo");
+
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .to("{{sys:FOO2}}")
+ .to("{{env:BAR2:mock:bar}}");
+ }
+ });
+ context.start();
+
+ getMockEndpoint("mock:foo").expectedMessageCount(1);
+ getMockEndpoint("mock:bar").expectedMessageCount(1);
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+
+ System.getProperties().remove("FOO2");
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/c17ba8b4/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentServiceTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentServiceTest.java b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentServiceTest.java
new file mode 100644
index 0000000..165b27f
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentServiceTest.java
@@ -0,0 +1,70 @@
+/**
+ * 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.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+public class PropertiesComponentServiceTest extends ContextTestSupport {
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ public void testFunction() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .to("mock:foo")
+ .transform().constant("{{service:FOO}}")
+ .to("mock:bar");
+ }
+ });
+ context.start();
+
+ String body = System.getenv("FOO_SERVICE_HOST") + ":" + System.getenv("FOO_SERVICE_PORT");
+ getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
+ getMockEndpoint("mock:bar").expectedBodiesReceived(body);
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testFunctionGetOrElse() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .to("mock:foo")
+ .transform().constant("{{service:BAR:myotherserver:8888}}")
+ .to("mock:bar");
+ }
+ });
+ context.start();
+
+ getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
+ getMockEndpoint("mock:bar").expectedBodiesReceived("myotherserver:8888");
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+}