Project

General

Profile

« Previous | Next » 

Revision 4711

Added by daigle over 15 years ago

Remove org configuration methods since they are not used. Get the backup configuration directories when doing a bypass. Implement the refresh method.

View differences:

PropertyService.java
37 37

  
38 38
import org.apache.log4j.Logger;
39 39

  
40
import edu.ucsb.nceas.metacat.util.OrganizationUtil;
41
import edu.ucsb.nceas.metacat.util.SystemUtil;
42 40
import edu.ucsb.nceas.metacat.util.UtilException;
43 41
import edu.ucsb.nceas.utilities.FileUtil;
44 42
import edu.ucsb.nceas.utilities.GeneralPropertyException;
......
60 58
	public static final String BYPASSED = "bypassed";
61 59
	
62 60
	private static final String MAIN_CONFIG_NAME = "metacat.properties";
63
	private static final String ORG_CONFIG_NAME = "org.properties";
64 61
	private static final String AUTH_CONFIG_NAME = "auth.properties";
65 62
	
66 63
	private static boolean bypassAlreadyChecked = false;
......
74 71
	private static String propertiesMetaDataFilePath = null;
75 72
	private static PropertiesMetaData mainMetaData = null;
76 73
	
77
	private static String orgBackupPropertiesFilePath = null;
78
	private static SortedProperties orgBackupProperties = null;
79
	
80
	private static String orgMetaDataFilePath = null;
81
	private static PropertiesMetaData orgMetaData = null;
82
	
83 74
	private static String authBackupPropertiesFilePath = null;
84 75
	private static SortedProperties authBackupProperties = null;
85 76
	
......
94 85
	 * 
95 86
	 * @param servletContext the context we will use to get relative paths
96 87
	 */
97
	private PropertyService(String configDir) throws ServiceException {
88
	private PropertyService() throws ServiceException {
98 89
		try {
99
			initialize(configDir);
90
			initialize();
100 91
		} catch (GeneralPropertyException gpe) {
101 92
			throw new ServiceException("Properties problem while initializing " + 
102 93
					"PropertyService: " + gpe.getMessage());
......
112 103
	 * @param servletContext the context we will use to get relative paths
113 104
	 * @return the single instance of PropertyService
114 105
	 */
115
	public static PropertyService getInstance(String configDir) throws ServiceException {
106
	public static PropertyService getInstance() throws ServiceException {
116 107
		if (propertyService == null) {
117
			propertyService = new PropertyService(configDir);
108
			
109
			propertyService = new PropertyService();
118 110
		}
119 111
		return propertyService;
120 112
	}
121 113
	
122 114
	public boolean refreshable() {
123
		return false;
115
		return true;
124 116
	}
125 117
	
126
	protected void doRefresh() {
127
		return;
118
	protected void doRefresh() throws ServiceException {
119
		try {
120
			initialize();
121
		} catch (IOException ioe) {
122
			throw new ServiceException("Could not refresh SkinPropertyService due to"
123
					+ " I/O error: " + ioe.getMessage());
124
		} catch (GeneralPropertyException gpe) {
125
			throw new ServiceException("Could not refresh SkinPropertyService due to"
126
					+ " property error: " + gpe.getMessage());
127
		}
128 128
	}
129 129
	
130 130
	/**
......
132 132
	 * 
133 133
	 * @param servletContext the context we will use to get relative paths
134 134
	 */
135
	public void initialize(String configDir)
136
			throws IOException, GeneralPropertyException {
135
	private void initialize()
136
			throws IOException, GeneralPropertyException, ServiceException {
137 137
		
138 138
		logMetacat.debug("Initializing PropertyService");
139
		
140
		String configDir = ServiceService.getRealConfigDir();
139 141

  
140 142
		// mainProperties will hold the primary configuration values for metacat.
141 143
		mainPropertiesFilePath = configDir + FileUtil.getFS() + MAIN_CONFIG_NAME;
142
		if (mainProperties == null) {
143
			mainProperties = new SortedProperties(mainPropertiesFilePath);
144
			mainProperties.load();
145
		}
144
		mainProperties = new SortedProperties(mainPropertiesFilePath);
145
		mainProperties.load();
146 146

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

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

  
166 154
			// authMetaData holds configuration information about organization level 
167 155
			// properties.  This is primarily used to display input fields on 
168 156
			// the auth configuration page. The information is retrieved 
169 157
			// from an xml metadata file dedicated just to auth properties.
170 158
			authMetaDataFilePath = configDir + FileUtil.getFS() + AUTH_CONFIG_NAME + ".metadata.xml";
171
			if (authMetaData == null) {
172
				authMetaData = new PropertiesMetaData(authMetaDataFilePath);
173
			}
159
			authMetaData = new PropertiesMetaData(authMetaDataFilePath);
174 160
		} catch (TransformerException te) {
175 161
			throw new GeneralPropertyException(te.getMessage());
176 162
		}
177 163

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

  
215 183
	/**
......
307 275
	}
308 276
	
309 277
	/**
310
	 * Get the organizational backup properties file. These are configurable 
311
	 * properties that are stored outside the metacat install directories so 
312
	 * the user does not need to re-enter all the configuration information 
313
	 * every time they do an upgrade.
314
	 * 
315
	 * @return a SortedProperties object with the backup properties
316
	 */
317
	public static SortedProperties getOrgBackupProperties() {
318
		return orgBackupProperties;
319
	}
320
	
321
	/**
322 278
	 * Get the auth backup properties file. These are configurable 
323 279
	 * properties that are stored outside the metacat install directories so 
324 280
	 * the user does not need to re-enter all the configuration information 
......
341 297
	}
342 298
	
343 299
	/**
344
	 * Get the organization properties metadata. This is retrieved from an xml
345
	 * file that describes the attributes of configurable properties.
346
	 * 
347
	 * @return a PropertiesMetaData object with the organization properties
348
	 *         metadata
349
	 */
350
	public static PropertiesMetaData getOrgMetaData() {
351
		return orgMetaData;
352
	}
353
	
354
	/**
355 300
	 * Get the auth properties metadata. This is retrieved from an xml
356 301
	 * file that describes the attributes of configurable properties.
357 302
	 * 
......
399 344
	/**
400 345
	 * Writes out backup configurable properties to a file.
401 346
	 */
402
	public static void persistOrgBackupProperties(ServletContext servletContext)
403
			throws GeneralPropertyException {
404

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

  
435
		} catch (TransformerException te) {
436
			throw new GeneralPropertyException("Could not transform backup properties xml: "
437
					+ te.getMessage());
438
		} catch (IOException ioe) {
439
			throw new GeneralPropertyException("Could not backup configurable properties: "
440
					+ ioe.getMessage());
441
		} catch (UtilException ue) {
442
			throw new GeneralPropertyException("Could not get organizations: " + ue.getMessage());
443
		}
444
	}
445
	
446
	/**
447
	 * Writes out backup configurable properties to a file.
448
	 */
449 347
	public static void persistAuthBackupProperties(ServletContext servletContext)
450 348
			throws GeneralPropertyException {
451 349

  
......
478 376
					+ ioe.getMessage());
479 377
		} 
480 378
	}
481

  
482
	/**
483
	 * Gets the backup properties directory
484
	 * 
485
	 * @return a string which holds the name of the backup directory. returns
486
	 *         null if directory could not be created.
487
	 */
488
	public static String getBackupDir() {
489
		return SystemUtil.discoverExternalDir() + FileUtil.getFS() + ".metacat";
490
	}
491 379
	
492 380
	/**
493 381
	 * Reports whether properties are fully configured.
......
495 383
	 * @return a boolean that is true if properties are not unconfigured and
496 384
	 *         false otherwise
497 385
	 */
498
	public static boolean arePropertiesConfigured() throws UtilException {
386
	public static boolean arePropertiesConfigured() throws UtilException {		
499 387
		try {
500
			return !PropertyService.getProperty("configutil.propertiesConfigured").equals(
501
					PropertyService.UNCONFIGURED);
388
			String propertiesConfigured = PropertyService.getProperty("configutil.propertiesConfigured");
389
			if (propertiesConfigured != null && !propertiesConfigured.equals(UNCONFIGURED)) {
390
				return true;
391
			}			
392
			return false;
502 393
		} catch (PropertyNotFoundException pnfe) {
503 394
			throw new UtilException("Could not determine if properties are configured: "
504 395
					+ pnfe.getMessage());
......
506 397
	}
507 398
	
508 399
	/**
400
	 * Determine if the system is configured to bypass configuration. If so, the
401
	 * system will look for backup configuration files at startup time and use
402
	 * those to configure metacat. The bypass options should only be set by
403
	 * developers. Production code should never bypass confguration.
404
	 * 
405
	 * @return true if dev.runConfiguration is set to true in metacat.properties
406
	 *         and we have not already checked for bypass, false otherwise.
407
	 */
408
	public static boolean doBypass() throws PropertyNotFoundException {
409
		// We only want to go through the check once to see if we want to
410
		// bypass the configuration.  We don't want to run through all of
411
		// this every time  we hit metacat. 
412
		if (bypassAlreadyChecked) {
413
			logMetacat.debug("bypassConfiguration not performing full bypass check.  Bypass set to false");
414
			return false;
415
		}
416
		
417
		// check how dev.runConfiguration is set in metacat.properties
418
		String strRunConfiguration = PropertyService.getProperty("dev.runConfiguration");
419
		boolean runConfiguration = Boolean.parseBoolean(strRunConfiguration);
420
		logMetacat.debug("bypassConfiguration: dev.runConfiguration property set to: " + strRunConfiguration);
421
		
422
		// if the dev.runConfiguration is true, return false here.
423
		if (runConfiguration) {
424
			bypassAlreadyChecked = true;
425
			return false;
426
		} 
427
		
428
		return true;
429
	}
430
	
431
	/**
509 432
	 * Reports whether the metacat configuration utility should be run.  
510 433
	 * Returns false if  
511 434
	 *   -- dev.runConfiguration=false and
......
516 439
	 * @return a boolean that is false if dev.runConfiguration is false and 
517 440
	 * the backup properties file exists.  
518 441
	 */
519
	public static boolean bypassConfiguration() {
520
		boolean bypass = false;
521
		
522
		// We only want to go through the check once to see if we want to
523
		// bypass the configuration.  We don't want to run through all of
524
		// this every time  we hit metacat. 
525
		if (bypassAlreadyChecked) {
526
			logMetacat.debug("bypassConfiguration not performing full bypass check.  Bypass set to " + bypass);
527
			return bypass;
528
		}
529
		
442
	public static void bypassConfiguration() throws ServiceException {
530 443
		try {
531
			// check how dev.runConfiguration is set in metacat.properties
532
			String strRunConfiguration = PropertyService.getProperty("dev.runConfiguration");
533
			bypass = !(Boolean.parseBoolean(strRunConfiguration));
534
			logMetacat.debug("bypassConfiguration: dev.runConfiguration property set to: " + strRunConfiguration);
535
			
536
			// if the dev.runConfiguration is true, return false here.
537
			if (!bypass) {
538
				bypassAlreadyChecked = true;
539
				return false;
540
			}
444
			boolean doBypass = doBypass();
541 445

  
446
			if (!doBypass) {
447
				throw new ServiceException(
448
						"Attempting to do bypass when system is not configured for it.");
449
			}			
450

  
542 451
			// The system is bypassing the configuration utility. We need to
543 452
			// get the backup properties and replace existing properties with
544 453
			// backup values.  We do this for main and org properties.		
......
550 459
				String value = mainBackupProperties.getProperty(backupPropertyName);
551 460
				setPropertyNoPersist(backupPropertyName, value);
552 461
			}
553
				
554
//			SortedProperties orgBackupProperties = getOrgBackupProperties();
555
//			Vector<String> orgBackupPropertyNames = 
556
//				orgBackupProperties.getPropertyNames();
557
//			for (String orgBackupPropertyName : orgBackupPropertyNames) {
558
//				String value = orgBackupProperties.getProperty(orgBackupPropertyName);
559
//				setPropertyNoPersist(orgBackupPropertyName, value);
560
//			}
561 462

  
562 463
			logMetacat.debug("bypassConfiguration: setting auth backup properties.");
563 464
			SortedProperties authBackupProperties = getAuthBackupProperties();
......
571 472
			logMetacat.debug("bypassConfiguration: setting configutil sections to true.");
572 473
			setPropertyNoPersist("configutil.propertiesConfigured", "true");
573 474
			setPropertyNoPersist("configutil.authConfigured", "true");
574
//			setPropertyNoPersist("configutil.organizationsConfigured", "true");
575 475
			setPropertyNoPersist("configutil.skinsConfigured", "true");
576 476
			setPropertyNoPersist("configutil.databaseConfigured", "true");
577 477
			setPropertyNoPersist("configutil.geoserverConfigured", "bypassed");
......
585 485
		}
586 486

  
587 487
		bypassAlreadyChecked = true;
588
		return bypass;
589 488
	}
590 489
	
591 490
	/**

Also available in: Unified diff