Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

clean up

  • Loading branch information...
commit 0d0fa12438e5964b637cfe13a5dda4342b8774f4 1 parent da53ed7
@jamesward authored
View
45 README.md
@@ -5,31 +5,58 @@ MongoDB-backed Session State Sample
Run Locally
-----------
-# Build
-
-Build the project with
+### Build the project with Maven:
mvn package
-# Configure
- export MONGOHQ_URL=mongodb://foo:foo@127.0.0.1:27017/test
+### Configure the `MONGOHQ_URL` environment variable to point to a local MongoDB instance:
+
+* Linux/Mac
+
+ export MONGOHQ_URL=mongodb://127.0.0.1:27017/test
+
+* Windows
-# Run
+ set MONGOHQ_URL=mongodb://127.0.0.1:27017/test
+
+
+### Run
Now you can run your webapp with:
- $ sh target/bin/webapp
+* Linux/Mac
+
+ $ sh target/bin/webapp
+
+* Windows
+
+ $ target\bin\webapp.bat
+
+### Try the application in your browser by navigating to:
+
+ http://localhost:8080/
Run on Heroku
-------------
-# Create the app on Heroku
+### Install the [Heroku Toolbelt](http://toolbelt.heroku.com)
+
+### Login to Heroku from the command line
+
+ heroku login
+
+In the root directory of this project do the following:
+
+### Create the app on Heroku with the "cedar" stack and the free MongoHQ add-on
heroku create --stack cedar --addons mongohq:free
-# Deploy to Heroku (assuming the files are already in a git repo)
+### Deploy to Heroku
git push heroku master
+### Open the application in your browser
+
+ heroku open
View
11 pom.xml
@@ -8,14 +8,6 @@
<packaging>jar</packaging>
<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>
@@ -30,8 +22,6 @@
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.6.5</version>
- <type>jar</type>
-<!-- <scope>compile</scope>-->
</dependency>
</dependencies>
@@ -49,7 +39,6 @@
</goals>
<configuration>
<assembleDirectory>target</assembleDirectory>
- <extraJvmArguments>-Xmx512m</extraJvmArguments>
<programs>
<program>
<mainClass>com.heroku.test.Main</mainClass>
View
57 src/main/java/com/heroku/test/Main.java
@@ -2,8 +2,6 @@
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;
@@ -14,24 +12,11 @@
import com.mongodb.DB;
import com.mongodb.MongoURI;
-/**
- *
- * 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";
@@ -39,23 +24,19 @@ public static void main(String[] args) throws Exception{
Server server = new Server(Integer.valueOf(webPort));
WebAppContext root = new WebAppContext();
-
- MongoURI mongoDbURI = new MongoURI(System.getenv("MONGOHQ_URL"));
+
+ MongoURI mongoURI = new MongoURI(System.getenv("MONGOHQ_URL"));
DB connectedDB = mongoURI.connectDB();
- // allow for localhost, non-authenticated Mongo use
- if (mongoURI.getUsername() != null) {
- connectedDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
- }
-
- //Set up session handling through MongoDb
- MongoSessionIdManager idMgr = new MongoSessionIdManager(server, db.getCollection("sessions"));
-
- //generate random worker name (should get this moved into Jetty)
+ if (mongoURI.getUsername() != null) {
+ connectedDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
+ }
+
+ MongoSessionIdManager idMgr = new MongoSessionIdManager(server, connectedDB.getCollection("sessions"));
+
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);
@@ -63,25 +44,17 @@ public static void main(String[] args) throws Exception{
MongoSessionManager mongoMgr = new MongoSessionManager();
mongoMgr.setSessionIdManager(server.getSessionIdManager());
sessionHandler.setSessionManager(mongoMgr);
- root.setSessionHandler(sessionHandler);
-
-
+ 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();
+ server.join();
}
}
View
13 src/main/java/com/heroku/test/servlet/TestServlet.java
@@ -1,16 +1,8 @@
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;
@@ -18,11 +10,6 @@
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 {
View
8 src/main/webapp/WEB-INF/web.xml
@@ -4,10 +4,6 @@
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>
@@ -16,7 +12,7 @@
<servlet-mapping>
<servlet-name>Test Servlet</servlet-name>
- <url-pattern>/test</url-pattern>
+ <url-pattern>/</url-pattern>
</servlet-mapping>
-</web-app>
+</web-app>
View
5 src/main/webapp/index.html
@@ -1,5 +0,0 @@
-<html>
-<body>
-hello, world
-</body>
-</html>
Please sign in to comment.
Something went wrong with that request. Please try again.