]> source.dussan.org Git - gitblit.git/commitdiff
Supported include keys in gitblit.properties
authorJames Moger <james.moger@gitblit.com>
Sun, 26 Oct 2014 14:09:50 +0000 (10:09 -0400)
committerJames Moger <james.moger@gitblit.com>
Sun, 26 Oct 2014 14:09:50 +0000 (10:09 -0400)
This allows you to build a hierarchy of properties files or for a properties file
to include default settings.

src/main/java/com/gitblit/FileSettings.java

index 21a204356b54158def172845fd86229c7db98375..2f5f04f2a57fb9fb5b8bfc6ef1496f99699e2699 100644 (file)
@@ -18,10 +18,13 @@ package com.gitblit;
 import java.io.File;\r
 import java.io.FileInputStream;\r
 import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
+import java.util.List;\r
 import java.util.Map;\r
 import java.util.Properties;\r
 \r
 import com.gitblit.utils.FileUtils;\r
+import com.gitblit.utils.StringUtils;\r
 \r
 /**\r
  * Dynamically loads and reloads a properties file by keeping track of the last\r
@@ -81,6 +84,9 @@ public class FileSettings extends IStoredSettings {
                                is = new FileInputStream(propertiesFile);\r
                                props.load(is);\r
 \r
+                               // ticket-110\r
+                               props = readIncludes(props);\r
+\r
                                // load properties after we have successfully read file\r
                                properties.clear();\r
                                properties.putAll(props);\r
@@ -103,6 +109,55 @@ public class FileSettings extends IStoredSettings {
                return properties;\r
        }\r
 \r
+       /**\r
+        * Recursively read "include" properties files.\r
+        *\r
+        * @param properties\r
+        * @return\r
+        * @throws IOException\r
+        */\r
+       private Properties readIncludes(Properties properties) throws IOException {\r
+\r
+               Properties baseProperties = new Properties();\r
+\r
+               String include = (String) properties.remove("include");\r
+               if (!StringUtils.isEmpty(include)) {\r
+\r
+                       // allow for multiples\r
+                       List<String> names = StringUtils.getStringsFromValue(include, " ");\r
+                       for (String name : names) {\r
+\r
+                               // try co-located\r
+                               File file = new File(propertiesFile.getParentFile(), name.trim());\r
+                               if (!file.exists()) {\r
+                                       // try absolute path\r
+                                       file = new File(name.trim());\r
+                               }\r
+\r
+                               if (file.exists()) {\r
+                                       // load properties\r
+                                       try (FileInputStream iis = new FileInputStream(file)) {\r
+                                               baseProperties.load(iis);\r
+                                       }\r
+\r
+                                       // read nested includes\r
+                                       baseProperties = readIncludes(baseProperties);\r
+\r
+                               }\r
+\r
+                       }\r
+\r
+               }\r
+\r
+               // includes are "default" properties, they must be set first and the\r
+               // props which specified the "includes" must override\r
+               Properties merged = new Properties();\r
+               merged.putAll(baseProperties);\r
+               merged.putAll(properties);\r
+\r
+               return merged;\r
+       }\r
+\r
        @Override\r
        public boolean saveSettings() {\r
                String content = FileUtils.readContent(propertiesFile, "\n");\r