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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 java.util.regex.Pattern;
  23. import com.gitblit.utils.FileUtils;
  24. /**
  25. * Dynamically loads and reloads a properties file by keeping track of the last
  26. * modification date.
  27. *
  28. * @author James Moger
  29. *
  30. */
  31. public class FileSettings extends IStoredSettings {
  32. protected final File propertiesFile;
  33. private final Properties properties = new Properties();
  34. private volatile long lastModified;
  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() && (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. } catch (FileNotFoundException f) {
  56. // IGNORE - won't happen because file.exists() check above
  57. } catch (Throwable t) {
  58. logger.error("Failed to read " + propertiesFile.getName(), t);
  59. } finally {
  60. if (is != null) {
  61. try {
  62. is.close();
  63. } catch (Throwable t) {
  64. // IGNORE
  65. }
  66. }
  67. }
  68. }
  69. return properties;
  70. }
  71. /**
  72. * Updates the specified settings in the settings file.
  73. */
  74. public synchronized boolean saveSettings(Map<String, String> settings) {
  75. String content = FileUtils.readContent(propertiesFile, "\n");
  76. for (Map.Entry<String, String> setting:settings.entrySet()) {
  77. String regex = "(?m)^(" + regExEscape(setting.getKey()) + "\\s*+=\\s*+)"
  78. + "(?:[^\r\n\\\\]++|\\\\(?:\r?\n|\r|.))*+$";
  79. content = content.replaceAll(regex, setting.getKey() + " = " + setting.getValue());
  80. }
  81. FileUtils.writeContent(propertiesFile, content);
  82. return true;
  83. }
  84. private String regExEscape(String input) {
  85. return input.replace(".", "\\.");
  86. }
  87. /**
  88. * @return the last modification date of the properties file
  89. */
  90. protected long lastModified() {
  91. return lastModified;
  92. }
  93. @Override
  94. public String toString() {
  95. return propertiesFile.getAbsolutePath();
  96. }
  97. }