Author: iocanel
Date: Fri Oct 26 15:31:08 2012
New Revision: 1402549

URL: http://svn.apache.org/viewvc?rev=1402549&view=rev
Log:
[CAMEL-5701] Upgraded to jclouds 1.5.2 and added support for context name.

Added:
    
camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsMultipleBlobStoreTest.java
Modified:
    
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComponent.java
    camel/trunk/parent/pom.xml

Modified: 
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComponent.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComponent.java?rev=1402549&r1=1402548&r2=1402549&view=diff
==============================================================================
--- 
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComponent.java
 (original)
+++ 
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComponent.java
 Fri Oct 26 15:31:08 2012
@@ -20,6 +20,7 @@ import java.util.List;
 import java.util.Map;
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultComponent;
+import org.jclouds.Context;
 import org.jclouds.blobstore.BlobStore;
 import org.jclouds.compute.ComputeService;
 
@@ -68,42 +69,76 @@ public class JcloudsComponent extends De
 
     /**
      * Returns the {@link BlobStore} that matches the given providerOrApi.
-     * @param providerOrApi The providerOrApi id.
+     * @param predicate The blobstore context name, provider or api.
      * @return The matching {@link BlobStore}
      */
-    protected BlobStore getBlobStore(String providerOrApi) throws Exception {
-
+    protected BlobStore getBlobStore(String predicate) throws Exception {
         if (blobStores != null && !blobStores.isEmpty()) {
+
+            //First try using name and then fallback to the provider or api.
+            if (isNameSupportedByContext()) {
+                for (BlobStore blobStore : blobStores) {
+                    if 
(blobStore.getContext().unwrap().getName().equals(predicate)) {
+                        return blobStore;
+                    }
+                }
+            }
+
             for (BlobStore blobStore : blobStores) {
-                if 
(blobStore.getContext().unwrap().getId().equals(providerOrApi)) {
+                if (blobStore.getContext().unwrap().getId().equals(predicate)) 
{
                     return blobStore;
                 }
             }
-            throw new Exception(String.format("No blobstore found for 
provider:%s", providerOrApi));
+            throw new Exception(String.format("No blobstore found for:%s", 
predicate));
         } else {
             throw new Exception("No blobstore available.");
         }
     }
 
     /**
-     * Returns the {@link ComputeService} that matches the given providerOrApi.
-     * @param providerOrApi The providerOrApi id.
+     * Returns the {@link ComputeService} that matches the given predicate.
+     * @param predicate The compute context name, provider or api.
      * @return The matching {@link ComputeService}
      */
-    protected ComputeService getComputeService(String providerOrApi) throws 
Exception {
+    protected ComputeService getComputeService(String predicate) throws 
Exception {
 
         if (computeServices != null && !computeServices.isEmpty()) {
+            //First try using name and then fallback to the provider or api.
+            if (isNameSupportedByContext()) {
+                for (ComputeService computeService : computeServices) {
+                    if 
(computeService.getContext().unwrap().getName().equals(predicate)) {
+                        return computeService;
+                    }
+                }
+            }
+
             for (ComputeService computeService : computeServices) {
-                if 
(computeService.getContext().unwrap().getId().equals(providerOrApi)) {
+                if 
(computeService.getContext().unwrap().getId().equals(predicate)) {
                     return computeService;
                 }
             }
-            throw new Exception(String.format("No compute service found for 
provider:%s", providerOrApi));
+            throw new Exception(String.format("No compute service found for 
:%s", predicate));
         } else {
             throw new Exception("No compute service available.");
         }
     }
 
+    /**
+     * Checks if jclouds {@link Context} supports the name.
+     * We need this method as getName is not supported in earlier micro 
version of 1.5.x.
+     * So we use this check to fallback to traditional means of looking up 
contexts and services, if name is not present.
+     *
+     * @return
+     */
+    private boolean isNameSupportedByContext() {
+        try {
+            Context.class.getMethod("getName", null);
+            return true;
+        } catch (NoSuchMethodException ex) {
+            return false;
+        }
+    }
+
     public List<BlobStore> getBlobStores() {
         return blobStores;
     }

Added: 
camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsMultipleBlobStoreTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsMultipleBlobStoreTest.java?rev=1402549&view=auto
==============================================================================
--- 
camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsMultipleBlobStoreTest.java
 (added)
+++ 
camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsMultipleBlobStoreTest.java
 Fri Oct 26 15:31:08 2012
@@ -0,0 +1,84 @@
+/**
+ * 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.jclouds;
+
+import com.google.common.collect.Lists;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.jclouds.ContextBuilder;
+import org.jclouds.blobstore.BlobStore;
+import org.jclouds.blobstore.BlobStoreContext;
+import org.jclouds.io.payloads.StringPayload;
+import org.junit.Test;
+
+public class JcloudsMultipleBlobStoreTest extends CamelTestSupport {
+
+    private static final String TEST_CONTAINER = "testContainer";
+    private static final String TEST_BLOB1 = "testBlob1";
+    private static final String TEST_BLOB2 = "testBlob2";
+
+    BlobStoreContext blobStoreContext1 = 
ContextBuilder.newBuilder("transient").name("b1").credentials("identity", 
"credential").build(BlobStoreContext.class);
+    BlobStore blobStore1 = blobStoreContext1.getBlobStore();
+
+    BlobStoreContext blobStoreContext2 = 
ContextBuilder.newBuilder("transient").name("b2").credentials("identity", 
"credential").build(BlobStoreContext.class);
+    BlobStore blobStore2 = blobStoreContext2.getBlobStore();
+
+
+    @Test
+    public void testWithMultipleServices() throws InterruptedException {
+        String message1 = "Blob 1";
+        JcloudsBlobStoreHelper.writeBlob(blobStore1, TEST_CONTAINER, 
TEST_BLOB1, new StringPayload(message1));
+
+        String message2 = "Blob 2";
+        JcloudsBlobStoreHelper.writeBlob(blobStore2, TEST_CONTAINER, 
TEST_BLOB2, new StringPayload(message2));
+
+        MockEndpoint mockEndpoint1 = resolveMandatoryEndpoint("mock:results1", 
MockEndpoint.class);
+        mockEndpoint1.expectedBodiesReceived(message1);
+
+        MockEndpoint mockEndpoint2 = resolveMandatoryEndpoint("mock:results2", 
MockEndpoint.class);
+        mockEndpoint2.expectedBodiesReceived(message2);
+
+
+        mockEndpoint1.assertIsSatisfied();
+        mockEndpoint2.assertIsSatisfied();
+    }
+
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+
+        blobStore1.createContainerInLocation(null, TEST_CONTAINER);
+        blobStore2.createContainerInLocation(null, TEST_CONTAINER);
+
+        ((JcloudsComponent) 
context.getComponent("jclouds")).setBlobStores(Lists.newArrayList(blobStore1, 
blobStore2));
+
+        return new RouteBuilder() {
+            public void configure() {
+
+                from("jclouds:blobstore:b1?container=" + TEST_CONTAINER)
+                        .convertBodyTo(String.class)
+                        .to("mock:results1");
+
+                from("jclouds:blobstore:b2?container=" + TEST_CONTAINER)
+                        .convertBodyTo(String.class)
+                        .to("mock:results2");
+            }
+        };
+    }
+}

Modified: camel/trunk/parent/pom.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/parent/pom.xml?rev=1402549&r1=1402548&r2=1402549&view=diff
==============================================================================
--- camel/trunk/parent/pom.xml (original)
+++ camel/trunk/parent/pom.xml Fri Oct 26 15:31:08 2012
@@ -180,7 +180,7 @@
     <jaxb-bundle-version>2.2.1.1_2</jaxb-bundle-version>
     <jaxen-version>1.1.4</jaxen-version>
     <jboss-javaee-6-version>1.0.0.Final</jboss-javaee-6-version>
-    <jclouds-version>1.5.1</jclouds-version>
+    <jclouds-version>1.5.2</jclouds-version>
     <jcr-version>2.0</jcr-version>
     <jdom-bundle-version>1.1_4</jdom-bundle-version>
     <jdom-version>1.1.3</jdom-version>


Reply via email to