Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FileSettings.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import java.io.IOException;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Properties;
  24. import com.gitblit.utils.FileUtils;
  25. import com.gitblit.utils.StringUtils;
  26. /**
  27. * Dynamically loads and reloads a properties file by keeping track of the last
  28. * modification date.
  29. *
  30. * @author James Moger
  31. *
  32. */
  33. public class FileSettings extends IStoredSettings {
  34. protected File propertiesFile;
  35. private final Properties properties = new Properties();
  36. private volatile long lastModified;
  37. private volatile boolean forceReload;
  38. public FileSettings() {
  39. super(FileSettings.class);
  40. }
  41. public FileSettings(String file) {
  42. this();
  43. load(file);
  44. }
  45. public void load(String file) {
  46. this.propertiesFile = new File(file);
  47. }
  48. /**
  49. * Merges the provided settings into this instance. This will also
  50. * set the target file for this instance IFF it is unset AND the merge
  51. * source is also a FileSettings. This is a little sneaky.
  52. */
  53. @Override
  54. public void merge(IStoredSettings settings) {
  55. super.merge(settings);
  56. // sneaky: set the target file from the merge source
  57. if (propertiesFile == null && settings instanceof FileSettings) {
  58. this.propertiesFile = ((FileSettings) settings).propertiesFile;
  59. }
  60. }
  61. /**
  62. * Returns a properties object which contains the most recent contents of
  63. * the properties file.
  64. */
  65. @Override
  66. protected synchronized Properties read() {
  67. if (propertiesFile != null && propertiesFile.exists() && (forceReload || (propertiesFile.lastModified() > lastModified))) {
  68. FileInputStream is = null;
  69. try {
  70. logger.debug("loading {}", propertiesFile);
  71. Properties props = new Properties();
  72. is = new FileInputStream(propertiesFile);
  73. props.load(is);
  74. // ticket-110
  75. props = readIncludes(props);
  76. // load properties after we have successfully read file
  77. properties.clear();
  78. properties.putAll(props);
  79. lastModified = propertiesFile.lastModified();
  80. forceReload = false;
  81. } catch (FileNotFoundException f) {
  82. // IGNORE - won't happen because file.exists() check above
  83. } catch (Throwable t) {
  84. logger.error("Failed to read " + propertiesFile.getName(), t);
  85. } finally {
  86. if (is != null) {
  87. try {
  88. is.close();
  89. } catch (Throwable t) {
  90. // IGNORE
  91. }
  92. }
  93. }
  94. }
  95. return properties;
  96. }
  97. /**
  98. * Recursively read "include" properties files.
  99. *
  100. * @param properties
  101. * @return
  102. * @throws IOException
  103. */
  104. private Properties readIncludes(Properties properties) throws IOException {
  105. Properties baseProperties = new Properties();
  106. String include = (String) properties.remove("include");
  107. if (!StringUtils.isEmpty(include)) {
  108. // allow for multiples
  109. List<String> names = StringUtils.getStringsFromValue(include, ",");
  110. for (String name : names) {
  111. if (StringUtils.isEmpty(name)) {
  112. continue;
  113. }
  114. // try co-located
  115. File file = new File(propertiesFile.getParentFile(), name.trim());
  116. if (!file.exists()) {
  117. // try absolute path
  118. file = new File(name.trim());
  119. }
  120. if (!file.exists()) {
  121. logger.warn("failed to locate {}", file);
  122. continue;
  123. }
  124. // load properties
  125. logger.debug("loading {}", file);
  126. try (FileInputStream iis = new FileInputStream(file)) {
  127. baseProperties.load(iis);
  128. }
  129. // read nested includes
  130. baseProperties = readIncludes(baseProperties);
  131. }
  132. }
  133. // includes are "default" properties, they must be set first and the
  134. // props which specified the "includes" must override
  135. Properties merged = new Properties();
  136. merged.putAll(baseProperties);
  137. merged.putAll(properties);
  138. return merged;
  139. }
  140. @Override
  141. public boolean saveSettings() {
  142. String content = FileUtils.readContent(propertiesFile, "\n");
  143. for (String key : removals) {
  144. String regex = "(?m)^(" + regExEscape(key) + "\\s*+=\\s*+)"
  145. + "(?:[^\r\n\\\\]++|\\\\(?:\r?\n|\r|.))*+$";
  146. content = content.replaceAll(regex, "");
  147. }
  148. removals.clear();
  149. FileUtils.writeContent(propertiesFile, content);
  150. // manually set the forceReload flag because not all JVMs support real
  151. // millisecond resolution of lastModified. (issue-55)
  152. forceReload = true;
  153. return true;
  154. }
  155. /**
  156. * Updates the specified settings in the settings file.
  157. */
  158. @Override
  159. public synchronized boolean saveSettings(Map<String, String> settings) {
  160. String content = FileUtils.readContent(propertiesFile, "\n");
  161. for (Map.Entry<String, String> setting:settings.entrySet()) {
  162. String regex = "(?m)^(" + regExEscape(setting.getKey()) + "\\s*+=\\s*+)"
  163. + "(?:[^\r\n\\\\]++|\\\\(?:\r?\n|\r|.))*+$";
  164. String oldContent = content;
  165. content = content.replaceAll(regex, setting.getKey() + " = " + setting.getValue());
  166. if (content.equals(oldContent)) {
  167. // did not replace value because it does not exist in the file
  168. // append new setting to content (issue-85)
  169. content += "\n" + setting.getKey() + " = " + setting.getValue();
  170. }
  171. }
  172. FileUtils.writeContent(propertiesFile, content);
  173. // manually set the forceReload flag because not all JVMs support real
  174. // millisecond resolution of lastModified. (issue-55)
  175. forceReload = true;
  176. return true;
  177. }
  178. private String regExEscape(String input) {
  179. return input.replace(".", "\\.").replace("$", "\\$").replace("{", "\\{");
  180. }
  181. /**
  182. * @return the last modification date of the properties file
  183. */
  184. protected long lastModified() {
  185. return lastModified;
  186. }
  187. /**
  188. * @return the state of the force reload flag
  189. */
  190. protected boolean forceReload() {
  191. return forceReload;
  192. }
  193. @Override
  194. public String toString() {
  195. if (propertiesFile == null) {
  196. return "[empty]";
  197. }
  198. return propertiesFile.getAbsolutePath();
  199. }
  200. }