You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2016/12/13 10:59:02 UTC

camel git commit: CAMEL-10563: camel-hazelcast: add an option to provide a custom configuration (custom Config object or configuration file location)

Repository: camel
Updated Branches:
  refs/heads/master b18dab5a1 -> 4be2c5127


CAMEL-10563: camel-hazelcast: add an option to provide a custom configuration (custom Config object or configuration file location)


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4be2c512
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4be2c512
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4be2c512

Branch: refs/heads/master
Commit: 4be2c5127c4c26667ceabaddaa5484163e19d618
Parents: b18dab5
Author: lburgazzoli <lb...@gmail.com>
Authored: Tue Dec 13 11:51:18 2016 +0100
Committer: lburgazzoli <lb...@gmail.com>
Committed: Tue Dec 13 11:51:42 2016 +0100

----------------------------------------------------------------------
 .../component/hazelcast/HazelcastComponent.java | 48 ++++++++---
 .../component/hazelcast/HazelcastConstants.java |  2 +
 .../hazelcast/HazelcastConfigurationTest.java   | 91 ++++++++++++++++++++
 .../src/test/resources/hazelcast-custom.xml     | 43 +++++++++
 4 files changed, 171 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/4be2c512/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponent.java b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponent.java
index 3b56368..7b49ef4 100644
--- a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponent.java
+++ b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponent.java
@@ -16,13 +16,13 @@
  */
 package org.apache.camel.component.hazelcast;
 
+import java.io.InputStream;
 import java.util.Map;
 
 import com.hazelcast.config.Config;
 import com.hazelcast.config.XmlConfigBuilder;
 import com.hazelcast.core.Hazelcast;
 import com.hazelcast.core.HazelcastInstance;
-
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.component.hazelcast.atomicnumber.HazelcastAtomicnumberEndpoint;
@@ -38,7 +38,10 @@ import org.apache.camel.component.hazelcast.seda.HazelcastSedaEndpoint;
 import org.apache.camel.component.hazelcast.set.HazelcastSetEndpoint;
 import org.apache.camel.component.hazelcast.topic.HazelcastTopicEndpoint;
 import org.apache.camel.impl.UriEndpointComponent;
+import org.apache.camel.util.ResourceHelper;
 
+import static org.apache.camel.component.hazelcast.HazelcastConstants.HAZELCAST_CONFIGU_PARAM;
+import static org.apache.camel.component.hazelcast.HazelcastConstants.HAZELCAST_CONFIGU_URI_PARAM;
 import static org.apache.camel.component.hazelcast.HazelcastConstants.HAZELCAST_INSTANCE_NAME_PARAM;
 import static org.apache.camel.component.hazelcast.HazelcastConstants.HAZELCAST_INSTANCE_PARAM;
 import static org.apache.camel.util.ObjectHelper.removeStartingCharacters;
@@ -62,7 +65,7 @@ public class HazelcastComponent extends UriEndpointComponent {
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
 
         // use the given hazelcast Instance or create one if not given
-        HazelcastInstance hzInstance = getOrCreateHzInstance(parameters);
+        HazelcastInstance hzInstance = getOrCreateHzInstance(getCamelContext(), parameters);
 
         int defaultOperation = -1;
         Object operation = getAndRemoveOrResolveReferenceParameter(parameters, HazelcastConstants.OPERATION_PARAM, Object.class);
@@ -197,27 +200,46 @@ public class HazelcastComponent extends UriEndpointComponent {
         this.hazelcastInstance = hazelcastInstance;
     }
 
-    private HazelcastInstance createOwnInstance() {
-        Config config = new XmlConfigBuilder().build();
-        // Disable the version check
-        config.getProperties().setProperty("hazelcast.version.check.enabled", "false");
-        return Hazelcast.newHazelcastInstance(config);
-    }
-
-    private HazelcastInstance getOrCreateHzInstance(Map<String, Object> parameters) {
+    private HazelcastInstance getOrCreateHzInstance(CamelContext context, Map<String, Object> parameters) throws Exception {
+        HazelcastInstance hzInstance = null;
+        Config config = null;
 
         // Query param named 'hazelcastInstance' (if exists) overrides the instance that was set
-        HazelcastInstance hzInstance = resolveAndRemoveReferenceParameter(parameters, HAZELCAST_INSTANCE_PARAM, HazelcastInstance.class);
-        // check if an already created instance is given then just get instance by its name.
+        hzInstance = resolveAndRemoveReferenceParameter(parameters, HAZELCAST_INSTANCE_PARAM, HazelcastInstance.class);
+
+        // Check if an already created instance is given then just get instance by its name.
         if (hzInstance == null && parameters.get(HAZELCAST_INSTANCE_NAME_PARAM) != null) {
             hzInstance = Hazelcast.getHazelcastInstanceByName((String) parameters.get(HAZELCAST_INSTANCE_NAME_PARAM));
         }
 
+        // If instance neither supplied nor found by name, try to lookup its config
+        // as reference or as xml configuration file.
+        if (hzInstance == null) {
+            config = resolveAndRemoveReferenceParameter(parameters, HAZELCAST_CONFIGU_PARAM, Config.class);
+            if (config == null) {
+                String configUri = getAndRemoveParameter(parameters, HAZELCAST_CONFIGU_URI_PARAM, String.class);
+                if (configUri != null) {
+                    configUri = getCamelContext().resolvePropertyPlaceholders(configUri);
+                }
+                if (configUri != null) {
+                    InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context, configUri);
+                    config = new XmlConfigBuilder(is).build();
+                }
+            }
+        }
+
+        if (config == null) {
+            config = new XmlConfigBuilder().build();
+            // Disable the version check
+            config.getProperties().setProperty("hazelcast.version.check.enabled", "false");
+            config.getProperties().setProperty("hazelcast.phone.home.enabled", "false");
+        }
+
         // Now create onw instance component
         if (hzInstance == null) {
             if (hazelcastInstance == null) {
                 createOwnInstance = true;
-                hazelcastInstance = createOwnInstance();
+                hazelcastInstance = Hazelcast.newHazelcastInstance(config);
             }
             hzInstance = hazelcastInstance;
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/4be2c512/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
index f2ef662..dffe7eb 100644
--- a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
+++ b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
@@ -131,6 +131,8 @@ public final class HazelcastConstants {
     public static final String OPERATION_PARAM = "operation";
     public static final String HAZELCAST_INSTANCE_NAME_PARAM = "hazelcastInstanceName";
     public static final String HAZELCAST_INSTANCE_PARAM = "hazelcastInstance";
+    public static final String HAZELCAST_CONFIGU_PARAM = "hazelcastConfig";
+    public static final String HAZELCAST_CONFIGU_URI_PARAM = "hazelcastConfigUri";
 
 
     private HazelcastConstants() {

http://git-wip-us.apache.org/repos/asf/camel/blob/4be2c512/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastConfigurationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastConfigurationTest.java b/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastConfigurationTest.java
new file mode 100644
index 0000000..2c9faba
--- /dev/null
+++ b/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastConfigurationTest.java
@@ -0,0 +1,91 @@
+/**
+ * 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.hazelcast;
+
+import com.hazelcast.config.Config;
+import com.hazelcast.core.Hazelcast;
+import com.hazelcast.core.HazelcastInstance;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.SimpleRegistry;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class HazelcastConfigurationTest {
+    @After
+    public void tearDown() throws Exception {
+        Hazelcast.shutdownAll();
+    }
+
+    @Test
+    public void testCustomConfigurationUri() throws Exception {
+        DefaultCamelContext context = null;
+
+        try {
+            context = new DefaultCamelContext();
+            context.start();
+            context.getEndpoint("hazelcast:map:my-cache?hazelcastConfigUri=classpath:hazelcast-custom.xml");
+
+            HazelcastComponent component = context.getComponent("hazelcast", HazelcastComponent.class);
+            HazelcastInstance hz = component.getHazelcastInstance();
+
+            Assert.assertFalse(hz.getConfig().getNetworkConfig().getJoin().getAwsConfig().isEnabled());
+            Assert.assertFalse(hz.getConfig().getNetworkConfig().getJoin().getMulticastConfig().isEnabled());
+            Assert.assertFalse(hz.getConfig().getNetworkConfig().getJoin().getTcpIpConfig().isEnabled());
+            Assert.assertEquals(9876, hz.getConfig().getNetworkConfig().getPort());
+
+        } finally {
+            if (context != null) {
+                context.stop();
+            }
+        }
+    }
+
+    @Test
+    public void testCustomConfiguration() throws Exception {
+        DefaultCamelContext context = null;
+
+        try {
+            Config config = new Config();
+            config.getNetworkConfig().setPort(6789);
+            config.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false);
+            config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(true);
+            config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false);
+
+            SimpleRegistry reg = new SimpleRegistry();
+            reg.put("my-config", config);
+
+            context = new DefaultCamelContext(reg);
+            context.start();
+            context.getEndpoint("hazelcast:map:my-cache?hazelcastConfig=#my-config");
+
+            HazelcastComponent component = context.getComponent("hazelcast", HazelcastComponent.class);
+            HazelcastInstance hz = component.getHazelcastInstance();
+
+            Assert.assertFalse(hz.getConfig().getNetworkConfig().getJoin().getAwsConfig().isEnabled());
+            Assert.assertTrue(hz.getConfig().getNetworkConfig().getJoin().getMulticastConfig().isEnabled());
+            Assert.assertFalse(hz.getConfig().getNetworkConfig().getJoin().getTcpIpConfig().isEnabled());
+            Assert.assertEquals(6789, hz.getConfig().getNetworkConfig().getPort());
+
+        } finally {
+            if (context != null) {
+                context.stop();
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/4be2c512/components/camel-hazelcast/src/test/resources/hazelcast-custom.xml
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/test/resources/hazelcast-custom.xml b/components/camel-hazelcast/src/test/resources/hazelcast-custom.xml
new file mode 100644
index 0000000..51c4333
--- /dev/null
+++ b/components/camel-hazelcast/src/test/resources/hazelcast-custom.xml
@@ -0,0 +1,43 @@
+<?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.
+-->
+<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.7.xsd"
+					 xmlns="http://www.hazelcast.com/schema/config"
+					 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+
+	<group>
+		<name>dev</name>
+		<password>dev-pass</password>
+	</group>
+	
+	<!-- Disable the version check -->
+	<properties>
+	  <property name="hazelcast.phone.home.enabled">false</property>
+		<property name="hazelcast.logging.type">slf4j</property>
+	</properties>
+
+	<network>
+		<port auto-increment="true">9876</port>
+		<join>
+			<multicast enabled="false"/>
+			<tcp-ip enabled="false"/>
+			<aws enabled="false"/>
+		</join>
+		<interfaces enabled="false"/>
+	</network>
+
+</hazelcast>