Repository: camel
Updated Branches:
  refs/heads/master 5142e8ce0 -> 33b96b0b5


* component creates its own instance only if it's not provided
* ability to reference the instance by its name


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

Branch: refs/heads/master
Commit: c44f797bf92b06de0314bacf33c43289d8b64481
Parents: 5142e8c
Author: Ayache Khettar <aya...@zotix-consulting.co.uk>
Authored: Tue Mar 24 11:12:20 2015 +0000
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Wed Mar 25 15:03:00 2015 +0100

----------------------------------------------------------------------
 .../component/hazelcast/HazelcastComponent.java | 42 ++++++++++-----
 .../component/hazelcast/HazelcastConstants.java |  3 ++
 ...omponentInstanceReferenceNameSpringTest.java | 49 ++++++++++++++++++
 ...ontext-hazelcast-instance-name-reference.xml | 54 ++++++++++++++++++++
 4 files changed, 135 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c44f797b/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 053ae8a..eb752dd 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
@@ -36,6 +36,7 @@ import 
org.apache.camel.component.hazelcast.topic.HazelcastTopicEndpoint;
 import org.apache.camel.impl.UriEndpointComponent;
 
 import static org.apache.camel.util.ObjectHelper.removeStartingCharacters;
+import static org.apache.camel.component.hazelcast.HazelcastConstants.*;
 
 public class HazelcastComponent extends UriEndpointComponent {
 
@@ -54,15 +55,9 @@ public class HazelcastComponent extends UriEndpointComponent 
{
 
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, 
Map<String, Object> parameters) throws Exception {
-        
-        // Query param named 'hazelcastInstance' (if exists) overrides the 
instance that was set
-        // programmatically and cancels local instance creation as well.
-        HazelcastInstance hzInstance = 
resolveAndRemoveReferenceParameter(parameters, "hazelcastInstance",
-                HazelcastInstance.class);
-        // Now we use the hazelcastInstance from component
-        if (hzInstance == null) {
-            hzInstance = hazelcastInstance;
-        }
+
+        // use the given hazelcast Instance or create one if not given
+        HazelcastInstance hzInstance = getOrCreateHzInstance(parameters);
 
         int defaultOperation = -1;
         Object operation = getAndRemoveOrResolveReferenceParameter(parameters, 
HazelcastConstants.OPERATION_PARAM, Object.class);
@@ -150,10 +145,6 @@ public class HazelcastComponent extends 
UriEndpointComponent {
     @Override
     public void doStart() throws Exception {
         super.doStart();
-        if (hazelcastInstance == null) {
-            createOwnInstance = true;
-            hazelcastInstance = createOwnInstance();
-        }
     }
 
     @Override
@@ -178,4 +169,29 @@ public class HazelcastComponent extends 
UriEndpointComponent {
         config.getProperties().setProperty("hazelcast.version.check.enabled", 
"false");
         return Hazelcast.newHazelcastInstance(config);
     }
+
+    private HazelcastInstance getOrCreateHzInstance(Map<String, Object> 
parameters) {
+
+        // 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.
+        if (hzInstance == null)
+        {
+            hzInstance = Hazelcast.getHazelcastInstanceByName((String) 
parameters.get(HAZELCAST_INSTANCE_NAME_PARAM));
+            parameters.remove(HAZELCAST_INSTANCE_NAME_PARAM);
+        }
+
+        // Now create onw instance component
+        if (hzInstance == null) {
+
+            if (hazelcastInstance == null) {
+                createOwnInstance = true;
+                hazelcastInstance = createOwnInstance();
+            }
+            hzInstance = hazelcastInstance;
+        }
+        return hzInstance;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/c44f797b/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 e1a6304..a16013e 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
@@ -100,6 +100,9 @@ public final class HazelcastConstants {
 
     // parameter names
     public static final String OPERATION_PARAM = "operation";
+    public static final String HAZELCAST_INSTANCE_NAME_PARAM = 
"hazelcastInstanceName";
+    public static final String HAZELCAST_INSTANCE_PARAM = "hazelcastInstance";
+
 
     private HazelcastConstants() {
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/c44f797b/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastComponentInstanceReferenceNameSpringTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastComponentInstanceReferenceNameSpringTest.java
 
b/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastComponentInstanceReferenceNameSpringTest.java
new file mode 100644
index 0000000..3f0b753
--- /dev/null
+++ 
b/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastComponentInstanceReferenceNameSpringTest.java
@@ -0,0 +1,49 @@
+/**
+ * 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 org.junit.Test;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+
+public class HazelcastComponentInstanceReferenceNameSpringTest extends 
HazelcastCamelSpringTestSupport {
+
+    private static final String TEST_VALUE = "TestValue";
+    private static final String TEST_KEY = "TestKey";
+
+
+    @Test
+    public void testComparePutAndGet() {
+        template.sendBodyAndHeader("direct:testHazelcastInstanceBeanRefPut", 
TEST_VALUE,
+                HazelcastConstants.OBJECT_ID, TEST_KEY);
+
+        template.sendBodyAndHeader("direct:testHazelcastInstanceBeanRefGet", 
null,
+                HazelcastConstants.OBJECT_ID, TEST_KEY);
+        final Object testValueReturn = consumer.receiveBody("seda:out");
+
+        assertEquals(TEST_VALUE, testValueReturn);
+    }
+
+    @Override
+    protected AbstractApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext(
+                
"/META-INF/spring/test-camel-context-hazelcast-instance-name-reference.xml"
+        );
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/camel/blob/c44f797b/components/camel-hazelcast/src/test/resources/META-INF/spring/test-camel-context-hazelcast-instance-name-reference.xml
----------------------------------------------------------------------
diff --git 
a/components/camel-hazelcast/src/test/resources/META-INF/spring/test-camel-context-hazelcast-instance-name-reference.xml
 
b/components/camel-hazelcast/src/test/resources/META-INF/spring/test-camel-context-hazelcast-instance-name-reference.xml
new file mode 100644
index 0000000..ecef9a0
--- /dev/null
+++ 
b/components/camel-hazelcast/src/test/resources/META-INF/spring/test-camel-context-hazelcast-instance-name-reference.xml
@@ -0,0 +1,54 @@
+<?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"; 
xmlns:camel="http://camel.apache.org/schema/spring";
+       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="hazelcastLifecycle" class="com.hazelcast.core.LifecycleService"
+          factory-bean="hazelcastInstance" factory-method="getLifecycleService"
+          destroy-method="shutdown" />
+
+    <bean id="config" class="com.hazelcast.config.Config">
+        <constructor-arg type="java.lang.String" value="HZ.INSTANCE" />
+    </bean>
+
+    <bean id="hazelcastInstance" class="com.hazelcast.core.Hazelcast" 
factory-method="newHazelcastInstance">
+        <constructor-arg type="com.hazelcast.config.Config" ref="config"/>
+    </bean>
+    <camelContext xmlns="http://camel.apache.org/schema/spring";>
+        <route id="testHazelcastInstanceBeanRefPut">
+            <from uri="direct:testHazelcastInstanceBeanRefPut"/>
+            <setHeader headerName="CamelHazelcastOperationType">
+                <constant>put</constant>
+            </setHeader>
+            <to uri="hazelcast:map:testmap?hazelcastInstanceName=HZ.INSTANCE"/>
+        </route>
+
+        <route id="testHazelcastInstanceBeanRefGet">
+            <from uri="direct:testHazelcastInstanceBeanRefGet" />
+            <setHeader headerName="CamelHazelcastOperationType">
+                <constant>get</constant>
+            </setHeader>
+            <to uri="hazelcast:map:testmap?hazelcastInstanceName=HZ.INSTANCE"/>
+            <to uri="seda:out" />
+        </route>
+    </camelContext>
+
+</beans>

Reply via email to