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