From 9e7041fad08a4e83be6ef18d30f0902b8a39bcdb Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Fri, 10 May 2013 08:20:39 -0400
Subject: [PATCH] Fix NPEs when initializing on a servlet container which returns a null contextFolder

---
 releases.moxie                         |    3 +++
 src/main/java/com/gitblit/GitBlit.java |   40 +++++++++++++++++++++++++++-------------
 2 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/releases.moxie b/releases.moxie
index 0ac22c6..64544bc 100644
--- a/releases.moxie
+++ b/releases.moxie
@@ -16,6 +16,7 @@
 	 - Use bash instead of sh in Linux/OSX shell scripts (issue 154)
 	 - Fix NPE when getting user's fork without repository list caching (issue 182)
 	 - Fix internal error on folder history links (issue 192)
+	 - Fix NPEs when initializing the context on a servlet containers which returns a null contextFolder (issue 199)
 	 - Fixed incorrect icon file name for .doc files (issue 200)
 	 - Do not queue emails with no recipients (issue 201)
 	 - Disable view and blame links for deleted blobs (issue 216)
@@ -27,6 +28,7 @@
 	 - Use standard ServletRequestWrapper instead of custom wrapper (issue 224)
 
 	changes:
+	 - Improved error logging for servlet containers which provide a null contextFolder (issue 199)
 	 - Improved the repository url display.  This display now indicates your repository access permission, per-protocol.
 	 - Improve Gerrit change ref decoration in the refs panel (issue 206)
  	 - Disable Gson's pretty printing which has a huge performance gain
@@ -64,6 +66,7 @@
 	- Lee Grofit
 	- Lukasz Jader
 	- Martijn Laan
+	- Matthias Bauer
 	- Michael Schaefers
 	- Philip Boutros
 	- Rafael Cavazin
diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java
index 9346e0a..3a79f8b 100644
--- a/src/main/java/com/gitblit/GitBlit.java
+++ b/src/main/java/com/gitblit/GitBlit.java
@@ -3412,7 +3412,8 @@
 			// Gitblit is running in a servlet container
 			ServletContext context = contextEvent.getServletContext();
 			WebXmlSettings webxmlSettings = new WebXmlSettings(context);
-			File contextFolder = new File(context.getRealPath("/"));
+			String contextRealPath = context.getRealPath("/");
+			File contextFolder = (contextRealPath != null) ? new File(contextRealPath) : null;
 			String openShift = System.getenv("OPENSHIFT_DATA_DIR");
 			
 			if (!StringUtils.isEmpty(openShift)) {
@@ -3444,27 +3445,40 @@
 				configureContext(webxmlSettings, base, true);
 			} else {
 				// Gitblit is running in a standard servlet container
-				logger.info("WAR contextFolder is " + contextFolder.getAbsolutePath());
+				logger.info("WAR contextFolder is " + ((contextFolder != null) ? contextFolder.getAbsolutePath() : "<empty>"));
 				
 				String path = webxmlSettings.getString(Constants.baseFolder, Constants.contextFolder$ + "/WEB-INF/data");
+				
+				if (path.contains(Constants.contextFolder$) && contextFolder == null) {
+					// warn about null contextFolder (issue-199)
+					logger.error("");
+					logger.error(MessageFormat.format("\"{0}\" depends on \"{1}\" but \"{2}\" is returning NULL for \"{1}\"!",
+							Constants.baseFolder, Constants.contextFolder$, context.getServerInfo()));
+					logger.error(MessageFormat.format("Please specify a non-parameterized path for <context-param> {0} in web.xml!!", Constants.baseFolder));
+					logger.error(MessageFormat.format("OR configure your servlet container to specify a \"{0}\" parameter in the context configuration!!", Constants.baseFolder));
+					logger.error("");
+				}
+				
 				File base = com.gitblit.utils.FileUtils.resolveParameter(Constants.contextFolder$, contextFolder, path);
 				base.mkdirs();
-				
+
 				// try to copy the data folder contents to the baseFolder
 				File localSettings = new File(base, "gitblit.properties");
-				if (!localSettings.exists()) {
-					File contextData = new File(contextFolder, "/WEB-INF/data");
-					if (!base.equals(contextData)) {
-						try {
-							com.gitblit.utils.FileUtils.copy(base, contextData.listFiles());
-						} catch (IOException e) {
-							logger.error(MessageFormat.format(
-									"Failed to copy included data from {0} to {1}",
-								contextData, base));
+				if (contextFolder != null) {
+					if (!localSettings.exists()) {
+						File contextData = new File(contextFolder, "/WEB-INF/data");
+						if (!base.equals(contextData)) {
+							try {
+								com.gitblit.utils.FileUtils.copy(base, contextData.listFiles());
+							} catch (IOException e) {
+								logger.error(MessageFormat.format(
+										"Failed to copy included data from {0} to {1}",
+										contextData, base));
+							}
 						}
 					}
 				}
-				
+
 				// delegate all config to baseFolder/gitblit.properties file
 				FileSettings settings = new FileSettings(localSettings.getAbsolutePath());				
 				configureContext(settings, base, true);

--
Gitblit v1.9.1