Project

General

Profile

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: 2008-09-03 09:09:10 -0700 (Wed, 03 Sep 2008) $'
10
 * '$Revision: 4340 $'
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.service;
28

    
29
import java.io.IOException;
30
import java.util.Map;
31
import java.util.Set;
32
import java.util.SortedMap;
33
import java.util.Vector;
34

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

    
39
import org.apache.log4j.Logger;
40

    
41
import edu.ucsb.nceas.metacat.util.OrganizationUtil;
42
import edu.ucsb.nceas.metacat.util.UtilException;
43
import edu.ucsb.nceas.utilities.FileUtil;
44
import edu.ucsb.nceas.utilities.GeneralPropertyException;
45
import edu.ucsb.nceas.utilities.MetaDataGroup;
46
import edu.ucsb.nceas.utilities.MetaDataProperty;
47
import edu.ucsb.nceas.utilities.PropertiesMetaData;
48
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
49
import edu.ucsb.nceas.utilities.SortedProperties;
50

    
51
/**
52
 * A suite of utility classes for the metadata configuration utility
53
 */
54
public class PropertyService implements ServiceInterface {
55
	
56
	private static PropertyService propertyService = null;
57
	
58
	// system is configured
59
	public static final String CONFIGURED = "true"; 
60
	// system has never been configured
61
	public static final String UNCONFIGURED = "false"; 
62
	public static final String BYPASSED = "bypassed";
63
	
64
	private static final String MAIN_CONFIG_NAME = "metacat.properties";
65
	private static final String ORG_CONFIG_NAME = "org.properties";
66
	private static final String LDAP_CONFIG_NAME = "ldap.properties";
67
	
68
	private static boolean bypassAlreadyChecked = false;
69
	
70
	private static String mainPropertiesFilePath = null;
71
	private static SortedProperties mainProperties = null;
72
	
73
	private static String mainBackupPropertiesFilePath = null;
74
	private static SortedProperties mainBackupProperties = null;
75
	
76
	private static String propertiesMetaDataFilePath = null;
77
	private static PropertiesMetaData mainMetaData = null;
78
	
79
	private static String orgBackupPropertiesFilePath = null;
80
	private static SortedProperties orgBackupProperties = null;
81
	
82
	private static String orgMetaDataFilePath = null;
83
	private static PropertiesMetaData orgMetaData = null;
84
	
85
	private static String ldapBackupPropertiesFilePath = null;
86
	private static SortedProperties ldapBackupProperties = null;
87
	
88
	private static String ldapMetaDataFilePath = null;
89
	private static PropertiesMetaData ldapMetaData = null;
90
	
91
	
92
	private static Logger logMetacat = Logger.getLogger(PropertyService.class);
93

    
94
	/**
95
	 * private constructor since this is a singleton
96
	 * 
97
	 * @param servletContext the context we will use to get relative paths
98
	 */
99
	private PropertyService(String configDir) throws ServiceException {
100
		try {
101
			initialize(configDir);
102
		} catch (GeneralPropertyException gpe) {
103
			throw new ServiceException("Properties problem while initializing " + 
104
					"PropertyService: " + gpe.getMessage());
105
		} catch (IOException ioe) {
106
			throw new ServiceException("I/O Problem while initializing " + 
107
					"PropertyService: " + ioe.getMessage());
108
		}
109
	}
110
	
111
	/**
112
	 * Get the single instance of AuthAdmin.
113
	 * 
114
	 * @param servletContext the context we will use to get relative paths
115
	 * @return the single instance of AuthAdmin
116
	 */
117
	public static PropertyService getInstance(String configDir) throws ServiceException {
118
		if (propertyService == null) {
119
			propertyService = new PropertyService(configDir);
120
		}
121
		return propertyService;
122
	}
123
	
124
	/**
125
	 * Initialize the singleton.
126
	 * 
127
	 * @param servletContext the context we will use to get relative paths
128
	 */
129
	public void initialize(String configDir)
130
			throws IOException, GeneralPropertyException {
131
		
132
		logMetacat.debug("Initializing PropertyService");
133

    
134
		// mainProperties will hold the primary configuration values for metacat.
135
		mainPropertiesFilePath = configDir + FileUtil.getFS() + MAIN_CONFIG_NAME;
136
		if (mainProperties == null) {
137
			mainProperties = new SortedProperties(mainPropertiesFilePath);
138
			mainProperties.load();
139
		}
140

    
141
		try {
142
			// mainMetaData holds configuration information about main properties.
143
			// This is primarily used to display input fields on the configuration
144
			// page. The information is retrieved from an xml metadata file
145
			propertiesMetaDataFilePath = configDir + FileUtil.getFS() + MAIN_CONFIG_NAME + ".metadata.xml";
146
			if (mainMetaData == null) {
147
				mainMetaData = new PropertiesMetaData(propertiesMetaDataFilePath);
148
			}
149

    
150
			// orgMetaData holds configuration information about organization level 
151
			// properties.  This is primarily used to display input fields on 
152
			// the organization configuration page. The information is retrieved 
153
			// from an xml metadata file dedicated just to organization properties.
154
			orgMetaDataFilePath = configDir + FileUtil.getFS() + ORG_CONFIG_NAME + ".metadata.xml";
155
			if (orgMetaData == null) {
156
				orgMetaData = new PropertiesMetaData(orgMetaDataFilePath);
157
			}
158
			
159

    
160
			// ldapMetaData holds configuration information about organization level 
161
			// properties.  This is primarily used to display input fields on 
162
			// the ldap configuration page. The information is retrieved 
163
			// from an xml metadata file dedicated just to ldap properties.
164
			ldapMetaDataFilePath = configDir + FileUtil.getFS() + LDAP_CONFIG_NAME + ".metadata.xml";
165
			if (ldapMetaData == null) {
166
				ldapMetaData = new PropertiesMetaData(ldapMetaDataFilePath);
167
			}
168
		} catch (TransformerException te) {
169
			throw new GeneralPropertyException(te.getMessage());
170
		}
171

    
172
		String backupDirPath = getBackupDir();
173
		
174
		// The mainBackupProperties hold properties that were backed up the 
175
		// last time the application was configured.  On disk, the file will
176
		// look like a smaller version of metacat.properties.  It is stored 
177
		// in the data storage directory outside the application directories.
178
		mainBackupPropertiesFilePath = backupDirPath + FileUtil.getFS() + MAIN_CONFIG_NAME + ".backup";
179
		if (mainBackupProperties == null) {
180
			mainBackupProperties = 
181
				new SortedProperties(mainBackupPropertiesFilePath);
182
			mainBackupProperties.load();
183
		}
184
		
185
		// The orgBackupProperties hold properties that were backed up the 
186
		// last time the organizations were configured.  On disk, the file will
187
		// look like a smaller version of metacat.properties.  It is stored 
188
		// in the data storage directory outside the application directories.
189
		orgBackupPropertiesFilePath = backupDirPath + FileUtil.getFS() +  ORG_CONFIG_NAME + ".backup";
190
		if (orgBackupProperties == null) {
191
			orgBackupProperties = 
192
				new SortedProperties(orgBackupPropertiesFilePath);
193
			orgBackupProperties.load();
194
		}
195
		
196
		// The ldapBackupProperties hold properties that were backed up the 
197
		// last time the LDAP was configured.  On disk, the file will
198
		// look like a smaller version of metacat.properties.  It is stored 
199
		// in the data storage directory outside the application directories.
200
		ldapBackupPropertiesFilePath = backupDirPath + FileUtil.getFS() +  LDAP_CONFIG_NAME + ".backup";
201
		if (ldapBackupProperties == null) {
202
			ldapBackupProperties = 
203
				new SortedProperties(ldapBackupPropertiesFilePath);
204
			ldapBackupProperties.load();
205
		}
206
	
207
	}
208

    
209
	/**
210
	 * Utility method to get a property value from the properties file
211
	 * 
212
	 * @param propertyName
213
	 *            the name of the property requested
214
	 * @return the String value for the property
215
	 */
216
	public static String getProperty(String propertyName)
217
			throws PropertyNotFoundException {
218
		return mainProperties.getProperty(propertyName);
219
	}
220
	
221
	/**
222
     * Get a set of all property names.
223
     * 
224
     * @return Set of property names  
225
     */
226
    public static Vector<String> getPropertyNames() {   	
227
    	return mainProperties.getPropertyNames();
228
    }
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 enumeration of property names
237
	 */
238
    public static Vector<String> getPropertyNamesByGroup(String groupName) {   	
239
    	return mainProperties.getPropertyNamesByGroup(groupName);
240
    }
241
    
242
	/**
243
	 * Get a Map of all properties that start with the groupName prefix.
244
	 * 
245
	 * @param groupName
246
	 *            the prefix of the keys to search for.
247
	 * @return Map of property names
248
	 */
249
    public static Map<String, String> getPropertiesByGroup(String groupName) throws PropertyNotFoundException {   	
250
    	return mainProperties.getPropertiesByGroup(groupName);
251
    }
252

    
253
	/**
254
	 * Utility method to set a property value both in memory and to the
255
	 * properties file
256
	 * 
257
	 * @param propertyName
258
	 *            the name of the property requested
259
	 * @param newValue
260
	 *            the new value for the property
261
	 */
262
	public static void setProperty(String propertyName, String newValue) throws GeneralPropertyException {
263
			mainProperties.setProperty(propertyName, newValue);
264
			mainProperties.store();
265
	}
266

    
267
	/**
268
	 * Utility method to set a property value in memory. This will NOT cause the
269
	 * property to be written to disk. Use this method to set multiple
270
	 * properties in a row without causing excessive I/O. You must call
271
	 * persistProperties() once you're done setting properties to have them
272
	 * written to disk.
273
	 * 
274
	 * @param propertyName
275
	 *            the name of the property requested
276
	 * @param newValue
277
	 *            the new value for the property
278
	 */
279
	public static void setPropertyNoPersist(String propertyName, String newValue) throws GeneralPropertyException {
280
		mainProperties.setPropertyNoPersist(propertyName, newValue);
281
	}
282

    
283
	/**
284
	 * Save the properties to a properties file. Note, the 
285
	 * order and comments will be preserved.
286
	 */
287
	public static void persistProperties() throws GeneralPropertyException {
288
		mainProperties.store();
289
	}
290
	
291
	/**
292
	 * Get the main backup properties file. These are configurable properties that
293
	 * are stored outside the metacat install directories so the user does not
294
	 * need to re-enter all the configuration information every time they do an
295
	 * upgrade.
296
	 * 
297
	 * @return a SortedProperties object with the backup properties
298
	 */
299
	public static SortedProperties getMainBackupProperties() {
300
		return mainBackupProperties;
301
	}
302
	
303
	/**
304
	 * Get the organizational backup properties file. These are configurable 
305
	 * properties that are stored outside the metacat install directories so 
306
	 * the user does not need to re-enter all the configuration information 
307
	 * every time they do an upgrade.
308
	 * 
309
	 * @return a SortedProperties object with the backup properties
310
	 */
311
	public static SortedProperties getOrgBackupProperties() {
312
		return orgBackupProperties;
313
	}
314
	
315
	/**
316
	 * Get the LDAP backup properties file. These are configurable 
317
	 * properties that are stored outside the metacat install directories so 
318
	 * the user does not need to re-enter all the configuration information 
319
	 * every time they do an upgrade.
320
	 * 
321
	 * @return a SortedProperties object with the backup properties
322
	 */
323
	public static SortedProperties getLDAPBackupProperties() {
324
		return ldapBackupProperties;
325
	}
326
	
327
	/**
328
	 * Get the main properties metadata. This is retrieved from an xml file that
329
	 * describes the attributes of configurable properties.
330
	 * 
331
	 * @return a PropertiesMetaData object with the main properties metadata
332
	 */
333
	public static PropertiesMetaData getMainMetaData() {
334
		return mainMetaData;
335
	}
336
	
337
	/**
338
	 * Get the organization properties metadata. This is retrieved from an xml
339
	 * file that describes the attributes of configurable properties.
340
	 * 
341
	 * @return a PropertiesMetaData object with the organization properties
342
	 *         metadata
343
	 */
344
	public static PropertiesMetaData getOrgMetaData() {
345
		return orgMetaData;
346
	}
347
	
348
	/**
349
	 * Get the LDAP properties metadata. This is retrieved from an xml
350
	 * file that describes the attributes of configurable properties.
351
	 * 
352
	 * @return a PropertiesMetaData object with the organization properties
353
	 *         metadata
354
	 */
355
	public static PropertiesMetaData getLDAPMetaData() {
356
		return ldapMetaData;
357
	}
358
	
359
	/**
360
	 * Writes out backup configurable properties to a file.
361
	 */
362
	public static void persistMainBackupProperties(ServletContext servletContext)
363
			throws GeneralPropertyException {
364

    
365
		// Use the metadata to extract configurable properties from the 
366
		// overall properties list, and store those properties.
367
		try {
368
			SortedProperties backupProperties = new SortedProperties(mainBackupPropertiesFilePath);
369
			
370
			// Populate the backup properties for main metacat properties using
371
			// the associated metadata file
372
			PropertiesMetaData mainMetadata = new PropertiesMetaData(propertiesMetaDataFilePath);
373
			Set<String> mainKeySet = mainMetadata.getKeys();
374
			for (String propertyKey : mainKeySet) {
375
				backupProperties.addProperty(propertyKey, getProperty(propertyKey));
376
			}
377
			
378
			// store the properties to file
379
			backupProperties.store();
380

    
381
		} catch (TransformerException te) {
382
			throw new GeneralPropertyException("Could not transform backup properties xml: "
383
					+ te.getMessage());
384
		} catch (IOException ioe) {
385
			throw new GeneralPropertyException("Could not backup configurable properties: "
386
					+ ioe.getMessage());
387
		}
388
	}
389
	
390
	/**
391
	 * Writes out backup configurable properties to a file.
392
	 */
393
	public static void persistOrgBackupProperties(ServletContext servletContext)
394
			throws GeneralPropertyException {
395

    
396
		// Use the metadata to extract configurable properties from the 
397
		// overall properties list, and store those properties.
398
		try {
399
			SortedProperties backupProperties = 
400
				new SortedProperties(orgBackupPropertiesFilePath);
401
			
402
			// Populate the backup properties for organization properties using
403
			// the associated metadata file
404
			PropertiesMetaData orgMetadata = new PropertiesMetaData(orgMetaDataFilePath);
405
			
406
			// We do the same thing here for organization specific properies
407
			// with the addition that we need to iterate through all available 
408
			// organizations.  For instance, a metadata section that defines a 
409
			// property as "organization.base" will be entered into the metacat.properties
410
			// file with a key of "organization.base.NCEAS" for the NCEAS organization. 
411
			// This will be repeated for all available organizations.
412
			Set<String> orgKeySet = orgMetadata.getKeys();
413
			for (String orgName : OrganizationUtil.getOrganizations()) {
414
				for (String propertyKey : orgKeySet) {
415
					String orgPropertyKey = propertyKey + "." + orgName;
416
					backupProperties.addProperty(orgPropertyKey, getProperty(orgPropertyKey));
417
				}
418
			}
419
			
420
			// store the properties to file
421
			backupProperties.store();
422

    
423
		} catch (TransformerException te) {
424
			throw new GeneralPropertyException("Could not transform backup properties xml: "
425
					+ te.getMessage());
426
		} catch (IOException ioe) {
427
			throw new GeneralPropertyException("Could not backup configurable properties: "
428
					+ ioe.getMessage());
429
		} catch (UtilException ue) {
430
			throw new GeneralPropertyException("Could not get organizations: " + ue.getMessage());
431
		}
432
	}
433
	
434
	/**
435
	 * Writes out backup configurable properties to a file.
436
	 */
437
	public static void persistLDAPBackupProperties(ServletContext servletContext)
438
			throws GeneralPropertyException {
439

    
440
		// Use the metadata to extract configurable properties from the 
441
		// overall properties list, and store those properties.
442
		try {
443
			SortedProperties backupProperties = 
444
				new SortedProperties(ldapBackupPropertiesFilePath);
445
			
446
			// Populate the backup properties for ldap properties using
447
			// the associated metadata file
448
			PropertiesMetaData ldapMetadata = new PropertiesMetaData(ldapMetaDataFilePath);
449

    
450
			Set<String> ldapKeySet = ldapMetadata.getKeys();
451
			for (String propertyKey : ldapKeySet) {
452
				backupProperties.addProperty(propertyKey, getProperty(propertyKey));
453
			}
454
			
455
			// store the properties to file
456
			backupProperties.store();
457

    
458
		} catch (TransformerException te) {
459
			throw new GeneralPropertyException("Could not transform backup properties xml: "
460
					+ te.getMessage());
461
		} catch (IOException ioe) {
462
			throw new GeneralPropertyException("Could not backup configurable properties: "
463
					+ ioe.getMessage());
464
		} 
465
	}
466

    
467
	/**
468
	 * Gets the backup properties directory
469
	 * 
470
	 * TODO MCD figure out how to expand this.  At least for Windows
471
	 * 
472
	 * @return a string which holds the name of the backup directory. returns
473
	 *         null if directory could not be created.
474
	 */
475
	public static String getBackupDir() {
476
		return "/var/metacat/.metacat";
477
	}
478
	
479
	/**
480
	 * Reports whether properties are fully configured.
481
	 * 
482
	 * @return a boolean that is true if properties are not unconfigured and
483
	 *         false otherwise
484
	 */
485
	public static boolean arePropertiesConfigured() throws UtilException {
486
		try {
487
			return !PropertyService.getProperty("configutil.propertiesConfigured").equals(
488
					PropertyService.UNCONFIGURED);
489
		} catch (PropertyNotFoundException pnfe) {
490
			throw new UtilException("Could not determine if properties are configured: "
491
					+ pnfe.getMessage());
492
		}
493
	}
494
	
495
	/**
496
	 * Reports whether the metacat configuration utility should be run.  
497
	 * Returns false if  
498
	 *   -- dev.runConfiguration=false and
499
	 *   -- backup properties file exists
500
	 * Note that dev.runConfiguration should only be set to false when
501
	 * reinstalling the same version of the application in developement.
502
	 * 
503
	 * @return a boolean that is false if dev.runConfiguration is false and 
504
	 * the backup properties file exists.  
505
	 */
506
	public static boolean bypassConfiguration() {
507
		boolean bypass = false;
508
		
509
		// We only want to go through the check once to see if we want to
510
		// bypass the configuration.  We don't want to run through all of
511
		// this every time  we hit metacat. 
512
		if (bypassAlreadyChecked) {
513
			return bypass;
514
		}
515
		
516
		try {
517
			// check how dev.runConfiguration is set in metacat.properties
518
			String strRunConfiguration = PropertyService.getProperty("dev.runConfiguration");
519
			bypass = !(Boolean.parseBoolean(strRunConfiguration));
520
			
521
			// if the deb.runConfiguration is true, return false here.
522
			if (!bypass) {
523
				bypassAlreadyChecked = true;
524
				return false;
525
			}
526

    
527
			// The system is bypassing the configuration utility. We need to
528
			// get the backup properties and replace existing properties with
529
			// backup values.  We do this for main and org properties.		
530
			SortedProperties mainBackupProperties = getMainBackupProperties();
531
			Vector<String> backupPropertyNames = 
532
				mainBackupProperties.getPropertyNames();
533
			for (String backupPropertyName : backupPropertyNames) {
534
				String value = mainBackupProperties.getProperty(backupPropertyName);
535
				setPropertyNoPersist(backupPropertyName, value);
536
			}
537
				
538
			SortedProperties orgBackupProperties = getOrgBackupProperties();
539
			Vector<String> orgBackupPropertyNames = 
540
				orgBackupProperties.getPropertyNames();
541
			for (String orgBackupPropertyName : orgBackupPropertyNames) {
542
				String value = orgBackupProperties.getProperty(orgBackupPropertyName);
543
				setPropertyNoPersist(orgBackupPropertyName, value);
544
			}
545
			
546
			SortedProperties ldapBackupProperties = getLDAPBackupProperties();
547
			Vector<String> ldapBackupPropertyNames = 
548
				ldapBackupProperties.getPropertyNames();
549
			for (String ldapBackupPropertyName : ldapBackupPropertyNames) {
550
				String value = ldapBackupProperties.getProperty(ldapBackupPropertyName);
551
				setPropertyNoPersist(ldapBackupPropertyName, value);
552
			}
553
			
554
			setPropertyNoPersist("configutil.propertiesConfigured", "true");
555
			setPropertyNoPersist("configutil.ldapConfigured", "true");
556
//			setPropertyNoPersist("configutil.organizationsConfigured", "true");
557
			setPropertyNoPersist("configutil.skinsConfigured", "true");
558
			setPropertyNoPersist("configutil.databaseConfigured", "true");
559
			setPropertyNoPersist("configutil.geoserverConfigured", "bypassed");
560
				
561
			persistProperties();
562

    
563
		} catch (PropertyNotFoundException pnfe) {
564
			logMetacat.error("Could not find property: " + pnfe.getMessage());
565
		} catch (GeneralPropertyException gpe) {
566
			logMetacat.error("General property error: " + gpe.getMessage());
567
		}
568

    
569
		bypassAlreadyChecked = true;
570
		return bypass;
571
	}
572
	
573
	/**
574
	 * Take input from the user in an HTTP request about an property to be changed
575
	 * and update the metacat property file with that new value if it has
576
	 * changed from the value that was originally set.
577
	 * 
578
	 * @param request
579
	 *            that was generated by the user
580
	 * @param response
581
	 *            to send output back to the user
582
	 * @param propertyName
583
	 *            the name of the property to be checked and set
584
	 */
585
	public static void checkAndSetProperty(HttpServletRequest request, String propertyName) 
586
			throws GeneralPropertyException {
587
		String value = PropertyService.getProperty(propertyName);
588
		String newValue = request.getParameter(propertyName);
589
		if (newValue != null && !newValue.equals(value)) {
590
			PropertyService.setPropertyNoPersist(propertyName, newValue);
591
		}
592
	}
593

    
594
}
(2-2/6)