Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesward committed Nov 30, 2011
1 parent da53ed7 commit 0d0fa12
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 86 deletions.
45 changes: 36 additions & 9 deletions README.md
Expand Up @@ -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
11 changes: 0 additions & 11 deletions pom.xml
Expand Up @@ -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>
Expand All @@ -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>

Expand All @@ -49,7 +39,6 @@
</goals>
<configuration>
<assembleDirectory>target</assembleDirectory>
<extraJvmArguments>-Xmx512m</extraJvmArguments>
<programs>
<program>
<mainClass>com.heroku.test.Main</mainClass>
Expand Down
57 changes: 15 additions & 42 deletions src/main/java/com/heroku/test/Main.java
Expand Up @@ -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;
Expand All @@ -14,74 +12,49 @@
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";
}

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);

SessionHandler sessionHandler = new SessionHandler();
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();
}

}
13 changes: 0 additions & 13 deletions src/main/java/com/heroku/test/servlet/TestServlet.java
@@ -1,28 +1,15 @@
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 {


Expand Down
8 changes: 2 additions & 6 deletions src/main/webapp/WEB-INF/web.xml
Expand Up @@ -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>
Expand All @@ -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>
5 changes: 0 additions & 5 deletions src/main/webapp/index.html

This file was deleted.

0 comments on commit 0d0fa12

Please sign in to comment.