Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

IStoredSettings.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.LinkedHashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Properties;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import com.gitblit.utils.StringUtils;
  25. /**
  26. * Base class for stored settings implementations.
  27. *
  28. * @author James Moger
  29. *
  30. */
  31. public abstract class IStoredSettings {
  32. protected final Logger logger;
  33. protected final Properties overrides = new Properties();
  34. public IStoredSettings(Class<? extends IStoredSettings> clazz) {
  35. logger = LoggerFactory.getLogger(clazz);
  36. }
  37. protected abstract Properties read();
  38. private Properties getSettings() {
  39. Properties props = read();
  40. props.putAll(overrides);
  41. return props;
  42. }
  43. /**
  44. * Returns the list of keys whose name starts with the specified prefix. If
  45. * the prefix is null or empty, all key names are returned.
  46. *
  47. * @param startingWith
  48. * @return list of keys
  49. */
  50. public List<String> getAllKeys(String startingWith) {
  51. List<String> keys = new ArrayList<String>();
  52. Properties props = getSettings();
  53. if (StringUtils.isEmpty(startingWith)) {
  54. keys.addAll(props.stringPropertyNames());
  55. } else {
  56. startingWith = startingWith.toLowerCase();
  57. for (Object o : props.keySet()) {
  58. String key = o.toString();
  59. if (key.toLowerCase().startsWith(startingWith)) {
  60. keys.add(key);
  61. }
  62. }
  63. }
  64. return keys;
  65. }
  66. /**
  67. * Returns the boolean value for the specified key. If the key does not
  68. * exist or the value for the key can not be interpreted as a boolean, the
  69. * defaultValue is returned.
  70. *
  71. * @param key
  72. * @param defaultValue
  73. * @return key value or defaultValue
  74. */
  75. public boolean getBoolean(String name, boolean defaultValue) {
  76. Properties props = getSettings();
  77. if (props.containsKey(name)) {
  78. String value = props.getProperty(name);
  79. if (!StringUtils.isEmpty(value)) {
  80. return Boolean.parseBoolean(value.trim());
  81. }
  82. }
  83. return defaultValue;
  84. }
  85. /**
  86. * Returns the integer value for the specified key. If the key does not
  87. * exist or the value for the key can not be interpreted as an integer, the
  88. * defaultValue is returned.
  89. *
  90. * @param key
  91. * @param defaultValue
  92. * @return key value or defaultValue
  93. */
  94. public int getInteger(String name, int defaultValue) {
  95. Properties props = getSettings();
  96. if (props.containsKey(name)) {
  97. try {
  98. String value = props.getProperty(name);
  99. if (!StringUtils.isEmpty(value)) {
  100. return Integer.parseInt(value.trim());
  101. }
  102. } catch (NumberFormatException e) {
  103. logger.warn("Failed to parse integer for " + name + " using default of "
  104. + defaultValue);
  105. }
  106. }
  107. return defaultValue;
  108. }
  109. /**
  110. * Returns the long value for the specified key. If the key does not
  111. * exist or the value for the key can not be interpreted as an long, the
  112. * defaultValue is returned.
  113. *
  114. * @param key
  115. * @param defaultValue
  116. * @return key value or defaultValue
  117. */
  118. public long getLong(String name, long defaultValue) {
  119. Properties props = getSettings();
  120. if (props.containsKey(name)) {
  121. try {
  122. String value = props.getProperty(name);
  123. if (!StringUtils.isEmpty(value)) {
  124. return Long.parseLong(value.trim());
  125. }
  126. } catch (NumberFormatException e) {
  127. logger.warn("Failed to parse long for " + name + " using default of "
  128. + defaultValue);
  129. }
  130. }
  131. return defaultValue;
  132. }
  133. /**
  134. * Returns an int filesize from a string value such as 50m or 50mb
  135. * @param name
  136. * @param defaultValue
  137. * @return an int filesize or defaultValue if the key does not exist or can
  138. * not be parsed
  139. */
  140. public int getFilesize(String name, int defaultValue) {
  141. String val = getString(name, null);
  142. if (StringUtils.isEmpty(val)) {
  143. return defaultValue;
  144. }
  145. return com.gitblit.utils.FileUtils.convertSizeToInt(val, defaultValue);
  146. }
  147. /**
  148. * Returns an long filesize from a string value such as 50m or 50mb
  149. * @param name
  150. * @param defaultValue
  151. * @return a long filesize or defaultValue if the key does not exist or can
  152. * not be parsed
  153. */
  154. public long getFilesize(String key, long defaultValue) {
  155. String val = getString(key, null);
  156. if (StringUtils.isEmpty(val)) {
  157. return defaultValue;
  158. }
  159. return com.gitblit.utils.FileUtils.convertSizeToLong(val, defaultValue);
  160. }
  161. /**
  162. * Returns the char value for the specified key. If the key does not exist
  163. * or the value for the key can not be interpreted as a char, the
  164. * defaultValue is returned.
  165. *
  166. * @param key
  167. * @param defaultValue
  168. * @return key value or defaultValue
  169. */
  170. public char getChar(String name, char defaultValue) {
  171. Properties props = getSettings();
  172. if (props.containsKey(name)) {
  173. String value = props.getProperty(name);
  174. if (!StringUtils.isEmpty(value)) {
  175. return value.trim().charAt(0);
  176. }
  177. }
  178. return defaultValue;
  179. }
  180. /**
  181. * Returns the string value for the specified key. If the key does not exist
  182. * or the value for the key can not be interpreted as a string, the
  183. * defaultValue is returned.
  184. *
  185. * @param key
  186. * @param defaultValue
  187. * @return key value or defaultValue
  188. */
  189. public String getString(String name, String defaultValue) {
  190. Properties props = getSettings();
  191. if (props.containsKey(name)) {
  192. String value = props.getProperty(name);
  193. if (value != null) {
  194. return value.trim();
  195. }
  196. }
  197. return defaultValue;
  198. }
  199. /**
  200. * Returns the string value for the specified key. If the key does not
  201. * exist an exception is thrown.
  202. *
  203. * @param key
  204. * @return key value
  205. */
  206. public String getRequiredString(String name) {
  207. Properties props = getSettings();
  208. if (props.containsKey(name)) {
  209. String value = props.getProperty(name);
  210. if (value != null) {
  211. return value.trim();
  212. }
  213. }
  214. throw new RuntimeException("Property (" + name + ") does not exist");
  215. }
  216. /**
  217. * Returns a list of space-separated strings from the specified key.
  218. *
  219. * @param name
  220. * @return list of strings
  221. */
  222. public List<String> getStrings(String name) {
  223. return getStrings(name, " ");
  224. }
  225. /**
  226. * Returns a list of strings from the specified key using the specified
  227. * string separator.
  228. *
  229. * @param name
  230. * @param separator
  231. * @return list of strings
  232. */
  233. public List<String> getStrings(String name, String separator) {
  234. List<String> strings = new ArrayList<String>();
  235. Properties props = getSettings();
  236. if (props.containsKey(name)) {
  237. String value = props.getProperty(name);
  238. strings = StringUtils.getStringsFromValue(value, separator);
  239. }
  240. return strings;
  241. }
  242. /**
  243. * Returns a map of strings from the specified key.
  244. *
  245. * @param name
  246. * @return map of string, string
  247. */
  248. public Map<String, String> getMap(String name) {
  249. Map<String, String> map = new LinkedHashMap<String, String>();
  250. for (String string : getStrings(name)) {
  251. String[] kvp = string.split("=", 2);
  252. String key = kvp[0];
  253. String value = kvp[1];
  254. map.put(key, value);
  255. }
  256. return map;
  257. }
  258. /**
  259. * Override the specified key with the specified value.
  260. *
  261. * @param key
  262. * @param value
  263. */
  264. public void overrideSetting(String key, String value) {
  265. overrides.put(key, value);
  266. }
  267. /**
  268. * Override the specified key with the specified value.
  269. *
  270. * @param key
  271. * @param value
  272. */
  273. public void overrideSetting(String key, int value) {
  274. overrides.put(key, "" + value);
  275. }
  276. /**
  277. * Updates the values for the specified keys and persists the entire
  278. * configuration file.
  279. *
  280. * @param map
  281. * of key, value pairs
  282. * @return true if successful
  283. */
  284. public abstract boolean saveSettings(Map<String, String> updatedSettings);
  285. }