Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow startup w/o MongoDB, using in-memory sessions

  • Loading branch information...
commit 25c5b65fb67e48e1dc533138cf7a3ee400b8f406 1 parent 41bb514
@hlship authored
View
75 tapx-heroku/src/main/java/com/howardlewiship/tapx/heroku/JettyMain.java
@@ -1,11 +1,14 @@
package com.howardlewiship.tapx.heroku;
import com.mongodb.DB;
+import com.mongodb.DBCollection;
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.SessionIdManager;
+import org.eclipse.jetty.server.session.HashSessionManager;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.webapp.WebAppContext;
@@ -16,43 +19,74 @@
private final String webappFolder;
- private final String mongoURLEnvVar;
+ private final String mongoURL;
private final String sessionCollectionName;
- public JettyMain(String webappFolder, String mongoURLEnvVar, String sessionCollectionName) {
+ public JettyMain(String webappFolder, String mongoURL, String sessionCollectionName) {
this.webappFolder = webappFolder;
- this.mongoURLEnvVar = mongoURLEnvVar;
+ this.mongoURL = mongoURL;
this.sessionCollectionName = sessionCollectionName;
}
+ private static boolean isBlank(String s) {
+ return s == null || s.trim().length() == 0;
+ }
+
+ private static String firstEnvValue(String... envNames) {
+ for (String name : envNames) {
+ String value = System.getenv(name);
+
+ if (!isBlank(name)) {
+ return value;
+ }
+ }
+
+ return null;
+ }
+
public void startServer() throws Exception {
String webPort = System.getenv("PORT");
- if (webPort == null || webPort.isEmpty()) {
- webPort = "8080";
- }
- Server server = new Server(Integer.valueOf(webPort));
+ int port = isBlank(webPort) ? 8080 : Integer.parseInt(webPort);
+
+
+ Server server = new Server(port);
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());
- }
+ SessionHandler sessionHandler = new SessionHandler();
- MongoSessionIdManager idMgr = new MongoSessionIdManager(server, connectedDB.getCollection(sessionCollectionName));
+ if (mongoURL != null) {
- server.setSessionIdManager(idMgr);
+ System.out.printf("Starting Jetty server, port %d, for '%s', using MongoDB URL '%s' (session collection '%s').\n", port, webappFolder, mongoURL, sessionCollectionName);
- SessionHandler sessionHandler = new SessionHandler();
- MongoSessionManager mongoMgr = new MongoSessionManager();
- mongoMgr.setSessionIdManager(server.getSessionIdManager());
- sessionHandler.setSessionManager(mongoMgr);
+ MongoURI mongoURI = new MongoURI(mongoURL);
+ DB connectedDB = mongoURI.connectDB();
+
+ if (mongoURI.getUsername() != null) {
+ connectedDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
+ }
+
+ DBCollection sessions = connectedDB.getCollection(sessionCollectionName);
+
+ SessionIdManager sessionIdManager = new MongoSessionIdManager(server, sessions);
+
+ MongoSessionManager manager = new MongoSessionManager();
+
+ manager.setSessionIdManager(sessionIdManager);
+
+ sessionHandler.setSessionManager(manager);
+
+ } else {
+ System.out.printf("Starting Jetty server, port %d, for '%s', using in-memory sessions only.\n", port, webappFolder);
+
+ sessionHandler.setSessionManager(new HashSessionManager());
+ }
root.setSessionHandler(sessionHandler);
+
root.setContextPath("/");
root.setDescriptor(webappFolder + "/WEB-INF/web.xml");
root.setResourceBase(webappFolder);
@@ -64,13 +98,14 @@ public void startServer() throws Exception {
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("m", "mongo-url", true, "MongoDB URL");
options.addOption("c", "session-collection", true, "Name of MongoDB collection storing session data");
options.addOption("h", "help", false, "Show command usage");
@@ -88,7 +123,7 @@ public static void main(String[] args) throws Exception {
JettyMain main = new JettyMain(
commandLine.getOptionValue("web-app-folder", "src/main/webapp"),
- commandLine.getOptionValue("mongo-env-var", "MONGOHQ_URL"),
+ commandLine.getOptionValue("mongo-url", firstEnvValue("MONGOHQ_URL", "MONGOLAB_URL")),
commandLine.getOptionValue("session-collection", "sessions"));
main.startServer();

0 comments on commit 25c5b65

Please sign in to comment.
Something went wrong with that request. Please try again.