Issue 2: updated sample to show usage of s3 api

git-svn-id: http://jclouds.googlecode.com/svn/trunk@66 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
ferncam1 2009-04-28 18:11:06 +00:00
parent 86050935a1
commit 090a909be3
6 changed files with 208 additions and 236 deletions

View File

@ -44,6 +44,8 @@
<appengine.home>/Users/adriancole/Desktop/appengine-java-sdk-1.2.0</appengine.home> <appengine.home>/Users/adriancole/Desktop/appengine-java-sdk-1.2.0</appengine.home>
<devappserver.address>localhost</devappserver.address> <devappserver.address>localhost</devappserver.address>
<devappserver.port>8088</devappserver.port> <devappserver.port>8088</devappserver.port>
<jclouds.aws.accesskeyid></jclouds.aws.accesskeyid>
<jclouds.aws.secretaccesskey></jclouds.aws.secretaccesskey>
</properties> </properties>
<repositories> <repositories>
@ -63,7 +65,7 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>jclouds-core</artifactId> <artifactId>jclouds-s3</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
@ -143,6 +145,14 @@
<configuration> <configuration>
<testClassesDirectory>target/test-classes</testClassesDirectory> <testClassesDirectory>target/test-classes</testClassesDirectory>
<systemProperties> <systemProperties>
<property>
<name>jclouds.aws.accesskeyid</name>
<value>${jclouds.aws.accesskeyid}</value>
</property>
<property>
<name>jclouds.aws.secretaccesskey</name>
<value>${jclouds.aws.secretaccesskey}</value>
</property>
<property> <property>
<name>appengine.home</name> <name>appengine.home</name>
<value>${appengine.home}</value> <value>${appengine.home}</value>

View File

@ -0,0 +1,74 @@
/**
*
* Copyright (C) 2009 Adrian Cole <adriancole@jclouds.org>
*
* ====================================================================
* 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.jclouds.samples.googleappengine.functest;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import org.testng.annotations.AfterTest;
import com.google.appengine.tools.KickStart;
/**
* Basic functionality to start a local google app engine instance.
*
* @author Adrian Cole
*
*/
public abstract class BaseGoogleAppEngineTest {
Thread server;
URL url;
protected void writePropertiesAndStartServer(final String address,
final String port, final String warfile, Properties props)
throws IOException, FileNotFoundException, InterruptedException {
url = new URL(String.format("http://%1s:%2s", address, port));
props.store(new FileOutputStream(String.format(
"%1s/WEB-INF/jclouds.properties", warfile)), "test");
this.server = new Thread(new Runnable() {
public void run() {
KickStart
.main(new String[] {
"com.google.appengine.tools.development.DevAppServerMain",
"--disable_update_check", "-a", address, "-p",
port, warfile });
}
});
server.start();
Thread.sleep(7 * 1000);
}
@AfterTest
public void stopDevAppServer() throws Exception {
server.stop();
}
}

View File

@ -23,55 +23,52 @@
*/ */
package org.jclouds.samples.googleappengine.functest; package org.jclouds.samples.googleappengine.functest;
import java.io.FileOutputStream; import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.Properties; import java.util.Properties;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters; import org.testng.annotations.Parameters;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.appengine.tools.KickStart; /**
* Starts up the Google App Engine for Java Development environment and deploys
* an application which tests S3.
*
* @author Adrian Cole
*
*/
@Test(groups = "integration", enabled = true, sequential = true, testName = "functionalTests") @Test(groups = "integration", enabled = true, sequential = true, testName = "functionalTests")
public class GoogleAppEngineTest { public class GoogleAppEngineTest extends BaseGoogleAppEngineTest {
Thread server; private static final String sysAWSAccessKeyId = System
URL url; .getProperty("jclouds.aws.accesskeyid");
private static final String sysAWSSecretAccessKey = System
.getProperty("jclouds.aws.secretaccesskey");
@BeforeTest @BeforeTest
@Parameters( { "warfile", "devappserver.address", "devappserver.port" }) @Parameters( { "warfile", "devappserver.address", "devappserver.port",
"jclouds.aws.accesskeyid", "jclouds.aws.secretaccesskey" })
public void startDevAppServer(final String warfile, final String address, public void startDevAppServer(final String warfile, final String address,
final String port) throws Exception { final String port, @Optional String AWSAccessKeyId,
url = new URL(String.format("http://%1s:%2s", address, port)); @Optional String AWSSecretAccessKey) throws Exception {
AWSAccessKeyId = AWSAccessKeyId != null ? AWSAccessKeyId
: sysAWSAccessKeyId;
AWSSecretAccessKey = AWSSecretAccessKey != null ? AWSSecretAccessKey
: sysAWSSecretAccessKey;
checkNotNull(AWSAccessKeyId, "AWSAccessKeyId");
checkNotNull(AWSSecretAccessKey, "AWSSecretAccessKey");
Properties props = new Properties(); Properties props = new Properties();
props.put("jclouds.http.address", address); props.put("jclouds.aws.accesskeyid", AWSAccessKeyId);
props.put("jclouds.http.port", port + ""); props.put("jclouds.aws.secretaccesskey", AWSSecretAccessKey);
props.put("jclouds.http.secure", "false"); writePropertiesAndStartServer(address, port, warfile, props);
props.store(new FileOutputStream(String.format(
"%1s/WEB-INF/jclouds.properties", warfile)), "test");
this.server = new Thread(new Runnable() {
public void run() {
KickStart
.main(new String[] {
"com.google.appengine.tools.development.DevAppServerMain",
"--disable_update_check", "-a", address, "-p",
port, warfile });
}
});
server.start();
Thread.sleep(7 * 1000);
}
@AfterTest
public void stopDevAppServer() throws Exception {
server.stop();
} }
@Test @Test
@ -81,22 +78,12 @@ public class GoogleAppEngineTest {
assert string.indexOf("Hello World!") >= 0 : string; assert string.indexOf("Hello World!") >= 0 : string;
} }
@Test(invocationCount = 50, enabled = true, threadPoolSize = 10)
public void testGuiceUrlServed() throws InterruptedException, IOException {
Thread.sleep(10000);
URL gurl = new URL(url, "/guice/fetch.url?uri=" + url.toExternalForm());
InputStream i = gurl.openStream();
String string = IOUtils.toString(i);
assert string.indexOf("Hello World!") >= 0 : string;
}
@Test(invocationCount = 50, enabled = true, threadPoolSize = 10) @Test(invocationCount = 50, enabled = true, threadPoolSize = 10)
public void testGuiceJCloudsServed() throws InterruptedException, public void testGuiceJCloudsServed() throws InterruptedException,
IOException { IOException {
Thread.sleep(10000); URL gurl = new URL(url, "/guice/listbuckets.s3");
URL gurl = new URL(url, "/guice/fetch.jclouds?uri=/");
InputStream i = gurl.openStream(); InputStream i = gurl.openStream();
String string = IOUtils.toString(i); String string = IOUtils.toString(i);
assert string.indexOf("Hello World!") >= 0 : string; assert string.indexOf("List") >= 0 : string;
} }
} }

View File

@ -1,77 +0,0 @@
/**
*
* Copyright (C) 2009 Adrian Cole <adriancole@jclouds.org>
*
* ====================================================================
* 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.jclouds.samples.googleappengine;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.Provider;
import org.apache.commons.io.IOUtils;
import org.jclouds.http.HttpFutureCommandClient;
import org.jclouds.http.commands.CommandFactory;
import org.jclouds.http.commands.GetString;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.net.URL;
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
/**
* // TODO: Adrian: Document this!
*
* @author Adrian Cole
*/
@Singleton
public class GetServlet extends HttpServlet {
@Inject
HttpFutureCommandClient client;
@Inject
CommandFactory factory;
@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
URL url = new URL(httpServletRequest.getParameter("uri"));
Writer writer = httpServletResponse.getWriter();
InputStream in = null;
try {
in = url.openStream();
writer.write(IOUtils.toString(in));
} catch (Exception e) {
throw new ServletException(e);
} finally {
IOUtils.closeQuietly(in);
}
writer.flush();
writer.close();
}
}

View File

@ -23,48 +23,51 @@
*/ */
package org.jclouds.samples.googleappengine; package org.jclouds.samples.googleappengine;
import com.google.inject.Inject; import java.io.IOException;
import com.google.inject.Singleton; import java.io.Writer;
import com.google.inject.Provider; import java.util.List;
import org.jclouds.http.HttpFutureCommandClient; import java.util.concurrent.TimeUnit;
import org.jclouds.http.commands.CommandFactory;
import org.jclouds.http.commands.GetString;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer; import org.jclouds.aws.s3.S3Connection;
import java.util.List; import org.jclouds.aws.s3.domain.S3Bucket;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.ExecutorService; import com.google.inject.Inject;
import java.util.concurrent.TimeUnit; import com.google.inject.Singleton;
/** /**
* // TODO: Adrian: Document this! * Shows an example of how to use @{link S3Connection} injected with Guice.
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@Singleton @Singleton
public class JCloudsServlet extends HttpServlet { public class JCloudsServlet extends HttpServlet {
@Inject @Inject
HttpFutureCommandClient client; S3Connection client;
@Inject
CommandFactory factory;
@Override @Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { protected void doGet(HttpServletRequest httpServletRequest,
GetString get = factory.createGetString(httpServletRequest.getParameter("uri")); HttpServletResponse httpServletResponse) throws ServletException,
client.submit(get); IOException {
Writer writer = httpServletResponse.getWriter(); Writer writer = httpServletResponse.getWriter();
try { try {
writer.write(get.get().trim()); List<S3Bucket> myBuckets = client.getBuckets().get(10,
} catch (Exception e) { TimeUnit.SECONDS);
throw new ServletException(e); writer.write("List:\n");
} for (S3Bucket bucket : myBuckets) {
writer.flush(); writer
writer.close(); .write(String.format(" Name: %1s%n", bucket
.getName()));
}
} catch (Exception e) {
throw new ServletException(e);
}
writer.flush();
writer.close();
} }
} }

View File

@ -21,114 +21,89 @@
* under the License. * under the License.
* ==================================================================== * ====================================================================
*/ */
package org.jclouds.samples.googleappengine.config; /** package org.jclouds.samples.googleappengine.config;
* // TODO: Adrian: Document this!
* @author Adrian Cole
*/
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import org.apache.commons.io.IOUtils;
import org.jclouds.aws.s3.S3ConnectionFactory;
import org.jclouds.aws.s3.S3ConnectionModule;
import org.jclouds.http.config.JavaUrlHttpFutureCommandClientModule;
import org.jclouds.lifecycle.Closer;
import org.jclouds.samples.googleappengine.JCloudsServlet;
import com.google.inject.AbstractModule;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.name.Names; import com.google.inject.name.Names;
import com.google.inject.servlet.GuiceServletContextListener; import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule; import com.google.inject.servlet.ServletModule;
import org.apache.commons.io.IOUtils;
import org.jclouds.http.commands.config.HttpCommandsModule;
import org.jclouds.http.config.JavaUrlHttpFutureCommandClientModule;
import org.jclouds.lifecycle.Closer;
import org.jclouds.samples.googleappengine.GetServlet;
import org.jclouds.samples.googleappengine.JCloudsServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
/**
* Setup Logging and create Injector for use in testing S3.
*
* @author Adrian Cole
*
*/
public class GuiceServletConfig extends GuiceServletContextListener { public class GuiceServletConfig extends GuiceServletContextListener {
@Inject @Inject
Closer closer; Closer closer;
ServletContext context; ServletContext context;
private static final Handler HANDLER = new ConsoleHandler() {
{
setLevel(Level.ALL);
setFormatter(new Formatter() {
@Override
public String format(LogRecord record) {
return String.format("[%tT %-7s] [%-7s] [%s]: %s %s\n",
new Date(record.getMillis()), record.getLevel(),
Thread.currentThread().getName(), record
.getLoggerName(), record.getMessage(),
record.getThrown() == null ? "" : record
.getThrown());
}
});
}
};
static {
Logger guiceLogger = Logger.getLogger("org.jclouds");
guiceLogger.addHandler(HANDLER);
guiceLogger.setLevel(Level.ALL);
}
@Override @Override
public void contextInitialized(ServletContextEvent servletContextEvent) { public void contextInitialized(ServletContextEvent servletContextEvent) {
this.context = servletContextEvent.getServletContext(); this.context = servletContextEvent.getServletContext();
super.contextInitialized(servletContextEvent); // TODO: Adrian: Customise this generated block super.contextInitialized(servletContextEvent);
} }
@Override @Override
protected Injector getInjector() { protected Injector getInjector() {
return Guice.createInjector( return Guice.createInjector(
new HttpCommandsModule() { new AbstractModule() {
@Override @Override
protected void configure() { protected void configure() {
super.configure(); Properties props = new Properties();
Properties props = new Properties(); InputStream input = null;
InputStream input = null; try {
try { input = context
input = context.getResourceAsStream("/WEB-INF/jclouds.properties"); .getResourceAsStream("/WEB-INF/jclouds.properties");
if (input != null) if (input != null)
props.load(input); props.load(input);
else else
throw new RuntimeException("not found in classloader"); throw new RuntimeException(
} catch (IOException e) { "not found in classloader");
throw new RuntimeException(e); } catch (IOException e) {
} finally { throw new RuntimeException(e);
IOUtils.closeQuietly(input); } finally {
} IOUtils.closeQuietly(input);
Names.bindProperties(binder(), props); }
install(new JavaUrlHttpFutureCommandClientModule()); props.putAll(S3ConnectionFactory.DEFAULT_PROPERTIES);
} Names.bindProperties(binder(), props);
} }
, new ServletModule() { }, new JavaUrlHttpFutureCommandClientModule(),
@Override new S3ConnectionModule(), new ServletModule() {
protected void configureServlets() { @Override
serve("*.jclouds").with(JCloudsServlet.class); protected void configureServlets() {
serve("*.url").with(GetServlet.class); serve("*.s3").with(JCloudsServlet.class);
requestInjection(this); requestInjection(this);
} }
}); });
} }
@Override @Override
public void contextDestroyed(ServletContextEvent servletContextEvent) { public void contextDestroyed(ServletContextEvent servletContextEvent) {
try { try {
closer.close(); closer.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); // TODO: Adrian: Customise this generated block e.printStackTrace();
} }
super.contextDestroyed(servletContextEvent); // TODO: Adrian: Customise this generated block super.contextDestroyed(servletContextEvent);
} }
} }