Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 25c5b65fb67e48e1dc533138cf7a3ee400b8f406 1 parent 41bb514
Howard M. Lewis Ship authored
75 tapx-heroku/src/main/java/com/howardlewiship/tapx/heroku/JettyMain.java
... ... @@ -1,11 +1,14 @@
1 1 package com.howardlewiship.tapx.heroku;
2 2
3 3 import com.mongodb.DB;
  4 +import com.mongodb.DBCollection;
4 5 import com.mongodb.MongoURI;
5 6 import org.apache.commons.cli.*;
6 7 import org.eclipse.jetty.nosql.mongodb.MongoSessionIdManager;
7 8 import org.eclipse.jetty.nosql.mongodb.MongoSessionManager;
8 9 import org.eclipse.jetty.server.Server;
  10 +import org.eclipse.jetty.server.SessionIdManager;
  11 +import org.eclipse.jetty.server.session.HashSessionManager;
9 12 import org.eclipse.jetty.server.session.SessionHandler;
10 13 import org.eclipse.jetty.webapp.WebAppContext;
11 14
@@ -16,43 +19,74 @@
16 19
17 20 private final String webappFolder;
18 21
19   - private final String mongoURLEnvVar;
  22 + private final String mongoURL;
20 23
21 24 private final String sessionCollectionName;
22 25
23   - public JettyMain(String webappFolder, String mongoURLEnvVar, String sessionCollectionName) {
  26 + public JettyMain(String webappFolder, String mongoURL, String sessionCollectionName) {
24 27 this.webappFolder = webappFolder;
25   - this.mongoURLEnvVar = mongoURLEnvVar;
  28 + this.mongoURL = mongoURL;
26 29 this.sessionCollectionName = sessionCollectionName;
27 30 }
28 31
  32 + private static boolean isBlank(String s) {
  33 + return s == null || s.trim().length() == 0;
  34 + }
  35 +
  36 + private static String firstEnvValue(String... envNames) {
  37 + for (String name : envNames) {
  38 + String value = System.getenv(name);
  39 +
  40 + if (!isBlank(name)) {
  41 + return value;
  42 + }
  43 + }
  44 +
  45 + return null;
  46 + }
  47 +
29 48 public void startServer() throws Exception {
30 49
31 50 String webPort = System.getenv("PORT");
32   - if (webPort == null || webPort.isEmpty()) {
33   - webPort = "8080";
34   - }
35 51
36   - Server server = new Server(Integer.valueOf(webPort));
  52 + int port = isBlank(webPort) ? 8080 : Integer.parseInt(webPort);
  53 +
  54 +
  55 + Server server = new Server(port);
37 56 WebAppContext root = new WebAppContext();
38 57
39   - MongoURI mongoURI = new MongoURI(System.getenv(mongoURLEnvVar));
40   - DB connectedDB = mongoURI.connectDB();
41 58
42   - if (mongoURI.getUsername() != null) {
43   - connectedDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
44   - }
  59 + SessionHandler sessionHandler = new SessionHandler();
45 60
46   - MongoSessionIdManager idMgr = new MongoSessionIdManager(server, connectedDB.getCollection(sessionCollectionName));
  61 + if (mongoURL != null) {
47 62
48   - server.setSessionIdManager(idMgr);
  63 + System.out.printf("Starting Jetty server, port %d, for '%s', using MongoDB URL '%s' (session collection '%s').\n", port, webappFolder, mongoURL, sessionCollectionName);
49 64
50   - SessionHandler sessionHandler = new SessionHandler();
51   - MongoSessionManager mongoMgr = new MongoSessionManager();
52   - mongoMgr.setSessionIdManager(server.getSessionIdManager());
53   - sessionHandler.setSessionManager(mongoMgr);
  65 + MongoURI mongoURI = new MongoURI(mongoURL);
  66 + DB connectedDB = mongoURI.connectDB();
  67 +
  68 + if (mongoURI.getUsername() != null) {
  69 + connectedDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
  70 + }
  71 +
  72 + DBCollection sessions = connectedDB.getCollection(sessionCollectionName);
  73 +
  74 + SessionIdManager sessionIdManager = new MongoSessionIdManager(server, sessions);
  75 +
  76 + MongoSessionManager manager = new MongoSessionManager();
  77 +
  78 + manager.setSessionIdManager(sessionIdManager);
  79 +
  80 + sessionHandler.setSessionManager(manager);
  81 +
  82 + } else {
  83 + System.out.printf("Starting Jetty server, port %d, for '%s', using in-memory sessions only.\n", port, webappFolder);
  84 +
  85 + sessionHandler.setSessionManager(new HashSessionManager());
  86 + }
54 87
55 88 root.setSessionHandler(sessionHandler);
  89 +
56 90 root.setContextPath("/");
57 91 root.setDescriptor(webappFolder + "/WEB-INF/web.xml");
58 92 root.setResourceBase(webappFolder);
@@ -64,13 +98,14 @@ public void startServer() throws Exception {
64 98 server.join();
65 99 }
66 100
  101 +
67 102 public static void main(String[] args) throws Exception {
68 103
69 104 CommandLineParser parser = new PosixParser();
70 105
71 106 Options options = new Options();
72 107 options.addOption("w", "web-app-folder", true, "Root folder of web application");
73   - options.addOption("m", "mongo-env-var", true, "Name of environment variable storing the MongoDB URL");
  108 + options.addOption("m", "mongo-url", true, "MongoDB URL");
74 109 options.addOption("c", "session-collection", true, "Name of MongoDB collection storing session data");
75 110 options.addOption("h", "help", false, "Show command usage");
76 111
@@ -88,7 +123,7 @@ public static void main(String[] args) throws Exception {
88 123
89 124 JettyMain main = new JettyMain(
90 125 commandLine.getOptionValue("web-app-folder", "src/main/webapp"),
91   - commandLine.getOptionValue("mongo-env-var", "MONGOHQ_URL"),
  126 + commandLine.getOptionValue("mongo-url", firstEnvValue("MONGOHQ_URL", "MONGOLAB_URL")),
92 127 commandLine.getOptionValue("session-collection", "sessions"));
93 128 main.startServer();
94 129

0 comments on commit 25c5b65

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