Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

FileSettings.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.ArrayList;
  21. import java.util.List;
  22. import java.util.Properties;
  23. import java.util.regex.PatternSyntaxException;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. /**
  27. * Reads GitBlit settings file.
  28. *
  29. */
  30. public class FileSettings implements IStoredSettings {
  31. private final Logger logger = LoggerFactory.getLogger(FileSettings.class);
  32. private Properties properties = new Properties();
  33. private long lastread;
  34. @Override
  35. public List<String> getAllKeys(String startingWith) {
  36. startingWith = startingWith.toLowerCase();
  37. List<String> keys = new ArrayList<String>();
  38. Properties props = read();
  39. for (Object o : props.keySet()) {
  40. String key = o.toString().toLowerCase();
  41. if (key.startsWith(startingWith)) {
  42. keys.add(key);
  43. }
  44. }
  45. return keys;
  46. }
  47. @Override
  48. public boolean getBoolean(String name, boolean defaultValue) {
  49. Properties props = read();
  50. if (props.containsKey(name)) {
  51. try {
  52. String value = props.getProperty(name);
  53. if (value != null && value.trim().length() > 0) {
  54. return Boolean.parseBoolean(value);
  55. }
  56. } catch (Exception e) {
  57. logger.warn("No override setting for " + name + " using default of " + defaultValue);
  58. }
  59. }
  60. return defaultValue;
  61. }
  62. @Override
  63. public int getInteger(String name, int defaultValue) {
  64. Properties props = read();
  65. if (props.containsKey(name)) {
  66. try {
  67. String value = props.getProperty(name);
  68. if (value != null && value.trim().length() > 0) {
  69. return Integer.parseInt(value);
  70. }
  71. } catch (Exception e) {
  72. logger.warn("No override setting for " + name + " using default of " + defaultValue);
  73. }
  74. }
  75. return defaultValue;
  76. }
  77. @Override
  78. public String getString(String name, String defaultValue) {
  79. Properties props = read();
  80. if (props.containsKey(name)) {
  81. try {
  82. String value = props.getProperty(name);
  83. if (value != null) {
  84. return value;
  85. }
  86. } catch (Exception e) {
  87. logger.warn("No override setting for " + name + " using default of " + defaultValue);
  88. }
  89. }
  90. return defaultValue;
  91. }
  92. @Override
  93. public List<String> getStrings(String name) {
  94. return getStrings(name, " ");
  95. }
  96. @Override
  97. public List<String> getStringsFromValue(String value) {
  98. return getStringsFromValue(value, " ");
  99. }
  100. @Override
  101. public List<String> getStrings(String name, String separator) {
  102. List<String> strings = new ArrayList<String>();
  103. Properties props = read();
  104. if (props.containsKey(name)) {
  105. String value = props.getProperty(name);
  106. strings = getStringsFromValue(value, separator);
  107. }
  108. return strings;
  109. }
  110. @Override
  111. public List<String> getStringsFromValue(String value, String separator) {
  112. List<String> strings = new ArrayList<String>();
  113. try {
  114. String[] chunks = value.split(separator);
  115. for (String chunk : chunks) {
  116. chunk = chunk.trim();
  117. if (chunk.length() > 0) {
  118. strings.add(chunk);
  119. }
  120. }
  121. } catch (PatternSyntaxException e) {
  122. logger.error("Failed to parse " + value, e);
  123. }
  124. return strings;
  125. }
  126. private synchronized Properties read() {
  127. File file = new File(Constants.PROPERTIES_FILE);
  128. if (file.exists() && (file.lastModified() > lastread)) {
  129. FileInputStream is = null;
  130. try {
  131. properties = new Properties();
  132. is = new FileInputStream(Constants.PROPERTIES_FILE);
  133. properties.load(is);
  134. lastread = file.lastModified();
  135. } catch (FileNotFoundException f) {
  136. // IGNORE - won't happen because file.exists() check above
  137. } catch (Throwable t) {
  138. t.printStackTrace();
  139. } finally {
  140. if (is != null) {
  141. try {
  142. is.close();
  143. } catch (Throwable t) {
  144. // IGNORE
  145. }
  146. }
  147. }
  148. }
  149. return properties;
  150. }
  151. @Override
  152. public String toString() {
  153. return new File(Constants.PROPERTIES_FILE).getAbsolutePath();
  154. }
  155. }