From 22ad1f4ce45615c888944b40f27916291f7ffad7 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Fri, 10 May 2013 11:03:06 -0400
Subject: [PATCH] Use SparkleShare icon instead of star icon for app menu

---
 src/main/java/com/gitblit/GitBlit.java |   91 ++++++++++++++++++++++++++++++++++-----------
 1 files changed, 69 insertions(+), 22 deletions(-)

diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java
index 3a79f8b..93293d8 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;
@@ -156,8 +160,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 +568,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) {
@@ -3462,21 +3488,10 @@
 				File base = com.gitblit.utils.FileUtils.resolveParameter(Constants.contextFolder$, contextFolder, path);
 				base.mkdirs();
 
-				// try to copy the data folder contents to the baseFolder
+				// try to extract the data folder resource to the baseFolder
 				File localSettings = new File(base, "gitblit.properties");
-				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));
-							}
-						}
-					}
+				if (!localSettings.exists()) {
+					extractResources(context, "/WEB-INF/data/", base);
 				}
 
 				// delegate all config to baseFolder/gitblit.properties file
@@ -3488,6 +3503,38 @@
 		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()) {
+				try {
+					if (resource.charAt(resource.length() - 1) == '/') {
+						// directory
+						f.mkdirs();
+						extractResources(context, resource, f);
+					} else {
+						// file
+						f.getParentFile().mkdirs();
+						InputStream is = context.getResourceAsStream(resource);
+						OutputStream os = new FileOutputStream(f);
+						byte [] buffer = new byte[4096];
+						int len = 0;
+						while ((len = is.read(buffer)) > -1) {
+							os.write(buffer, 0, len);
+						}
+						is.close();
+						os.close();
+					}
+				} catch (FileNotFoundException e) {
+					logger.error("Failed to find resource \"" + resource + "\"", e);
+				} catch (IOException e) {
+					logger.error("Failed to copy resource \"" + resource + "\" to " + f, e);
+				}
+			}
+		}
+	}
 
 	/**
 	 * Gitblit is being shutdown either because the servlet container is

--
Gitblit v1.9.1