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-10-09 09:53:18 -0700 (Thu, 09 Oct 2008) $'
10
 * '$Revision: 4429 $'
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 PropertyService.
113
	 * 
114
	 * @param servletContext the context we will use to get relative paths
115
	 * @return the single instance of PropertyService
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
			mainBackupProperties = 
381
				new SortedProperties(mainBackupPropertiesFilePath);
382
			mainBackupProperties.load();
383

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

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

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

    
446
		// Use the metadata to extract configurable properties from the 
447
		// overall properties list, and store those properties.
448
		try {
449
			SortedProperties backupProperties = 
450
				new SortedProperties(ldapBackupPropertiesFilePath);
451
			
452
			// Populate the backup properties for ldap properties using
453
			// the associated metadata file
454
			PropertiesMetaData ldapMetadata = new PropertiesMetaData(ldapMetaDataFilePath);
455

    
456
			Set<String> ldapKeySet = ldapMetadata.getKeys();
457
			for (String propertyKey : ldapKeySet) {
458
				backupProperties.addProperty(propertyKey, getProperty(propertyKey));
459
			}
460
			
461
			// store the properties to file
462
			backupProperties.store();
463
			ldapBackupProperties = 
464
				new SortedProperties(ldapBackupPropertiesFilePath);
465
			ldapBackupProperties.load();
466

    
467
		} catch (TransformerException te) {
468
			throw new GeneralPropertyException("Could not transform backup properties xml: "
469
					+ te.getMessage());
470
		} catch (IOException ioe) {
471
			throw new GeneralPropertyException("Could not backup configurable properties: "
472
					+ ioe.getMessage());
473
		} 
474
	}
475

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

    
538
			// The system is bypassing the configuration utility. We need to
539
			// get the backup properties and replace existing properties with
540
			// backup values.  We do this for main and org properties.		
541
			logMetacat.debug("bypassConfiguration: setting main backup properties.");
542
			SortedProperties mainBackupProperties = getMainBackupProperties();
543
			Vector<String> backupPropertyNames = 
544
				mainBackupProperties.getPropertyNames();
545
			for (String backupPropertyName : backupPropertyNames) {
546
				String value = mainBackupProperties.getProperty(backupPropertyName);
547
				setPropertyNoPersist(backupPropertyName, value);
548
			}
549
				
550
//			SortedProperties orgBackupProperties = getOrgBackupProperties();
551
//			Vector<String> orgBackupPropertyNames = 
552
//				orgBackupProperties.getPropertyNames();
553
//			for (String orgBackupPropertyName : orgBackupPropertyNames) {
554
//				String value = orgBackupProperties.getProperty(orgBackupPropertyName);
555
//				setPropertyNoPersist(orgBackupPropertyName, value);
556
//			}
557

    
558
			logMetacat.debug("bypassConfiguration: setting ldap backup properties.");
559
			SortedProperties ldapBackupProperties = getLDAPBackupProperties();
560
			Vector<String> ldapBackupPropertyNames = 
561
				ldapBackupProperties.getPropertyNames();
562
			for (String ldapBackupPropertyName : ldapBackupPropertyNames) {
563
				String value = ldapBackupProperties.getProperty(ldapBackupPropertyName);
564
				setPropertyNoPersist(ldapBackupPropertyName, value);
565
			}
566

    
567
			logMetacat.debug("bypassConfiguration: setting configutil sections to true.");
568
			setPropertyNoPersist("configutil.propertiesConfigured", "true");
569
			setPropertyNoPersist("configutil.ldapConfigured", "true");
570
//			setPropertyNoPersist("configutil.organizationsConfigured", "true");
571
			setPropertyNoPersist("configutil.skinsConfigured", "true");
572
			setPropertyNoPersist("configutil.databaseConfigured", "true");
573
			setPropertyNoPersist("configutil.geoserverConfigured", "bypassed");
574
				
575
			persistProperties();
576

    
577
		} catch (PropertyNotFoundException pnfe) {
578
			logMetacat.error("bypassConfiguration: Could not find property: " + pnfe.getMessage());
579
		} catch (GeneralPropertyException gpe) {
580
			logMetacat.error("bypassConfiguration: General property error: " + gpe.getMessage());
581
		}
582

    
583
		bypassAlreadyChecked = true;
584
		return bypass;
585
	}
586
	
587
	/**
588
	 * Take input from the user in an HTTP request about an property to be changed
589
	 * and update the metacat property file with that new value if it has
590
	 * changed from the value that was originally set.
591
	 * 
592
	 * @param request
593
	 *            that was generated by the user
594
	 * @param response
595
	 *            to send output back to the user
596
	 * @param propertyName
597
	 *            the name of the property to be checked and set
598
	 */
599
	public static void checkAndSetProperty(HttpServletRequest request, String propertyName) 
600
			throws GeneralPropertyException {
601
		String value = PropertyService.getProperty(propertyName);
602
		String newValue = request.getParameter(propertyName);
603
		if (newValue != null && !newValue.equals(value)) {
604
			PropertyService.setPropertyNoPersist(propertyName, newValue);
605
		}
606
	}
607

    
608
}
(2-2/6)