From 034e4bc7cd5ca3271e59ebdba1b25beb37b4b73c Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Tue, 28 May 2013 07:39:01 -0400
Subject: [PATCH] Enabled SparkleShare client menu using 1.1.0 invite handler redesign

---
 src/main/java/com/gitblit/GitBlit.java |  136 ++++++++++++++++++++++++++++++++++++---------
 1 files changed, 108 insertions(+), 28 deletions(-)

diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java
index 9346e0a..081b74a 100644
--- a/src/main/java/com/gitblit/GitBlit.java
+++ b/src/main/java/com/gitblit/GitBlit.java
@@ -18,9 +18,13 @@
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileFilter;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.OutputStream;
 import java.lang.reflect.Field;
 import java.lang.reflect.Type;
 import java.net.URI;
@@ -124,7 +128,6 @@
 import com.gitblit.wicket.GitBlitWebSession;
 import com.gitblit.wicket.WicketUtils;
 import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
 import com.google.gson.JsonIOException;
 import com.google.gson.JsonSyntaxException;
 import com.google.gson.reflect.TypeToken;
@@ -156,8 +159,7 @@
 	private final List<FederationModel> federationRegistrations = Collections
 			.synchronizedList(new ArrayList<FederationModel>());
 	
-	private final List<GitClientApplication> clientApplications = Collections
-			.synchronizedList(new ArrayList<GitClientApplication>());
+	private final ObjectCache<Collection<GitClientApplication>> clientApplications = new ObjectCache<Collection<GitClientApplication>>();
 
 	private final Map<String, FederationModel> federationPullResults = new ConcurrentHashMap<String, FederationModel>();
 
@@ -565,23 +567,46 @@
 	 * Returns the list of custom client applications to be used for the
 	 * repository url panel;
 	 * 
-	 * @return a list of client applications
+	 * @return a collection of client applications
 	 */
-	public List<GitClientApplication> getClientApplications() {
-		if (clientApplications.isEmpty()) {
+	public Collection<GitClientApplication> getClientApplications() {
+		// prefer user definitions, if they exist
+		File userDefs = new File(baseFolder, "clientapps.json");
+		if (userDefs.exists()) {
+			Date lastModified = new Date(userDefs.lastModified());
+			if (clientApplications.hasCurrent("user", lastModified)) {
+				return clientApplications.getObject("user");
+			} else {
+				// (re)load user definitions
+				try {
+					InputStream is = new FileInputStream(userDefs);
+					Collection<GitClientApplication> clients = readClientApplications(is);
+					is.close();
+					if (clients != null) {
+						clientApplications.updateObject("user", lastModified, clients);
+						return clients;
+					}				
+				} catch (IOException e) {
+					logger.error("Failed to deserialize " + userDefs.getAbsolutePath(), e);
+				}
+			}
+		}
+		
+		// no user definitions, use system definitions
+		if (!clientApplications.hasCurrent("system", new Date(0))) {
 			try {
 				InputStream is = getClass().getResourceAsStream("/clientapps.json");
 				Collection<GitClientApplication> clients = readClientApplications(is);
 				is.close();
 				if (clients != null) {
-					clientApplications.clear();
-					clientApplications.addAll(clients);
+					clientApplications.updateObject("system", new Date(0), clients);
 				}
 			} catch (IOException e) {
 				logger.error("Failed to deserialize clientapps.json resource!", e);
 			}
 		}
-		return clientApplications;
+		
+		return clientApplications.getObject("system");
 	}
 	
 	private Collection<GitClientApplication> readClientApplications(InputStream is) {
@@ -589,7 +614,7 @@
 			Type type = new TypeToken<Collection<GitClientApplication>>() {
 			}.getType();
 			InputStreamReader reader = new InputStreamReader(is);
-			Gson gson = new GsonBuilder().create();
+			Gson gson = JsonUtils.gson();
 			Collection<GitClientApplication> links = gson.fromJson(reader, type);
 			return links;
 		} catch (JsonIOException e) {
@@ -1339,11 +1364,12 @@
 
 				// optionally (re)calculate repository sizes
 				if (getBoolean(Keys.web.showRepositorySizes, true)) {
+					ByteFormat byteFormat = new ByteFormat();
 					msg = "{0} repositories identified with calculated folder sizes in {1} msecs";
 					for (String repository : repositories) {
 						RepositoryModel model = getRepositoryModel(repository);
 						if (!model.skipSizeCalculation) {
-							calculateSize(model);
+							model.size = byteFormat.format(calculateSize(model));
 						}
 					}
 				} else {
@@ -1534,6 +1560,10 @@
 			}
 
 			model.lastChange = JGitUtils.getLastChange(r);
+			if (!model.skipSizeCalculation) {
+				ByteFormat byteFormat = new ByteFormat();
+				model.size = byteFormat.format(calculateSize(model));
+			}
 		}
 		r.close();
 		
@@ -3412,7 +3442,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 +3475,29 @@
 				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");
-				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 (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 extract the data folder resource to the baseFolder
+				File localSettings = new File(base, "gitblit.properties");
+				if (!localSettings.exists()) {
+					extractResources(context, "/WEB-INF/data/", base);
+				}
+
 				// delegate all config to baseFolder/gitblit.properties file
 				FileSettings settings = new FileSettings(localSettings.getAbsolutePath());				
 				configureContext(settings, base, true);
@@ -3474,6 +3507,53 @@
 		settingsModel = loadSettingModels();
 		serverStatus.servletContainer = servletContext.getServerInfo();
 	}
+	
+	protected void extractResources(ServletContext context, String path, File toDir) {
+		for (String resource : context.getResourcePaths(path)) {
+			// extract the resource to the directory if it does not exist
+			File f = new File(toDir, resource.substring(path.length()));
+			if (!f.exists()) {
+				InputStream is = null;
+				OutputStream os = null;
+				try {
+					if (resource.charAt(resource.length() - 1) == '/') {
+						// directory
+						f.mkdirs();
+						extractResources(context, resource, f);
+					} else {
+						// file
+						f.getParentFile().mkdirs();
+						is = context.getResourceAsStream(resource);
+						os = new FileOutputStream(f);
+						byte [] buffer = new byte[4096];
+						int len = 0;
+						while ((len = is.read(buffer)) > -1) {
+							os.write(buffer, 0, len);
+						}
+					}
+				} catch (FileNotFoundException e) {
+					logger.error("Failed to find resource \"" + resource + "\"", e);
+				} catch (IOException e) {
+					logger.error("Failed to copy resource \"" + resource + "\" to " + f, e);
+				} finally {
+					if (is != null) {
+						try {
+							is.close();
+						} catch (IOException e) {
+							// ignore
+						}
+					}
+					if (os != null) {
+						try {
+							os.close();
+						} catch (IOException e) {
+							// ignore
+						}
+					}
+				}
+			}
+		}
+	}
 
 	/**
 	 * Gitblit is being shutdown either because the servlet container is

--
Gitblit v1.9.1