Project

General

Profile

« Previous | Next » 

Revision 5033

Added by daigle about 15 years ago

Make properties handlers implement an interface so you can use configurable or non-configurable properties.

View differences:

src/edu/ucsb/nceas/metacat/properties/SimpleProperties.java
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 set a property value both in memory and to the
144
	 * properties file
145
	 * 
146
	 * @param propertyName
147
	 *            the name of the property requested
148
	 * @param newValue
149
	 *            the new value for the property
150
	 */
151
	public void setProperty(String propertyName, String newValue) throws GeneralPropertyException {
152
		sortedProperties.setProperty(propertyName, newValue);
153
		sortedProperties.store();
154
	}
155

  
156
	/**
157
	 * Utility method to set a property value in memory. This will NOT cause the
158
	 * property to be written to disk. Use this method to set multiple
159
	 * properties in a row without causing excessive I/O. You must call
160
	 * persistProperties() once you're done setting properties to have them
161
	 * written to disk.
162
	 * 
163
	 * @param propertyName
164
	 *            the name of the property requested
165
	 * @param newValue
166
	 *            the new value for the property
167
	 */
168
	public void setPropertyNoPersist(String propertyName, String newValue) throws GeneralPropertyException {
169
		sortedProperties.setPropertyNoPersist(propertyName, newValue);
170
	}
171

  
172
	/**
173
	 * Save the properties to a properties file. Note, the 
174
	 * order and comments will be preserved.
175
	 */
176
	public void persistProperties() throws GeneralPropertyException {
177
		sortedProperties.store();
178
	}
179
	
180
	/**
181
	 * Take input from the user in an HTTP request about an property to be changed
182
	 * and update the metacat property file with that new value if it has
183
	 * changed from the value that was originally set.
184
	 * 
185
	 * @param request
186
	 *            that was generated by the user
187
	 * @param response
188
	 *            to send output back to the user
189
	 * @param propertyName
190
	 *            the name of the property to be checked and set
191
	 */
192
	public void checkAndSetProperty(HttpServletRequest request, String propertyName) 
193
			throws GeneralPropertyException {
194
		String value = getProperty(propertyName);
195
		String newValue = request.getParameter(propertyName);
196
		if (newValue != null && !newValue.trim().equals(value)) {
197
			setPropertyNoPersist(propertyName, newValue.trim());
198
		}
199
	}
200
	
201
	public SortedProperties getMainBackupProperties() {
202
		return sortedProperties;
203
	}
204
	
205
	public  SortedProperties getAuthBackupProperties() throws GeneralPropertyException {
206
		throw new GeneralPropertyException("SimpleProperties.getAuthBackupProperties - " +
207
				"SimpleProperties does not support backup properties");
208
	}
209
	
210
	public PropertiesMetaData getMainMetaData() throws GeneralPropertyException {
211
		throw new GeneralPropertyException("SimpleProperties.getMainMetaData - " +
212
				"SimpleProperties does not support metadata");
213
	}
214
	
215
	public PropertiesMetaData getAuthMetaData() throws GeneralPropertyException {
216
		throw new GeneralPropertyException("SimpleProperties.getAuthMetaData - " +
217
				"SimpleProperties does not support auth metadata");
218
	}
219
	
220
	public void persistMainBackupProperties() throws GeneralPropertyException {
221
		throw new GeneralPropertyException("SimpleProperties.persistMainBackupProperties - " +
222
			"SimpleProperties does not support backup properties");
223
	}
224
	
225
	public void persistAuthBackupProperties(ServletContext servletContext) throws GeneralPropertyException {
226
		throw new GeneralPropertyException("SimpleProperties.persistAuthBackupProperties - " +
227
				"SimpleProperties does not support backup properties");
228
	}
229
	
230
	public boolean arePropertiesConfigured()  throws GeneralPropertyException {
231
		return true;
232
	}
233
	
234
	public boolean doBypass() throws GeneralPropertyException {
235
		throw new GeneralPropertyException("SimpleProperties.doBypass - " +
236
			"SimpleProperties does not support doBypass method.");
237
	}
238
	
239
	public void bypassConfiguration()  throws GeneralPropertyException {
240
		throw new GeneralPropertyException("SimpleProperties.doBypass - " +
241
			"SimpleProperties does not support bypassConfiguration method.");
242
	}
243

  
244
}
0 245

  
src/edu/ucsb/nceas/metacat/properties/SkinPropertyService.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements properties methods for metacat
4
 *             skins
5
 *  Copyright: 2008 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Michael Daigle
8
 *
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

  
28
package edu.ucsb.nceas.metacat.properties;
29

  
30
import java.io.IOException;
31
import java.util.HashMap;
32
import java.util.Map;
33
import java.util.Set;
34
import java.util.Vector;
35

  
36

  
37
import javax.servlet.http.HttpServletRequest;
38
import javax.xml.transform.TransformerException;
39

  
40
import org.apache.log4j.Logger;
41

  
42
import edu.ucsb.nceas.metacat.service.ServiceService;
43
import edu.ucsb.nceas.metacat.shared.BaseService;
44
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
45
import edu.ucsb.nceas.metacat.shared.ServiceException;
46
import edu.ucsb.nceas.metacat.util.SkinUtil;
47
import edu.ucsb.nceas.utilities.FileUtil;
48
import edu.ucsb.nceas.utilities.GeneralPropertyException;
49
import edu.ucsb.nceas.utilities.MetaDataProperty;
50
import edu.ucsb.nceas.utilities.PropertiesMetaData;
51
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
52
import edu.ucsb.nceas.utilities.SortedProperties;
53

  
54
/**
55
 * A suite of utility classes for the skin configuration utility
56
 */
57
public class SkinPropertyService extends BaseService {
58

  
59
	private static SkinPropertyService skinService = null;
60

  
61
	private static boolean bypassAlreadyChecked = false;
62

  
63
	private static String BACKUP_DIR = null;
64

  
65
	private static Vector<String> skinNames = null;
66

  
67
	private static HashMap<String, SortedProperties> skinPropertiesMap = null;
68
	private static HashMap<String, SortedProperties> skinBackupPropertiesMap = null;
69
	private static HashMap<String, PropertiesMetaData> skinMetaDataMap = null;
70

  
71
	private static Logger logMetacat = Logger.getLogger(SkinPropertyService.class);
72

  
73
	/**
74
	 * private constructor since this is a singleton
75
	 * 
76
	 * @param servletContext
77
	 *            the context we will use to get relative paths
78
	 */
79
	private SkinPropertyService() throws ServiceException {
80
		try {
81
			_serviceName = "SkinPropertyService";
82
			
83
			initialize();
84
		} catch (GeneralPropertyException gpe) {
85
			throw new ServiceException(
86
					"Properties problem while initializing SkinPropertyService: "
87
							+ gpe.getMessage());
88
		} catch (IOException ioe) {
89
			throw new ServiceException("I/O Problem while initializing SkinPropertyService: "
90
					+ ioe.getMessage());
91
		}
92
	}
93

  
94
	/**
95
	 * Get the single instance of SkinPropertyService.
96
	 * 
97
	 * @param servletContext
98
	 *            the context we will use to get relative paths
99
	 * @return the single instance of SkinPropertyService
100
	 */
101
	public static SkinPropertyService getInstance() throws ServiceException {
102
		if (skinService == null) {
103
			skinService = new SkinPropertyService();
104
		}
105
		return skinService;
106
	}
107

  
108
	public boolean refreshable() {
109
		return true;
110
	}
111

  
112
	public void doRefresh() throws ServiceException {
113
		try {
114
			initialize();
115
		} catch (IOException ioe) {
116
			throw new ServiceException("Could not refresh SkinPropertyService due to"
117
					+ " I/O error: " + ioe.getMessage());
118
		} catch (GeneralPropertyException gpe) {
119
			throw new ServiceException("Could not refresh SkinPropertyService due to"
120
					+ " property error: " + gpe.getMessage());
121
		}
122
	}
123
	
124
	public void stop() throws ServiceException {
125
		return;
126
	}
127

  
128
	/**
129
	 * Initialize the singleton.
130
	 * 
131
	 * @param servletContext
132
	 *            the context we will use to get relative paths
133
	 */
134
	private void initialize() throws IOException, GeneralPropertyException,
135
			ServiceException {
136

  
137
		logMetacat.debug("Initializing SkinService");
138

  
139
		BACKUP_DIR = PropertyService.getProperty("application.backupDir");
140

  
141
		skinNames = SkinUtil.getSkinNames();
142

  
143
		skinPropertiesMap = new HashMap<String, SortedProperties>();
144
		skinBackupPropertiesMap = new HashMap<String, SortedProperties>();
145
		skinMetaDataMap = new HashMap<String, PropertiesMetaData>();
146

  
147
		try {
148
			for (String skinName : skinNames) {
149
				String propertyFilePath = ServiceService.getRealSkinDir()
150
						+ FileUtil.getFS() + skinName + FileUtil.getFS() + skinName
151
						+ ".properties";
152

  
153
				if (FileUtil.getFileStatus(propertyFilePath) < FileUtil.EXISTS_READ_WRITABLE) {
154
					logMetacat.error("Skin property file: " + propertyFilePath
155
							+ " does not exist read/writable. This skin will not be available.");
156
					continue;
157
				}
158

  
159
				SortedProperties skinProperties = new SortedProperties(propertyFilePath);
160
				skinProperties.load();
161
				skinPropertiesMap.put(skinName, skinProperties);
162

  
163
				String metaDataFilePath = ServiceService.getRealSkinDir()
164
						+ FileUtil.getFS() + skinName + FileUtil.getFS() + skinName
165
						+ ".properties.metadata.xml";
166
				if (FileUtil.getFileStatus(metaDataFilePath) > FileUtil.DOES_NOT_EXIST) {
167
					PropertiesMetaData skinMetaData = new PropertiesMetaData(metaDataFilePath);
168
					skinMetaDataMap.put(skinName, skinMetaData);
169
				} else {
170
					skinPropertiesMap.remove(skinName);
171
					logMetacat.error("Could not find skin property metadata file for skin: " 
172
							+ skinName + " at: " + metaDataFilePath  
173
							+ ". This skin will not be available.");
174
					continue;
175
				}
176

  
177
				String backupPropertyFilePath = BACKUP_DIR + FileUtil.getFS() + skinName
178
						+ ".properties.backup";
179
				if (FileUtil.getFileStatus(backupPropertyFilePath) > FileUtil.DOES_NOT_EXIST) {
180
					SortedProperties skinBackupProperties = new SortedProperties(
181
							backupPropertyFilePath);
182
					skinBackupProperties.load();
183
					skinBackupPropertiesMap.put(skinName, skinBackupProperties);
184
				} else {
185
					logMetacat.warn("Could not find backup properties for skin: "
186
							+ skinName + " at: " + backupPropertyFilePath);
187
				}
188
			}
189
		} catch (TransformerException te) {
190
			throw new GeneralPropertyException(te.getMessage());
191
		}
192
	}
193

  
194
	/**
195
	 * Utility method to get a property value from the properties file for a
196
	 * specific skin.
197
	 * 
198
	 * @param skinName
199
	 *            the skin for which we want to retrieve the property
200
	 * @param propertyName
201
	 *            the name of the property requested
202
	 * @return the String value for the property
203
	 */
204
	public static String getProperty(String skinName, String propertyName)
205
			throws PropertyNotFoundException {
206
		SortedProperties skinProperties = skinPropertiesMap.get(skinName);
207
		if (skinProperties == null) {
208
			throw new PropertyNotFoundException("There is not property map for "
209
					+ skinName);
210
		}
211
		return skinProperties.getProperty(propertyName);
212
	}
213

  
214
	/**
215
	 * Get a set of all property names for a given skin.
216
	 * 
217
	 * @param skinName
218
	 *            the skin for which we want to retrieve the property names
219
	 * @return Set of property names
220
	 */
221
	public static Vector<String> getPropertyNames(String skinName)
222
			throws PropertyNotFoundException {
223
		SortedProperties skinProperties = skinPropertiesMap.get(skinName);
224
		if (skinProperties == null) {
225
			throw new PropertyNotFoundException("There is not property map for "
226
					+ skinName);
227
		}
228
		return skinProperties.getPropertyNames();
229
	}
230

  
231
	/**
232
	 * Get a Set of all property names that start with the groupName prefix.
233
	 * 
234
	 * @param groupName
235
	 *            the prefix of the keys to search for.
236
	 * @return Vector of property names
237
	 */
238
	public static Vector<String> getPropertyNamesByGroup(String skinName, String groupName)
239
			throws PropertyNotFoundException {
240
		SortedProperties skinProperties = skinPropertiesMap.get(skinName);
241
		if (skinProperties == null) {
242
			throw new PropertyNotFoundException("There is not property map for "
243
					+ skinName);
244
		}
245
		return skinProperties.getPropertyNamesByGroup(groupName);
246
	}
247

  
248
	/**
249
	 * Get the main backup properties file. These are configurable properties
250
	 * that are stored outside the metacat install directories so the user does
251
	 * not need to re-enter all the configuration information every time they do
252
	 * an upgrade.
253
	 * 
254
	 * @return a SortedProperties object with the backup properties
255
	 */
256
	public static HashMap<String, SortedProperties> getProperties() {
257
		return skinPropertiesMap;
258
	}
259

  
260
	/**
261
	 * Get the main backup properties file. These are configurable properties
262
	 * that are stored outside the metacat install directories so the user does
263
	 * not need to re-enter all the configuration information every time they do
264
	 * an upgrade.
265
	 * 
266
	 * @return a SortedProperties object with the backup properties
267
	 */
268
	public static SortedProperties getProperties(String skinName) {
269
		return skinPropertiesMap.get(skinName);
270
	}
271

  
272
	/**
273
	 * Get the main backup properties file. These are configurable properties
274
	 * that are stored outside the metacat install directories so the user does
275
	 * not need to re-enter all the configuration information every time they do
276
	 * an upgrade.
277
	 * 
278
	 * @return a SortedProperties object with the backup properties
279
	 */
280
	public static HashMap<String, SortedProperties> getBackupProperties() {
281
		return skinBackupPropertiesMap;
282
	}
283

  
284
	/**
285
	 * Get the main backup properties file. These are configurable properties
286
	 * that are stored outside the metacat install directories so the user does
287
	 * not need to re-enter all the configuration information every time they do
288
	 * an upgrade.
289
	 * 
290
	 * @return a SortedProperties object with the backup properties
291
	 */
292
	public static SortedProperties getBackupProperties(String skinName) {
293
		return skinBackupPropertiesMap.get(skinName);
294
	}
295

  
296
	/**
297
	 * Get the main properties metadata. This is retrieved from an xml file that
298
	 * describes the attributes of configurable properties.
299
	 * 
300
	 * @return a PropertiesMetaData object with the main properties metadata
301
	 */
302
	public static HashMap<String, PropertiesMetaData> getMetaData() {
303
		return skinMetaDataMap;
304
	}
305

  
306
	/**
307
	 * Get the main properties metadata. This is retrieved from an xml file that
308
	 * describes the attributes of configurable properties.
309
	 * 
310
	 * @return a PropertiesMetaData object with the main properties metadata
311
	 */
312
	public static PropertiesMetaData getMetaData(String skinName) {
313
		return skinMetaDataMap.get(skinName);
314
	}
315

  
316
	/**
317
	 * Utility method to set a property value both in memory and to the
318
	 * properties file
319
	 * 
320
	 * @param propertyName
321
	 *            the name of the property requested
322
	 * @param newValue
323
	 *            the new value for the property
324
	 */
325
	public static void setProperty(String skinName, String propertyName, String newValue)
326
			throws IOException, GeneralPropertyException {
327
		SortedProperties skinProperties = skinPropertiesMap.get(skinName);
328
		if (skinProperties == null) {
329
			throw new GeneralPropertyException("There is not property map for "
330
					+ skinName);
331
		}
332
		skinProperties.setProperty(propertyName, newValue);
333
		skinProperties.store();
334

  
335
	}
336

  
337
	/**
338
	 * Utility method to set a property value in memory. This will NOT cause the
339
	 * property to be written to disk. Use this method to set multiple
340
	 * properties in a row without causing excessive I/O. You must call
341
	 * persistProperties() once you're done setting properties to have them
342
	 * written to disk.
343
	 * 
344
	 * @param propertyName
345
	 *            the name of the property requested
346
	 * @param newValue
347
	 *            the new value for the property
348
	 */
349
	public static void setPropertyNoPersist(String skinName, String propertyName,
350
			String newValue) throws GeneralPropertyException {
351
		SortedProperties skinProperties = skinPropertiesMap.get(skinName);
352
		if (skinProperties == null) {
353
			throw new GeneralPropertyException("There is not property map for "
354
					+ skinName);
355
		}
356
		skinProperties.setPropertyNoPersist(propertyName, newValue);
357
	}
358

  
359
	/**
360
	 * Save the properties to a properties file. Note, the order and comments
361
	 * will be preserved.
362
	 */
363
	public static void persistProperties(String skinName) throws IOException,
364
			GeneralPropertyException {
365
		SortedProperties skinProperties = skinPropertiesMap.get(skinName);
366
		if (skinProperties == null) {
367
			throw new GeneralPropertyException("There is not property map for "
368
					+ skinName);
369
		}
370
		skinProperties.store();
371
	}
372

  
373
	/**
374
	 * Save the properties to a properties file. Note, the order and comments
375
	 * will be preserved.
376
	 */
377
	public static void persistAllProperties() throws IOException,
378
			GeneralPropertyException {
379
		for (String skinName : skinNames) {
380
			persistProperties(skinName);
381
		}
382
	}
383

  
384
	/**
385
	 * Writes out backup configurable properties to a file.
386
	 */
387
	public static void persistBackupProperties(String skinName)
388
			throws GeneralPropertyException {
389
		try {
390
			String metaDataFilePath = ServiceService.getRealSkinDir() + FileUtil.getFS()
391
					+ skinName + FileUtil.getFS() + skinName + ".properties.metadata.xml";
392

  
393
			String backupPropertyFilePath = BACKUP_DIR + FileUtil.getFS() + skinName
394
					+ ".properties.backup";
395

  
396
			// Use the metadata to extract configurable properties from the
397
			// overall properties list, and store those properties.
398
			SortedProperties backupProperties = new SortedProperties(
399
					backupPropertyFilePath);
400

  
401
			// Populate the backup properties for main metacat properties using
402
			// the associated metadata file
403
			PropertiesMetaData skinMetadata = new PropertiesMetaData(metaDataFilePath);
404
			
405
			Map<String, MetaDataProperty> skinKeyMap = skinMetadata.getProperties();
406
			Set<String> skinKeySet = skinKeyMap.keySet();
407
			for (String propertyKey : skinKeySet) {
408
				// don't backup passwords
409
				MetaDataProperty metaData = skinKeyMap.get(propertyKey);
410
				if (!metaData.getFieldType().equals(MetaDataProperty.PASSWORD_TYPE)) {
411
					backupProperties.addProperty(propertyKey, getProperty(skinName, propertyKey));
412
				}
413
			}			
414

  
415
			// store the properties to file
416
			backupProperties.store();
417

  
418
		} catch (TransformerException te) {
419
			throw new GeneralPropertyException(
420
					"Could not transform backup properties xml: " + te.getMessage());
421
		} catch (IOException ioe) {
422
			throw new GeneralPropertyException(
423
					"Could not backup configurable properties: " + ioe.getMessage());
424
		} catch (ServiceException se) {
425
			throw new GeneralPropertyException("Could not get skins property file: "
426
					+ se.getMessage());
427
		}
428
	}
429

  
430
	/**
431
	 * Reports whether properties are fully configured.
432
	 * 
433
	 * @return a boolean that is true if properties are not unconfigured and
434
	 *         false otherwise
435
	 */
436
	public static boolean areSkinsConfigured() throws MetacatUtilException {
437
		try {
438
			return !PropertyService.getProperty("configutil.skinsConfigured").equals(
439
					PropertyService.UNCONFIGURED);
440
		} catch (PropertyNotFoundException pnfe) {
441
			throw new MetacatUtilException("Could not determine if skins are configured: "
442
					+ pnfe.getMessage());
443
		}
444
	}
445

  
446
	/**
447
	 * Take input from the user in an HTTP request about an property to be
448
	 * changed and update the metacat property file with that new value if it
449
	 * has changed from the value that was originally set.
450
	 * 
451
	 * @param request
452
	 *            that was generated by the user
453
	 * @param response
454
	 *            to send output back to the user
455
	 * @param propertyName
456
	 *            the name of the property to be checked and set
457
	 */
458
	public static void checkAndSetProperty(HttpServletRequest request, String skinName,
459
			String propertyName) throws GeneralPropertyException {
460
		String newValue = request.getParameter(skinName + "." + propertyName);
461
		checkAndSetProperty(newValue, skinName, propertyName);
462
	}
463

  
464
	/**
465
	 * Check user input against existing value and update the metacat property
466
	 * file with that new value if it has changed from the value that was
467
	 * originally set.
468
	 * 
469
	 * @param newValue
470
	 *            the value that was returned by the form
471
	 * @param skinname
472
	 *            the skin that we are checking
473
	 * @param propertyName
474
	 *            the name of the property to be checked and set
475
	 */
476
	public static void checkAndSetProperty(String newValue, String skinName,
477
			String propertyName) throws GeneralPropertyException {
478
		String oldValue = SkinPropertyService.getProperty(skinName, propertyName);
479
		if (newValue != null && !newValue.equals(oldValue)) {
480
			SkinPropertyService.setPropertyNoPersist(skinName, propertyName, newValue);
481
		}
482
	}
483

  
484
	/**
485
	 * Reports whether the metacat configuration utility should be run. Returns
486
	 * false if -- dev.runConfiguration=false and -- backup properties file
487
	 * exists Note that dev.runConfiguration should only be set to false when
488
	 * reinstalling the same version of the application in developement.
489
	 * 
490
	 * @return a boolean that is false if dev.runConfiguration is false and the
491
	 *         backup properties file exists.
492
	 */
493
	public static boolean bypassConfiguration() {
494
		boolean bypass = false;
495

  
496
		// We only want to go through the check once to see if we want to
497
		// bypass the configuration. We don't want to run through all of
498
		// this every time we hit metacat.
499
		if (bypassAlreadyChecked) {
500
			return bypass;
501
		}
502

  
503
		try {
504
			// check how dev.runConfiguration is set in metacat.properties
505
			String strRunConfiguration = PropertyService
506
					.getProperty("dev.runConfiguration");
507
			bypass = !(Boolean.parseBoolean(strRunConfiguration));
508

  
509
			// if the deb.runConfiguration is true, return false here.
510
			if (!bypass) {
511
				bypassAlreadyChecked = true;
512
				return false;
513
			}
514

  
515
			// the system is bypassing the configuration utility. We need to
516
			// get the backup properties and replace existing properties with
517
			// backup values. We do this for main and org properties.
518
			for (String skinName : skinNames) {
519
				SortedProperties backupProperties = getBackupProperties(skinName);
520
				Vector<String> backupPropertyNames = backupProperties.getPropertyNames();
521
				for (String backupPropertyName : backupPropertyNames) {
522
					String value = backupProperties.getProperty(backupPropertyName);
523
					backupProperties.setPropertyNoPersist(backupPropertyName, value);
524
				}
525
				backupProperties.store();
526
			}
527
		} catch (PropertyNotFoundException pnfe) {
528
			logMetacat.error("Could not find property: " + pnfe.getMessage());
529
		} catch (GeneralPropertyException gpe) {
530
			logMetacat.error("General property error: " + gpe.getMessage());
531
		}
532

  
533
		bypassAlreadyChecked = true;
534
		return bypass;
535
	}
536

  
537
}
0 538

  
src/edu/ucsb/nceas/metacat/properties/ConfigurableProperties.java
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.Set;
32
import java.util.Vector;
33

  
34
import javax.servlet.ServletContext;
35
import javax.servlet.http.HttpServletRequest;
36
import javax.xml.transform.TransformerException;
37

  
38
import org.apache.log4j.Logger;
39

  
40
import edu.ucsb.nceas.metacat.service.ServiceService;
41
import edu.ucsb.nceas.metacat.shared.BaseService;
42
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
43
import edu.ucsb.nceas.metacat.shared.ServiceException;
44
import edu.ucsb.nceas.metacat.util.SystemUtil;
45
import edu.ucsb.nceas.utilities.FileUtil;
46
import edu.ucsb.nceas.utilities.GeneralPropertyException;
47
import edu.ucsb.nceas.utilities.MetaDataProperty;
48
import edu.ucsb.nceas.utilities.PropertiesMetaData;
49
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
50
import edu.ucsb.nceas.utilities.SortedProperties;
51

  
52
/**
53
 * A suite of utility classes for the metadata configuration utility
54
 */
55
public class ConfigurableProperties extends BaseService implements PropertiesInterface {
56
	
57
	private static final String MAIN_CONFIG_FILE_NAME = "metacat.properties";
58
	private static String mainConfigFilePath  = null;
59
	private static SortedProperties mainProperties = null;
60
	
61
	private static final String MAIN_METADATA_FILE_NAME = "metacat.properties.metadata.xml";
62
	private static String mainMetadataFilePath = null;
63
	private static PropertiesMetaData mainMetaData = null;
64
	
65
	private static final String MAIN_BACKUP_FILE_NAME = "metacat.properties.backup";
66
	private static String mainBackupFilePath  = null;
67
	private static SortedProperties mainBackupProperties = null;
68
	
69
	private static final String AUTH_METADATA_FILE_NAME = "auth.properties.metadata.xml";
70
	private static String authMetadataFilePath = null;
71
	private static PropertiesMetaData authMetaData = null;
72
	
73
	private static final String AUTH_BACKUP_FILE_NAME = "auth.properties.backup";
74
	private static String authBackupFilePath = null;
75
	private static SortedProperties authBackupProperties = null;
76
	
77
	private static boolean bypassAlreadyChecked = false;
78
	
79
	private static Logger logMetacat = Logger.getLogger(ConfigurableProperties.class);
80

  
81
	/**
82
	 * private constructor since this is a singleton
83
	 * 
84
	 * @param servletContext the context we will use to get relative paths
85
	 */
86
	protected ConfigurableProperties() throws ServiceException {		
87
		_serviceName = "ConfigurableProperties";
88
		
89
		initialize();		
90
	}
91
	
92
	public boolean refreshable() {
93
		return true;
94
	}
95
	
96
	public void doRefresh() throws ServiceException {
97
		initialize();
98
	}
99
	
100
	public void stop() throws ServiceException {
101
		return;
102
	}
103
	
104
	/**
105
	 * Initialize the singleton.
106
	 * 
107
	 * @param servletContext the context we will use to get relative paths
108
	 */
109
	private void initialize() throws ServiceException {
110
		
111
		logMetacat.debug("Initializing ConfigurableProperties");
112
		
113
		try {
114
			mainConfigFilePath = 
115
				PropertyService.CONFIG_FILE_DIR + FileUtil.getFS() + MAIN_CONFIG_FILE_NAME;
116
			mainMetadataFilePath = 
117
				PropertyService.CONFIG_FILE_DIR + FileUtil.getFS() + MAIN_METADATA_FILE_NAME;
118
//			mainBackupFilePath = 
119
//				PropertyService.CONFIG_FILE_DIR + FileUtil.getFS() + MAIN_BACKUP_FILE_NAME;
120
			authMetadataFilePath = 
121
				PropertyService.CONFIG_FILE_DIR + FileUtil.getFS() + AUTH_METADATA_FILE_NAME;
122
//			authBackupFilePath = 
123
//				PropertyService.CONFIG_FILE_DIR + FileUtil.getFS() + AUTH_BACKUP_FILE_NAME;
124
			
125
			
126
			// mainProperties will hold the primary configuration values for
127
			// metacat.
128
			mainProperties = new SortedProperties(mainConfigFilePath);
129
			mainProperties.load();
130

  
131
			// mainMetaData holds configuration information about main
132
			// properties. This is primarily used to display input fields on 
133
			// the configuration page. The information is retrieved from an 
134
			// xml metadata file
135
			mainMetaData = new PropertiesMetaData(mainMetadataFilePath);
136

  
137
			// authMetaData holds configuration information about organization
138
			// level
139
			// properties. This is primarily used to display input fields on
140
			// the auth configuration page. The information is retrieved
141
			// from an xml metadata file dedicated just to auth properties.
142
			authMetaData = new PropertiesMetaData(authMetadataFilePath);
143

  
144
			String recommendedExternalDir = SystemUtil.discoverExternalDir();
145
			PropertyService.setRecommendedExternalDir(recommendedExternalDir);
146
			
147
			String backupPath = getProperty("application.backupDir");
148
			if (backupPath == null || backupPath.equals("")) {
149
				backupPath = SystemUtil.getStoredBackupDir();
150
			}
151
			if (backupPath == null || backupPath.equals("") && recommendedExternalDir != null) {
152
				backupPath = 
153
					recommendedExternalDir + FileUtil.getFS() + "." + ServiceService.getRealApplicationContext();
154
			}
155

  
156
			// if backupPath is still null, no reason to initialize the
157
			// backup properties. The system will need to prompt the user for 
158
			// the backup properties and reinitialize ConfigurableProperties.
159
			if (backupPath != null && !backupPath.equals("")) {		
160
				setProperty("application.backupDir", backupPath);
161
				SystemUtil.writeStoredBackupFile(backupPath);
162

  
163
				// The mainBackupProperties hold properties that were backed up
164
				// the last time the application was configured. On disk, the 
165
				// file will look like a smaller version of metacat.properties. 
166
				// It is stored in the data storage directory outside the 
167
				// application directories.
168
				mainBackupFilePath = backupPath + FileUtil.getFS() + MAIN_BACKUP_FILE_NAME;
169
				mainBackupProperties = new SortedProperties(mainBackupFilePath);
170
				mainBackupProperties.load();
171

  
172
				// The authBackupProperties hold properties that were backed up
173
				// the last time the auth was configured. On disk, the file 
174
				// will look like a smaller version of metacat.properties. It 
175
				// is stored in the data storage directory outside the 
176
				// application directories.
177
				authBackupFilePath = backupPath + FileUtil.getFS() + AUTH_BACKUP_FILE_NAME;
178
				authBackupProperties = new SortedProperties(authBackupFilePath);
179
				authBackupProperties.load();
180
			}
181
		} catch (TransformerException te) {
182
			throw new ServiceException("Transform problem while loading properties: "
183
					+ te.getMessage());
184
		} catch (IOException ioe) {
185
			throw new ServiceException("I/O problem while loading properties: "
186
					+ ioe.getMessage());
187
		} catch (GeneralPropertyException gpe) {
188
			throw new ServiceException("General properties problem while loading properties: "
189
					+ gpe.getMessage());
190
		} catch (MetacatUtilException ue) {
191
			throw new ServiceException("Utilities problem while loading properties: "
192
					+ ue.getMessage());
193
		} 
194
	}
195

  
196
	/**
197
	 * Utility method to get a property value from the properties file
198
	 * 
199
	 * @param propertyName
200
	 *            the name of the property requested
201
	 * @return the String value for the property
202
	 */
203
	public String getProperty(String propertyName) throws PropertyNotFoundException {
204
		return mainProperties.getProperty(propertyName);
205
	}
206
	
207
	/**
208
     * Get a set of all property names.
209
     * 
210
     * @return Set of property names  
211
     */
212
    public Vector<String> getPropertyNames() {   	
213
    	return mainProperties.getPropertyNames();
214
    }
215
    
216

  
217
	/**
218
	 * Get a Set of all property names that start with the groupName prefix.
219
	 * 
220
	 * @param groupName
221
	 *            the prefix of the keys to search for.
222
	 * @return enumeration of property names
223
	 */
224
    public Vector<String> getPropertyNamesByGroup(String groupName) {   	
225
    	return mainProperties.getPropertyNamesByGroup(groupName);
226
    }
227
    
228
	/**
229
	 * Get a Map of all properties that start with the groupName prefix.
230
	 * 
231
	 * @param groupName
232
	 *            the prefix of the keys to search for.
233
	 * @return Map of property names
234
	 */
235
    public Map<String, String> getPropertiesByGroup(String groupName) throws PropertyNotFoundException {   	
236
    	return mainProperties.getPropertiesByGroup(groupName);
237
    }
238

  
239
	/**
240
	 * Utility method to set a property value both in memory and to the
241
	 * properties file
242
	 * 
243
	 * @param propertyName
244
	 *            the name of the property requested
245
	 * @param newValue
246
	 *            the new value for the property
247
	 */
248
	public void setProperty(String propertyName, String newValue) throws GeneralPropertyException {
249
			mainProperties.setProperty(propertyName, newValue);
250
			mainProperties.store();
251
	}
252

  
253
	/**
254
	 * Utility method to set a property value in memory. This will NOT cause the
255
	 * property to be written to disk. Use this method to set multiple
256
	 * properties in a row without causing excessive I/O. You must call
257
	 * persistProperties() once you're done setting properties to have them
258
	 * written to disk.
259
	 * 
260
	 * @param propertyName
261
	 *            the name of the property requested
262
	 * @param newValue
263
	 *            the new value for the property
264
	 */
265
	public void setPropertyNoPersist(String propertyName, String newValue) throws GeneralPropertyException {
266
		mainProperties.setPropertyNoPersist(propertyName, newValue);
267
	}
268

  
269
	/**
270
	 * Save the properties to a properties file. Note, the 
271
	 * order and comments will be preserved.
272
	 */
273
	public void persistProperties() throws GeneralPropertyException {
274
		mainProperties.store();
275
	}
276
	
277
	/**
278
	 * Get the main backup properties file. These are configurable properties that
279
	 * are stored outside the metacat install directories so the user does not
280
	 * need to re-enter all the configuration information every time they do an
281
	 * upgrade.
282
	 * 
283
	 * @return a SortedProperties object with the backup properties
284
	 */
285
	public SortedProperties getMainBackupProperties() {
286
		return mainBackupProperties;
287
	}
288
	
289
	/**
290
	 * Get the auth backup properties file. These are configurable 
291
	 * properties that are stored outside the metacat install directories so 
292
	 * the user does not need to re-enter all the configuration information 
293
	 * every time they do an upgrade.
294
	 * 
295
	 * @return a SortedProperties object with the backup properties
296
	 */
297
	public SortedProperties getAuthBackupProperties() {
298
		return authBackupProperties;
299
	}
300
	
301
	/**
302
	 * Get the main properties metadata. This is retrieved from an xml file that
303
	 * describes the attributes of configurable properties.
304
	 * 
305
	 * @return a PropertiesMetaData object with the main properties metadata
306
	 */
307
	public PropertiesMetaData getMainMetaData() {
308
		return mainMetaData;
309
	}
310
	
311
	/**
312
	 * Get the auth properties metadata. This is retrieved from an xml
313
	 * file that describes the attributes of configurable properties.
314
	 * 
315
	 * @return a PropertiesMetaData object with the organization properties
316
	 *         metadata
317
	 */
318
	public PropertiesMetaData getAuthMetaData() {
319
		return authMetaData;
320
	}
321
	
322
	/**
323
	 * Writes out backup configurable properties to a file.
324
	 */
325
	public void persistMainBackupProperties()
326
			throws GeneralPropertyException {
327

  
328
		// Use the metadata to extract configurable properties from the 
329
		// overall properties list, and store those properties.
330
		try {
331
			SortedProperties backupProperties = new SortedProperties(mainBackupFilePath);
332
			
333
			// Populate the backup properties for main metacat properties using
334
			// the associated metadata file
335
			PropertiesMetaData mainMetadata = new PropertiesMetaData(mainMetadataFilePath);
336

  
337
			Map<String, MetaDataProperty> mainKeyMap = mainMetadata.getProperties();
338
			Set<String> mainKeySet = mainKeyMap.keySet();
339
			for (String propertyKey : mainKeySet) {
340
				// don't backup passwords
341
				MetaDataProperty metaData = mainKeyMap.get(propertyKey);
342
				if (!metaData.getFieldType().equals(MetaDataProperty.PASSWORD_TYPE)) {
343
					backupProperties.addProperty(propertyKey, getProperty(propertyKey));
344
				}
345
			}
346
			
347
			// store the properties to file
348
			backupProperties.store();
349
			mainBackupProperties = 
350
				new SortedProperties(mainBackupFilePath);
351
			mainBackupProperties.load();
352

  
353
		} catch (TransformerException te) {
354
			throw new GeneralPropertyException("Could not transform backup properties xml: "
355
					+ te.getMessage());
356
		} catch (IOException ioe) {
357
			throw new GeneralPropertyException("Could not backup configurable properties: "
358
					+ ioe.getMessage());
359
		}
360
	}
361
	
362
	/**
363
	 * Writes out backup configurable properties to a file.
364
	 */
365
	public void persistAuthBackupProperties(ServletContext servletContext)
366
			throws GeneralPropertyException {
367

  
368
		// Use the metadata to extract configurable properties from the 
369
		// overall properties list, and store those properties.
370
		try {
371
			SortedProperties backupProperties = 
372
				new SortedProperties(authBackupFilePath);
373
			
374
			// Populate the backup properties for auth properties using
375
			// the associated metadata file
376
			PropertiesMetaData authMetadata = new PropertiesMetaData(authMetadataFilePath);
377
			
378
			Map<String, MetaDataProperty> authKeyMap = authMetadata.getProperties();
379
			Set<String> authKeySet = authKeyMap.keySet();
380
			for (String propertyKey : authKeySet) {
381
				// don't backup passwords
382
				MetaDataProperty metaData = authKeyMap.get(propertyKey);
383
				if (!metaData.getFieldType().equals(MetaDataProperty.PASSWORD_TYPE)) {
384
					backupProperties.addProperty(propertyKey, getProperty(propertyKey));
385
				}
386
			}
387
			
388
			// store the properties to file
389
			backupProperties.store();
390
			authBackupProperties = 
391
				new SortedProperties(authBackupFilePath);
392
			authBackupProperties.load();
393

  
394
		} catch (TransformerException te) {
395
			throw new GeneralPropertyException("Could not transform backup properties xml: "
396
					+ te.getMessage());
397
		} catch (IOException ioe) {
398
			throw new GeneralPropertyException("Could not backup configurable properties: "
399
					+ ioe.getMessage());
400
		} 
401
	}
402
	
403
	/**
404
	 * Reports whether properties are fully configured.
405
	 * 
406
	 * @return a boolean that is true if properties are not unconfigured and
407
	 *         false otherwise
408
	 */
409
	public boolean arePropertiesConfigured() throws GeneralPropertyException {		
410
		String propertiesConfigured = getProperty("configutil.propertiesConfigured");
411
		if (propertiesConfigured != null && !propertiesConfigured.equals(UNCONFIGURED)) {
412
			return true;
413
		}			
414
		return false;
415
	}
416
	
417
	/**
418
	 * Determine if the system is configured to bypass configuration. If so, the
419
	 * system will look for backup configuration files at startup time and use
420
	 * those to configure metacat. The bypass options should only be set by
421
	 * developers. Production code should never bypass confguration.
422
	 * 
423
	 * @return true if dev.runConfiguration is set to true in metacat.properties
424
	 *         and we have not already checked for bypass, false otherwise.
425
	 */
426
	public boolean doBypass() throws PropertyNotFoundException {
427
		// We only want to go through the check once to see if we want to
428
		// bypass the configuration.  We don't want to run through all of
429
		// this every time  we hit metacat. 
430
		if (bypassAlreadyChecked) {
431
			logMetacat.debug("bypassConfiguration not performing full bypass check.  Bypass set to false");
432
			return false;
433
		}
434
		
435
		// check how dev.runConfiguration is set in metacat.properties
436
		String strRunConfiguration = getProperty("dev.runConfiguration");
437
		boolean runConfiguration = Boolean.parseBoolean(strRunConfiguration);
438
		logMetacat.debug("bypassConfiguration: dev.runConfiguration property set to: " + strRunConfiguration);
439
		
440
		// if the dev.runConfiguration is true, return false here.
441
		if (runConfiguration) {
442
			bypassAlreadyChecked = true;
443
			return false;
444
		} 
445
		
446
		return true;
447
	}
448
	
449
	/**
450
	 * Reports whether the metacat configuration utility should be run.  
451
	 * Returns false if  
452
	 *   -- dev.runConfiguration=false and
453
	 *   -- backup properties file exists
454
	 * Note that dev.runConfiguration should only be set to false when
455
	 * reinstalling the same version of the application in developement.
456
	 * 
457
	 * @return a boolean that is false if dev.runConfiguration is false and 
458
	 * the backup properties file exists.  
459
	 */
460
	public void bypassConfiguration() throws GeneralPropertyException {
461
		try {
462
			boolean doBypass = doBypass();
463

  
464
			if (!doBypass) {
465
				throw new GeneralPropertyException(
466
						"Attempting to do bypass when system is not configured for it.");
467
			}			
468

  
469
			// The system is bypassing the configuration utility. We need to
470
			// get the backup properties and replace existing properties with
471
			// backup values.  We do this for main and org properties.		
472
			logMetacat.debug("bypassConfiguration: setting main backup properties.");
473
			SortedProperties mainBackupProperties = getMainBackupProperties();
474
			Vector<String> backupPropertyNames = 
475
				mainBackupProperties.getPropertyNames();
476
			for (String backupPropertyName : backupPropertyNames) {
477
				String value = mainBackupProperties.getProperty(backupPropertyName);
478
				setPropertyNoPersist(backupPropertyName, value);
479
			}
480

  
481
			logMetacat.debug("bypassConfiguration: setting auth backup properties.");
482
			SortedProperties authBackupProperties = getAuthBackupProperties();
483
			Vector<String> authBackupPropertyNames = 
484
				authBackupProperties.getPropertyNames();
485
			for (String authBackupPropertyName : authBackupPropertyNames) {
486
				String value = authBackupProperties.getProperty(authBackupPropertyName);
487
				setPropertyNoPersist(authBackupPropertyName, value);
488
			}
489

  
490
			logMetacat.debug("bypassConfiguration: setting configutil sections to true.");
491
			setPropertyNoPersist("configutil.propertiesConfigured", "true");
492
			setPropertyNoPersist("configutil.authConfigured", "true");
493
			setPropertyNoPersist("configutil.skinsConfigured", "true");
494
			setPropertyNoPersist("configutil.databaseConfigured", "true");
495
			setPropertyNoPersist("configutil.geoserverConfigured", "bypassed");
496
				
497
			persistProperties();
498

  
499
		} catch (PropertyNotFoundException pnfe) {
500
			logMetacat.error("bypassConfiguration: Could not find property: " + pnfe.getMessage());
501
		} catch (GeneralPropertyException gpe) {
502
			logMetacat.error("bypassConfiguration: General property error: " + gpe.getMessage());
503
		}
504

  
505
		bypassAlreadyChecked = true;
506
	}
507
	
508
	/**
509
	 * Take input from the user in an HTTP request about an property to be changed
510
	 * and update the metacat property file with that new value if it has
511
	 * changed from the value that was originally set.
512
	 * 
513
	 * @param request
514
	 *            that was generated by the user
515
	 * @param response
516
	 *            to send output back to the user
517
	 * @param propertyName
518
	 *            the name of the property to be checked and set
519
	 */
520
	public void checkAndSetProperty(HttpServletRequest request, String propertyName) 
521
			throws GeneralPropertyException {
522
		String value = getProperty(propertyName);
523
		String newValue = request.getParameter(propertyName);
524
		if (newValue != null && !newValue.trim().equals(value)) {
525
			setPropertyNoPersist(propertyName, newValue.trim());
526
		}
527
	}
528

  
529
}
0 530

  
src/edu/ucsb/nceas/metacat/properties/PropertiesInterface.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that loads eml-access.xml file containing ACL 
4
 *             for a metadata document into relational DB
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Jivka Bojilova
8
 *
9
 *   '$Author: daigle $'
10
 *     '$Date: 2009-03-25 14:04:06 -0800 (Wed, 25 Mar 2009) $'
11
 * '$Revision: 4863 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

  
28
package edu.ucsb.nceas.metacat.properties;
29

  
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 edu.ucsb.nceas.metacat.shared.MetacatUtilException;
37
import edu.ucsb.nceas.metacat.shared.ServiceException;
38
import edu.ucsb.nceas.utilities.GeneralPropertyException;
39
import edu.ucsb.nceas.utilities.PropertiesMetaData;
40
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
41
import edu.ucsb.nceas.utilities.SortedProperties;
42

  
43
/**
44
 * This interface will handle properties access
45
 */
46
public interface PropertiesInterface {
47
	
48
	// system is configured
49
	public static final String CONFIGURED = "true"; 
50
	// system has never been configured
51
	public static final String UNCONFIGURED = "false"; 
52
	public static final String BYPASSED = "bypassed";
53
	
54
//	public PropertiesInterface getInstance() throws ServiceException;
55
	
56
	public String getProperty(String propertyName) throws PropertyNotFoundException;
57
	
58
	public  Vector<String> getPropertyNames();
59
	
60
	public Vector<String> getPropertyNamesByGroup(String groupName);
61
	
62
	public  Map<String, String> getPropertiesByGroup(String groupName) throws PropertyNotFoundException;
63
	
64
	public void setProperty(String propertyName, String newValue) throws GeneralPropertyException;
65
	
66
	public void setPropertyNoPersist(String propertyName, String newValue) throws GeneralPropertyException;
67
	
68
	public void persistProperties() throws GeneralPropertyException;
69
	
70
	public void checkAndSetProperty(HttpServletRequest request, String propertyName) throws GeneralPropertyException;
71

  
72
	public SortedProperties getMainBackupProperties() throws GeneralPropertyException;
73
	
74
	public  SortedProperties getAuthBackupProperties() throws GeneralPropertyException;
75
	
76
	public PropertiesMetaData getMainMetaData() throws GeneralPropertyException;
77
	
78
	public PropertiesMetaData getAuthMetaData() throws GeneralPropertyException;
79
	
80
	public void persistMainBackupProperties() throws GeneralPropertyException;
81
	
82
	public void persistAuthBackupProperties(ServletContext servletContext) throws GeneralPropertyException;
83
	
84
	public boolean arePropertiesConfigured()  throws GeneralPropertyException;
85
	
86
	public boolean doBypass() throws GeneralPropertyException;
87
	
88
	public void bypassConfiguration()  throws GeneralPropertyException;
89
 
90
}
0 91

  

Also available in: Unified diff