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