feature[ats_ATS148512]: Improve jetty configuration and startup

Change-Id: I7f5d97b111c5ecb62b51cfb70fcba21f96b04b10
diff --git a/plugins/org.eclipse.osee.config.admin/src/org/eclipse/osee/config/admin/internal/ConfigManagerImpl.java b/plugins/org.eclipse.osee.config.admin/src/org/eclipse/osee/config/admin/internal/ConfigManagerImpl.java
index 630d6af..5a591d1 100644
--- a/plugins/org.eclipse.osee.config.admin/src/org/eclipse/osee/config/admin/internal/ConfigManagerImpl.java
+++ b/plugins/org.eclipse.osee.config.admin/src/org/eclipse/osee/config/admin/internal/ConfigManagerImpl.java
@@ -136,6 +136,15 @@
    }
 
    private void configureServices(Map<String, Dictionary<String, Object>> newConfigs) {
+      Iterable<String> removed =
+         org.eclipse.osee.framework.jdk.core.util.Collections.setComplement(services.keySet(), newConfigs.keySet());
+      for (String id : removed) {
+         ServiceConfig component = services.remove(id);
+         if (component != null) {
+            component.stop();
+         }
+      }
+
       for (Entry<String, Dictionary<String, Object>> entry : newConfigs.entrySet()) {
          String serviceId = entry.getKey();
          ServiceConfig component = services.get(serviceId);
@@ -145,15 +154,6 @@
          }
          component.update(entry.getValue());
       }
-
-      Iterable<String> removed =
-         org.eclipse.osee.framework.jdk.core.util.Collections.setComplement(services.keySet(), newConfigs.keySet());
-      for (String id : removed) {
-         ServiceConfig component = services.remove(id);
-         if (component != null) {
-            component.stop();
-         }
-      }
    }
 
    private final class ServiceConfig {
diff --git a/plugins/org.eclipse.osee.http.jetty/OSGI-INF/http.service.xml b/plugins/org.eclipse.osee.http.jetty/OSGI-INF/in.memory.http.service.xml
similarity index 92%
rename from plugins/org.eclipse.osee.http.jetty/OSGI-INF/http.service.xml
rename to plugins/org.eclipse.osee.http.jetty/OSGI-INF/in.memory.http.service.xml
index 779e9dd..6b4a84b 100644
--- a/plugins/org.eclipse.osee.http.jetty/OSGI-INF/http.service.xml
+++ b/plugins/org.eclipse.osee.http.jetty/OSGI-INF/in.memory.http.service.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="start" configuration-policy="require" deactivate="stop" modified="update">
-	<implementation class="org.eclipse.osee.http.jetty.internal.osgi.JettyHttpService" />
+	<implementation class="org.eclipse.osee.http.jetty.internal.osgi.InMemoryJettyHttpService" />
    <reference bind="setLogger" cardinality="1..1" interface="org.eclipse.osee.logger.Log" name="Log" policy="static"/>
 </scr:component>
diff --git a/plugins/org.eclipse.osee.http.jetty/OSGI-INF/http.service.xml b/plugins/org.eclipse.osee.http.jetty/OSGI-INF/jdbc.http.service.xml
similarity index 65%
copy from plugins/org.eclipse.osee.http.jetty/OSGI-INF/http.service.xml
copy to plugins/org.eclipse.osee.http.jetty/OSGI-INF/jdbc.http.service.xml
index 779e9dd..82f9cf6 100644
--- a/plugins/org.eclipse.osee.http.jetty/OSGI-INF/http.service.xml
+++ b/plugins/org.eclipse.osee.http.jetty/OSGI-INF/jdbc.http.service.xml
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="start" configuration-policy="require" deactivate="stop" modified="update">
-	<implementation class="org.eclipse.osee.http.jetty.internal.osgi.JettyHttpService" />
+	<implementation class="org.eclipse.osee.http.jetty.internal.osgi.JdbcJettyHttpService" />
    <reference bind="setLogger" cardinality="1..1" interface="org.eclipse.osee.logger.Log" name="Log" policy="static"/>
+   <reference bind="setJdbcService" cardinality="1..1" interface="org.eclipse.osee.jdbc.JdbcService" name="JdbcService" policy="static" target="(osgi.binding=jetty.jdbc.service)"/>
 </scr:component>
diff --git a/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/JettyConstants.java b/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/JettyConstants.java
index 28475b0..3aa0124 100644
--- a/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/JettyConstants.java
+++ b/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/JettyConstants.java
@@ -10,8 +10,6 @@
  *******************************************************************************/
 package org.eclipse.osee.http.jetty;
 
-import org.eclipse.osee.framework.jdk.core.util.Strings;
-
 /**
  * @author Roberto E. Escobar
  */
@@ -112,26 +110,4 @@
    public static final String ORG_OSGI_SERVICE_HTTP_PORT = "org.osgi.service.http.port";
    public static final String ORG_OSGI_SERVICE_HTTP_PORT_SECURE = "org.osgi.service.http.port.secure";
 
-   public static final String JETTY_SESSION_MANAGER_FACTORY = qualify("session.manager.factory");
-   public static final String DEFAULT_JETTY_SESSION_MANAGER_FACTORY = SessionManagerType.IN_MEMORY.name();
-
-   public static enum SessionManagerType {
-      IN_MEMORY,
-      JDBC,
-      UNKNOWN;
-
-      public static SessionManagerType fromString(String value) {
-         SessionManagerType toReturn = SessionManagerType.UNKNOWN;
-         if (Strings.isValid(value)) {
-            for (SessionManagerType type : SessionManagerType.values()) {
-               if (type.name().equalsIgnoreCase(value)) {
-                  toReturn = type;
-                  break;
-               }
-            }
-         }
-         return toReturn;
-      }
-   }
-
 }
diff --git a/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/internal/osgi/JettyHttpService.java b/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/internal/osgi/AbstractJettyHttpService.java
similarity index 66%
rename from plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/internal/osgi/JettyHttpService.java
rename to plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/internal/osgi/AbstractJettyHttpService.java
index 70252e4..4931fbe 100644
--- a/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/internal/osgi/JettyHttpService.java
+++ b/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/internal/osgi/AbstractJettyHttpService.java
@@ -15,28 +15,21 @@
 import static org.eclipse.osee.http.jetty.JettyConstants.ORG_OSGI_SERVICE_HTTP_PORT;
 import static org.eclipse.osee.http.jetty.JettyConstants.ORG_OSGI_SERVICE_HTTP_PORT_SECURE;
 import java.io.File;
-import java.util.Collection;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicReference;
-import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
 import org.eclipse.osee.framework.jdk.core.util.Strings;
-import org.eclipse.osee.http.jetty.JettyConstants;
-import org.eclipse.osee.http.jetty.JettyConstants.SessionManagerType;
 import org.eclipse.osee.http.jetty.JettyLogger;
 import org.eclipse.osee.http.jetty.JettyServer;
 import org.eclipse.osee.http.jetty.JettyServer.Builder;
 import org.eclipse.osee.http.jetty.internal.JettyUtil;
-import org.eclipse.osee.jdbc.JdbcService;
 import org.eclipse.osee.logger.Log;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
 
 /**
  * @author Roberto E. Escobar
  */
-public class JettyHttpService {
+public abstract class AbstractJettyHttpService {
 
    private static final String DIR_PREFIX = "pid_";
    private final AtomicReference<JettyServer> reference = new AtomicReference<JettyServer>();
@@ -81,22 +74,8 @@
       contextWorkDir.mkdir();
       builder.workingDirectory(contextWorkDir.getAbsolutePath());
 
-      String sessionManager =
-         getSessionManager(props, JettyConstants.JETTY_SESSION_MANAGER_FACTORY,
-            JettyConstants.DEFAULT_JETTY_SESSION_MANAGER_FACTORY);
-      SessionManagerType type = SessionManagerType.fromString(sessionManager);
-      switch (type) {
-         case JDBC:
-            JdbcService jdbcService = getJdbcService(bundleContext, "jetty.jdbc.service");
-            builder.jdbcSessionManagerFactory(jdbcService.getClient());
-            break;
-         case UNKNOWN:
-            logger.warn("JettySessionManagerFactory [%s] was %s - defaulting to IN_MEMORY - ", sessionManager, type);
-            break;
-         default:
-            // do nothing - default is in-memory
-            break;
-      }
+      customizeJettyServer(builder, props);
+
       JettyServer newServer = builder.build();
       JettyServer server = reference.getAndSet(newServer);
       if (server != null) {
@@ -106,28 +85,7 @@
       newServer.start();
    }
 
-   private String getSessionManager(Map<String, Object> props, String key, String defaultValue) {
-      String value = JettyUtil.get(props, key, defaultValue);
-      return Strings.isValid(value) ? value.toLowerCase() : value;
-   }
-
-   private JdbcService getJdbcService(BundleContext context, String jdbcServiceBinding) {
-      JdbcService toReturn = null;
-      try {
-         String filter = String.format("(osgi.binding=%s)", jdbcServiceBinding);
-         Collection<ServiceReference<JdbcService>> references = context.getServiceReferences(JdbcService.class, filter);
-         ServiceReference<JdbcService> reference = null;
-         if (!references.isEmpty()) {
-            reference = references.iterator().next();
-         }
-         if (reference != null) {
-            toReturn = context.getService(reference);
-         }
-      } catch (InvalidSyntaxException ex) {
-         throw new OseeCoreException(ex, "Error finding JdbcService reference with osgi.binding=%s", jdbcServiceBinding);
-      }
-      return toReturn;
-   }
+   protected abstract void customizeJettyServer(Builder builder, Map<String, Object> props);
 
    private String getWorkingDirectoryPath(Map<String, Object> props) {
       return DIR_PREFIX + props.get(Constants.SERVICE_PID).hashCode();
diff --git a/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/internal/osgi/InMemoryJettyHttpService.java b/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/internal/osgi/InMemoryJettyHttpService.java
new file mode 100644
index 0000000..fc58fdc
--- /dev/null
+++ b/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/internal/osgi/InMemoryJettyHttpService.java
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2015 Boeing.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Boeing - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.osee.http.jetty.internal.osgi;
+
+import java.util.Map;
+import org.eclipse.osee.http.jetty.JettyServer.Builder;
+import org.eclipse.osee.http.jetty.internal.session.InMemoryJettySessionManagerFactory;
+
+/**
+ * @author Roberto E. Escobar
+ */
+public class InMemoryJettyHttpService extends AbstractJettyHttpService {
+
+   @Override
+   protected void customizeJettyServer(Builder builder, Map<String, Object> props) {
+      builder.sessionManagerFactory(new InMemoryJettySessionManagerFactory());
+   }
+
+}
\ No newline at end of file
diff --git a/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/internal/osgi/JdbcJettyHttpService.java b/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/internal/osgi/JdbcJettyHttpService.java
new file mode 100644
index 0000000..df038fd
--- /dev/null
+++ b/plugins/org.eclipse.osee.http.jetty/src/org/eclipse/osee/http/jetty/internal/osgi/JdbcJettyHttpService.java
@@ -0,0 +1,33 @@
+/*******************************************************************************
+ * Copyright (c) 2015 Boeing.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Boeing - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.osee.http.jetty.internal.osgi;
+
+import java.util.Map;
+import org.eclipse.osee.http.jetty.JettyServer.Builder;
+import org.eclipse.osee.jdbc.JdbcService;
+
+/**
+ * @author Roberto E. Escobar
+ */
+public class JdbcJettyHttpService extends AbstractJettyHttpService {
+
+   private JdbcService jdbcService;
+
+   public void setJdbcService(JdbcService jdbcService) {
+      this.jdbcService = jdbcService;
+   }
+
+   @Override
+   protected void customizeJettyServer(Builder builder, Map<String, Object> props) {
+      builder.jdbcSessionManagerFactory(jdbcService.getClient());
+   }
+
+}
\ No newline at end of file
diff --git a/plugins/org.eclipse.osee.ote.master.product/etc/ote.server.config.json b/plugins/org.eclipse.osee.ote.master.product/etc/ote.server.config.json
index 028ec0c..1272c61 100644
--- a/plugins/org.eclipse.osee.ote.master.product/etc/ote.server.config.json
+++ b/plugins/org.eclipse.osee.ote.master.product/etc/ote.server.config.json
@@ -1,9 +1,8 @@
 {
     "config": [
          {
-                 "service.pid": "org.eclipse.osee.http.jetty.internal.osgi.JettyHttpService",
-                 "jetty.server.context.session.inactive.interval": 3600,
-                 "jetty.server.session.manager.factory": "IN_MEMORY"
+                 "service.pid": "org.eclipse.osee.http.jetty.internal.osgi.InMemoryJettyHttpService",
+                 "jetty.server.context.session.inactive.interval": 3600
          }
     ]
 }
diff --git a/plugins/org.eclipse.osee.parent/tools/osee-application-server.xml b/plugins/org.eclipse.osee.parent/tools/osee-application-server.xml
index 7975dcf..49fe352 100644
--- a/plugins/org.eclipse.osee.parent/tools/osee-application-server.xml
+++ b/plugins/org.eclipse.osee.parent/tools/osee-application-server.xml
@@ -158,10 +158,9 @@
               ]
         },    
         {
-            "service.pid": "org.eclipse.osee.http.jetty.internal.osgi.JettyHttpService",
+            "service.pid": "org.eclipse.osee.http.jetty.internal.osgi.JdbcJettyHttpService",
 				"jetty.server.http.port": ${xserver-port},
             "jetty.server.context.session.inactive.interval": 3600,
-            "jetty.server.session.manager.factory": "JDBC",
             "jetty.jdbc.cluster.name" : "OSEE",
             "jetty.jdbc.save.interval.secs" : 30
         }				
diff --git a/plugins/org.eclipse.osee.support.config/launchConfig/osee.hsql.json b/plugins/org.eclipse.osee.support.config/launchConfig/osee.hsql.json
index 244075e..edf3c55 100644
--- a/plugins/org.eclipse.osee.support.config/launchConfig/osee.hsql.json
+++ b/plugins/org.eclipse.osee.support.config/launchConfig/osee.hsql.json
@@ -24,9 +24,8 @@
             ]
         },    
     	  {
-    		   "service.pid": "org.eclipse.osee.http.jetty.internal.osgi.JettyHttpService",
+    		   "service.pid": "org.eclipse.osee.http.jetty.internal.osgi.JdbcJettyHttpService",
     		   "jetty.server.context.session.inactive.interval": 3600,
-    		   "jetty.server.session.manager.factory": "JDBC",
     		   "jetty.jdbc.cluster.name" : "OSEE",
     		   "jetty.jdbc.save.interval.secs" : 30
     	  }
diff --git a/plugins/org.eclipse.osee.support.config/launchConfig/osee.postgresql.json b/plugins/org.eclipse.osee.support.config/launchConfig/osee.postgresql.json
index cbed858..bec34c3 100644
--- a/plugins/org.eclipse.osee.support.config/launchConfig/osee.postgresql.json
+++ b/plugins/org.eclipse.osee.support.config/launchConfig/osee.postgresql.json
@@ -17,15 +17,15 @@
                         "orcs.jdbc.service",
                         "account.jdbc.service",
                         "oauth.jdbc.service",
-                        "app.server.jdbc.service"
+                        "app.server.jdbc.service",
+                        "jetty.jdbc.service"
                     ]
                 }
             ]
         },
         {
-    		   "service.pid": "org.eclipse.osee.http.jetty.internal.osgi.JettyHttpService",
+    		   "service.pid": "org.eclipse.osee.http.jetty.internal.osgi.JdbcJettyHttpService",
     		   "jetty.server.context.session.inactive.interval": 3600,
-    		   "jetty.server.session.manager.factory": "JDBC",
     		   "jetty.jdbc.cluster.name" : "OSEE",
     		   "jetty.jdbc.save.interval.secs" : 30
     	  } 
diff --git a/plugins/org.eclipse.osee.x.server.p2/etc/osee.hsql.json b/plugins/org.eclipse.osee.x.server.p2/etc/osee.hsql.json
index 244075e..edf3c55 100644
--- a/plugins/org.eclipse.osee.x.server.p2/etc/osee.hsql.json
+++ b/plugins/org.eclipse.osee.x.server.p2/etc/osee.hsql.json
@@ -24,9 +24,8 @@
             ]
         },    
     	  {
-    		   "service.pid": "org.eclipse.osee.http.jetty.internal.osgi.JettyHttpService",
+    		   "service.pid": "org.eclipse.osee.http.jetty.internal.osgi.JdbcJettyHttpService",
     		   "jetty.server.context.session.inactive.interval": 3600,
-    		   "jetty.server.session.manager.factory": "JDBC",
     		   "jetty.jdbc.cluster.name" : "OSEE",
     		   "jetty.jdbc.save.interval.secs" : 30
     	  }
diff --git a/plugins/org.eclipse.osee.x.server.p2/etc/osee.postgresql.json b/plugins/org.eclipse.osee.x.server.p2/etc/osee.postgresql.json
index 31420f5..24fb170 100644
--- a/plugins/org.eclipse.osee.x.server.p2/etc/osee.postgresql.json
+++ b/plugins/org.eclipse.osee.x.server.p2/etc/osee.postgresql.json
@@ -24,9 +24,8 @@
             ]
         },    
     	  {
-    		   "service.pid": "org.eclipse.osee.http.jetty.internal.osgi.JettyHttpService",
+    		   "service.pid": "org.eclipse.osee.http.jetty.internal.osgi.JdbcJettyHttpService",
     		   "jetty.server.context.session.inactive.interval": 3600,
-    		   "jetty.server.session.manager.factory": "JDBC",
     		   "jetty.jdbc.cluster.name" : "OSEE",
     		   "jetty.jdbc.save.interval.secs" : 30
     	  }