switched aws demo to use s3 and ec2

This commit is contained in:
Adrian Cole 2010-03-20 12:40:52 -07:00
parent 7a4b513191
commit 72da36bf33
9 changed files with 139 additions and 121 deletions

View File

@ -44,26 +44,6 @@
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jclouds-blobstore</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jclouds-azure</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jclouds-rackspace</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jclouds-enterprise</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jclouds-gae</artifactId>
@ -205,22 +185,6 @@
</goals>
<configuration>
<systemProperties>
<property>
<name>jclouds.azure.storage.account</name>
<value>${jclouds.azure.storage.account}</value>
</property>
<property>
<name>jclouds.azure.storage.key</name>
<value>${jclouds.azure.storage.key}</value>
</property>
<property>
<name>jclouds.rackspace.user</name>
<value>${jclouds.rackspace.user}</value>
</property>
<property>
<name>jclouds.rackspace.key</name>
<value>${jclouds.rackspace.key}</value>
</property>
<property>
<name>jclouds.aws.accesskeyid</name>
<value>${jclouds.aws.accesskeyid}</value>

View File

@ -33,8 +33,10 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.logging.Logger;
import org.jclouds.samples.googleappengine.functions.BlobStoreContextToContainerResult;
import org.jclouds.samples.googleappengine.functions.BlobStoreContextToStatusResult;
import org.jclouds.samples.googleappengine.functions.ComputeServiceContextToStatusResult;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
@ -45,39 +47,48 @@ import com.google.common.collect.Sets;
* @author Adrian Cole
*/
@Singleton
public class GetAllContainersController extends HttpServlet {
public class GetAllStatusController extends HttpServlet {
private static final long serialVersionUID = 1L;
private Map<String, BlobStoreContext> contexts;
private final BlobStoreContextToContainerResult blobStoreContextToContainerResult;
private final Map<String, BlobStoreContext> blobsStoreContexts;
private final Map<String, ComputeServiceContext> computeServiceContexts;
private final BlobStoreContextToStatusResult blobStoreContextToContainerResult;
private final ComputeServiceContextToStatusResult computeServiceContextToContainerResult;
@Resource
protected Logger logger = Logger.NULL;
@Inject
public GetAllContainersController(Map<String, BlobStoreContext> contexts,
BlobStoreContextToContainerResult blobStoreContextToContainerResult) {
this.contexts = contexts;
GetAllStatusController(Map<String, BlobStoreContext> blobsStoreContexts,
Map<String, ComputeServiceContext> computeServiceContexts,
BlobStoreContextToStatusResult blobStoreContextToContainerResult,
ComputeServiceContextToStatusResult computeServiceContextToContainerResult) {
this.blobsStoreContexts = blobsStoreContexts;
this.computeServiceContexts = computeServiceContexts;
this.blobStoreContextToContainerResult = blobStoreContextToContainerResult;
this.computeServiceContextToContainerResult = computeServiceContextToContainerResult;
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
addMyContainersToRequest(request);
addStatusResultsToRequest(request);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(
"/WEB-INF/jsp/containers.jsp");
"/WEB-INF/jsp/status.jsp");
dispatcher.forward(request, response);
} catch (Exception e) {
logger.error(e, "Error listing containers");
logger.error(e, "Error listing status");
throw new ServletException(e);
}
}
private void addMyContainersToRequest(HttpServletRequest request) throws InterruptedException,
private void addStatusResultsToRequest(HttpServletRequest request) throws InterruptedException,
ExecutionException, TimeoutException {
request.setAttribute("containers", Sets.newTreeSet(Iterables.transform(contexts.keySet(),
blobStoreContextToContainerResult)));
request.setAttribute("status", Sets
.newTreeSet(Iterables.concat(Iterables.transform(blobsStoreContexts.keySet(),
blobStoreContextToContainerResult), Iterables.transform(
computeServiceContexts.keySet(), computeServiceContextToContainerResult))));
}
}

View File

@ -18,26 +18,27 @@
*/
package org.jclouds.samples.googleappengine.config;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletContextEvent;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.BlobStoreContextBuilder;
import org.jclouds.blobstore.BlobStoreContextFactory;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.gae.config.GoogleAppEngineConfigurationModule;
import org.jclouds.samples.googleappengine.GetAllContainersController;
import org.jclouds.samples.googleappengine.GetAllStatusController;
import com.google.appengine.repackaged.com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import com.google.appengine.repackaged.com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Closeables;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
@ -49,31 +50,23 @@ import com.google.inject.servlet.ServletModule;
*/
public class GuiceServletConfig extends GuiceServletContextListener {
public static final String PROPERTY_BLOBSTORE_CONTEXTS = "blobstore.contexts";
private Map<String, BlobStoreContext> blobsStoreContexts;
private Map<String, ComputeServiceContext> computeServiceContexts;
private Map<String, BlobStoreContext> contexts;
@SuppressWarnings("unchecked")
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
Properties props = loadJCloudsProperties(servletContextEvent);
ImmutableList<String> list = ImmutableList.<String> of(checkNotNull(
props.getProperty(PROPERTY_BLOBSTORE_CONTEXTS), PROPERTY_BLOBSTORE_CONTEXTS).split(
","));
contexts = Maps.newHashMap();
for (String className : list) {
ImmutableSet<Module> modules = ImmutableSet
.<Module> of(new GoogleAppEngineConfigurationModule());
try {
Class<BlobStoreContextBuilder<?, ?>> builderClass;
builderClass = (Class<BlobStoreContextBuilder<?, ?>>) Class.forName(className);
String name = builderClass.getSimpleName().replaceAll("BlobStoreContextBuilder", "");
Constructor<BlobStoreContextBuilder<?, ?>> constructor = builderClass
.getConstructor(Properties.class);
contexts.put(name, constructor.newInstance(props).withModules(
new GoogleAppEngineConfigurationModule()).buildBlobStoreContext());
} catch (Exception e) {
throw new RuntimeException(e);
}
blobsStoreContexts = ImmutableMap.<String, BlobStoreContext> of("s3",
new BlobStoreContextFactory().createContext("s3", modules, props));
computeServiceContexts = ImmutableMap.<String, ComputeServiceContext> of("ec2",
new ComputeServiceContextFactory().createContext("ec2", modules, props));
} catch (IOException e) {
Throwables.propagate(e);
}
super.contextInitialized(servletContextEvent);
}
@ -97,8 +90,10 @@ public class GuiceServletConfig extends GuiceServletContextListener {
@Override
protected void configureServlets() {
bind(new TypeLiteral<Map<String, BlobStoreContext>>() {
}).toInstance(GuiceServletConfig.this.contexts);
serve("*.blobstore").with(GetAllContainersController.class);
}).toInstance(GuiceServletConfig.this.blobsStoreContexts);
bind(new TypeLiteral<Map<String, ComputeServiceContext>>() {
}).toInstance(GuiceServletConfig.this.computeServiceContexts);
serve("*.check").with(GetAllStatusController.class);
requestInjection(this);
}
}
@ -108,7 +103,10 @@ public class GuiceServletConfig extends GuiceServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
for (BlobStoreContext context : contexts.values()) {
for (BlobStoreContext context : blobsStoreContexts.values()) {
context.close();
}
for (ComputeServiceContext context : computeServiceContexts.values()) {
context.close();
}
super.contextDestroyed(servletContextEvent);

View File

@ -24,7 +24,7 @@ import java.io.Serializable;
*
* @author Adrian Cole
*/
public class ContainerResult implements Comparable<ContainerResult>, Serializable {
public class StatusResult implements Comparable<StatusResult>, Serializable {
/** The serialVersionUID */
private static final long serialVersionUID = -3257496189689220018L;
private final String service;
@ -32,7 +32,7 @@ public class ContainerResult implements Comparable<ContainerResult>, Serializabl
private final String name;
private final String status;
public ContainerResult(String service, String host, String name, String status) {
public StatusResult(String service, String host, String name, String status) {
this.service = service;
this.host = host;
this.name = name;
@ -58,7 +58,7 @@ public class ContainerResult implements Comparable<ContainerResult>, Serializabl
return false;
if (getClass() != obj.getClass())
return false;
ContainerResult other = (ContainerResult) obj;
StatusResult other = (StatusResult) obj;
if (host == null) {
if (other.host != null)
return false;
@ -82,7 +82,7 @@ public class ContainerResult implements Comparable<ContainerResult>, Serializabl
return true;
}
public int compareTo(ContainerResult o) {
public int compareTo(StatusResult o) {
return (this == o) ? 0 : getService().compareTo(o.getService());
}

View File

@ -29,7 +29,7 @@ import org.jclouds.blobstore.ContainerNotFoundException;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.logging.Logger;
import org.jclouds.samples.googleappengine.domain.ContainerResult;
import org.jclouds.samples.googleappengine.domain.StatusResult;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
@ -37,8 +37,8 @@ import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
@Singleton
public class BlobStoreContextToContainerResult implements Function<String, ContainerResult> {
private final class BuildContainerResult implements Function<StorageMetadata, ContainerResult> {
public class BlobStoreContextToStatusResult implements Function<String, StatusResult> {
private final class BuildContainerResult implements Function<StorageMetadata, StatusResult> {
private final String host;
private final BlobStoreContext context;
private final String contextName;
@ -49,7 +49,7 @@ public class BlobStoreContextToContainerResult implements Function<String, Conta
this.contextName = contextName;
}
public ContainerResult apply(StorageMetadata from) {
public StatusResult apply(StorageMetadata from) {
String status;
try {
try {
@ -63,7 +63,7 @@ public class BlobStoreContextToContainerResult implements Function<String, Conta
logger.error(e, "Error listing container %s//%s", contextName, from);
status = (e.getMessage());
}
return new ContainerResult(contextName, host, from.getName(), status);
return new StatusResult(contextName, host, from.getName(), status);
}
}
@ -73,7 +73,7 @@ public class BlobStoreContextToContainerResult implements Function<String, Conta
@Resource
protected Logger logger = Logger.NULL;
public ContainerResult apply(final String contextName) {
public StatusResult apply(final String contextName) {
final BlobStoreContext context = contexts.get(contextName);
final String host = context.getProviderSpecificContext().getEndPoint().getHost();
try {
@ -87,7 +87,7 @@ public class BlobStoreContextToContainerResult implements Function<String, Conta
})));
return new BuildContainerResult(host, context, contextName).apply(md);
} catch (Exception e) {
ContainerResult result = new ContainerResult(contextName, host, null, e.getMessage());
StatusResult result = new StatusResult(contextName, host, null, e.getMessage());
logger.error(e, "Error listing service %s", contextName);
return result;
}

View File

@ -0,0 +1,61 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.jclouds.samples.googleappengine.functions;
import java.util.Map;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.logging.Logger;
import org.jclouds.samples.googleappengine.domain.StatusResult;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
@Singleton
public class ComputeServiceContextToStatusResult implements Function<String, StatusResult> {
@Inject
private Map<String, ComputeServiceContext> contexts;
@Resource
protected Logger logger = Logger.NULL;
public StatusResult apply(final String contextName) {
final ComputeServiceContext context = contexts.get(contextName);
final String host = context.getProviderSpecificContext().getEndPoint().getHost();
String status;
String name = "not found";
try {
long start = System.currentTimeMillis();
Map<String, ? extends ComputeMetadata> nodes = context.getComputeService().getNodes();
if (nodes.size() > 0)
name = Iterables.get(nodes.keySet(), 0);
status = ((System.currentTimeMillis() - start) + "ms");
} catch (Exception e) {
logger.error(e, "Error listing service %s", contextName);
status = (e.getMessage());
}
return new StatusResult(contextName, host, name, status);
}
}

View File

@ -22,14 +22,14 @@
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<html>
<head>
<title>jclouds: anyweight cloudware for java</title>
<title>jclouds: multi-cloud framework</title>
</head>
<body>
<h2>Container List</h2>
<display:table name="containers" >
<h2>Status List</h2>
<display:table name="status" >
<display:column property="service" title="Service" />
<display:column property="host" title="Host" />
<display:column property="name" title="Container" />
<display:column property="name" title="Item" />
<display:column property="status" title="Status" />
</display:table>
</body>

View File

@ -20,12 +20,11 @@
--%>
<html>
<head>
<title>jclouds: anyweight cloudware for java</title>
<title>jclouds: multi-cloud framework</title>
</head>
<body>
<h2>Welcome!</h2>
Click
<a href="/guice/containers.blobstore">here</a>
to list my containers.
<a href="/guice/status.check">here</a> to get status of cloud services.
</body>
</html>

View File

@ -21,23 +21,14 @@ package org.jclouds.samples.googleappengine.functest;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.aws.reference.AWSConstants.PROPERTY_AWS_ACCESSKEYID;
import static org.jclouds.aws.reference.AWSConstants.PROPERTY_AWS_SECRETACCESSKEY;
import static org.jclouds.azure.storage.reference.AzureStorageConstants.PROPERTY_AZURESTORAGE_ACCOUNT;
import static org.jclouds.azure.storage.reference.AzureStorageConstants.PROPERTY_AZURESTORAGE_KEY;
import static org.jclouds.rackspace.reference.RackspaceConstants.PROPERTY_RACKSPACE_KEY;
import static org.jclouds.rackspace.reference.RackspaceConstants.PROPERTY_RACKSPACE_USER;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import org.jclouds.aws.s3.S3ContextBuilder;
import org.jclouds.aws.ec2.EC2PropertiesBuilder;
import org.jclouds.aws.s3.S3PropertiesBuilder;
import org.jclouds.azure.storage.blob.AzureBlobPropertiesBuilder;
import org.jclouds.azure.storage.blob.AzureBlobContextBuilder;
import org.jclouds.rackspace.cloudfiles.CloudFilesContextBuilder;
import org.jclouds.rackspace.cloudfiles.CloudFilesPropertiesBuilder;
import org.jclouds.samples.googleappengine.config.GuiceServletConfig;
import org.jclouds.util.Utils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
@ -62,10 +53,6 @@ public class GoogleAppEngineLiveTest {
url = new URL(String.format("http://%s:%s", address, port));
Properties props = new Properties();
props.setProperty(GuiceServletConfig.PROPERTY_BLOBSTORE_CONTEXTS, String.format("%s,%s,%s",
S3ContextBuilder.class.getName(), CloudFilesContextBuilder.class.getName(),
AzureBlobContextBuilder.class.getName()));
props = new S3PropertiesBuilder(props)
.withCredentials(
checkNotNull(System.getProperty(PROPERTY_AWS_ACCESSKEYID),
@ -73,14 +60,12 @@ public class GoogleAppEngineLiveTest {
System.getProperty(PROPERTY_AWS_SECRETACCESSKEY,
PROPERTY_AWS_SECRETACCESSKEY)).build();
props = new CloudFilesPropertiesBuilder(props).withCredentials(
checkNotNull(System.getProperty(PROPERTY_RACKSPACE_USER), PROPERTY_RACKSPACE_USER),
System.getProperty(PROPERTY_RACKSPACE_KEY, PROPERTY_RACKSPACE_KEY)).build();
props = new AzureBlobPropertiesBuilder(props).withCredentials(
checkNotNull(System.getProperty(PROPERTY_AZURESTORAGE_ACCOUNT),
PROPERTY_AZURESTORAGE_ACCOUNT),
System.getProperty(PROPERTY_AZURESTORAGE_KEY, PROPERTY_AZURESTORAGE_KEY)).build();
props = new EC2PropertiesBuilder(props)
.withCredentials(
checkNotNull(System.getProperty(PROPERTY_AWS_ACCESSKEYID),
PROPERTY_AWS_ACCESSKEYID),
System.getProperty(PROPERTY_AWS_SECRETACCESSKEY,
PROPERTY_AWS_SECRETACCESSKEY)).build();
server = new GoogleDevServer();
server.writePropertiesAndStartServer(address, port, warfile, props);
@ -95,7 +80,7 @@ public class GoogleAppEngineLiveTest {
@Test(invocationCount = 5, enabled = true)
public void testGuiceJCloudsSerial() throws InterruptedException, IOException {
URL gurl = new URL(url, "/guice/containers.blobstore");
URL gurl = new URL(url, "/guice/status.check");
InputStream i = gurl.openStream();
String string = Utils.toStringAndClose(i);
assert string.indexOf("List") >= 0 : string;
@ -103,7 +88,7 @@ public class GoogleAppEngineLiveTest {
@Test(invocationCount = 10, enabled = true, threadPoolSize = 3)
public void testGuiceJCloudsParallel() throws InterruptedException, IOException {
URL gurl = new URL(url, "/guice/containers.blobstore");
URL gurl = new URL(url, "/guice/status.check");
InputStream i = gurl.openStream();
String string = Utils.toStringAndClose(i);
assert string.indexOf("List") >= 0 : string;