camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lburgazz...@apache.org
Subject camel git commit: CAMEL-10563: camel-hazelcast: add an option to provide a custom configuration (custom Config object or configuration file location)
Date Tue, 13 Dec 2016 13:20:04 GMT
Repository: camel
Updated Branches:
  refs/heads/master 6d6a13b36 -> 97c021e47


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/97c021e4
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/97c021e4
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/97c021e4

Branch: refs/heads/master
Commit: 97c021e47cf777b676fc57e35beec9cc322532c4
Parents: 6d6a13b
Author: lburgazzoli <lburgazzoli@gmail.com>
Authored: Tue Dec 13 14:07:36 2016 +0100
Committer: lburgazzoli <lburgazzoli@gmail.com>
Committed: Tue Dec 13 14:07:54 2016 +0100

----------------------------------------------------------------------
 .../component/hazelcast/HazelcastComponent.java |  53 +++--
 .../hazelcast/HazelcastComponentHelper.java     | 118 +++++-----
 .../hazelcast/HazelcastDefaultProducer.java     |   5 +-
 .../hazelcast/HazelcastConfigurationTest.java   | 218 ++++++++++++++++++-
 .../src/test/resources/hazelcast-named.xml      |  45 ++++
 5 files changed, 351 insertions(+), 88 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/97c021e4/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 7b49ef4..ad901a9 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
@@ -17,7 +17,9 @@
 package org.apache.camel.component.hazelcast;
 
 import java.io.InputStream;
+import java.util.LinkedHashSet;
 import java.util.Map;
+import java.util.Set;
 
 import com.hazelcast.config.Config;
 import com.hazelcast.config.XmlConfigBuilder;
@@ -38,7 +40,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.ObjectHelper;
 import org.apache.camel.util.ResourceHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import static org.apache.camel.component.hazelcast.HazelcastConstants.HAZELCAST_CONFIGU_PARAM;
 import static org.apache.camel.component.hazelcast.HazelcastConstants.HAZELCAST_CONFIGU_URI_PARAM;
@@ -47,18 +52,19 @@ import static org.apache.camel.component.hazelcast.HazelcastConstants.HAZELCAST_
 import static org.apache.camel.util.ObjectHelper.removeStartingCharacters;
 
 public class HazelcastComponent extends UriEndpointComponent {
+    private static final Logger LOGGER = LoggerFactory.getLogger(HazelcastComponent.class);
 
-    private final HazelcastComponentHelper helper = new HazelcastComponentHelper();
-
+    private final Set<HazelcastInstance> customHazelcastInstances;
     private HazelcastInstance hazelcastInstance;
-    private transient boolean createOwnInstance;
 
     public HazelcastComponent() {
         super(HazelcastDefaultEndpoint.class);
+        this.customHazelcastInstances = new LinkedHashSet<>();
     }
 
     public HazelcastComponent(final CamelContext context) {
         super(context, HazelcastDefaultEndpoint.class);
+        this.customHazelcastInstances = new LinkedHashSet<>();
     }
 
     @Override
@@ -73,7 +79,7 @@ public class HazelcastComponent extends UriEndpointComponent {
             operation = getAndRemoveOrResolveReferenceParameter(parameters, "defaultOperation",
Object.class);
         }
         if (operation != null) {
-            defaultOperation = helper.extractOperationNumber(operation, -1);
+            defaultOperation = HazelcastComponentHelper.extractOperationNumber(operation,
-1);
         }
        
         HazelcastDefaultEndpoint endpoint = null;
@@ -182,9 +188,12 @@ public class HazelcastComponent extends UriEndpointComponent {
 
     @Override
     public void doStop() throws Exception {
-        if (createOwnInstance && hazelcastInstance != null) {
+        for (HazelcastInstance hazelcastInstance : customHazelcastInstances) {
             hazelcastInstance.getLifecycleService().shutdown();
         }
+
+        customHazelcastInstances.clear();
+
         super.doStop();
     }
 
@@ -226,23 +235,29 @@ public class HazelcastComponent extends UriEndpointComponent {
                     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");
-        }
+            if (hazelcastInstance == null && 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");
+
+                hzInstance = Hazelcast.newHazelcastInstance(config);
+            } else if (config != null) {
+                if (ObjectHelper.isNotEmpty(config.getInstanceName())) {
+                    hzInstance = Hazelcast.getOrCreateHazelcastInstance(config);
+                } else {
+                    hzInstance = Hazelcast.newHazelcastInstance(config);
+                }
+            }
 
-        // Now create onw instance component
-        if (hzInstance == null) {
-            if (hazelcastInstance == null) {
-                createOwnInstance = true;
-                hazelcastInstance = Hazelcast.newHazelcastInstance(config);
+            if (hzInstance != null) {
+                if (this.customHazelcastInstances.add(hzInstance)) {
+                    LOGGER.debug("Add managed HZ instance {}", hzInstance.getName());
+                }
             }
-            hzInstance = hazelcastInstance;
         }
-        return hzInstance;
+
+        return hzInstance == null ? hazelcastInstance : hzInstance;
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/97c021e4/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponentHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponentHelper.java
b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponentHelper.java
index ba7c6de..ace8f4f 100644
--- a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponentHelper.java
+++ b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastComponentHelper.java
@@ -24,10 +24,57 @@ import org.apache.camel.Exchange;
 
 public final class HazelcastComponentHelper {
 
-    private final HashMap<String, Integer> mapping = new HashMap<String, Integer>();
+    private static final HashMap<String, Integer> MAPPING;
+
+    static {
+        MAPPING = new HashMap<>();
+        addMapping(MAPPING, "put", HazelcastConstants.PUT_OPERATION);
+        addMapping(MAPPING, "delete", HazelcastConstants.DELETE_OPERATION);
+        addMapping(MAPPING, "get", HazelcastConstants.GET_OPERATION);
+        addMapping(MAPPING, "update", HazelcastConstants.UPDATE_OPERATION);
+        addMapping(MAPPING, "query", HazelcastConstants.QUERY_OPERATION);
+        addMapping(MAPPING, "getAll", HazelcastConstants.GET_ALL_OPERATION);
+        addMapping(MAPPING, "clear", HazelcastConstants.CLEAR_OPERATION);
+        addMapping(MAPPING, "evict", HazelcastConstants.EVICT_OPERATION);
+        addMapping(MAPPING, "evictAll", HazelcastConstants.EVICT_ALL_OPERATION);
+        addMapping(MAPPING, "putIfAbsent", HazelcastConstants.PUT_IF_ABSENT_OPERATION);
+        addMapping(MAPPING, "addAll", HazelcastConstants.ADD_ALL_OPERATION);
+        addMapping(MAPPING, "removeAll", HazelcastConstants.REMOVE_ALL_OPERATION);
+        addMapping(MAPPING, "retainAll", HazelcastConstants.RETAIN_ALL_OPERATION);
+        addMapping(MAPPING, "valueCount", HazelcastConstants.VALUE_COUNT_OPERATION);
+        addMapping(MAPPING, "containsKey", HazelcastConstants.CONTAINS_KEY_OPERATION);
+        addMapping(MAPPING, "containsValue", HazelcastConstants.CONTAINS_VALUE_OPERATION);
+        addMapping(MAPPING, "keySet", HazelcastConstants.GET_KEYS_OPERATION);
 
-    public HazelcastComponentHelper() {
-        this.init();
+        // multimap
+        addMapping(MAPPING, "removevalue", HazelcastConstants.REMOVEVALUE_OPERATION);
+
+        // atomic numbers
+        addMapping(MAPPING, "increment", HazelcastConstants.INCREMENT_OPERATION);
+        addMapping(MAPPING, "decrement", HazelcastConstants.DECREMENT_OPERATION);
+        addMapping(MAPPING, "setvalue", HazelcastConstants.SETVALUE_OPERATION);
+        addMapping(MAPPING, "destroy", HazelcastConstants.DESTROY_OPERATION);
+        addMapping(MAPPING, "compareAndSet", HazelcastConstants.COMPARE_AND_SET_OPERATION);
+        addMapping(MAPPING, "getAndAdd", HazelcastConstants.GET_AND_ADD_OPERATION);
+
+        // queue
+        addMapping(MAPPING, "add", HazelcastConstants.ADD_OPERATION);
+        addMapping(MAPPING, "offer", HazelcastConstants.OFFER_OPERATION);
+        addMapping(MAPPING, "peek", HazelcastConstants.PEEK_OPERATION);
+        addMapping(MAPPING, "poll", HazelcastConstants.POLL_OPERATION);
+        addMapping(MAPPING, "remainingCapacity", HazelcastConstants.REMAINING_CAPACITY_OPERATION);
+        addMapping(MAPPING, "drainTo", HazelcastConstants.DRAIN_TO_OPERATION);
+
+        // topic
+        addMapping(MAPPING, "publish", HazelcastConstants.PUBLISH_OPERATION);
+
+        // ringbuffer
+        addMapping(MAPPING, "capacity", HazelcastConstants.GET_CAPACITY_OPERATION);
+        addMapping(MAPPING, "readonceHead", HazelcastConstants.READ_ONCE_HEAD_OPERATION);
+        addMapping(MAPPING, "readonceTail", HazelcastConstants.READ_ONCE_TAIL_OPERATION);
+    }
+
+    private HazelcastComponentHelper() {
     }
 
     public static void copyHeaders(Exchange ex) {
@@ -60,11 +107,11 @@ public final class HazelcastComponentHelper {
         ex.getIn().setHeader(HazelcastConstants.LISTENER_TIME, new Date().getTime());
     }
 
-    public int lookupOperationNumber(Exchange exchange, int defaultOperation) {
+    public static int lookupOperationNumber(Exchange exchange, int defaultOperation) {
         return extractOperationNumber(exchange.getIn().getHeader(HazelcastConstants.OPERATION),
defaultOperation);
     }
 
-    public int extractOperationNumber(Object value, int defaultOperation) {
+    public static int extractOperationNumber(Object value, int defaultOperation) {
         int operation = defaultOperation;
         if (value instanceof String) {
             operation = mapToOperationNumber((String) value);
@@ -77,65 +124,16 @@ public final class HazelcastComponentHelper {
     /**
      * Allows the use of speaking operation names (e.g. for usage in Spring DSL)
      */
-    private int mapToOperationNumber(String operationName) {
-        if (this.mapping.containsKey(operationName)) {
-            return this.mapping.get(operationName);
+    private static int mapToOperationNumber(String operationName) {
+        if (MAPPING.containsKey(operationName)) {
+            return MAPPING.get(operationName);
         } else {
             throw new IllegalArgumentException(String.format("Operation '%s' is not supported
by this component.", operationName));
         }
     }
 
-    private void init() {
-        // fill map with values
-        addMapping("put", HazelcastConstants.PUT_OPERATION);
-        addMapping("delete", HazelcastConstants.DELETE_OPERATION);
-        addMapping("get", HazelcastConstants.GET_OPERATION);
-        addMapping("update", HazelcastConstants.UPDATE_OPERATION);
-        addMapping("query", HazelcastConstants.QUERY_OPERATION);
-        addMapping("getAll", HazelcastConstants.GET_ALL_OPERATION);
-        addMapping("clear", HazelcastConstants.CLEAR_OPERATION);
-        addMapping("evict", HazelcastConstants.EVICT_OPERATION);
-        addMapping("evictAll", HazelcastConstants.EVICT_ALL_OPERATION);
-        addMapping("putIfAbsent", HazelcastConstants.PUT_IF_ABSENT_OPERATION);
-        addMapping("addAll", HazelcastConstants.ADD_ALL_OPERATION);
-        addMapping("removeAll", HazelcastConstants.REMOVE_ALL_OPERATION);
-        addMapping("retainAll", HazelcastConstants.RETAIN_ALL_OPERATION);
-        addMapping("valueCount", HazelcastConstants.VALUE_COUNT_OPERATION);
-        addMapping("containsKey", HazelcastConstants.CONTAINS_KEY_OPERATION);
-        addMapping("containsValue", HazelcastConstants.CONTAINS_VALUE_OPERATION);
-        addMapping("keySet", HazelcastConstants.GET_KEYS_OPERATION);
-
-        // multimap
-        addMapping("removevalue", HazelcastConstants.REMOVEVALUE_OPERATION);
-
-        // atomic numbers
-        addMapping("increment", HazelcastConstants.INCREMENT_OPERATION);
-        addMapping("decrement", HazelcastConstants.DECREMENT_OPERATION);
-        addMapping("setvalue", HazelcastConstants.SETVALUE_OPERATION);
-        addMapping("destroy", HazelcastConstants.DESTROY_OPERATION);
-        addMapping("compareAndSet", HazelcastConstants.COMPARE_AND_SET_OPERATION);
-        addMapping("getAndAdd", HazelcastConstants.GET_AND_ADD_OPERATION);
-
-        // queue
-        addMapping("add", HazelcastConstants.ADD_OPERATION);
-        addMapping("offer", HazelcastConstants.OFFER_OPERATION);
-        addMapping("peek", HazelcastConstants.PEEK_OPERATION);
-        addMapping("poll", HazelcastConstants.POLL_OPERATION);
-        addMapping("remainingCapacity", HazelcastConstants.REMAINING_CAPACITY_OPERATION);
-        addMapping("drainTo", HazelcastConstants.DRAIN_TO_OPERATION);
-
-        // topic
-        addMapping("publish", HazelcastConstants.PUBLISH_OPERATION);
-        
-        // ringbuffer
-        addMapping("capacity", HazelcastConstants.GET_CAPACITY_OPERATION);
-        addMapping("readonceHead", HazelcastConstants.READ_ONCE_HEAD_OPERATION);
-        addMapping("readonceTail", HazelcastConstants.READ_ONCE_TAIL_OPERATION);
+    private static void addMapping(HashMap<String, Integer> mapping, String operationName,
int operationNumber) {
+        mapping.put(operationName, operationNumber);
+        mapping.put(String.valueOf(operationNumber), operationNumber);
     }
-
-    private void addMapping(String operationName, int operationNumber) {
-        this.mapping.put(operationName, operationNumber);
-        this.mapping.put(String.valueOf(operationNumber), operationNumber);
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/97c021e4/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastDefaultProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastDefaultProducer.java
b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastDefaultProducer.java
index 43e7836..5cb55dd 100644
--- a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastDefaultProducer.java
+++ b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastDefaultProducer.java
@@ -21,9 +21,6 @@ import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultProducer;
 
 public abstract class HazelcastDefaultProducer extends DefaultProducer {
-
-    private final HazelcastComponentHelper helper = new HazelcastComponentHelper();
-
     public HazelcastDefaultProducer(HazelcastDefaultEndpoint endpoint) {
         super(endpoint);
     }
@@ -34,6 +31,6 @@ public abstract class HazelcastDefaultProducer extends DefaultProducer {
     }
 
     protected int lookupOperationNumber(Exchange exchange) {
-        return helper.lookupOperationNumber(exchange, getEndpoint().getDefaultOperation());
+        return HazelcastComponentHelper.lookupOperationNumber(exchange, getEndpoint().getDefaultOperation());
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/97c021e4/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
index 2c9faba..181b670 100644
--- 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
@@ -17,9 +17,13 @@
 
 package org.apache.camel.component.hazelcast;
 
+import java.util.Arrays;
+import java.util.UUID;
+
 import com.hazelcast.config.Config;
 import com.hazelcast.core.Hazelcast;
 import com.hazelcast.core.HazelcastInstance;
+import org.apache.camel.CamelContext;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.SimpleRegistry;
 import org.junit.After;
@@ -33,17 +37,99 @@ public class HazelcastConfigurationTest {
     }
 
     @Test
-    public void testCustomConfigurationUri() throws Exception {
+    public void testNamedInstance() throws Exception {
         DefaultCamelContext context = null;
 
         try {
+            String instanceName = UUID.randomUUID().toString();
+            Config config = new Config();
+            config.setInstanceName(instanceName);
+            config.getNetworkConfig().setPort(6789);
+            config.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false);
+            config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(true);
+            config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false);
+
+            Hazelcast.newHazelcastInstance(config);
+
             context = new DefaultCamelContext();
             context.start();
-            context.getEndpoint("hazelcast:map:my-cache?hazelcastConfigUri=classpath:hazelcast-custom.xml");
+
+            HazelcastDefaultEndpoint endpoint1 = getHzEndpoint(context, "hazelcast:map:my-cache-1?hazelcastInstanceName="
+ instanceName);
+            HazelcastDefaultEndpoint endpoint2 = getHzEndpoint(context, "hazelcast:map:my-cache-2?hazelcastInstanceName="
+ instanceName);
+
+            Assert.assertNotNull(endpoint1.getHazelcastInstance());
+            Assert.assertNotNull(endpoint2.getHazelcastInstance());
+            Assert.assertTrue(endpoint1.getHazelcastInstance() == endpoint2.getHazelcastInstance());
 
             HazelcastComponent component = context.getComponent("hazelcast", HazelcastComponent.class);
-            HazelcastInstance hz = component.getHazelcastInstance();
+            Assert.assertNull(component.getHazelcastInstance());
+
+            for (HazelcastDefaultEndpoint endpoint : Arrays.asList(endpoint1, endpoint2))
{
+                HazelcastInstance hz = endpoint.getHazelcastInstance();
+                Assert.assertEquals(instanceName, hz.getName());
+                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();
+            }
+        }
+    }
+
+    @Test
+    public void testDefaultConfiguration() throws Exception {
+        DefaultCamelContext context = null;
+
+        try {
+            context = new DefaultCamelContext();
+            context.start();
 
+            HazelcastDefaultEndpoint endpoint1 = getHzEndpoint(context, "hazelcast:map:my-cache-1");
+            HazelcastDefaultEndpoint endpoint2 = getHzEndpoint(context, "hazelcast:map:my-cache-2");
+
+            Assert.assertNotNull(endpoint1.getHazelcastInstance());
+            Assert.assertNotNull(endpoint2.getHazelcastInstance());
+            Assert.assertTrue(endpoint1.getHazelcastInstance() != endpoint2.getHazelcastInstance());
+
+            HazelcastComponent component = context.getComponent("hazelcast", HazelcastComponent.class);
+            Assert.assertNull(component.getHazelcastInstance());
+
+            for (HazelcastDefaultEndpoint endpoint : Arrays.asList(endpoint1, endpoint2))
{
+                HazelcastInstance hz = endpoint.getHazelcastInstance();
+                Assert.assertFalse(hz.getConfig().getNetworkConfig().getJoin().getAwsConfig().isEnabled());
+                Assert.assertFalse(hz.getConfig().getNetworkConfig().getJoin().getMulticastConfig().isEnabled());
+                Assert.assertTrue(hz.getConfig().getNetworkConfig().getJoin().getTcpIpConfig().isEnabled());
+                Assert.assertEquals(5701, hz.getConfig().getNetworkConfig().getPort());
+            }
+        } finally {
+            if (context != null) {
+                context.stop();
+            }
+        }
+    }
+
+    @Test
+    public void testNamedInstanceWithConfigurationUri() throws Exception {
+        DefaultCamelContext context = null;
+
+        try {
+            context = new DefaultCamelContext();
+            context.start();
+
+            HazelcastDefaultEndpoint endpoint1 = getHzEndpoint(context, "hazelcast:map:my-cache-1?hazelcastConfigUri=classpath:hazelcast-named.xml");
+            HazelcastDefaultEndpoint endpoint2 = getHzEndpoint(context, "hazelcast:map:my-cache-2?hazelcastConfigUri=classpath:hazelcast-named.xml");
+
+            Assert.assertNotNull(endpoint1.getHazelcastInstance());
+            Assert.assertNotNull(endpoint2.getHazelcastInstance());
+            Assert.assertTrue(endpoint1.getHazelcastInstance() == endpoint2.getHazelcastInstance());
+
+            HazelcastComponent component = context.getComponent("hazelcast", HazelcastComponent.class);
+            Assert.assertNull(component.getHazelcastInstance());
+
+            HazelcastInstance hz = endpoint1.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());
@@ -57,7 +143,40 @@ public class HazelcastConfigurationTest {
     }
 
     @Test
-    public void testCustomConfiguration() throws Exception {
+    public void testCustomConfigurationUri() throws Exception {
+        DefaultCamelContext context = null;
+
+        try {
+            context = new DefaultCamelContext();
+            context.start();
+
+            HazelcastDefaultEndpoint endpoint1 = getHzEndpoint(context, "hazelcast:map:my-cache-1?hazelcastConfigUri=classpath:hazelcast-custom.xml");
+            HazelcastDefaultEndpoint endpoint2 = getHzEndpoint(context, "hazelcast:map:my-cache-2?hazelcastConfigUri=classpath:hazelcast-custom.xml");
+
+            Assert.assertNotNull(endpoint1.getHazelcastInstance());
+            Assert.assertNotNull(endpoint2.getHazelcastInstance());
+            Assert.assertTrue(endpoint1.getHazelcastInstance() != endpoint2.getHazelcastInstance());
+
+            HazelcastComponent component = context.getComponent("hazelcast", HazelcastComponent.class);
+            Assert.assertNull(component.getHazelcastInstance());
+
+            for (HazelcastDefaultEndpoint endpoint : Arrays.asList(endpoint1, endpoint2))
{
+                HazelcastInstance hz = endpoint.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 testCustomConfigurationReference() throws Exception {
         DefaultCamelContext context = null;
 
         try {
@@ -74,9 +193,13 @@ public class HazelcastConfigurationTest {
             context.start();
             context.getEndpoint("hazelcast:map:my-cache?hazelcastConfig=#my-config");
 
+            HazelcastDefaultEndpoint endpoint = getHzEndpoint(context, "hazelcast:map:my-cache?hazelcastConfig=#my-config");
+            Assert.assertNotNull(endpoint.getHazelcastInstance());
+
             HazelcastComponent component = context.getComponent("hazelcast", HazelcastComponent.class);
-            HazelcastInstance hz = component.getHazelcastInstance();
+            Assert.assertNull(component.getHazelcastInstance());
 
+            HazelcastInstance hz = endpoint.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());
@@ -88,4 +211,89 @@ public class HazelcastConfigurationTest {
             }
         }
     }
+
+    @Test
+    public void testMix() throws Exception {
+        DefaultCamelContext context = null;
+
+        try {
+            String instanceName = UUID.randomUUID().toString();
+
+            Config namedConfig = new Config();
+            namedConfig.setInstanceName("named-" + instanceName);
+            namedConfig.getNetworkConfig().setPort(9001);
+            namedConfig.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false);
+            namedConfig.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(true);
+            namedConfig.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false);
+
+            Config customConfig = new Config();
+            customConfig.setInstanceName("custom-" + instanceName);
+            customConfig.getNetworkConfig().setPort(9002);
+            customConfig.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false);
+            customConfig.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(true);
+            customConfig.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false);
+
+            Config sharedConfig = new Config();
+            sharedConfig.setInstanceName("custom-" + instanceName);
+            sharedConfig.getNetworkConfig().setPort(9003);
+            sharedConfig.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false);
+            sharedConfig.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(true);
+            sharedConfig.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false);
+
+            Config componentConfig = new Config();
+            sharedConfig.setInstanceName("component-" + instanceName);
+            sharedConfig.getNetworkConfig().setPort(9004);
+            sharedConfig.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false);
+            sharedConfig.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(true);
+            sharedConfig.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false);
+
+            HazelcastInstance hzNamed = Hazelcast.newHazelcastInstance(namedConfig);
+            HazelcastInstance hzShared = Hazelcast.newHazelcastInstance(sharedConfig);
+            HazelcastInstance hzComponent = Hazelcast.newHazelcastInstance(componentConfig);
+
+            SimpleRegistry reg = new SimpleRegistry();
+            reg.put(customConfig.getInstanceName(), customConfig);
+            reg.put(sharedConfig.getInstanceName(), hzShared);
+
+            HazelcastComponent component = new HazelcastComponent();
+            component.setHazelcastInstance(hzComponent);
+
+            context = new DefaultCamelContext(reg);
+            context.addComponent("hazelcast", component);
+            context.start();
+
+            HazelcastDefaultEndpoint endpoint1 = getHzEndpoint(context, "hazelcast:map:my-cache-1?hazelcastInstanceName="
+ namedConfig.getInstanceName());
+            HazelcastDefaultEndpoint endpoint2 = getHzEndpoint(context, "hazelcast:map:my-cache-2?hazelcastConfig=#"
+ customConfig.getInstanceName());
+            HazelcastDefaultEndpoint endpoint3 = getHzEndpoint(context, "hazelcast:map:my-cache-2?hazelcastInstance=#"
+ sharedConfig.getInstanceName());
+            HazelcastDefaultEndpoint endpoint4 = getHzEndpoint(context, "hazelcast:map:my-cache-4");
+
+            Assert.assertNotNull(endpoint1.getHazelcastInstance());
+            Assert.assertNotNull(endpoint2.getHazelcastInstance());
+            Assert.assertNotNull(endpoint3.getHazelcastInstance());
+            Assert.assertNotNull(endpoint4.getHazelcastInstance());
+
+            Assert.assertEquals(4, Hazelcast.getAllHazelcastInstances().size());
+
+            Assert.assertTrue(hzNamed == endpoint1.getHazelcastInstance());
+            Assert.assertTrue(Hazelcast.getHazelcastInstanceByName(customConfig.getInstanceName())
== endpoint2.getHazelcastInstance());
+            Assert.assertTrue(hzShared == endpoint3.getHazelcastInstance());
+            Assert.assertTrue(hzComponent == endpoint4.getHazelcastInstance());
+
+            Assert.assertNotNull(component.getHazelcastInstance());
+            Assert.assertTrue(hzComponent == component.getHazelcastInstance());
+
+            context.stop();
+
+            Assert.assertEquals(3, Hazelcast.getAllHazelcastInstances().size());
+
+        } finally {
+            if (context != null && context.isStarted()) {
+                context.stop();
+            }
+        }
+    }
+
+    private HazelcastDefaultEndpoint getHzEndpoint(CamelContext context, String uri) {
+        return (HazelcastDefaultEndpoint)context.getEndpoint(uri);
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/97c021e4/components/camel-hazelcast/src/test/resources/hazelcast-named.xml
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/test/resources/hazelcast-named.xml b/components/camel-hazelcast/src/test/resources/hazelcast-named.xml
new file mode 100644
index 0000000..3181744
--- /dev/null
+++ b/components/camel-hazelcast/src/test/resources/hazelcast-named.xml
@@ -0,0 +1,45 @@
+<?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">
+
+	<instance-name>camel-hz</instance-name>
+
+	<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>


Mime
View raw message