forked from jsimone/jetty-mongo-session-test
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dcaa05c
Showing
7 changed files
with
346 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.classpath | ||
.project | ||
.settings | ||
.springBeans | ||
target | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.heroku.test</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>jettySessionTest</name> | ||
<artifactId>jettySessionTest</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<java.version>1.6</java.version> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- Servlet API --> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>2.5</version> | ||
</dependency> | ||
|
||
<!-- Jetty --> | ||
<dependency> | ||
<groupId>org.eclipse.jetty</groupId> | ||
<artifactId>jetty-webapp</artifactId> | ||
<version>7.5.1.v20110908</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.jetty</groupId> | ||
<artifactId>jetty-nosql</artifactId> | ||
<version>7.5.1.v20110908</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mongodb</groupId> | ||
<artifactId>mongo-java-driver</artifactId> | ||
<version>2.6.1</version> | ||
<type>jar</type> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mortbay.jetty</groupId> | ||
<artifactId>jsp-2.1-glassfish</artifactId> | ||
<version>2.1.v20100127</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>2.3.2</version> | ||
<configuration> | ||
<source>${java.version}</source> | ||
<target>${java.version}</target> | ||
</configuration> | ||
</plugin> | ||
|
||
<!-- The maven app assembler plugin will generate a script that sets up the classpath and runs your project --> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>appassembler-maven-plugin</artifactId> | ||
<version>1.1.1</version> | ||
<configuration> | ||
<assembleDirectory>target</assembleDirectory> | ||
<programs> | ||
<program> | ||
<mainClass>com.heroku.test.Main</mainClass> | ||
<name>webapp</name> | ||
</program> | ||
</programs> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>assemble</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<profiles> | ||
<profile> | ||
<id>debug-launch</id> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.2</version> | ||
<configuration> | ||
<executable>sh</executable> | ||
<arguments> | ||
<argument>${project.build.directory}/bin/webapp</argument> | ||
</arguments> | ||
<environmentVariables> | ||
<JAVA_OPTS>-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n</JAVA_OPTS> | ||
</environmentVariables> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<phase>install</phase> | ||
<goals><goal>exec</goal></goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
</profiles> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://java.sun.com/xml/ns/javaee" | ||
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | ||
version="2.5"> | ||
|
||
<welcome-file-list> | ||
<welcome-file>index.html</welcome-file> | ||
</welcome-file-list> | ||
|
||
<servlet> | ||
<servlet-name>Test Servlet</servlet-name> | ||
<servlet-class>com.heroku.test.servlet.TestServlet</servlet-class> | ||
</servlet> | ||
|
||
<servlet-mapping> | ||
<servlet-name>Test Servlet</servlet-name> | ||
<url-pattern>/test</url-pattern> | ||
</servlet-mapping> | ||
|
||
</web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
hello, world | ||
</body> | ||
</html> |