diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..099e6a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.classpath +.project +.settings +.springBeans +target +target diff --git a/README.md b/README.md new file mode 100644 index 0000000..2264002 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# Build + +Build the project with + + $ mvn install + +# Configure + +You will need to set the `REPO` environment variable, so the execution wrapper script knows where to find the maven dependencies. For example: + + $ export REPO=$HOME/.m2/repository + +# Run + +Now you can run your webapp with: + + $ sh target/bin/webapp + +(the wrapper script is not executable by default). + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..74610d1 --- /dev/null +++ b/pom.xml @@ -0,0 +1,115 @@ + + + 4.0.0 + com.heroku.test + 1.0-SNAPSHOT + jettySessionTest + jettySessionTest + jar + + + 1.6 + UTF-8 + + + + + + javax.servlet + servlet-api + 2.5 + + + + + org.eclipse.jetty + jetty-webapp + 7.5.1.v20110908 + + + org.eclipse.jetty + jetty-nosql + 7.5.1.v20110908 + + + org.mongodb + mongo-java-driver + 2.6.1 + jar + compile + + + org.mortbay.jetty + jsp-2.1-glassfish + 2.1.v20100127 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + ${java.version} + ${java.version} + + + + + + org.codehaus.mojo + appassembler-maven-plugin + 1.1.1 + + target + + + com.heroku.test.Main + webapp + + + + + + package + + assemble + + + + + + + + + + debug-launch + + + + org.codehaus.mojo + exec-maven-plugin + 1.2 + + sh + + ${project.build.directory}/bin/webapp + + + -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n + + + + + install + exec + + + + + + + + diff --git a/src/main/java/com/heroku/test/Main.java b/src/main/java/com/heroku/test/Main.java new file mode 100644 index 0000000..5965929 --- /dev/null +++ b/src/main/java/com/heroku/test/Main.java @@ -0,0 +1,101 @@ +package com.heroku.test; + +import java.util.Date; +import java.util.Random; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.eclipse.jetty.nosql.mongodb.MongoSessionIdManager; +import org.eclipse.jetty.nosql.mongodb.MongoSessionManager; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.session.SessionHandler; +import org.eclipse.jetty.webapp.WebAppContext; + +import com.mongodb.DB; +import com.mongodb.Mongo; + +/** + * + * This class launches the web application in an embedded Jetty container. + * This is the entry point to your application. The Java command that is used for + * launching should fire this main method. + * + * @author John Simone + */ +public class Main { + + /** + * @param args + */ + public static void main(String[] args) throws Exception{ + String webappDirLocation = "src/main/webapp/"; + + //The port that we should run on can be set into an environment variable + //Look for that variable and default to 8080 if it isn't there. + String webPort = System.getenv("PORT"); + if(webPort == null || webPort.isEmpty()) { + webPort = "8080"; + } + + Server server = new Server(Integer.valueOf(webPort)); + WebAppContext root = new WebAppContext(); + + + + + //Parse mango db URL + String mongoDbURL = System.getenv("MONGOHQ_URL"); + + System.out.println("mongo url: " + mongoDbURL); + + Matcher matcher = Pattern.compile("mongodb://(.*):(.*)@(.*):(.*)/(.*)").matcher(mongoDbURL); + matcher.find(); + + String mongoUser = matcher.group(1); + String mongoPassword = matcher.group(2); + String mongoHost = matcher.group(3); + String mongoPort = matcher.group(4); + String mongoDatabase = matcher.group(5); + + + Mongo mongo = new Mongo(mongoHost, Integer.valueOf(mongoPort)); + DB db = mongo.getDB(mongoDatabase); + db.authenticate(mongoUser, mongoPassword.toCharArray()); + + //Set up session handling through MongoDb + MongoSessionIdManager idMgr = new MongoSessionIdManager(server, db.getCollection("sessions")); + + //generate random worker name (should get this moved into Jetty) + Random rand = new Random((new Date()).getTime()); + //generate a random number between 1000 and 9999 + int workerNum = 1000 + rand.nextInt(8999); + + idMgr.setWorkerName(String.valueOf(workerNum)); + server.setSessionIdManager(idMgr); + + SessionHandler sessionHandler = new SessionHandler(); + MongoSessionManager mongoMgr = new MongoSessionManager(); + mongoMgr.setSessionIdManager(server.getSessionIdManager()); + sessionHandler.setSessionManager(mongoMgr); + root.setSessionHandler(sessionHandler); + + + + root.setContextPath("/"); + root.setDescriptor(webappDirLocation+"/WEB-INF/web.xml"); + root.setResourceBase(webappDirLocation); + + //Parent loader priority is a class loader setting that Jetty accepts. + //By default Jetty will behave like most web containers in that it will + //allow your application to replace non-server libraries that are part of the + //container. Setting parent loader priority to true changes this behavior. + //Read more here: http://wiki.eclipse.org/Jetty/Reference/Jetty_Classloading + root.setParentLoaderPriority(true); + + server.setHandler(root); + + server.start(); + server.join(); + } + +} diff --git a/src/main/java/com/heroku/test/servlet/TestServlet.java b/src/main/java/com/heroku/test/servlet/TestServlet.java new file mode 100644 index 0000000..0da16c5 --- /dev/null +++ b/src/main/java/com/heroku/test/servlet/TestServlet.java @@ -0,0 +1,77 @@ +package com.heroku.test.servlet; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import com.mongodb.BasicDBObject; +import com.mongodb.DB; +import com.mongodb.DBCollection; +import com.mongodb.Mongo; + +public class TestServlet extends HttpServlet { + + + private class CountHolder implements Serializable{ + + private static final long serialVersionUID = 1L; + private Integer count; + private Date time; + + public CountHolder() { + count = 0; + time = new Date(); + } + + public Integer getCount() { + return count; + } + + public void plusPlus() { + count++; + } + + public void setTime(Date time) { + this.time = time; + } + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + HttpSession session = req.getSession(); + + CountHolder count; + + if(session.getAttribute("count") != null) { + count = (CountHolder) session.getAttribute("count"); + } else { + count = new CountHolder(); + } + + count.setTime(new Date()); + count.plusPlus(); + + System.out.println("Count: " + count.getCount()); + + session.setAttribute("count", count); + + super.doGet(req, resp); + } + +} diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..1d46778 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,22 @@ + + + + + index.html + + + + Test Servlet + com.heroku.test.servlet.TestServlet + + + + Test Servlet + /test + + + diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html new file mode 100644 index 0000000..c1496f1 --- /dev/null +++ b/src/main/webapp/index.html @@ -0,0 +1,5 @@ + + +hello, world + +