mirror of https://github.com/apache/jclouds.git
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:
parent
86050935a1
commit
090a909be3
|
@ -44,6 +44,8 @@
|
|||
<appengine.home>/Users/adriancole/Desktop/appengine-java-sdk-1.2.0</appengine.home>
|
||||
<devappserver.address>localhost</devappserver.address>
|
||||
<devappserver.port>8088</devappserver.port>
|
||||
<jclouds.aws.accesskeyid></jclouds.aws.accesskeyid>
|
||||
<jclouds.aws.secretaccesskey></jclouds.aws.secretaccesskey>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
|
@ -63,7 +65,7 @@
|
|||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>jclouds-core</artifactId>
|
||||
<artifactId>jclouds-s3</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -143,6 +145,14 @@
|
|||
<configuration>
|
||||
<testClassesDirectory>target/test-classes</testClassesDirectory>
|
||||
<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>
|
||||
<name>appengine.home</name>
|
||||
<value>${appengine.home}</value>
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -23,55 +23,52 @@
|
|||
*/
|
||||
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.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.testng.annotations.AfterTest;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Optional;
|
||||
import org.testng.annotations.Parameters;
|
||||
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")
|
||||
public class GoogleAppEngineTest {
|
||||
public class GoogleAppEngineTest extends BaseGoogleAppEngineTest {
|
||||
|
||||
Thread server;
|
||||
URL url;
|
||||
private static final String sysAWSAccessKeyId = System
|
||||
.getProperty("jclouds.aws.accesskeyid");
|
||||
private static final String sysAWSSecretAccessKey = System
|
||||
.getProperty("jclouds.aws.secretaccesskey");
|
||||
|
||||
@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,
|
||||
final String port) throws Exception {
|
||||
url = new URL(String.format("http://%1s:%2s", address, port));
|
||||
final String port, @Optional String AWSAccessKeyId,
|
||||
@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();
|
||||
props.put("jclouds.http.address", address);
|
||||
props.put("jclouds.http.port", port + "");
|
||||
props.put("jclouds.http.secure", "false");
|
||||
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();
|
||||
props.put("jclouds.aws.accesskeyid", AWSAccessKeyId);
|
||||
props.put("jclouds.aws.secretaccesskey", AWSSecretAccessKey);
|
||||
writePropertiesAndStartServer(address, port, warfile, props);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -81,22 +78,12 @@ public class GoogleAppEngineTest {
|
|||
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)
|
||||
public void testGuiceJCloudsServed() throws InterruptedException,
|
||||
IOException {
|
||||
Thread.sleep(10000);
|
||||
URL gurl = new URL(url, "/guice/fetch.jclouds?uri=/");
|
||||
URL gurl = new URL(url, "/guice/listbuckets.s3");
|
||||
InputStream i = gurl.openStream();
|
||||
String string = IOUtils.toString(i);
|
||||
assert string.indexOf("Hello World!") >= 0 : string;
|
||||
assert string.indexOf("List") >= 0 : string;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -23,48 +23,51 @@
|
|||
*/
|
||||
package org.jclouds.samples.googleappengine;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import com.google.inject.Provider;
|
||||
import org.jclouds.http.HttpFutureCommandClient;
|
||||
import org.jclouds.http.commands.CommandFactory;
|
||||
import org.jclouds.http.commands.GetString;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
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.Writer;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.AbstractExecutorService;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.jclouds.aws.s3.S3Connection;
|
||||
import org.jclouds.aws.s3.domain.S3Bucket;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
/**
|
||||
* // TODO: Adrian: Document this!
|
||||
* Shows an example of how to use @{link S3Connection} injected with Guice.
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
@Singleton
|
||||
public class JCloudsServlet extends HttpServlet {
|
||||
@Inject
|
||||
HttpFutureCommandClient client;
|
||||
@Inject
|
||||
CommandFactory factory;
|
||||
S3Connection client;
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
|
||||
GetString get = factory.createGetString(httpServletRequest.getParameter("uri"));
|
||||
client.submit(get);
|
||||
Writer writer = httpServletResponse.getWriter();
|
||||
try {
|
||||
writer.write(get.get().trim());
|
||||
} catch (Exception e) {
|
||||
throw new ServletException(e);
|
||||
}
|
||||
writer.flush();
|
||||
writer.close();
|
||||
protected void doGet(HttpServletRequest httpServletRequest,
|
||||
HttpServletResponse httpServletResponse) throws ServletException,
|
||||
IOException {
|
||||
Writer writer = httpServletResponse.getWriter();
|
||||
try {
|
||||
List<S3Bucket> myBuckets = client.getBuckets().get(10,
|
||||
TimeUnit.SECONDS);
|
||||
writer.write("List:\n");
|
||||
for (S3Bucket bucket : myBuckets) {
|
||||
writer
|
||||
.write(String.format(" Name: %1s%n", bucket
|
||||
.getName()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new ServletException(e);
|
||||
}
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
|
||||
}
|
|
@ -21,114 +21,89 @@
|
|||
* under the License.
|
||||
* ====================================================================
|
||||
*/
|
||||
package org.jclouds.samples.googleappengine.config; /**
|
||||
* // TODO: Adrian: Document this!
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
package org.jclouds.samples.googleappengine.config;
|
||||
|
||||
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.Inject;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.name.Names;
|
||||
import com.google.inject.servlet.GuiceServletContextListener;
|
||||
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 {
|
||||
@Inject
|
||||
Closer closer;
|
||||
|
||||
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
|
||||
public void contextInitialized(ServletContextEvent servletContextEvent) {
|
||||
this.context = servletContextEvent.getServletContext();
|
||||
super.contextInitialized(servletContextEvent); // TODO: Adrian: Customise this generated block
|
||||
this.context = servletContextEvent.getServletContext();
|
||||
super.contextInitialized(servletContextEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Injector getInjector() {
|
||||
return Guice.createInjector(
|
||||
new HttpCommandsModule() {
|
||||
@Override
|
||||
protected void configure() {
|
||||
super.configure();
|
||||
Properties props = new Properties();
|
||||
InputStream input = null;
|
||||
try {
|
||||
input = context.getResourceAsStream("/WEB-INF/jclouds.properties");
|
||||
if (input != null)
|
||||
props.load(input);
|
||||
else
|
||||
throw new RuntimeException("not found in classloader");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(input);
|
||||
}
|
||||
Names.bindProperties(binder(), props);
|
||||
install(new JavaUrlHttpFutureCommandClientModule());
|
||||
}
|
||||
}
|
||||
, new ServletModule() {
|
||||
@Override
|
||||
protected void configureServlets() {
|
||||
serve("*.jclouds").with(JCloudsServlet.class);
|
||||
serve("*.url").with(GetServlet.class);
|
||||
requestInjection(this);
|
||||
}
|
||||
});
|
||||
return Guice.createInjector(
|
||||
new AbstractModule() {
|
||||
@Override
|
||||
protected void configure() {
|
||||
Properties props = new Properties();
|
||||
InputStream input = null;
|
||||
try {
|
||||
input = context
|
||||
.getResourceAsStream("/WEB-INF/jclouds.properties");
|
||||
if (input != null)
|
||||
props.load(input);
|
||||
else
|
||||
throw new RuntimeException(
|
||||
"not found in classloader");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(input);
|
||||
}
|
||||
props.putAll(S3ConnectionFactory.DEFAULT_PROPERTIES);
|
||||
Names.bindProperties(binder(), props);
|
||||
}
|
||||
}, new JavaUrlHttpFutureCommandClientModule(),
|
||||
new S3ConnectionModule(), new ServletModule() {
|
||||
@Override
|
||||
protected void configureServlets() {
|
||||
serve("*.s3").with(JCloudsServlet.class);
|
||||
requestInjection(this);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent servletContextEvent) {
|
||||
try {
|
||||
closer.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(); // TODO: Adrian: Customise this generated block
|
||||
}
|
||||
super.contextDestroyed(servletContextEvent); // TODO: Adrian: Customise this generated block
|
||||
try {
|
||||
closer.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
super.contextDestroyed(servletContextEvent);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue