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 4.2KB

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