1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that implements properties methods for metacat
|
4
|
* Copyright: 2008 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Michael Daigle
|
7
|
*
|
8
|
* '$Author: daigle $'
|
9
|
* '$Date: 2009-08-14 17:38:05 -0700 (Fri, 14 Aug 2009) $'
|
10
|
* '$Revision: 5028 $'
|
11
|
*
|
12
|
* This program is free software; you can redistribute it and/or modify
|
13
|
* it under the terms of the GNU General Public License as published by
|
14
|
* the Free Software Foundation; either version 2 of the License, or
|
15
|
* (at your option) any later version.
|
16
|
*
|
17
|
* This program is distributed in the hope that it will be useful,
|
18
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20
|
* GNU General Public License for more details.
|
21
|
*
|
22
|
* You should have received a copy of the GNU General Public License
|
23
|
* along with this program; if not, write to the Free Software
|
24
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25
|
*/
|
26
|
|
27
|
package edu.ucsb.nceas.metacat.properties;
|
28
|
|
29
|
import java.io.IOException;
|
30
|
import java.util.Map;
|
31
|
import java.util.Vector;
|
32
|
|
33
|
import javax.servlet.ServletContext;
|
34
|
import javax.servlet.http.HttpServletRequest;
|
35
|
|
36
|
import org.apache.log4j.Logger;
|
37
|
|
38
|
import edu.ucsb.nceas.metacat.shared.BaseService;
|
39
|
import edu.ucsb.nceas.metacat.shared.ServiceException;
|
40
|
import edu.ucsb.nceas.utilities.GeneralPropertyException;
|
41
|
import edu.ucsb.nceas.utilities.PropertiesMetaData;
|
42
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
43
|
import edu.ucsb.nceas.utilities.SortedProperties;
|
44
|
|
45
|
/**
|
46
|
* A suite of utility classes for the metadata configuration utility
|
47
|
*/
|
48
|
public class SimpleProperties extends BaseService implements PropertiesInterface {
|
49
|
|
50
|
private static SortedProperties sortedProperties = null;
|
51
|
|
52
|
private static Logger logMetacat = Logger.getLogger(SimpleProperties.class);
|
53
|
|
54
|
/**
|
55
|
* private constructor since this is a singleton
|
56
|
*
|
57
|
* @param servletContext the context we will use to get relative paths
|
58
|
*/
|
59
|
protected SimpleProperties() throws ServiceException {
|
60
|
_serviceName = "SimpleProperties";
|
61
|
|
62
|
initialize();
|
63
|
}
|
64
|
|
65
|
public boolean refreshable() {
|
66
|
return true;
|
67
|
}
|
68
|
|
69
|
public void doRefresh() throws ServiceException {
|
70
|
initialize();
|
71
|
}
|
72
|
|
73
|
public void stop() throws ServiceException {
|
74
|
return;
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Initialize the singleton.
|
79
|
*
|
80
|
* @param servletContext the context we will use to get relative paths
|
81
|
*/
|
82
|
private void initialize() throws ServiceException {
|
83
|
|
84
|
logMetacat.debug("Initializing SimpleProperties");
|
85
|
|
86
|
String mainConfigFilePath =
|
87
|
PropertyService.CONFIG_FILE_PATH;
|
88
|
sortedProperties = new SortedProperties(mainConfigFilePath);
|
89
|
|
90
|
try {
|
91
|
sortedProperties.load();
|
92
|
} catch (IOException ioe) {
|
93
|
throw new ServiceException("I/O problem while loading properties: "
|
94
|
+ ioe.getMessage());
|
95
|
}
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Utility method to get a property value from the properties file
|
100
|
*
|
101
|
* @param propertyName
|
102
|
* the name of the property requested
|
103
|
* @return the String value for the property
|
104
|
*/
|
105
|
public String getProperty(String propertyName)
|
106
|
throws PropertyNotFoundException {
|
107
|
return sortedProperties.getProperty(propertyName);
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Get a set of all property names.
|
112
|
*
|
113
|
* @return Set of property names
|
114
|
*/
|
115
|
public Vector<String> getPropertyNames() {
|
116
|
return sortedProperties.getPropertyNames();
|
117
|
}
|
118
|
|
119
|
|
120
|
/**
|
121
|
* Get a Set of all property names that start with the groupName prefix.
|
122
|
*
|
123
|
* @param groupName
|
124
|
* the prefix of the keys to search for.
|
125
|
* @return enumeration of property names
|
126
|
*/
|
127
|
public Vector<String> getPropertyNamesByGroup(String groupName) {
|
128
|
return sortedProperties.getPropertyNamesByGroup(groupName);
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Get a Map of all properties that start with the groupName prefix.
|
133
|
*
|
134
|
* @param groupName
|
135
|
* the prefix of the keys to search for.
|
136
|
* @return Map of property names
|
137
|
*/
|
138
|
public Map<String, String> getPropertiesByGroup(String groupName) throws PropertyNotFoundException {
|
139
|
return sortedProperties.getPropertiesByGroup(groupName);
|
140
|
}
|
141
|
|
142
|
/**
|
143
|
* Utility method to add a property value both in memory and to the
|
144
|
* properties file
|
145
|
*
|
146
|
* @param propertyName
|
147
|
* the name of the property to add
|
148
|
* @param newValue
|
149
|
* the new value for the property
|
150
|
*/
|
151
|
public void addProperty(String propertyName, String value) throws GeneralPropertyException {
|
152
|
sortedProperties.addProperty(propertyName, value);
|
153
|
sortedProperties.store();
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* Utility method to set a property value both in memory and to the
|
158
|
* properties file
|
159
|
*
|
160
|
* @param propertyName
|
161
|
* the name of the property requested
|
162
|
* @param newValue
|
163
|
* the new value for the property
|
164
|
*/
|
165
|
public void setProperty(String propertyName, String newValue) throws GeneralPropertyException {
|
166
|
sortedProperties.setProperty(propertyName, newValue);
|
167
|
sortedProperties.store();
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* Utility method to set a property value in memory. This will NOT cause the
|
172
|
* property to be written to disk. Use this method to set multiple
|
173
|
* properties in a row without causing excessive I/O. You must call
|
174
|
* persistProperties() once you're done setting properties to have them
|
175
|
* written to disk.
|
176
|
*
|
177
|
* @param propertyName
|
178
|
* the name of the property requested
|
179
|
* @param newValue
|
180
|
* the new value for the property
|
181
|
*/
|
182
|
public void setPropertyNoPersist(String propertyName, String newValue) throws GeneralPropertyException {
|
183
|
sortedProperties.setPropertyNoPersist(propertyName, newValue);
|
184
|
}
|
185
|
|
186
|
/**
|
187
|
* Save the properties to a properties file. Note, the
|
188
|
* order and comments will be preserved.
|
189
|
*/
|
190
|
public void persistProperties() throws GeneralPropertyException {
|
191
|
sortedProperties.store();
|
192
|
}
|
193
|
|
194
|
/**
|
195
|
* Take input from the user in an HTTP request about an property to be changed
|
196
|
* and update the metacat property file with that new value if it has
|
197
|
* changed from the value that was originally set.
|
198
|
*
|
199
|
* @param request
|
200
|
* that was generated by the user
|
201
|
* @param response
|
202
|
* to send output back to the user
|
203
|
* @param propertyName
|
204
|
* the name of the property to be checked and set
|
205
|
*/
|
206
|
public boolean checkAndSetProperty(HttpServletRequest request, String propertyName)
|
207
|
throws GeneralPropertyException {
|
208
|
boolean changed = false;
|
209
|
String value = getProperty(propertyName);
|
210
|
String newValue = request.getParameter(propertyName);
|
211
|
if (newValue != null && !newValue.trim().equals(value)) {
|
212
|
setPropertyNoPersist(propertyName, newValue.trim());
|
213
|
changed = true;
|
214
|
}
|
215
|
return changed;
|
216
|
}
|
217
|
|
218
|
public SortedProperties getMainBackupProperties() {
|
219
|
return sortedProperties;
|
220
|
}
|
221
|
|
222
|
public SortedProperties getAuthBackupProperties() throws GeneralPropertyException {
|
223
|
throw new GeneralPropertyException("SimpleProperties.getAuthBackupProperties - " +
|
224
|
"SimpleProperties does not support backup properties");
|
225
|
}
|
226
|
|
227
|
public PropertiesMetaData getMainMetaData() throws GeneralPropertyException {
|
228
|
throw new GeneralPropertyException("SimpleProperties.getMainMetaData - " +
|
229
|
"SimpleProperties does not support metadata");
|
230
|
}
|
231
|
|
232
|
public PropertiesMetaData getAuthMetaData() throws GeneralPropertyException {
|
233
|
throw new GeneralPropertyException("SimpleProperties.getAuthMetaData - " +
|
234
|
"SimpleProperties does not support auth metadata");
|
235
|
}
|
236
|
|
237
|
public void persistMainBackupProperties() throws GeneralPropertyException {
|
238
|
throw new GeneralPropertyException("SimpleProperties.persistMainBackupProperties - " +
|
239
|
"SimpleProperties does not support backup properties");
|
240
|
}
|
241
|
|
242
|
public void persistAuthBackupProperties(ServletContext servletContext) throws GeneralPropertyException {
|
243
|
throw new GeneralPropertyException("SimpleProperties.persistAuthBackupProperties - " +
|
244
|
"SimpleProperties does not support backup properties");
|
245
|
}
|
246
|
|
247
|
public boolean arePropertiesConfigured() throws GeneralPropertyException {
|
248
|
return true;
|
249
|
}
|
250
|
|
251
|
public boolean doBypass() throws GeneralPropertyException {
|
252
|
throw new GeneralPropertyException("SimpleProperties.doBypass - " +
|
253
|
"SimpleProperties does not support doBypass method.");
|
254
|
}
|
255
|
|
256
|
public void bypassConfiguration() throws GeneralPropertyException {
|
257
|
throw new GeneralPropertyException("SimpleProperties.doBypass - " +
|
258
|
"SimpleProperties does not support bypassConfiguration method.");
|
259
|
}
|
260
|
|
261
|
}
|