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.

IStoredSettings.java 9.8KB

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