You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ha...@apache.org on 2011/09/28 22:51:59 UTC
svn commit: r1177058 - in /camel/trunk/components/camel-jclouds: ./
src/main/java/org/apache/camel/component/jclouds/
src/test/java/org/apache/camel/component/jclouds/ src/test/resources/
Author: hadrian
Date: Wed Sep 28 20:51:58 2011
New Revision: 1177058
URL: http://svn.apache.org/viewvc?rev=1177058&view=rev
Log:
CAMEL-4034. Add support for compute service
Added:
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreHelper.java
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeEndpoint.java
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeProducer.java
camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducerTest.java
camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsSpringBlobstoreTest.java
- copied, changed from r1177040, camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsComponentTest.java
camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsSpringComputeTest.java
camel/trunk/components/camel-jclouds/src/test/resources/compute-test.xml
Removed:
camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsComponentTest.java
Modified:
camel/trunk/components/camel-jclouds/pom.xml
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreConsumer.java
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreEndpoint.java
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducer.java
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComponent.java
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsConstants.java
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsConsumer.java
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsEndpoint.java
camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsProducer.java
camel/trunk/components/camel-jclouds/src/test/resources/blobstore-test.xml
Modified: camel/trunk/components/camel-jclouds/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/pom.xml?rev=1177058&r1=1177057&r2=1177058&view=diff
==============================================================================
--- camel/trunk/components/camel-jclouds/pom.xml (original)
+++ camel/trunk/components/camel-jclouds/pom.xml Wed Sep 28 20:51:58 2011
@@ -57,6 +57,12 @@
<version>${jclouds-version}</version>
</dependency>
+ <dependency>
+ <groupId>org.jclouds</groupId>
+ <artifactId>jclouds-compute</artifactId>
+ <version>${jclouds-version}</version>
+ </dependency>
+
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
Modified: camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreConsumer.java?rev=1177058&r1=1177057&r2=1177058&view=diff
==============================================================================
--- camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreConsumer.java (original)
+++ camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreConsumer.java Wed Sep 28 20:51:58 2011
@@ -32,7 +32,6 @@ import org.apache.camel.spi.Synchronizat
import org.apache.camel.util.CastUtils;
import org.apache.camel.util.ObjectHelper;
import org.jclouds.blobstore.BlobStore;
-import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.slf4j.Logger;
@@ -44,7 +43,6 @@ public class JcloudsBlobStoreConsumer ex
private final JcloudsBlobStoreEndpoint endpoint;
- private BlobStoreContext blobStoreContext;
private String container;
private BlobStore blobStore;
@@ -54,12 +52,10 @@ public class JcloudsBlobStoreConsumer ex
private volatile int pendingExchanges;
- public JcloudsBlobStoreConsumer(JcloudsBlobStoreEndpoint endpoint, Processor processor, BlobStoreContext blobStoreContext, String container) {
+ public JcloudsBlobStoreConsumer(JcloudsBlobStoreEndpoint endpoint, Processor processor, BlobStore blobStore) {
super(endpoint, processor);
+ this.blobStore = blobStore;
this.endpoint = endpoint;
- this.blobStoreContext = blobStoreContext;
- this.container = container;
- blobStore = blobStoreContext.getBlobStore();
}
@Override
@@ -72,10 +68,10 @@ public class JcloudsBlobStoreConsumer ex
ListContainerOptions opt = new ListContainerOptions();
- for (StorageMetadata md : blobStore.list(container, opt.maxResults(maxMessagesPerPoll))) {
+ for (StorageMetadata md : blobStore.list(endpoint.getContainer(), opt.maxResults(maxMessagesPerPoll))) {
messages++;
String blobName = md.getName();
- Object body = readBlob(container, blobName);
+ Object body = JcloudsBlobStoreHelper.readBlob(blobStore, endpoint.getContainer(), blobName, Thread.currentThread().getContextClassLoader());
Exchange exchange = endpoint.createExchange();
exchange.getIn().setBody(body);
exchange.setProperty(JcloudsConstants.BLOB_NAME, blobName);
@@ -159,56 +155,14 @@ public class JcloudsBlobStoreConsumer ex
}
- /**
- * Reads from a {@link BlobStore}. It returns an Object.
- *
- * @param container
- * @param blobName
- * @return
- */
- protected Object readBlob(String container, String blobName) {
- Object result = null;
- ObjectInputStream ois = null;
- BlobStore blobStore = blobStoreContext.getBlobStore();
- blobStore.createContainerInLocation(null, container);
-
- InputStream is = blobStore.getBlob(container, blobName).getPayload().getInput();
-
- try {
- ois = new ObjectInputStream(is) {
- @Override
- public Class<?> resolveClass(ObjectStreamClass desc) throws IOException,
- ClassNotFoundException {
- try {
- return getEndpoint().getCamelContext().getClassResolver().resolveClass(desc.getName());
- } catch (Exception e) {
- }
- return super.resolveClass(desc);
- }
- };
- result = ois.readObject();
- } catch (IOException
- e) {
- e.printStackTrace();
- } catch (ClassNotFoundException
- e) {
- e.printStackTrace();
- } finally {
- if (ois != null) {
- try {
- ois.close();
- } catch (IOException e) {
- }
- }
- if (is != null) {
- try {
- is.close();
- } catch (IOException e) {
- }
- }
- }
+ public String getContainer() {
+ return container;
+ }
- return result;
+ public void setContainer(String container) {
+ this.container = container;
}
+
+
}
Modified: camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreEndpoint.java?rev=1177058&r1=1177057&r2=1177058&view=diff
==============================================================================
--- camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreEndpoint.java (original)
+++ camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreEndpoint.java Wed Sep 28 20:51:58 2011
@@ -19,35 +19,60 @@ package org.apache.camel.component.jclou
import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
-import org.jclouds.blobstore.BlobStoreContext;
+import org.jclouds.blobstore.BlobStore;
public class JcloudsBlobStoreEndpoint extends JcloudsEndpoint {
+ private String blobName;
private String container;
+ private String operation;
- private BlobStoreContext blobStoreContext;
+ private BlobStore blobStore;
/**
* Constructor
*
* @param uri
* @param component
- * @param blobStoreContext
+ * @param blobStore
*/
- public JcloudsBlobStoreEndpoint(String uri, JcloudsComponent component, BlobStoreContext blobStoreContext, String container) {
+ public JcloudsBlobStoreEndpoint(String uri, JcloudsComponent component, BlobStore blobStore) {
super(uri, component);
- this.blobStoreContext = blobStoreContext;
- this.container = container;
+ this.blobStore = blobStore;
}
@Override
public Producer createProducer() throws Exception {
- return new JcloudsBlobStoreProducer(this, blobStoreContext, container);
+ return new JcloudsBlobStoreProducer(this, blobStore);
}
@Override
public Consumer createConsumer(Processor processor) {
- return new JcloudsBlobStoreConsumer(this, processor, blobStoreContext, container);
+ return new JcloudsBlobStoreConsumer(this, processor, blobStore);
+ }
+
+ public String getContainer() {
+ return container;
+ }
+
+ public void setContainer(String container) {
+ this.container = container;
+ }
+
+ public String getOperation() {
+ return operation;
+ }
+
+ public void setOperation(String operation) {
+ this.operation = operation;
+ }
+
+ public String getBlobName() {
+ return blobName;
+ }
+
+ public void setBlobName(String blobName) {
+ this.blobName = blobName;
}
}
Added: camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreHelper.java?rev=1177058&view=auto
==============================================================================
--- camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreHelper.java (added)
+++ camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreHelper.java Wed Sep 28 20:51:58 2011
@@ -0,0 +1,126 @@
+/**
+ * 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 java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamClass;
+import org.jclouds.blobstore.BlobStore;
+import org.jclouds.blobstore.domain.Blob;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class JcloudsBlobStoreHelper {
+
+ private static final Logger LOG = LoggerFactory.getLogger(JcloudsBlobStoreHelper.class);
+
+ private JcloudsBlobStoreHelper() {
+ //Utility Class
+ }
+
+ /**
+ * Writes payload to the the blobstore.
+ *
+ * @param blobStore
+ * @param container
+ * @param blobName
+ * @param payload
+ */
+ public static void writeBlob(BlobStore blobStore, String container, String blobName, Object payload) {
+ if (blobName != null && payload != null) {
+ Blob blob = blobStore.blobBuilder(blobName).build();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = null;
+ try {
+ oos = new ObjectOutputStream(baos);
+ oos.writeObject(payload);
+ blob.setPayload(baos.toByteArray());
+ blobStore.putBlob(container, blob);
+ } catch (IOException e) {
+ LOG.error("Error while writing blob", e);
+ } finally {
+ if (oos != null) {
+ try {
+ oos.close();
+ } catch (IOException e) {
+ }
+ }
+
+ if (baos != null) {
+ try {
+ baos.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+ }
+
+ }
+
+ /**
+ * Reads from a {@link BlobStore}. It returns an Object.
+ *
+ * @param container
+ * @param blobName
+ * @return
+ */
+ public static Object readBlob(BlobStore blobStore, String container, String blobName, final ClassLoader classLoader) {
+ Object result = null;
+ ObjectInputStream ois = null;
+ blobStore.createContainerInLocation(null, container);
+
+ InputStream is = blobStore.getBlob(container, blobName).getPayload().getInput();
+
+ try {
+ ois = new ObjectInputStream(is) {
+ @Override
+ public Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
+ try {
+ return classLoader.loadClass(desc.getName());
+ } catch (Exception e) {
+ }
+ return super.resolveClass(desc);
+ }
+ };
+ result = ois.readObject();
+ } catch (IOException
+ e) {
+ e.printStackTrace();
+ } catch (ClassNotFoundException
+ e) {
+ e.printStackTrace();
+ } finally {
+ if (ois != null) {
+ try {
+ ois.close();
+ } catch (IOException e) {
+ }
+ }
+ if (is != null) {
+ try {
+ is.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+ return result;
+ }
+}
Modified: camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducer.java?rev=1177058&r1=1177057&r2=1177058&view=diff
==============================================================================
--- camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducer.java (original)
+++ camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducer.java Wed Sep 28 20:51:58 2011
@@ -23,7 +23,6 @@ import java.io.ObjectOutputStream;
import org.apache.camel.Exchange;
import org.jclouds.blobstore.BlobStore;
-import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.domain.Blob;
import org.slf4j.Logger;
@@ -33,65 +32,67 @@ public class JcloudsBlobStoreProducer ex
private static final Logger LOG = LoggerFactory.getLogger(JcloudsBlobStoreProducer.class);
- private BlobStoreContext blobStoreContext;
- private String container;
+ private BlobStore blobStore;
- public JcloudsBlobStoreProducer(JcloudsEndpoint endpoint, BlobStoreContext blobStoreContext, String container) {
+ public JcloudsBlobStoreProducer(JcloudsEndpoint endpoint, BlobStore blobStore) {
super(endpoint);
- this.blobStoreContext = blobStoreContext;
- this.container = container;
+ this.blobStore = blobStore;
}
@Override
public void process(Exchange exchange) throws Exception {
- String blobName = extractBlobName(exchange);
- if (blobName != null && exchange.getIn() != null && exchange.getIn().getBody() != null) {
- Object body = exchange.getIn().getBody();
- BlobStore blobStore = blobStoreContext.getBlobStore();
- Blob blob = blobStore.blobBuilder(blobName).build();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ObjectOutputStream oos = null;
- try {
- oos = new ObjectOutputStream(baos);
- oos.writeObject(body);
- blob.setPayload(baos.toByteArray());
- blobStore.putBlob(container, blob);
- } catch (IOException e) {
- LOG.error("Error while writing blob", e);
- } finally {
- if (oos != null) {
- try {
- oos.close();
- } catch (IOException e) {
- }
- }
-
- if (baos != null) {
- try {
- baos.close();
- } catch (IOException e) {
- }
- }
- }
+ String container = getContainerName(exchange);
+ String blobName = getBlobName(exchange);
+ String operation = getOperation(exchange);
+
+ Object body = exchange.getIn().getBody();
+ if (JcloudsConstants.GET.equals(operation)) {
+ exchange.getOut().setBody(JcloudsBlobStoreHelper.readBlob(blobStore, container, blobName, Thread.currentThread().getContextClassLoader()));
+ } else {
+ JcloudsBlobStoreHelper.writeBlob(blobStore, container, blobName, body);
}
-
}
/**
- * Extracts the BLOB_NAME from Exchange headers / properties.
+ * Retrieves the blobName from the URI or from the exchange headers. The header will take precedence over the URI.
*
* @param exchange
* @return
*/
- public String extractBlobName(Exchange exchange) {
- String blobName = null;
- if (exchange != null) {
- if (exchange.getProperty(JcloudsConstants.BLOB_NAME) != null) {
- blobName = (String) exchange.getProperty(JcloudsConstants.BLOB_NAME);
- } else if (exchange.getIn() != null && exchange.getIn().getHeader(JcloudsConstants.BLOB_NAME) != null) {
- blobName = (String) exchange.getIn().getHeader(JcloudsConstants.BLOB_NAME);
- }
+ protected String getBlobName(Exchange exchange) {
+ String blobName = ((JcloudsBlobStoreEndpoint) getEndpoint()).getBlobName();
+ if (exchange.getIn().getHeader(JcloudsConstants.BLOB_NAME) != null) {
+ blobName = (String) exchange.getIn().getHeader(JcloudsConstants.BLOB_NAME);
}
return blobName;
}
+
+ /**
+ * Retrieves the containerName from the URI or from the exchange headers. The header will take precedence over the URI.
+ *
+ * @param exchange
+ * @return
+ */
+ protected String getContainerName(Exchange exchange) {
+ String containerName = ((JcloudsBlobStoreEndpoint) getEndpoint()).getContainer();
+ if (exchange.getIn().getHeader(JcloudsConstants.CONTAINER_NAME) != null) {
+ containerName = (String) exchange.getIn().getHeader(JcloudsConstants.CONTAINER_NAME);
+ }
+ return containerName;
+ }
+
+ /**
+ * Retrieves the operation from the URI or from the exchange headers. The header will take precedence over the URI.
+ *
+ * @param exchange
+ * @return
+ */
+ public String getOperation(Exchange exchange) {
+ String operation = ((JcloudsBlobStoreEndpoint) getEndpoint()).getOperation();
+
+ if (exchange.getIn().getHeader(JcloudsConstants.OPERATION) != null) {
+ operation = (String) exchange.getIn().getHeader(JcloudsConstants.OPERATION);
+ }
+ return operation;
+ }
}
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=1177058&r1=1177057&r2=1177058&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 Wed Sep 28 20:51:58 2011
@@ -16,20 +16,21 @@
*/
package org.apache.camel.component.jclouds;
+import java.util.List;
import java.util.Map;
import org.apache.camel.Endpoint;
import org.apache.camel.impl.DefaultComponent;
-import org.jclouds.blobstore.BlobStoreContext;
-import org.jclouds.blobstore.BlobStoreContextFactory;
+import org.jclouds.blobstore.BlobStore;
+import org.jclouds.compute.ComputeService;
/**
* Represents the component that manages {@link JcloudsEndpoint}.
*/
public class JcloudsComponent extends DefaultComponent {
- private String provider;
- private String identity;
- private String creadential;
+
+ private List<BlobStore> blobStores;
+ private List<ComputeService> computeServices;
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
Endpoint endpoint = null;
@@ -45,13 +46,20 @@ public class JcloudsComponent extends De
}
if (JcloudsConstants.BLOBSTORE.endsWith(endpointType)) {
-
if (uriParts.length >= 2) {
- String container = uriParts[1];
- BlobStoreContext blobStoreContext = new BlobStoreContextFactory().createContext(provider, identity, creadential);
- endpoint = new JcloudsBlobStoreEndpoint(uri, this, blobStoreContext, container);
+ String provider = uriParts[1];
+ BlobStore blobStore = getBlobStoreForProvider(provider);
+ endpoint = new JcloudsBlobStoreEndpoint(uri, this, blobStore);
+ } else {
+ throw new Exception("Invalid Endpoint URI. It should contains a valid provider name");
+ }
+ } else if (JcloudsConstants.COMPUTE.endsWith(endpointType)) {
+ if (uriParts.length >= 2) {
+ String provider = uriParts[1];
+ ComputeService computeService = getComputeServiceForProvider(provider);
+ endpoint = new JcloudsComputeEndpoint(uri, this, computeService);
} else {
- throw new Exception("Invalid Endpoint URI. It should contains a valid container name");
+ throw new Exception("Invalid Endpoint URI. It should contains a valid provider name");
}
}
@@ -59,27 +67,57 @@ public class JcloudsComponent extends De
return endpoint;
}
- public String getProvider() {
- return provider;
+ /**
+ * Returns the {@link BlobStore} that matches the given provider.
+ * @param provider The provider id.
+ * @return The matching {@link BlobStore}
+ */
+ protected BlobStore getBlobStoreForProvider(String provider) throws Exception {
+
+ if (blobStores != null && !blobStores.isEmpty()) {
+ for (BlobStore blobStore : blobStores) {
+ if (blobStore.getContext().getProviderSpecificContext().getId().equals(provider)) {
+ return blobStore;
+ }
+ }
+ throw new Exception(String.format("No blobstore found for provider:%s", provider));
+ } else {
+ throw new Exception("No blobstore available.");
+ }
}
- public void setProvider(String provider) {
- this.provider = provider;
+ /**
+ * Returns the {@link ComputeService} that matches the given provider.
+ * @param provider The provider id.
+ * @return The matching {@link ComputeService}
+ */
+ protected ComputeService getComputeServiceForProvider(String provider) throws Exception {
+
+ if (computeServices != null && !computeServices.isEmpty()) {
+ for (ComputeService computeService : computeServices) {
+ if (computeService.getContext().getProviderSpecificContext().getId().equals(provider)) {
+ return computeService;
+ }
+ }
+ throw new Exception(String.format("No compute service found for provider:%s", provider));
+ } else {
+ throw new Exception("No compute service available.");
+ }
}
- public String getIdentity() {
- return identity;
+ public List<BlobStore> getBlobStores() {
+ return blobStores;
}
- public void setIdentity(String identity) {
- this.identity = identity;
+ public void setBlobStores(List<BlobStore> blobStores) {
+ this.blobStores = blobStores;
}
- public String getCreadential() {
- return creadential;
+ public List<ComputeService> getComputeServices() {
+ return computeServices;
}
- public void setCreadential(String creadential) {
- this.creadential = creadential;
+ public void setComputeServices(List<ComputeService> computeServices) {
+ this.computeServices = computeServices;
}
}
Added: camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeEndpoint.java?rev=1177058&view=auto
==============================================================================
--- camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeEndpoint.java (added)
+++ camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeEndpoint.java Wed Sep 28 20:51:58 2011
@@ -0,0 +1,109 @@
+/**
+ * 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 org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.jclouds.compute.ComputeService;
+
+public class JcloudsComputeEndpoint extends JcloudsEndpoint {
+
+ private ComputeService computeService;
+
+ private String imageId;
+ private String locationId;
+ private String hardwareId;
+
+ private String operation;
+ private String nodeId;
+ private String group;
+ private String user;
+
+ public JcloudsComputeEndpoint(String uri, JcloudsComponent component, ComputeService computeService) {
+ super(uri, component);
+ this.computeService = computeService;
+ }
+
+
+ @Override
+ public Producer createProducer() throws Exception {
+ return new JcloudsComputeProducer(this, computeService);
+ }
+
+ @Override
+ public Consumer createConsumer(Processor processor) {
+ throw new UnsupportedOperationException("Consumer not supported for JcloudsComputeEndpoint!");
+ }
+
+ public String getImageId() {
+ return imageId;
+ }
+
+ public void setImageId(String imageId) {
+ this.imageId = imageId;
+ }
+
+ public String getLocationId() {
+ return locationId;
+ }
+
+ public void setLocationId(String locationId) {
+ this.locationId = locationId;
+ }
+
+ public String getHardwareId() {
+ return hardwareId;
+ }
+
+ public void setHardwareId(String hardwareId) {
+ this.hardwareId = hardwareId;
+ }
+
+ public String getOperation() {
+ return operation;
+ }
+
+ public void setOperation(String operation) {
+ this.operation = operation;
+ }
+
+ public String getNodeId() {
+ return nodeId;
+ }
+
+ public void setNodeId(String nodeId) {
+ this.nodeId = nodeId;
+ }
+
+ public String getGroup() {
+ return group;
+ }
+
+ public void setGroup(String group) {
+ this.group = group;
+ }
+
+ public String getUser() {
+ return user;
+ }
+
+ public void setUser(String user) {
+ this.user = user;
+ }
+}
Added: camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeProducer.java?rev=1177058&view=auto
==============================================================================
--- camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeProducer.java (added)
+++ camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsComputeProducer.java Wed Sep 28 20:51:58 2011
@@ -0,0 +1,295 @@
+/**
+ * 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 java.util.Set;
+import org.apache.camel.CamelException;
+import org.apache.camel.Exchange;
+import org.jclouds.compute.ComputeService;
+import org.jclouds.compute.RunNodesException;
+import org.jclouds.compute.domain.ComputeMetadata;
+import org.jclouds.compute.domain.ExecResponse;
+import org.jclouds.compute.domain.Hardware;
+import org.jclouds.compute.domain.Image;
+import org.jclouds.compute.domain.NodeMetadata;
+import org.jclouds.compute.domain.TemplateBuilder;
+import org.jclouds.compute.options.RunScriptOptions;
+import org.jclouds.domain.Credentials;
+
+public class JcloudsComputeProducer extends JcloudsProducer {
+
+ private ComputeService computeService;
+
+ public JcloudsComputeProducer(JcloudsEndpoint endpoint, ComputeService computeService) {
+ super(endpoint);
+ this.computeService = computeService;
+ }
+
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ String operation = getOperation(exchange);
+
+ if (operation == null) {
+ throw new CamelException("Operation must be specified in the endpoitn URI or as a property on the exchange.");
+ }
+
+ if (JcloudsConstants.LIST_NODES.equals(operation)) {
+ listNodes(exchange);
+
+ } else if (JcloudsConstants.LIST_IMAGES.equals(operation)) {
+ listImages(exchange);
+
+ } else if (JcloudsConstants.LIST_HARDWARE.equals(operation)) {
+ listHardware(exchange);
+
+ } else if (JcloudsConstants.RUN_SCRIPT.equals(operation)) {
+ runScriptOnNode(exchange);
+
+ } else if (JcloudsConstants.CREATE_NODE.equals(operation)) {
+ createNode(exchange);
+
+ } else if (JcloudsConstants.DESTROY_NODE.equals(operation)) {
+ destroyNode(exchange);
+ } else {
+
+ }
+ }
+
+ /**
+ * Create a node with the specified group.
+ *
+ * @param exchange
+ * @throws CamelException
+ */
+ protected void createNode(Exchange exchange) throws CamelException {
+ String group = getGroup(exchange);
+ String imageId = getImageId(exchange);
+ String locationId = getLocationId(exchange);
+ String hardwareId = getHardwareId(exchange);
+
+ if (group == null) {
+ throw new CamelException("Group must be specific in the URI or as exchange property for the destroy node operation.");
+ }
+ TemplateBuilder builder = computeService.templateBuilder();
+ builder.any();
+
+ if (locationId != null) {
+ builder.locationId(locationId);
+ }
+ if (imageId != null) {
+ builder.imageId(imageId);
+ }
+ if (hardwareId != null) {
+ builder.hardwareId(hardwareId);
+ }
+
+ try {
+ Set<? extends NodeMetadata> nodeMetadatas = computeService.createNodesInGroup(group, 1, builder.build());
+ exchange.getOut().setBody(nodeMetadatas);
+ exchange.getOut().setHeaders(exchange.getIn().getHeaders());
+ } catch (RunNodesException e) {
+ throw new CamelException("Error creating jclouds node.", e);
+ }
+ }
+
+ /**
+ * Runs a script on the target node.
+ *
+ * @param exchange
+ * @throws CamelException
+ */
+ protected void runScriptOnNode(Exchange exchange) throws CamelException {
+ String script = exchange.getIn().getBody(String.class);
+ String nodeId = getNodeId(exchange);
+ String user = getUser(exchange);
+
+ Credentials credentials = null;
+
+ if (user != null) {
+ credentials = new Credentials(user, null);
+ }
+ ExecResponse execResponse = null;
+
+ if (credentials == null) {
+ execResponse = computeService.runScriptOnNode(nodeId, script);
+ } else {
+ execResponse = computeService.runScriptOnNode(nodeId, script, RunScriptOptions.Builder.overrideCredentialsWith(credentials).runAsRoot(false));
+ }
+
+ if (execResponse == null) {
+ throw new CamelException("Failed to receive response for run script operation.");
+ }
+
+ exchange.setProperty(JcloudsConstants.RUN_SCRIPT_ERROR, execResponse.getError());
+ exchange.setProperty(JcloudsConstants.RUN_SCRIPT_EXIT_CODE, execResponse.getExitCode());
+ if (execResponse != null) {
+ exchange.getOut().setBody(execResponse.getOutput());
+ }
+ }
+
+ /**
+ * Destroys the node with the specified nodeId.
+ *
+ * @param exchange
+ * @throws CamelException
+ */
+ protected void destroyNode(Exchange exchange) throws CamelException {
+ String nodeId = getNodeId(exchange);
+ if (nodeId == null) {
+ throw new CamelException("Node id must be specific in the URI or as exchange property for the destroy node operation.");
+ }
+ computeService.destroyNode(nodeId);
+ }
+
+ /**
+ * Sets the metadata of the available nodes to the out message.
+ *
+ * @param exchange
+ * @throws CamelException
+ */
+ protected void listNodes(Exchange exchange) throws CamelException {
+ Set<? extends ComputeMetadata> computeMetadatas = computeService.listNodes();
+ exchange.getOut().setBody(computeMetadatas);
+ }
+
+ /**
+ * Sets the available images to the out message.
+ *
+ * @param exchange
+ * @throws CamelException
+ */
+ protected void listImages(Exchange exchange) throws CamelException {
+ Set<? extends Image> images = computeService.listImages();
+ exchange.getOut().setBody(images);
+ }
+
+ /**
+ * Sets the available hardware profiles to the out message.
+ *
+ * @param exchange
+ * @throws CamelException
+ */
+ protected void listHardware(Exchange exchange) throws CamelException {
+ Set<? extends Hardware> hardwareProfiles = computeService.listHardwareProfiles();
+ exchange.getOut().setBody(hardwareProfiles);
+ }
+
+ /**
+ * Retrieves the operation from the URI or from the exchange headers. The header will take precedence over the URI.
+ *
+ * @param exchange
+ * @return
+ */
+ public String getOperation(Exchange exchange) {
+ String operation = ((JcloudsComputeEndpoint) getEndpoint()).getOperation();
+
+ if (exchange.getIn().getHeader(JcloudsConstants.OPERATION) != null) {
+ operation = (String) exchange.getIn().getHeader(JcloudsConstants.OPERATION);
+ }
+ return operation;
+ }
+
+ /**
+ * Retrieves the image id from the URI or from the exchange properties. The property will take precedence over the URI.
+ *
+ * @param exchange
+ * @return
+ */
+ protected String getImageId(Exchange exchange) {
+ String imageId = ((JcloudsComputeEndpoint) getEndpoint()).getImageId();
+
+ if (exchange.getIn().getHeader(JcloudsConstants.IMAGE_ID) != null) {
+ imageId = (String) exchange.getIn().getHeader(JcloudsConstants.IMAGE_ID);
+ }
+ return imageId;
+ }
+
+ /**
+ * Retrieves the hardware id from the URI or from the exchange headers. The header will take precedence over the URI.
+ *
+ * @param exchange
+ * @return
+ */
+ protected String getHardwareId(Exchange exchange) {
+ String hardwareId = ((JcloudsComputeEndpoint) getEndpoint()).getHardwareId();
+
+ if (exchange.getIn().getHeader(JcloudsConstants.HARDWARE_ID) != null) {
+ hardwareId = (String) exchange.getIn().getHeader(JcloudsConstants.HARDWARE_ID);
+ }
+ return hardwareId;
+ }
+
+ /**
+ * Retrieves the location id from the URI or from the exchange headers. The header will take precedence over the URI.
+ *
+ * @param exchange
+ * @return
+ */
+ protected String getLocationId(Exchange exchange) {
+ String locationId = ((JcloudsComputeEndpoint) getEndpoint()).getLocationId();
+
+ if (exchange.getIn().getHeader(JcloudsConstants.LOCATION_ID) != null) {
+ locationId = (String) exchange.getIn().getHeader(JcloudsConstants.LOCATION_ID);
+ }
+ return locationId;
+ }
+
+ /**
+ * Retrieves the node id from the URI or from the exchange headers. The header will take precedence over the URI.
+ *
+ * @param exchange
+ * @return
+ */
+ protected String getNodeId(Exchange exchange) {
+ String nodeId = ((JcloudsComputeEndpoint) getEndpoint()).getNodeId();
+
+ if (exchange.getIn().getHeader(JcloudsConstants.NODE_ID) != null) {
+ nodeId = (String) exchange.getIn().getHeader(JcloudsConstants.NODE_ID);
+ }
+ return nodeId;
+ }
+
+ /**
+ * Retrieves the group from the URI or from the exchange headers. The header will take precedence over the URI.
+ *
+ * @param exchange
+ * @return
+ */
+ protected String getGroup(Exchange exchange) {
+ String group = ((JcloudsComputeEndpoint) getEndpoint()).getGroup();
+
+ if (exchange.getIn().getHeader(JcloudsConstants.GROUP) != null) {
+ group = (String) exchange.getIn().getHeader(JcloudsConstants.GROUP);
+ }
+ return group;
+ }
+
+ /**
+ * Retrieves the user from the URI or from the exchange headers. The header will take precedence over the URI.
+ *
+ * @param exchange
+ * @return
+ */
+ protected String getUser(Exchange exchange) {
+ String user = ((JcloudsComputeEndpoint) getEndpoint()).getUser();
+
+ if (exchange.getIn().getHeader(JcloudsConstants.USER) != null) {
+ user = (String) exchange.getIn().getHeader(JcloudsConstants.USER);
+ }
+ return user;
+ }
+}
Modified: camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsConstants.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsConstants.java?rev=1177058&r1=1177057&r2=1177058&view=diff
==============================================================================
--- camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsConstants.java (original)
+++ camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsConstants.java Wed Sep 28 20:51:58 2011
@@ -19,7 +19,31 @@ package org.apache.camel.component.jclou
public final class JcloudsConstants {
public static final String DELIMETER = ":";
public static final String BLOBSTORE = "blobstore";
- public static final String BLOB_NAME = "BLOB_NAME";
+ public static final String COMPUTE = "compute";
+ public static final String BLOB_NAME = "CamelJcloudsBlobName";
+ public static final String CONTAINER_NAME = "CamelJcloudsContainerName";
+
+ public static final String OPERATION = "CamelJcloudsOperation";
+ public static final String PUT = "CamelJcloudsPut";
+ public static final String GET = "CamelJcloudsGet";
+ public static final String LIST_IMAGES = "CamelJcloudsListImages";
+ public static final String LIST_HARDWARE = "CamelJcloudsListHardware";
+ public static final String LIST_NODES = "CamelJcloudsListNodes";
+ public static final String CREATE_NODE = "CamelJcloudsCreateNode";
+ public static final String DESTROY_NODE = "CamelJcloudsDestroyNode";
+ public static final String RUN_SCRIPT = "CamelJcloudsRunScript";
+
+ public static final String IMAGE_ID = "CamelJcloudsImageId";
+ public static final String LOCATION_ID = "CamelJcloudsLocationId";
+ public static final String HARDWARE_ID = "CamelJcloudsHardwareId";
+ public static final String GROUP = "CamelJcloudsGroup";
+ public static final String NODE_ID = "CamelJcloudsNodeId";
+ public static final String USER = "CamelJcloudsUser";
+
+ public static final String RUN_SCRIPT_ERROR = "CamelJcloudsRunScriptError";
+ public static final String RUN_SCRIPT_EXIT_CODE = "CamelJcloudsRunScriptErrorCode";
+
+
private JcloudsConstants() {
// utility class
Modified: camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsConsumer.java?rev=1177058&r1=1177057&r2=1177058&view=diff
==============================================================================
--- camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsConsumer.java (original)
+++ camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsConsumer.java Wed Sep 28 20:51:58 2011
@@ -16,8 +16,6 @@
*/
package org.apache.camel.component.jclouds;
-import java.util.Date;
-
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.impl.ScheduledPollConsumer;
@@ -34,10 +32,6 @@ public class JcloudsConsumer extends Sch
protected int poll() throws Exception {
Exchange exchange = endpoint.createExchange();
- // create a message body
- Date now = new Date();
- exchange.getIn().setBody("Hello World! The time is " + now);
-
try {
// send message to next processor in the route
getProcessor().process(exchange);
Modified: camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsEndpoint.java?rev=1177058&r1=1177057&r2=1177058&view=diff
==============================================================================
--- camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsEndpoint.java (original)
+++ camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsEndpoint.java Wed Sep 28 20:51:58 2011
@@ -16,9 +16,6 @@
*/
package org.apache.camel.component.jclouds;
-import org.apache.camel.Consumer;
-import org.apache.camel.Processor;
-import org.apache.camel.Producer;
import org.apache.camel.impl.DefaultEndpoint;
public abstract class JcloudsEndpoint extends DefaultEndpoint {
Modified: camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsProducer.java?rev=1177058&r1=1177057&r2=1177058&view=diff
==============================================================================
--- camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsProducer.java (original)
+++ camel/trunk/components/camel-jclouds/src/main/java/org/apache/camel/component/jclouds/JcloudsProducer.java Wed Sep 28 20:51:58 2011
@@ -31,4 +31,5 @@ public class JcloudsProducer extends Def
public void process(Exchange exchange) throws Exception {
LOG.debug("JcloudsProducer received: {}", exchange.getIn().getBody());
}
+
}
Added: camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducerTest.java?rev=1177058&view=auto
==============================================================================
--- camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducerTest.java (added)
+++ camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsBlobStoreProducerTest.java Wed Sep 28 20:51:58 2011
@@ -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.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.blobstore.BlobStore;
+import org.jclouds.blobstore.BlobStoreContext;
+import org.jclouds.blobstore.BlobStoreContextFactory;
+import org.junit.Test;
+
+public class JcloudsBlobStoreProducerTest extends CamelTestSupport {
+
+ private static final String TEST_CONTAINER = "testContainer";
+ private static final String TEST_BLOB = "testBlob";
+
+ BlobStoreContextFactory contextFactory = new BlobStoreContextFactory();
+ BlobStoreContext blobStoreContext = contextFactory.createContext("transient", "identity", "credential");
+ BlobStore blobStore = blobStoreContext.getBlobStore();
+ JcloudsComponent jcloudsComponent = new JcloudsComponent();
+
+
+ @Test
+ public void testBlobStorePut() throws InterruptedException {
+ MockEndpoint mockEndpoint = resolveMandatoryEndpoint("mock:results", MockEndpoint.class);
+ mockEndpoint.expectedMessageCount(1);
+ template.sendBody("direct:put", "Some message");
+ mockEndpoint.assertIsSatisfied();
+ }
+
+ @Test
+ public void testBlobStorePutAndGet() throws InterruptedException {
+ String message = "Some message";
+ template.sendBody("direct:put-and-get", message);
+ Object result = template.requestBodyAndHeader("direct:put-and-get", null, JcloudsConstants.OPERATION, JcloudsConstants.GET);
+ assertEquals(message, result);
+ }
+
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+
+ blobStore.createContainerInLocation(null, TEST_CONTAINER);
+ jcloudsComponent.setBlobStores(Lists.newArrayList(blobStore));
+
+ return new RouteBuilder() {
+ public void configure() {
+ getContext().addComponent("jclouds", jcloudsComponent);
+ from("direct:put")
+ .setHeader(JcloudsConstants.BLOB_NAME, constant(TEST_BLOB))
+ .setHeader(JcloudsConstants.CONTAINER_NAME, constant(TEST_CONTAINER))
+ .to("jclouds:blobstore:transient").to("mock:results");
+
+ from("direct:put-and-get")
+ .setHeader(JcloudsConstants.BLOB_NAME, constant(TEST_BLOB))
+ .setHeader(JcloudsConstants.CONTAINER_NAME, constant(TEST_CONTAINER))
+ .to("jclouds:blobstore:transient");
+ }
+ };
+ }
+}
Copied: camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsSpringBlobstoreTest.java (from r1177040, camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsComponentTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsSpringBlobstoreTest.java?p2=camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsSpringBlobstoreTest.java&p1=camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsComponentTest.java&r1=1177040&r2=1177058&rev=1177058&view=diff
==============================================================================
--- camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsComponentTest.java (original)
+++ camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsSpringBlobstoreTest.java Wed Sep 28 20:51:58 2011
@@ -26,16 +26,21 @@ import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
-public class JcloudsComponentTest extends CamelSpringTestSupport {
+public class JcloudsSpringBlobstoreTest extends CamelSpringTestSupport {
- @EndpointInject(uri = "mock:result")
- protected MockEndpoint result;
+ @EndpointInject(uri = "mock:result-foo")
+ protected MockEndpoint resultFoo;
+
+
+ @EndpointInject(uri = "mock:result-bar")
+ protected MockEndpoint resultBar;
@BeforeClass
public static void setUpClass() throws Exception {
BlobStoreContext context = new BlobStoreContextFactory().createContext("transient", "id", "credential");
context.getBlobStore().createContainerInLocation(null, "foo");
+ context.getBlobStore().createContainerInLocation(null, "bar");
}
@Override
@@ -44,8 +49,23 @@ public class JcloudsComponentTest extend
}
@Test
- public void testProduce() throws InterruptedException {
- result.expectedMessageCount(1);
- result.assertIsSatisfied();
+ public void testBlobStorePut() throws InterruptedException {
+ template.sendBody("direct:start", "Some message");
+ resultFoo.expectedMessageCount(1);
+ resultFoo.assertIsSatisfied();
+ }
+
+ @Test
+ public void testBlobStoreGet() throws InterruptedException {
+ template.sendBody("direct:start", "Some message");
+ resultFoo.expectedMessageCount(1);
+ resultFoo.assertIsSatisfied();
+ }
+
+ @Test
+ public void testProduceWithUrlParametes() throws InterruptedException {
+ template.sendBody("direct:start-with-url-parameters", "Some message");
+ resultBar.expectedMessageCount(1);
+ resultBar.assertIsSatisfied();
}
}
Added: camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsSpringComputeTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsSpringComputeTest.java?rev=1177058&view=auto
==============================================================================
--- camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsSpringComputeTest.java (added)
+++ camel/trunk/components/camel-jclouds/src/test/java/org/apache/camel/component/jclouds/JcloudsSpringComputeTest.java Wed Sep 28 20:51:58 2011
@@ -0,0 +1,150 @@
+/**
+ * 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 java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelSpringTestSupport;
+import org.jclouds.compute.domain.Hardware;
+import org.jclouds.compute.domain.Image;
+import org.jclouds.compute.domain.NodeMetadata;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class JcloudsSpringComputeTest extends CamelSpringTestSupport {
+
+ @EndpointInject(uri = "mock:result")
+ protected MockEndpoint result;
+
+ @BeforeClass
+ public static void setUpClass() throws Exception {
+ }
+
+ @Override
+ protected AbstractApplicationContext createApplicationContext() {
+ return new ClassPathXmlApplicationContext("classpath:compute-test.xml");
+ }
+
+ @Test
+ public void testListImages() throws InterruptedException {
+ template.sendBodyAndHeader("direct:start", "Some message", JcloudsConstants.OPERATION, JcloudsConstants.LIST_IMAGES);
+ result.expectedMessageCount(1);
+ result.assertIsSatisfied();
+ List<Exchange> exchanges = result.getExchanges();
+ if (exchanges != null && !exchanges.isEmpty()) {
+ for (Exchange exchange : exchanges) {
+ Set images = (Set) exchange.getIn().getBody();
+ assertTrue(images.size() > 0);
+ for (Object obj : images) {
+ assertTrue(obj instanceof Image);
+ }
+ }
+ }
+ }
+
+ @Test
+ public void testListHardware() throws InterruptedException {
+ template.sendBodyAndHeader("direct:start", "Some message", JcloudsConstants.OPERATION, JcloudsConstants.LIST_HARDWARE);
+ result.expectedMessageCount(1);
+ result.assertIsSatisfied();
+ List<Exchange> exchanges = result.getExchanges();
+ if (exchanges != null && !exchanges.isEmpty()) {
+ for (Exchange exchange : exchanges) {
+ Set hardwares = (Set) exchange.getIn().getBody();
+ assertTrue(hardwares.size() > 0);
+ for (Object obj : hardwares) {
+ assertTrue(obj instanceof Hardware);
+ }
+ }
+ }
+ }
+
+ @Test
+ public void testListNodes() throws InterruptedException {
+ template.sendBodyAndHeader("direct:start", "Some message", JcloudsConstants.OPERATION, JcloudsConstants.LIST_NODES);
+ result.expectedMessageCount(1);
+ result.assertIsSatisfied();
+ List<Exchange> exchanges = result.getExchanges();
+ if (exchanges != null && !exchanges.isEmpty()) {
+ for (Exchange exchange : exchanges) {
+ Set nodeMetadatas = (Set) exchange.getIn().getBody();
+ assertEquals("Nodes should be 0", 0, nodeMetadatas.size());
+ }
+ }
+ }
+
+
+ @Test
+ public void testCreateAndDestroyNode() throws InterruptedException {
+ Map<String, Object> createHeaders = new HashMap<String, Object>();
+ createHeaders.put(JcloudsConstants.OPERATION, JcloudsConstants.CREATE_NODE);
+ createHeaders.put(JcloudsConstants.IMAGE_ID, "1");
+ createHeaders.put(JcloudsConstants.GROUP, "default");
+
+ Map<String, Object> destroyHeaders = new HashMap<String, Object>();
+ destroyHeaders.put(JcloudsConstants.OPERATION, JcloudsConstants.DESTROY_NODE);
+
+ template.sendBodyAndHeaders("direct:start", "Some message", createHeaders);
+ result.expectedMessageCount(1);
+ result.assertIsSatisfied();
+ List<Exchange> exchanges = result.getExchanges();
+ if (exchanges != null && !exchanges.isEmpty()) {
+ for (Exchange exchange : exchanges) {
+ Set nodeMetadatas = (Set) exchange.getIn().getBody();
+ assertEquals("There should be no node running", 1, nodeMetadatas.size());
+
+ for (Object obj : nodeMetadatas) {
+ NodeMetadata nodeMetadata = (NodeMetadata) obj;
+ destroyHeaders.put(JcloudsConstants.NODE_ID, nodeMetadata.getId());
+ template.sendBodyAndHeaders("direct:start", "Some message", destroyHeaders);
+ }
+ }
+ }
+ }
+
+ @Ignore("For now not possible to combine stub provider with ssh module, requird for runScript")
+ @Test
+ public void testRunScript() throws InterruptedException {
+ Map<String, Object> createHeaders = new HashMap<String, Object>();
+ createHeaders.put(JcloudsConstants.OPERATION, JcloudsConstants.CREATE_NODE);
+ createHeaders.put(JcloudsConstants.IMAGE_ID, "1");
+ createHeaders.put(JcloudsConstants.GROUP, "default");
+
+ Map<String, Object> runScriptHeaders = new HashMap<String, Object>();
+ runScriptHeaders.put(JcloudsConstants.OPERATION, JcloudsConstants.RUN_SCRIPT);
+
+ Map<String, Object> destroyHeaders = new HashMap<String, Object>();
+ destroyHeaders.put(JcloudsConstants.OPERATION, JcloudsConstants.DESTROY_NODE);
+
+ Set<? extends NodeMetadata> nodeMetadatas = (Set<? extends NodeMetadata>) template.requestBodyAndHeaders("direct:in-out", "Some message", createHeaders);
+ assertEquals("There should be a node running", 1, nodeMetadatas.size());
+ for (NodeMetadata nodeMetadata : nodeMetadatas) {
+ runScriptHeaders.put(JcloudsConstants.NODE_ID, nodeMetadata.getId());
+ destroyHeaders.put(JcloudsConstants.NODE_ID, nodeMetadata.getId());
+ String output = (String) template.requestBodyAndHeaders("direct:in-out", "Some message", runScriptHeaders);
+ template.sendBodyAndHeaders("direct:in-out", "Some message", destroyHeaders);
+ }
+ }
+}
Modified: camel/trunk/components/camel-jclouds/src/test/resources/blobstore-test.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/test/resources/blobstore-test.xml?rev=1177058&r1=1177057&r2=1177058&view=diff
==============================================================================
--- camel/trunk/components/camel-jclouds/src/test/resources/blobstore-test.xml (original)
+++ camel/trunk/components/camel-jclouds/src/test/resources/blobstore-test.xml Wed Sep 28 20:51:58 2011
@@ -1,43 +1,72 @@
<?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://activemq.apache.org/camel/schema/spring"
- xmlns:util="http://www.springframework.org/schema/util"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:ctx="http://www.springframework.org/schema/context"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
- http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns="http://camel.apache.org/schema/spring">
+
<route>
- <from uri="dataset:testset"/>
- <setProperty propertyName="BLOB_NAME">
+ <from uri="direct:start"/>
+ <setHeader headerName="CamelJcloudsBlobName">
<constant>testName</constant>
- </setProperty>
- <to uri="jclouds:blobstore:foo"/>
+ </setHeader>
+ <setHeader headerName="CamelJcloudsContainerName">
+ <constant>foo</constant>
+ </setHeader>
+ <to uri="jclouds:blobstore:transient"/>
</route>
<route>
- <from uri="jclouds:blobstore:foo"/>
- <to uri="mock:result"/>
+ <from uri="jclouds:blobstore:transient?container=foo"/>
+ <to uri="mock:result-foo"/>
</route>
- </camelContext>
+ <route>
+ <from uri="direct:start-with-url-parameters"/>
+ <to uri="jclouds:blobstore:transient?container=bar&blobName=testName"/>
+ </route>
- <bean id="testset" class="org.apache.camel.component.dataset.SimpleDataSet">
- <property name="size" value="1"/>
- </bean>
+ <route>
+ <from uri="jclouds:blobstore:transient?container=bar"/>
+ <to uri="mock:result-bar"/>
+ </route>
+
+ </camelContext>
<bean id="jclouds" class="org.apache.camel.component.jclouds.JcloudsComponent">
- <property name="provider" value="transient"/>
- <property name="identity" value="identity"/>
- <property name="creadential" value="creadential"/>
+ <property name="blobStores">
+ <list>
+ <ref bean="blobStore"/>
+ </list>
+ </property>
+ </bean>
+
+ <bean id="blobStoreContextFactory" class="org.jclouds.blobstore.BlobStoreContextFactory"/>
+
+ <bean id="blobStoreContext" factory-bean="blobStoreContextFactory" factory-method="createContext">
+ <constructor-arg name="provider" value="transient"/>
+ <constructor-arg name="identity" value="identity"/>
+ <constructor-arg name="credential" value="credential"/>
</bean>
+ <bean id="blobStore" factory-bean="blobStoreContext" factory-method="getBlobStore"/>
+
</beans>
\ No newline at end of file
Added: camel/trunk/components/camel-jclouds/src/test/resources/compute-test.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jclouds/src/test/resources/compute-test.xml?rev=1177058&view=auto
==============================================================================
--- camel/trunk/components/camel-jclouds/src/test/resources/compute-test.xml (added)
+++ camel/trunk/components/camel-jclouds/src/test/resources/compute-test.xml Wed Sep 28 20:51:58 2011
@@ -0,0 +1,57 @@
+<?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"
+ 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">
+
+ <camelContext xmlns="http://camel.apache.org/schema/spring">
+
+ <route>
+ <from uri="direct:start"/>
+ <to uri="jclouds:compute:stub?operation=CamelJcloudsListImages"/>
+ <to uri="mock:result"/>
+ </route>
+
+ <route>
+ <from uri="direct:in-out"/>
+ <to uri="jclouds:compute:stub?operation=CamelJcloudsListImages"/>
+ </route>
+
+ </camelContext>
+
+ <bean id="jclouds" class="org.apache.camel.component.jclouds.JcloudsComponent">
+ <property name="computeServices">
+ <list>
+ <ref bean="computeService"/>
+ </list>
+ </property>
+ </bean>
+
+ <bean id="computeServiceContextFactory" class="org.jclouds.compute.ComputeServiceContextFactory"/>
+
+ <bean id="computeServiceContext" factory-bean="computeServiceContextFactory" factory-method="createContext">
+ <constructor-arg name="provider" value="stub"/>
+ <constructor-arg name="identity" value="identity"/>
+ <constructor-arg name="credential" value="credential"/>
+ </bean>
+
+ <bean id="computeService" factory-bean="computeServiceContext" factory-method="getComputeService"/>
+
+</beans>
\ No newline at end of file