-
Notifications
You must be signed in to change notification settings - Fork 7
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
Showing
3 changed files
with
123 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
include "tapx-core", "tapx-kaptcha", "tapx-datefield", "tapx-plainmessage" | ||
include "tapx-templating", "tapx-yui" | ||
include "tapx-json", "tapx-jms-core" | ||
include "tapx-json", "tapx-jms-core", "tapx-heroku" |
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,14 @@ | ||
description = "Common general components" | ||
|
||
versions = [ | ||
jetty : "8.0.3.v20111011", | ||
mongo : "2.6.5" | ||
] | ||
|
||
dependencies { | ||
compile "org.eclipse.jetty:jetty-webapp:$versions.jetty" | ||
compile "org.eclipse.jetty:jetty-nosql:$versions.jetty" | ||
compile "org.mongodb:mongo-java-driver:$versions.mongo" | ||
compile "commons-cli:commons-cli:1.2" | ||
} | ||
|
108 changes: 108 additions & 0 deletions
108
tapx-heroku/src/main/java/com/howardlewiship/tapx/heroku/JettyMain.java
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,108 @@ | ||
package com.howardlewiship.tapx.heroku; | ||
|
||
import com.mongodb.DB; | ||
import com.mongodb.MongoURI; | ||
import org.apache.commons.cli.*; | ||
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; | ||
|
||
/** | ||
* Launches Jetty to service the web application at {@code src/main/webapp}. | ||
*/ | ||
public class JettyMain { | ||
|
||
private final String webappFolder; | ||
|
||
private final String mongoURLEnvVar; | ||
|
||
private final String sessionCollectionName; | ||
|
||
public JettyMain(String webappFolder, String mongoURLEnvVar, String sessionCollectionName) { | ||
this.webappFolder = webappFolder; | ||
this.mongoURLEnvVar = mongoURLEnvVar; | ||
this.sessionCollectionName = sessionCollectionName; | ||
} | ||
|
||
public void startServer() throws Exception { | ||
|
||
String webPort = System.getenv("PORT"); | ||
if (webPort == null || webPort.isEmpty()) { | ||
webPort = "8080"; | ||
} | ||
|
||
Server server = new Server(Integer.valueOf(webPort)); | ||
WebAppContext root = new WebAppContext(); | ||
|
||
MongoURI mongoURI = new MongoURI(System.getenv(mongoURLEnvVar)); | ||
DB connectedDB = mongoURI.connectDB(); | ||
|
||
if (mongoURI.getUsername() != null) { | ||
connectedDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword()); | ||
} | ||
|
||
MongoSessionIdManager idMgr = new MongoSessionIdManager(server, connectedDB.getCollection(sessionCollectionName)); | ||
|
||
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(webappFolder + "/WEB-INF/web.xml"); | ||
root.setResourceBase(webappFolder); | ||
root.setParentLoaderPriority(true); | ||
|
||
server.setHandler(root); | ||
|
||
server.start(); | ||
server.join(); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
CommandLineParser parser = new PosixParser(); | ||
|
||
Options options = new Options(); | ||
options.addOption("w", "web-app-folder", true, "Root folder of web application"); | ||
options.addOption("m", "mongo-env-var", true, "Name of environment variable storing the MongoDB URL"); | ||
options.addOption("c", "session-collection", true, "Name of MongoDB collection storing session data"); | ||
options.addOption("h", "help", false, "Show command usage"); | ||
|
||
try { | ||
CommandLine commandLine = parser.parse(options, args, true); | ||
|
||
if (!commandLine.getArgList().isEmpty()) { | ||
throw new ParseException("Unexpected command line arguments."); | ||
} | ||
|
||
if (commandLine.hasOption("help")) { | ||
showHelp(options); | ||
System.exit(0); | ||
} | ||
|
||
JettyMain main = new JettyMain( | ||
commandLine.getOptionValue("web-app-folder", "src/main/webapp"), | ||
commandLine.getOptionValue("mongo-env-var", "MONGOHQ_URL"), | ||
commandLine.getOptionValue("session-collection", "sessions")); | ||
main.startServer(); | ||
|
||
} catch (ParseException ex) { | ||
System.err.println(ex.getMessage()); | ||
showHelp(options); | ||
System.exit(-1); | ||
} | ||
|
||
} | ||
|
||
private static void showHelp(Options options) { | ||
HelpFormatter helpFormatter = new HelpFormatter(); | ||
helpFormatter.printHelp(JettyMain.class.getName(), options); | ||
} | ||
|
||
} |