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