From 589c37ec0496a0d5843a3203b5e66c44a110829f Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Sun, 10 May 2009 18:04:51 +0000 Subject: [PATCH] moved samples to s3 module git-svn-id: http://jclouds.googlecode.com/svn/trunk@642 3d8758e0-26b5-11de-8745-db77d3ebf521 --- s3/samples/googleappengine/README.txt | 54 +++++ s3/samples/googleappengine/pom.xml | 194 ++++++++++++++++++ .../functest/BaseGoogleAppEngineTest.java | 75 +++++++ .../functest/GoogleAppEngineTest.java | 101 +++++++++ .../googleappengine/JCloudsServlet.java | 90 ++++++++ .../config/GuiceServletConfig.java | 100 +++++++++ .../src/main/webapp/WEB-INF/appengine-web.xml | 30 +++ .../src/main/webapp/WEB-INF/web.xml | 48 +++++ .../googleappengine/src/main/webapp/index.jsp | 30 +++ 9 files changed, 722 insertions(+) create mode 100644 s3/samples/googleappengine/README.txt create mode 100644 s3/samples/googleappengine/pom.xml create mode 100644 s3/samples/googleappengine/src/it/java/org/jclouds/samples/googleappengine/functest/BaseGoogleAppEngineTest.java create mode 100644 s3/samples/googleappengine/src/it/java/org/jclouds/samples/googleappengine/functest/GoogleAppEngineTest.java create mode 100644 s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/JCloudsServlet.java create mode 100644 s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/config/GuiceServletConfig.java create mode 100644 s3/samples/googleappengine/src/main/webapp/WEB-INF/appengine-web.xml create mode 100644 s3/samples/googleappengine/src/main/webapp/WEB-INF/web.xml create mode 100644 s3/samples/googleappengine/src/main/webapp/index.jsp diff --git a/s3/samples/googleappengine/README.txt b/s3/samples/googleappengine/README.txt new file mode 100644 index 0000000000..6221dd9b45 --- /dev/null +++ b/s3/samples/googleappengine/README.txt @@ -0,0 +1,54 @@ +==== + + Copyright (C) 2009 Adrian Cole + + ==================================================================== + 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. + ==================================================================== +==== +This samples uses the Google App Engine for Java SDK located at http://googleappengine.googlecode.com/files/appengine-java-sdk-1.2.0.zip + +Please unzip the above file and modify your maven settings.xml like below before attempting to run 'mvn install' + + + appengine + + true + + + /path/to/appengine-java-sdk-1.2.0 + + + + + aws + + true + + + YOUR_ACCESS_KEY_ID + YOUR_SECRET_KEY + + + + + + jclouds + http://jclouds.googlecode.com/svn/trunk/repo + + diff --git a/s3/samples/googleappengine/pom.xml b/s3/samples/googleappengine/pom.xml new file mode 100644 index 0000000000..235cadfc70 --- /dev/null +++ b/s3/samples/googleappengine/pom.xml @@ -0,0 +1,194 @@ + + + + + jclouds-project + org.jclouds + 1.0-SNAPSHOT + ../../../project/pom.xml + + 4.0.0 + jclouds-gae-s3-example + war + JClouds Sample for Google App Engine + JClouds Sample for Google App Engine + + + + /Users/adriancole/Desktop/appengine-java-sdk-1.2.0 + localhost + 8088 + + + + + + + guice-snapshot + http://guice-maven.googlecode.com/svn/trunk + + + + + + Main Maven Repo + http://repo1.maven.org/maven2/ + + + + + + ${project.groupId} + jclouds-gae + ${project.version} + + + ${project.groupId} + jclouds-s3 + ${project.version} + + + ${project.groupId} + jclouds-s3 + ${project.version} + test + test-jar + + + com.google.code.guice + guice-servlet + 2.0-r943 + + + standard + taglibs + 1.1.2 + jar + runtime + + + jstl + javax.servlet + 1.1.2 + jar + compile + + + org.apache.geronimo.specs + geronimo-el_1.0_spec + 1.0.1 + compile + + + org.apache.geronimo.specs + geronimo-jsp_2.1_spec + 1.0.1 + provided + + + org.apache.geronimo.specs + geronimo-servlet_2.5_spec + 1.2 + provided + + + + com.google.appengine + appengine-tools-api + 1.2.0 + system + ${appengine.home}/lib/appengine-tools-api.jar + + + + + ${project.artifactId} + src/it/java + + + org.apache.maven.plugins + maven-surefire-plugin + 2.4.3 + + + integration + integration-test + + test + + + target/test-classes + + + jclouds.aws.accesskeyid + ${jclouds.aws.accesskeyid} + + + jclouds.aws.secretaccesskey + ${jclouds.aws.secretaccesskey} + + + appengine.home + ${appengine.home} + + + devappserver.address + ${devappserver.address} + + + devappserver.port + ${devappserver.port} + + + warfile + ${project.build.directory}/${project.artifactId} + + + + ${appengine.home}/lib/appengine-tools-api.jar + + + + true + ${appengine.home}/bin + ${appengine.home}/lib + ${appengine.home}/config/sdk + + + + + + + target/classes + + + + + + diff --git a/s3/samples/googleappengine/src/it/java/org/jclouds/samples/googleappengine/functest/BaseGoogleAppEngineTest.java b/s3/samples/googleappengine/src/it/java/org/jclouds/samples/googleappengine/functest/BaseGoogleAppEngineTest.java new file mode 100644 index 0000000000..d96d535231 --- /dev/null +++ b/s3/samples/googleappengine/src/it/java/org/jclouds/samples/googleappengine/functest/BaseGoogleAppEngineTest.java @@ -0,0 +1,75 @@ +/** + * + * Copyright (C) 2009 Adrian Cole + * + * ==================================================================== + * 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://%1$s:%2$s", address, port)); + + props.store(new FileOutputStream(String.format( + "%1$s/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); + } + + @SuppressWarnings("deprecation") + @AfterTest + public void stopDevAppServer() throws Exception { + server.stop(); + } + +} \ No newline at end of file diff --git a/s3/samples/googleappengine/src/it/java/org/jclouds/samples/googleappengine/functest/GoogleAppEngineTest.java b/s3/samples/googleappengine/src/it/java/org/jclouds/samples/googleappengine/functest/GoogleAppEngineTest.java new file mode 100644 index 0000000000..51240ae7f3 --- /dev/null +++ b/s3/samples/googleappengine/src/it/java/org/jclouds/samples/googleappengine/functest/GoogleAppEngineTest.java @@ -0,0 +1,101 @@ +/** + * + * Copyright (C) 2009 Adrian Cole + * + * ==================================================================== + * 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 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.jclouds.aws.s3.reference.S3Constants; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Optional; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; + + +/** + * 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 extends BaseGoogleAppEngineTest { + + private static final String sysAWSAccessKeyId = System + .getProperty(S3Constants.PROPERTY_AWS_ACCESSKEYID); + private static final String sysAWSSecretAccessKey = System + .getProperty(S3Constants.PROPERTY_AWS_SECRETACCESSKEY); + + @BeforeTest + @Parameters( { "warfile", "devappserver.address", "devappserver.port", + S3Constants.PROPERTY_AWS_ACCESSKEYID, + S3Constants.PROPERTY_AWS_SECRETACCESSKEY }) + public void startDevAppServer(final String warfile, final String address, + 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(S3Constants.PROPERTY_AWS_ACCESSKEYID, AWSAccessKeyId); + props.put(S3Constants.PROPERTY_AWS_SECRETACCESSKEY, AWSSecretAccessKey); + writePropertiesAndStartServer(address, port, warfile, props); + } + + @Test + public void shouldPass() throws InterruptedException, IOException { + InputStream i = url.openStream(); + String string = IOUtils.toString(i); + assert string.indexOf("Hello World!") >= 0 : string; + } + + @Test(invocationCount = 5, enabled = true) + public void testGuiceJCloudsSerial() throws InterruptedException, + IOException { + URL gurl = new URL(url, "/guice/listbuckets.s3"); + InputStream i = gurl.openStream(); + String string = IOUtils.toString(i); + assert string.indexOf("List") >= 0 : string; + } + + @Test(invocationCount = 50, enabled = true, threadPoolSize = 10) + public void testGuiceJCloudsParallel() throws InterruptedException, + IOException { + URL gurl = new URL(url, "/guice/listbuckets.s3"); + InputStream i = gurl.openStream(); + String string = IOUtils.toString(i); + assert string.indexOf("List") >= 0 : string; + } +} diff --git a/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/JCloudsServlet.java b/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/JCloudsServlet.java new file mode 100644 index 0000000000..aac9157983 --- /dev/null +++ b/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/JCloudsServlet.java @@ -0,0 +1,90 @@ +/** + * + * Copyright (C) 2009 Adrian Cole + * + * ==================================================================== + * 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 java.io.IOException; +import java.io.Writer; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import javax.annotation.Resource; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.jclouds.aws.s3.S3Context; +import org.jclouds.aws.s3.S3ResponseException; +import org.jclouds.aws.s3.domain.S3Bucket; +import org.jclouds.logging.Logger; + +import com.google.inject.Inject; +import com.google.inject.Singleton; + +/** + * Shows an example of how to use @{link S3Connection} injected with Guice. + * + * @author Adrian Cole + */ +@Singleton +public class JCloudsServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + + @Inject + S3Context context; + + @Resource + protected Logger logger = Logger.NULL; + + @Override + protected void doGet(HttpServletRequest httpServletRequest, + HttpServletResponse httpServletResponse) throws ServletException, + IOException { + httpServletResponse.setContentType("text/plain"); + Writer writer = httpServletResponse.getWriter(); + try { + List myBuckets = context.getConnection() + .listOwnedBuckets().get(10, TimeUnit.SECONDS); + writer.write("List:\n"); + for (S3Bucket.Metadata bucket : myBuckets) { + writer.write(String.format(" %1$s", bucket)); + try { + writer.write(String.format(": %1$s entries%n", context + .createInputStreamMap(bucket.getName()).size())); + } catch (S3ResponseException e) { + String message = String.format( + ": unable to list entries due to: %1$s%n", e + .getError().getCode()); + writer.write(message); + logger.warn(e, "message"); + } + + } + } catch (Exception e) { + throw new ServletException(e); + } + writer.flush(); + writer.close(); + } +} \ No newline at end of file diff --git a/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/config/GuiceServletConfig.java b/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/config/GuiceServletConfig.java new file mode 100644 index 0000000000..e5e0900c89 --- /dev/null +++ b/s3/samples/googleappengine/src/main/java/org/jclouds/samples/googleappengine/config/GuiceServletConfig.java @@ -0,0 +1,100 @@ +/** + * + * Copyright (C) 2009 Adrian Cole + * + * ==================================================================== + * 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.config; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +import javax.servlet.ServletContextEvent; + +import org.apache.commons.io.IOUtils; +import org.jclouds.aws.s3.S3Context; +import org.jclouds.aws.s3.S3ContextFactory; +import org.jclouds.aws.s3.reference.S3Constants; +import org.jclouds.gae.config.URLFetchServiceClientModule; +import org.jclouds.samples.googleappengine.JCloudsServlet; + + +import com.google.inject.Inject; +import com.google.inject.Injector; +import com.google.inject.servlet.GuiceServletContextListener; +import com.google.inject.servlet.ServletModule; + +/** + * Setup Logging and create Injector for use in testing S3. + * + * @author Adrian Cole + * + */ +public class GuiceServletConfig extends GuiceServletContextListener { + @Inject + S3Context context; + String accessKeyId; + String secretAccessKey; + + @Override + public void contextInitialized(ServletContextEvent servletContextEvent) { + Properties props = loadJCloudsProperties(servletContextEvent); + this.accessKeyId = props + .getProperty(S3Constants.PROPERTY_AWS_ACCESSKEYID); + this.secretAccessKey = props + .getProperty(S3Constants.PROPERTY_AWS_SECRETACCESSKEY); + super.contextInitialized(servletContextEvent); + } + + private Properties loadJCloudsProperties( + ServletContextEvent servletContextEvent) { + InputStream input = servletContextEvent.getServletContext() + .getResourceAsStream("/WEB-INF/jclouds.properties"); + Properties props = new Properties(); + try { + props.load(input); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(input); + } + return props; + } + + @Override + protected Injector getInjector() { + return S3ContextFactory.createInjector(accessKeyId, secretAccessKey, + false, new URLFetchServiceClientModule(), + new ServletModule() { + @Override + protected void configureServlets() { + serve("*.s3").with(JCloudsServlet.class); + requestInjection(this); + } + }); + } + + @Override + public void contextDestroyed(ServletContextEvent servletContextEvent) { + context.close(); + super.contextDestroyed(servletContextEvent); + } +} \ No newline at end of file diff --git a/s3/samples/googleappengine/src/main/webapp/WEB-INF/appengine-web.xml b/s3/samples/googleappengine/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 0000000000..8f17694b44 --- /dev/null +++ b/s3/samples/googleappengine/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,30 @@ + + + + jclouds-s3-example + 1 + diff --git a/s3/samples/googleappengine/src/main/webapp/WEB-INF/web.xml b/s3/samples/googleappengine/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..73a4a41c95 --- /dev/null +++ b/s3/samples/googleappengine/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,48 @@ + + + + + jclouds-s3-example + + + + guiceFilter + com.google.inject.servlet.GuiceFilter + + + + guiceFilter + /guice/* + + + + + org.jclouds.samples.googleappengine.config.GuiceServletConfig + + diff --git a/s3/samples/googleappengine/src/main/webapp/index.jsp b/s3/samples/googleappengine/src/main/webapp/index.jsp new file mode 100644 index 0000000000..6529d030ec --- /dev/null +++ b/s3/samples/googleappengine/src/main/webapp/index.jsp @@ -0,0 +1,30 @@ +<%-- + + + Copyright (C) 2009 Adrian Cole + + ==================================================================== + 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. + ==================================================================== + +--%> + + +

Hello World!

+ +