Issue 474: updated example to use gae sdk 1.4.0

This commit is contained in:
Adrian Cole 2011-02-15 15:35:30 +01:00
parent 19981da7c7
commit e0c7e1c0b3
7 changed files with 103 additions and 145 deletions

View File

@ -22,34 +22,56 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.jclouds</groupId> <parent>
<groupId>org.jclouds</groupId>
<artifactId>jclouds-project</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../project/pom.xml</relativePath>
</parent>
<groupId>org.jclouds.examples</groupId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<artifactId>googleappengine</artifactId> <artifactId>jclouds-googleappengine-example</artifactId>
<packaging>war</packaging> <packaging>war</packaging>
<name>JClouds Sample for Google App Engine</name> <name>JClouds Sample for Google App Engine</name>
<description>JClouds Sample for Google App Engine</description> <description>JClouds Sample for Google App Engine</description>
<properties> <properties>
<!--
note you must set the property ${appengine.home} to a valid
extraction of appengine-java-sdk
-->
<appengine.home>YOUR_APPENGINE_HOME</appengine.home>
<appengine.applicationid>aws-common-demo</appengine.applicationid> <appengine.applicationid>aws-common-demo</appengine.applicationid>
<devappserver.address>localhost</devappserver.address> <devappserver.address>localhost</devappserver.address>
<devappserver.port>8088</devappserver.port> <devappserver.port>8088</devappserver.port>
<jclouds.test.listener></jclouds.test.listener>
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.jclouds</groupId>
<artifactId>jclouds-blobstore</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jclouds</groupId>
<artifactId>jclouds-compute</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jclouds.provider</groupId>
<artifactId>aws-s3</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jclouds.provider</groupId>
<artifactId>aws-ec2</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.jclouds.driver</groupId> <groupId>org.jclouds.driver</groupId>
<artifactId>jclouds-gae</artifactId> <artifactId>jclouds-gae</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.code.guice</groupId> <groupId>com.google.inject.extensions</groupId>
<artifactId>guice-servlet</artifactId> <artifactId>guice-servlet</artifactId>
<version>3.0-snapshot-20101120</version> <version>3.0-rc2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>displaytag</groupId> <groupId>displaytag</groupId>
@ -97,10 +119,10 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.appengine</groupId> <groupId>com.google.appengine</groupId>
<artifactId>appengine-tools-api</artifactId> <artifactId>appengine-tools-sdk</artifactId>
<version>1.3.5</version> <version>1.4.0</version>
<scope>system</scope> <scope>system</scope>
<systemPath>${appengine.home}/lib/appengine-tools-api.jar</systemPath> <systemPath>${env.APPENGINE_HOME}/lib/appengine-tools-api.jar</systemPath>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
@ -131,10 +153,6 @@
</goals> </goals>
<configuration> <configuration>
<systemProperties> <systemProperties>
<property>
<name>appengine.home</name>
<value>${appengine.home}</value>
</property>
<property> <property>
<name>devappserver.address</name> <name>devappserver.address</name>
<value>${devappserver.address}</value> <value>${devappserver.address}</value>
@ -149,14 +167,13 @@
</property> </property>
</systemProperties> </systemProperties>
<additionalClasspathElements> <additionalClasspathElements>
<additionalClasspathElement>${appengine.home}/lib/appengine-tools-api.jar <additionalClasspathElement>${env.APPENGINE_HOME}/lib/appengine-tools-api.jar</additionalClasspathElement>
</additionalClasspathElement>
</additionalClasspathElements> </additionalClasspathElements>
<environmentVariables> <environmentVariables>
<DEBUG>true</DEBUG> <DEBUG>true</DEBUG>
<SDK_BIN>${appengine.home}/bin</SDK_BIN> <SDK_BIN>${env.APPENGINE_HOME}/bin</SDK_BIN>
<SDK_LIB>${appengine.home}/lib</SDK_LIB> <SDK_LIB>${env.APPENGINE_HOME}/lib</SDK_LIB>
<SDK_CONFIG>${appengine.home}/config/sdk</SDK_CONFIG> <SDK_CONFIG>${env.APPENGINE_HOME}/config/sdk</SDK_CONFIG>
</environmentVariables> </environmentVariables>
</configuration> </configuration>
</execution> </execution>
@ -183,16 +200,16 @@
<configuration> <configuration>
<systemProperties> <systemProperties>
<property> <property>
<name>jclouds.test.identity</name> <name>test.aws.identity</name>
<value>${test.aws.identity}</value> <value>${test.aws.identity}</value>
</property> </property>
<property> <property>
<name>jclouds.test.credential</name> <name>test.aws.credential</name>
<value>${test.aws.credential}</value> <value>${test.aws.credential}</value>
</property> </property>
<property> <property>
<name>appengine.home</name> <name>appengine.sdk.root</name>
<value>${appengine.home}</value> <value>${env.APPENGINE_HOME}</value>
</property> </property>
<property> <property>
<name>devappserver.address</name> <name>devappserver.address</name>

View File

@ -20,7 +20,6 @@
package org.jclouds.samples.googleappengine; package org.jclouds.samples.googleappengine;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
@ -39,11 +38,12 @@ import org.jclouds.logging.Logger;
import org.jclouds.samples.googleappengine.functions.BlobStoreContextToStatusResult; import org.jclouds.samples.googleappengine.functions.BlobStoreContextToStatusResult;
import org.jclouds.samples.googleappengine.functions.ComputeServiceContextToStatusResult; import org.jclouds.samples.googleappengine.functions.ComputeServiceContextToStatusResult;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
/** /**
* Shows an example of how to use @{link BlobStoreContext} injected with Guice. * Shows an example of how to use {@link BlobStoreContext} and {@link ComputeServiceContext}
* injected with Guice.
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@ -51,8 +51,8 @@ import com.google.common.collect.Sets;
public class GetAllStatusController extends HttpServlet { public class GetAllStatusController extends HttpServlet {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final Map<String, BlobStoreContext> blobsStoreContexts; private final Iterable<BlobStoreContext> blobsStoreContexts;
private final Map<String, ComputeServiceContext> computeServiceContexts; private final Iterable<ComputeServiceContext> computeServiceContexts;
private final BlobStoreContextToStatusResult blobStoreContextToContainerResult; private final BlobStoreContextToStatusResult blobStoreContextToContainerResult;
private final ComputeServiceContextToStatusResult computeServiceContextToContainerResult; private final ComputeServiceContextToStatusResult computeServiceContextToContainerResult;
@ -60,10 +60,10 @@ public class GetAllStatusController extends HttpServlet {
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
@Inject @Inject
GetAllStatusController(Map<String, BlobStoreContext> blobsStoreContexts, GetAllStatusController(Iterable<BlobStoreContext> blobsStoreContexts,
Map<String, ComputeServiceContext> computeServiceContexts, Iterable<ComputeServiceContext> computeServiceContexts,
BlobStoreContextToStatusResult blobStoreContextToContainerResult, BlobStoreContextToStatusResult blobStoreContextToContainerResult,
ComputeServiceContextToStatusResult computeServiceContextToContainerResult) { ComputeServiceContextToStatusResult computeServiceContextToContainerResult) {
this.blobsStoreContexts = blobsStoreContexts; this.blobsStoreContexts = blobsStoreContexts;
this.computeServiceContexts = computeServiceContexts; this.computeServiceContexts = computeServiceContexts;
this.blobStoreContextToContainerResult = blobStoreContextToContainerResult; this.blobStoreContextToContainerResult = blobStoreContextToContainerResult;
@ -71,12 +71,10 @@ public class GetAllStatusController extends HttpServlet {
} }
@Override @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
throws ServletException, IOException {
try { try {
addStatusResultsToRequest(request); addStatusResultsToRequest(request);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsp/status.jsp");
"/WEB-INF/jsp/status.jsp");
dispatcher.forward(request, response); dispatcher.forward(request, response);
} catch (Exception e) { } catch (Exception e) {
logger.error(e, "Error listing status"); logger.error(e, "Error listing status");
@ -84,12 +82,13 @@ public class GetAllStatusController extends HttpServlet {
} }
} }
private void addStatusResultsToRequest(HttpServletRequest request) throws InterruptedException, private void addStatusResultsToRequest(HttpServletRequest request) throws InterruptedException, ExecutionException,
ExecutionException, TimeoutException { TimeoutException {
request.setAttribute("status", Sets request.setAttribute(
.newTreeSet(Iterables.concat(Iterables.transform(blobsStoreContexts.keySet(), "status",
blobStoreContextToContainerResult), Iterables.transform( ImmutableSet.copyOf(Iterables.concat(
computeServiceContexts.keySet(), computeServiceContextToContainerResult)))); Iterables.transform(blobsStoreContexts, blobStoreContextToContainerResult),
Iterables.transform(computeServiceContexts, computeServiceContextToContainerResult))));
} }
} }

View File

@ -26,7 +26,6 @@ import static org.jclouds.compute.reference.ComputeServiceConstants.PROPERTY_TIM
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
@ -38,7 +37,6 @@ import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.gae.config.GoogleAppEngineConfigurationModule; import org.jclouds.gae.config.GoogleAppEngineConfigurationModule;
import org.jclouds.samples.googleappengine.GetAllStatusController; import org.jclouds.samples.googleappengine.GetAllStatusController;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
import com.google.inject.Guice; import com.google.inject.Guice;
@ -49,14 +47,14 @@ import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule; import com.google.inject.servlet.ServletModule;
/** /**
* Setup Logging and create Injector for use in testing S3. * Setup Logging and create {@link Injector} for use in testing Amazon EC2 and S3.
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
public class GuiceServletConfig extends GuiceServletContextListener { public class GuiceServletConfig extends GuiceServletContextListener {
private Map<String, BlobStoreContext> blobsStoreContexts; private Iterable<BlobStoreContext> blobsStoreContexts;
private Map<String, ComputeServiceContext> computeServiceContexts; private Iterable<ComputeServiceContext> computeServiceContexts;
@Override @Override
public void contextInitialized(ServletContextEvent servletContextEvent) { public void contextInitialized(ServletContextEvent servletContextEvent) {
@ -66,20 +64,18 @@ public class GuiceServletConfig extends GuiceServletContextListener {
props.setProperty(PROPERTY_TIMEOUT_SCRIPT_COMPLETE, "25000"); props.setProperty(PROPERTY_TIMEOUT_SCRIPT_COMPLETE, "25000");
props.setProperty(PROPERTY_TIMEOUT_PORT_OPEN, "25000"); props.setProperty(PROPERTY_TIMEOUT_PORT_OPEN, "25000");
ImmutableSet<Module> modules = ImmutableSet ImmutableSet<Module> modules = ImmutableSet.<Module> of(new GoogleAppEngineConfigurationModule());
.<Module> of(new GoogleAppEngineConfigurationModule());
blobsStoreContexts = ImmutableMap.<String, BlobStoreContext> of("s3", blobsStoreContexts = ImmutableSet.<BlobStoreContext> of(new BlobStoreContextFactory().createContext("aws-s3",
new BlobStoreContextFactory().createContext("s3", modules, props)); modules, props));
computeServiceContexts = ImmutableMap.<String, ComputeServiceContext> of("ec2", computeServiceContexts = ImmutableSet.<ComputeServiceContext> of(new ComputeServiceContextFactory()
new ComputeServiceContextFactory().createContext("ec2", modules, props)); .createContext("aws-ec2", modules, props));
super.contextInitialized(servletContextEvent); super.contextInitialized(servletContextEvent);
} }
private Properties loadJCloudsProperties(ServletContextEvent servletContextEvent) { private Properties loadJCloudsProperties(ServletContextEvent servletContextEvent) {
InputStream input = servletContextEvent.getServletContext().getResourceAsStream( InputStream input = servletContextEvent.getServletContext().getResourceAsStream("/WEB-INF/jclouds.properties");
"/WEB-INF/jclouds.properties");
Properties props = new Properties(); Properties props = new Properties();
try { try {
props.load(input); props.load(input);
@ -96,9 +92,9 @@ public class GuiceServletConfig extends GuiceServletContextListener {
return Guice.createInjector(new ServletModule() { return Guice.createInjector(new ServletModule() {
@Override @Override
protected void configureServlets() { protected void configureServlets() {
bind(new TypeLiteral<Map<String, BlobStoreContext>>() { bind(new TypeLiteral<Iterable<BlobStoreContext>>() {
}).toInstance(GuiceServletConfig.this.blobsStoreContexts); }).toInstance(GuiceServletConfig.this.blobsStoreContexts);
bind(new TypeLiteral<Map<String, ComputeServiceContext>>() { bind(new TypeLiteral<Iterable<ComputeServiceContext>>() {
}).toInstance(GuiceServletConfig.this.computeServiceContexts); }).toInstance(GuiceServletConfig.this.computeServiceContexts);
serve("*.check").with(GetAllStatusController.class); serve("*.check").with(GetAllStatusController.class);
requestInjection(this); requestInjection(this);
@ -110,10 +106,10 @@ public class GuiceServletConfig extends GuiceServletContextListener {
@Override @Override
public void contextDestroyed(ServletContextEvent servletContextEvent) { public void contextDestroyed(ServletContextEvent servletContextEvent) {
for (BlobStoreContext context : blobsStoreContexts.values()) { for (BlobStoreContext context : blobsStoreContexts) {
context.close(); context.close();
} }
for (ComputeServiceContext context : computeServiceContexts.values()) { for (ComputeServiceContext context : computeServiceContexts) {
context.close(); context.close();
} }
super.contextDestroyed(servletContextEvent); super.contextDestroyed(servletContextEvent);

View File

@ -19,79 +19,39 @@
package org.jclouds.samples.googleappengine.functions; package org.jclouds.samples.googleappengine.functions;
import java.util.Map;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.BlobStoreContext;
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.logging.Logger;
import org.jclouds.samples.googleappengine.domain.StatusResult; import org.jclouds.samples.googleappengine.domain.StatusResult;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
/**
*
* @author Adrian Cole
*/
@Singleton @Singleton
public class BlobStoreContextToStatusResult implements Function<String, StatusResult> { public class BlobStoreContextToStatusResult implements Function<BlobStoreContext, StatusResult> {
private final class BuildContainerResult implements Function<StorageMetadata, StatusResult> {
private final String host;
private final BlobStoreContext context;
private final String contextName;
private BuildContainerResult(String host, BlobStoreContext context, String contextName) {
this.host = host;
this.context = context;
this.contextName = contextName;
}
public StatusResult apply(StorageMetadata from) {
String status;
try {
try {
long start = System.currentTimeMillis();
context.getBlobStore().containerExists(from.getName());
status = ((System.currentTimeMillis() - start) + "ms");
} catch (ContainerNotFoundException ex) {
status = ("not found");
}
} catch (Exception e) {
logger.error(e, "Error listing container %s//%s", contextName, from);
status = (e.getMessage());
}
return new StatusResult(contextName, host, from.getName(), status);
}
}
@Inject
private Map<String, BlobStoreContext> contexts;
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
public StatusResult apply(final String contextName) { public StatusResult apply(BlobStoreContext in) {
final BlobStoreContext context = contexts.get(contextName); String host = in.getProviderSpecificContext().getEndpoint().getHost();
final String host = context.getProviderSpecificContext().getEndpoint().getHost(); String status;
String name = "not found";
try { try {
StorageMetadata md = Iterables.getLast(Sets.newTreeSet(Iterables.filter(context long start = System.currentTimeMillis();
.getBlobStore().list(), new Predicate<StorageMetadata>() {
public boolean apply(StorageMetadata input) { name = String.format("%d containers", in.getBlobStore().list().size());
return input.getType() == StorageType.CONTAINER;
}
}))); status = ((System.currentTimeMillis() - start) + "ms");
return new BuildContainerResult(host, context, contextName).apply(md);
} catch (Exception e) { } catch (Exception e) {
StatusResult result = new StatusResult(contextName, host, null, e.getMessage()); logger.error(e, "Error listing context %s", in);
logger.error(e, "Error listing service %s", contextName); status = (e.getMessage());
return result;
} }
return new StatusResult(in.getProviderSpecificContext().getId(), host, name, status);
} }
} }

View File

@ -19,52 +19,39 @@
package org.jclouds.samples.googleappengine.functions; package org.jclouds.samples.googleappengine.functions;
import java.util.Map;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.samples.googleappengine.domain.StatusResult; import org.jclouds.samples.googleappengine.domain.StatusResult;
import com.google.common.base.Function; import com.google.common.base.Function;
/**
*
* @author Adrian Cole
*/
@Singleton @Singleton
public class ComputeServiceContextToStatusResult implements public class ComputeServiceContextToStatusResult implements Function<ComputeServiceContext, StatusResult> {
Function<String, StatusResult> {
@Inject
private Map<String, ComputeServiceContext> contexts;
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
public StatusResult apply(final String contextName) { public StatusResult apply(ComputeServiceContext in) {
final ComputeServiceContext context = contexts.get(contextName); String host = in.getProviderSpecificContext().getEndpoint().getHost();
final String host = context.getProviderSpecificContext().getEndpoint()
.getHost();
String status; String status;
String name = "not found"; String name = "not found";
try { try {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
// set options that don't block so that we can avoid
// DeadLineExceededExceptions
TemplateOptions options = context.getComputeService()
.templateOptions().blockUntilRunning(false);
// set the name to the default template to show that name = String.format("%d nodes", in.getComputeService().listNodes().size());
// it works
name = context.getComputeService().templateBuilder().options(options)
.build().toString();
status = ((System.currentTimeMillis() - start) + "ms"); status = ((System.currentTimeMillis() - start) + "ms");
} catch (Exception e) { } catch (Exception e) {
logger.error(e, "Error listing service %s", contextName); logger.error(e, "Error listing context %s", in);
status = (e.getMessage()); status = (e.getMessage());
} }
return new StatusResult(contextName, host, name, status); return new StatusResult(in.getProviderSpecificContext().getId(), host, name, status);
} }
} }

View File

@ -33,7 +33,7 @@ import org.testng.annotations.Test;
/** /**
* Starts up the Google App Engine for Java Development environment and deploys an application which * Starts up the Google App Engine for Java Development environment and deploys an application which
* tests S3. * tests Amazon EC2 and S3.
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@ -49,10 +49,10 @@ public class GoogleAppEngineLiveTest {
throws Exception { throws Exception {
url = new URL(String.format("http://%s:%s", address, port)); url = new URL(String.format("http://%s:%s", address, port));
Properties props = new Properties(); Properties props = new Properties();
String identity = checkNotNull(System.getProperty("jclouds.test.identity"), String identity = checkNotNull(System.getProperty("test.aws.identity"),
"jclouds.test.identity"); "test.aws.identity");
String credential = checkNotNull(System.getProperty("jclouds.test.credential"), String credential = checkNotNull(System.getProperty("test.aws.credential"),
"jclouds.test.credential"); "test.aws.credential");
/** /**
* Since both s3 and ec2 use the same credentials, we can take a shortcut and specify both * Since both s3 and ec2 use the same credentials, we can take a shortcut and specify both

View File

@ -53,7 +53,6 @@
<groupId>com.google.appengine</groupId> <groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId> <artifactId>appengine-api-1.0-sdk</artifactId>
<version>1.4.0</version> <version>1.4.0</version>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>