Issue 2: added sample that uses jclouds api

git-svn-id: http://jclouds.googlecode.com/svn/trunk@55 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
ferncam1 2009-04-28 16:56:11 +00:00
parent c9b8f4c2e9
commit e44c46348b
3 changed files with 163 additions and 40 deletions

View File

@ -23,66 +23,80 @@
*/ */
package org.jclouds.samples.googleappengine.functest; package org.jclouds.samples.googleappengine.functest;
import com.google.appengine.tools.KickStart;
import org.apache.commons.io.IOUtils;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.io.FileOutputStream; import java.io.FileOutputStream;
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;
@Test(groups = "integration", enabled = false, sequential = true, testName = "functionalTests") import org.apache.commons.io.IOUtils;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.google.appengine.tools.KickStart;
@Test(groups = "integration", enabled = true, sequential = true, testName = "functionalTests")
public class GoogleAppEngineTest { public class GoogleAppEngineTest {
Thread server; Thread server;
URL url; URL url;
@BeforeTest @BeforeTest
@Parameters({"warfile", "devappserver.address", "devappserver.port"}) @Parameters( { "warfile", "devappserver.address", "devappserver.port" })
public void startDevAppServer(final String warfile, final String address, final String port) throws Exception { public void startDevAppServer(final String warfile, final String address,
url = new URL(String.format("http://%1s:%2s", address, port)); final String port) throws Exception {
Properties props = new Properties(); url = new URL(String.format("http://%1s:%2s", address, port));
props.put("jclouds.http.address", address); Properties props = new Properties();
props.put("jclouds.http.port", port + ""); props.put("jclouds.http.address", address);
props.put("jclouds.http.secure", "false"); props.put("jclouds.http.port", port + "");
props.store(new FileOutputStream(String.format("%1s/WEB-INF/jclouds.properties", warfile)), "test"); props.put("jclouds.http.secure", "false");
this.server = new Thread(new Runnable() { props.store(new FileOutputStream(String.format(
public void run() { "%1s/WEB-INF/jclouds.properties", warfile)), "test");
KickStart.main(new String[]{ this.server = new Thread(new Runnable() {
"com.google.appengine.tools.development.DevAppServerMain", public void run() {
"--disable_update_check", KickStart
"-a", address, .main(new String[] {
"-p", port, "com.google.appengine.tools.development.DevAppServerMain",
warfile}); "--disable_update_check", "-a", address, "-p",
port, warfile });
} }
}); });
server.start(); server.start();
Thread.sleep(7 * 1000); Thread.sleep(7 * 1000);
} }
@AfterTest @AfterTest
public void stopDevAppServer() throws Exception { public void stopDevAppServer() throws Exception {
server.stop(); server.stop();
} }
@Test
public void shouldPass() throws InterruptedException, IOException { public void shouldPass() throws InterruptedException, IOException {
InputStream i = url.openStream(); InputStream i = url.openStream();
String string = IOUtils.toString(i); String string = IOUtils.toString(i);
assert string.indexOf("Hello World!") >= 0 : string; assert string.indexOf("Hello World!") >= 0 : string;
} }
@Test(invocationCount = 50, enabled = false, threadPoolSize = 10) @Test(invocationCount = 50, enabled = true, threadPoolSize = 10)
public void testGuiceJCloudsServed() throws InterruptedException, IOException { public void testGuiceUrlServed() throws InterruptedException, IOException {
Thread.sleep(10000); Thread.sleep(10000);
URL gurl = new URL(url, "/guice/fetch?uri=/"); URL gurl = new URL(url, "/guice/fetch.url?uri=" + url.toExternalForm());
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("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=/");
InputStream i = gurl.openStream();
String string = IOUtils.toString(i);
assert string.indexOf("Hello World!") >= 0 : string;
} }
} }

View File

@ -0,0 +1,77 @@
/**
*
* 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

@ -36,13 +36,21 @@ import org.apache.commons.io.IOUtils;
import org.jclouds.http.commands.config.HttpCommandsModule; import org.jclouds.http.commands.config.HttpCommandsModule;
import org.jclouds.http.config.JavaUrlHttpFutureCommandClientModule; import org.jclouds.http.config.JavaUrlHttpFutureCommandClientModule;
import org.jclouds.lifecycle.Closer; import org.jclouds.lifecycle.Closer;
import org.jclouds.samples.googleappengine.GetServlet;
import org.jclouds.samples.googleappengine.JCloudsServlet; import org.jclouds.samples.googleappengine.JCloudsServlet;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Date;
import java.util.Properties; 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;
public class GuiceServletConfig extends GuiceServletContextListener { public class GuiceServletConfig extends GuiceServletContextListener {
@Inject @Inject
@ -50,7 +58,30 @@ public class GuiceServletConfig extends GuiceServletContextListener {
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();
@ -84,7 +115,8 @@ public class GuiceServletConfig extends GuiceServletContextListener {
, new ServletModule() { , new ServletModule() {
@Override @Override
protected void configureServlets() { protected void configureServlets() {
serve("/*").with(JCloudsServlet.class); serve("*.jclouds").with(JCloudsServlet.class);
serve("*.url").with(GetServlet.class);
requestInjection(this); requestInjection(this);
} }
}); });