Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

IStoredSettings.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Properties;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import com.gitblit.utils.StringUtils;
  24. /**
  25. * Base class for stored settings implementations.
  26. *
  27. * @author James Moger
  28. *
  29. */
  30. public abstract class IStoredSettings {
  31. protected final Logger logger;
  32. protected final Properties overrides = new Properties();
  33. public IStoredSettings(Class<? extends IStoredSettings> clazz) {
  34. logger = LoggerFactory.getLogger(clazz);
  35. }
  36. protected abstract Properties read();
  37. private Properties getSettings() {
  38. Properties props = read();
  39. props.putAll(overrides);
  40. return props;
  41. }
  42. /**
  43. * Returns the list of keys whose name starts with the specified prefix. If
  44. * the prefix is null or empty, all key names are returned.
  45. *
  46. * @param startingWith
  47. * @return list of keys
  48. */
  49. public List<String> getAllKeys(String startingWith) {
  50. List<String> keys = new ArrayList<String>();
  51. Properties props = getSettings();
  52. if (StringUtils.isEmpty(startingWith)) {
  53. keys.addAll(props.stringPropertyNames());
  54. } else {
  55. startingWith = startingWith.toLowerCase();
  56. for (Object o : props.keySet()) {
  57. String key = o.toString();
  58. if (key.toLowerCase().startsWith(startingWith)) {
  59. keys.add(key);
  60. }
  61. }
  62. }
  63. return keys;
  64. }
  65. /**
  66. * Returns the boolean value for the specified key. If the key does not
  67. * exist or the value for the key can not be interpreted as a boolean, the
  68. * defaultValue is returned.
  69. *
  70. * @param key
  71. * @param defaultValue
  72. * @return key value or defaultValue
  73. */
  74. public boolean getBoolean(String name, boolean defaultValue) {
  75. Properties props = getSettings();
  76. if (props.containsKey(name)) {
  77. String value = props.getProperty(name);
  78. if (!StringUtils.isEmpty(value)) {
  79. return Boolean.parseBoolean(value.trim());
  80. }
  81. }
  82. return defaultValue;
  83. }
  84. /**
  85. * Returns the integer value for the specified key. If the key does not
  86. * exist or the value for the key can not be interpreted as an integer, the
  87. * defaultValue is returned.
  88. *
  89. * @param key
  90. * @param defaultValue
  91. * @return key value or defaultValue
  92. */
  93. public int getInteger(String name, int defaultValue) {
  94. Properties props = getSettings();
  95. if (props.containsKey(name)) {
  96. try {
  97. String value = props.getProperty(name);
  98. if (!StringUtils.isEmpty(value)) {
  99. return Integer.parseInt(value.trim());
  100. }
  101. } catch (NumberFormatException e) {
  102. logger.warn("Failed to parse integer for " + name + " using default of "
  103. + defaultValue);
  104. }
  105. }
  106. return defaultValue;
  107. }
  108. /**
  109. * Returns the char value for the specified key. If the key does not exist
  110. * or the value for the key can not be interpreted as a char, the
  111. * defaultValue is returned.
  112. *
  113. * @param key
  114. * @param defaultValue
  115. * @return key value or defaultValue
  116. */
  117. public char getChar(String name, char defaultValue) {
  118. Properties props = getSettings();
  119. if (props.containsKey(name)) {
  120. String value = props.getProperty(name);
  121. if (!StringUtils.isEmpty(value)) {
  122. return value.trim().charAt(0);
  123. }
  124. }
  125. return defaultValue;
  126. }
  127. /**
  128. * Returns the string value for the specified key. If the key does not exist
  129. * or the value for the key can not be interpreted as a string, the
  130. * defaultValue is returned.
  131. *
  132. * @param key
  133. * @param defaultValue
  134. * @return key value or defaultValue
  135. */
  136. public String getString(String name, String defaultValue) {
  137. Properties props = getSettings();
  138. if (props.containsKey(name)) {
  139. String value = props.getProperty(name);
  140. if (value != null) {
  141. return value.trim();
  142. }
  143. }
  144. return defaultValue;
  145. }
  146. /**
  147. * Returns a list of space-separated strings from the specified key.
  148. *
  149. * @param name
  150. * @return list of strings
  151. */
  152. public List<String> getStrings(String name) {
  153. return getStrings(name, " ");
  154. }
  155. /**
  156. * Returns a list of strings from the specified key using the specified
  157. * string separator.
  158. *
  159. * @param name
  160. * @param separator
  161. * @return list of strings
  162. */
  163. public List<String> getStrings(String name, String separator) {
  164. List<String> strings = new ArrayList<String>();
  165. Properties props = getSettings();
  166. if (props.containsKey(name)) {
  167. String value = props.getProperty(name);
  168. strings = StringUtils.getStringsFromValue(value, separator);
  169. }
  170. return strings;
  171. }
  172. /**
  173. * Override the specified key with the specified value.
  174. *
  175. * @param key
  176. * @param value
  177. */
  178. public void overrideSetting(String key, String value) {
  179. overrides.put(key, value);
  180. }
  181. /**
  182. * Updates the values for the specified keys and persists the entire
  183. * configuration file.
  184. *
  185. * @param map
  186. * of key, value pairs
  187. * @return true if successful
  188. */
  189. public abstract boolean saveSettings(Map<String, String> updatedSettings);
  190. }