You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileSettings.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.util.Map;
  21. import java.util.Properties;
  22. import com.gitblit.utils.FileUtils;
  23. /**
  24. * Dynamically loads and reloads a properties file by keeping track of the last
  25. * modification date.
  26. *
  27. * @author James Moger
  28. *
  29. */
  30. public class FileSettings extends IStoredSettings {
  31. protected final File propertiesFile;
  32. private final Properties properties = new Properties();
  33. private volatile long lastModified;
  34. private volatile boolean forceReload;
  35. public FileSettings(String file) {
  36. super(FileSettings.class);
  37. this.propertiesFile = new File(file);
  38. }
  39. /**
  40. * Returns a properties object which contains the most recent contents of
  41. * the properties file.
  42. */
  43. @Override
  44. protected synchronized Properties read() {
  45. if (propertiesFile.exists() && (forceReload || (propertiesFile.lastModified() > lastModified))) {
  46. FileInputStream is = null;
  47. try {
  48. Properties props = new Properties();
  49. is = new FileInputStream(propertiesFile);
  50. props.load(is);
  51. // load properties after we have successfully read file
  52. properties.clear();
  53. properties.putAll(props);
  54. lastModified = propertiesFile.lastModified();
  55. forceReload = false;
  56. } catch (FileNotFoundException f) {
  57. // IGNORE - won't happen because file.exists() check above
  58. } catch (Throwable t) {
  59. logger.error("Failed to read " + propertiesFile.getName(), t);
  60. } finally {
  61. if (is != null) {
  62. try {
  63. is.close();
  64. } catch (Throwable t) {
  65. // IGNORE
  66. }
  67. }
  68. }
  69. }
  70. return properties;
  71. }
  72. /**
  73. * Updates the specified settings in the settings file.
  74. */
  75. public synchronized boolean saveSettings(Map<String, String> settings) {
  76. String content = FileUtils.readContent(propertiesFile, "\n");
  77. for (Map.Entry<String, String> setting:settings.entrySet()) {
  78. String regex = "(?m)^(" + regExEscape(setting.getKey()) + "\\s*+=\\s*+)"
  79. + "(?:[^\r\n\\\\]++|\\\\(?:\r?\n|\r|.))*+$";
  80. String oldContent = content;
  81. content = content.replaceAll(regex, setting.getKey() + " = " + setting.getValue());
  82. if (content.equals(oldContent)) {
  83. // did not replace value because it does not exist in the file
  84. // append new setting to content (issue-85)
  85. content += "\n" + setting.getKey() + " = " + setting.getValue();
  86. }
  87. }
  88. FileUtils.writeContent(propertiesFile, content);
  89. // manually set the forceReload flag because not all JVMs support real
  90. // millisecond resolution of lastModified. (issue-55)
  91. forceReload = true;
  92. return true;
  93. }
  94. private String regExEscape(String input) {
  95. return input.replace(".", "\\.");
  96. }
  97. /**
  98. * @return the last modification date of the properties file
  99. */
  100. protected long lastModified() {
  101. return lastModified;
  102. }
  103. /**
  104. * @return the state of the force reload flag
  105. */
  106. protected boolean forceReload() {
  107. return forceReload;
  108. }
  109. @Override
  110. public String toString() {
  111. return propertiesFile.getAbsolutePath();
  112. }
  113. }