From 778187e7ee7db369d7dd9394e139ca52fb8c213b Mon Sep 17 00:00:00 2001
From: Nikolaus Krismer <niko@krismer.de>
Date: Wed, 19 Feb 2014 20:12:24 +0100
Subject: [PATCH] added waiting thread for geoserver (now doing this in a
 separate thread... see javadoc for more details)

---
 .../unibz/inf/isoga/web/StartupListener.java  | 30 +++++++++++++++----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/src/main/java/it/unibz/inf/isoga/web/StartupListener.java b/src/main/java/it/unibz/inf/isoga/web/StartupListener.java
index 80cc5a31..57f04724 100644
--- a/src/main/java/it/unibz/inf/isoga/web/StartupListener.java
+++ b/src/main/java/it/unibz/inf/isoga/web/StartupListener.java
@@ -13,6 +13,8 @@ import org.slf4j.LoggerFactory;
 public class StartupListener implements ServletContextListener {
 	private static final Logger LOGGER = LoggerFactory.getLogger(StartupListener.class.getName());
 
+	// Public methods
+
 	@Override
 	public void contextInitialized(final ServletContextEvent sce) {
 		LOGGER.info("Initializing context");
@@ -24,15 +26,33 @@ public class StartupListener implements ServletContextListener {
 		// 4) Is the geoserver reachable?
 		// 5) Can we connect to the geoserver with given username/password?
 
-		final ConfigWeb config = ConfigurationContainer.getGlobalConfig();
-		final GeoserverHelper gh = new GeoserverHelper(config);
-		gh.waitForGeoserver();
-		gh.ensureEnvironment();
-		gh.performCleanup();
+		startGeoserverThread();
 	}
 
 	@Override
 	public void contextDestroyed(final ServletContextEvent sce) { }
 
+	// Private methods
+
+	/**
+	 * Waits for geoserver and perform a cleanup.
+	 * We do this in a separat thread so we do not get any problems if this webapp is hosted
+	 * on the same servlet container as the geoserver (we have to do this, since there is no
+	 * possibility to define an application startup order)
+	 */
+	private void startGeoserverThread() {
+		final Thread t = new Thread() {
+			@Override
+			public void run() {
+				final ConfigWeb config = ConfigurationContainer.getGlobalConfig();
+				final GeoserverHelper gh = new GeoserverHelper(config);
+				gh.waitForGeoserver();
+				gh.ensureEnvironment();
+				gh.performCleanup();
+			}
+		};
+
+		t.start();
+	}
 
 }
-- 
GitLab