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: tao $'
9
 *     '$Date: 2016-08-24 09:45:44 -0700 (Wed, 24 Aug 2016) $'
10
 * '$Revision: 9900 $'
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.util.Map;
30
import java.util.Vector;
31

    
32
import javax.servlet.ServletContext;
33
import javax.servlet.http.HttpServletRequest;
34

    
35
import org.apache.commons.configuration.ConfigurationException;
36
import org.apache.log4j.Logger;
37
import org.dataone.configuration.Settings;
38

    
39
import edu.ucsb.nceas.metacat.shared.BaseService;
40
import edu.ucsb.nceas.metacat.shared.ServiceException;
41
import edu.ucsb.nceas.utilities.FileUtil;
42
import edu.ucsb.nceas.utilities.GeneralPropertyException;
43
import edu.ucsb.nceas.utilities.PropertiesMetaData;
44
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
45
import edu.ucsb.nceas.utilities.SortedProperties;
46

    
47
/**
48
 * A suite of utility classes for the metadata configuration utility
49
 */
50
public class PropertyService extends BaseService {
51

    
52
	private static PropertyService propertyService = null;
53

    
54
	private static PropertiesInterface properties = null;
55

    
56
	// system is configured
57
	public static final String CONFIGURED = PropertiesInterface.CONFIGURED;
58
	// system has never been configured
59
	public static final String UNCONFIGURED = PropertiesInterface.UNCONFIGURED;
60
	public static final String BYPASSED = PropertiesInterface.BYPASSED;
61

    
62
	private static String DEFAULT_CONFIG_FILE_DIR = "WEB-INF";
63
	public static String CONFIG_FILE_DIR = null;
64
	public static String CONFIG_FILE_NAME = null;
65
	public static String CONFIG_FILE_PATH = null;
66
	
67
	public static String TEST_CONFIG_FILE_NAME = null;
68

    
69
	private static String DEFAULT_PROPERTY_CLASS_NAME = "edu.ucsb.nceas.metacat.properties.ConfigurableProperties";
70
	private static String PROPERTY_CLASS_NAME = null;
71

    
72
	private static String RECOMMENDED_EXTERNAL_DIR = null;
73

    
74
	private static Logger logMetacat = Logger.getLogger(PropertyService.class);
75

    
76
	/**
77
	 * private constructor since this is a singleton
78
	 * 
79
	 * @param servletContext
80
	 *            the context we will use to get relative paths
81
	 */
82
	private PropertyService() throws ServiceException {
83
		_serviceName = "PropertyService";
84
		
85
		initialize();
86
	}
87

    
88
	/**
89
	 * Get the single instance of PropertyService.
90
	 * 
91
	 * @return the single instance of PropertyService
92
	 */
93
	public static PropertyService getInstance(ServletContext context)
94
			throws ServiceException {
95
		if (propertyService == null) {
96
			
97
			String applicationName = (String)context.getAttribute("APPLICATION_NAME");
98
			
99
			CONFIG_FILE_DIR = context.getInitParameter("configFileDir");
100
			if (CONFIG_FILE_DIR == null) {
101
				String configDir = context.getRealPath(DEFAULT_CONFIG_FILE_DIR);
102
				CONFIG_FILE_DIR = configDir;
103
			}
104
			
105
			CONFIG_FILE_PATH = CONFIG_FILE_DIR + FileUtil.getFS() + applicationName + ".properties";
106

    
107
			PROPERTY_CLASS_NAME = context.getInitParameter("propertyClassName");
108
			if (PROPERTY_CLASS_NAME == null) {
109
				PROPERTY_CLASS_NAME = DEFAULT_PROPERTY_CLASS_NAME;
110
			}
111

    
112
			propertyService = new PropertyService();
113
		}
114
		return propertyService;
115
	}
116

    
117
	 /**
118
	 * Get the single instance of PropertyService for test purposes. In this
119
	 * case, we allow the configuration directory to be passed in.
120
	 *
121
	 * @param configDir the configuration directory we need to look in
122
	 * @return the single instance of PropertyService
123
	 */
124
	 public static PropertyService getInstance(String testConfigFileDir)
125
			throws ServiceException {
126
		if (propertyService == null) {
127
			CONFIG_FILE_DIR = testConfigFileDir;
128

    
129
			PROPERTY_CLASS_NAME = DEFAULT_PROPERTY_CLASS_NAME;
130

    
131
			propertyService = new PropertyService();
132
		}
133
		return propertyService;
134
	}
135
	 
136
	 /**
137
		 * Get the single instance of PropertyService for test purposes. In this
138
		 * case, we allow the configuration directory to be passed in.
139
		 *
140
		 * @param configDir the configuration directory we need to look in
141
		 * @return the single instance of PropertyService
142
		 */
143
		 public static PropertyService getInstance() throws ServiceException {
144
			if (propertyService == null) {
145
				throw new ServiceException("PropertyService.getInstance - cannot call " +
146
						"getInstance without parameters until property service has been created " +
147
						"with either servlet context or config file path.");
148
			}
149
			return propertyService;
150
		}
151

    
152
	public boolean refreshable() {
153
		return true;
154
	}
155

    
156
	public void doRefresh() throws ServiceException {
157
	    initialize();
158
	}
159

    
160
	public void stop() throws ServiceException {
161
		return;
162
	}
163

    
164
	/**
165
	 * Initialize the singleton.
166
	 * 
167
	 * @param servletContext
168
	 *            the context we will use to get relative paths
169
	 */
170
	private void initialize() throws ServiceException {
171

    
172
		logMetacat.debug("Initializing PropertyService");
173
		
174
		try {
175
			Class<?> classDef = Class.forName(PROPERTY_CLASS_NAME);
176
			properties = (PropertiesInterface) classDef.newInstance();
177
		} catch (InstantiationException ie) {
178
			throw new ServiceException("Could not instantiate property class: "
179
					+ PROPERTY_CLASS_NAME + " " + ie.getMessage());
180
		} catch (IllegalAccessException iae) {
181
			throw new ServiceException("Access error when intantiating property class: "
182
					+ PROPERTY_CLASS_NAME + " " + iae.getMessage());
183
		} catch (ClassNotFoundException cnfe) {
184
			throw new ServiceException("Could not find property class: "
185
					+ PROPERTY_CLASS_NAME + " " + cnfe.getMessage());
186
		}
187
	}
188

    
189
	/**
190
	 * Utility method to get a property value from the properties file
191
	 * 
192
	 * @param propertyName
193
	 *            the name of the property requested
194
	 * @return the String value for the property
195
	 */
196
	public static String getProperty(String propertyName)
197
			throws PropertyNotFoundException {
198
		return properties.getProperty(propertyName);
199
	}
200

    
201
	/**
202
	 * Get a set of all property names.
203
	 * 
204
	 * @return Set of property names
205
	 */
206
	public static Vector<String> getPropertyNames() {
207
		return properties.getPropertyNames();
208
	}
209

    
210
	/**
211
	 * Get a Set of all property names that start with the groupName prefix.
212
	 * 
213
	 * @param groupName
214
	 *            the prefix of the keys to search for.
215
	 * @return enumeration of property names
216
	 */
217
	public static Vector<String> getPropertyNamesByGroup(String groupName) {
218
		return properties.getPropertyNamesByGroup(groupName);
219
	}
220

    
221
	/**
222
	 * Get a Map of all properties that start with the groupName prefix.
223
	 * 
224
	 * @param groupName
225
	 *            the prefix of the keys to search for.
226
	 * @return Map of property names
227
	 */
228
	public static Map<String, String> getPropertiesByGroup(String groupName)
229
			throws PropertyNotFoundException {
230
		return properties.getPropertiesByGroup(groupName);
231
	}
232

    
233
	/**
234
	 * Utility method to set a property value both in memory and to the
235
	 * properties file
236
	 * 
237
	 * @param propertyName
238
	 *            the name of the property requested
239
	 * @param newValue
240
	 *            the new value for the property
241
	 */
242
	public static void setProperty(String propertyName, String newValue)
243
			throws GeneralPropertyException {
244
		properties.setProperty(propertyName, newValue);
245
		properties.persistProperties();
246
	}
247
	
248
	/**
249
	 * Utility method to add a property value both in memory and to the
250
	 * properties file
251
	 * 
252
	 * @param propertyName
253
	 *            the name of the property to add
254
	 * @param newValue
255
	 *            the value for the property
256
	 */
257
	public static void addProperty(String propertyName, String value)
258
			throws GeneralPropertyException {
259
		properties.addProperty(propertyName, value);
260
		properties.persistProperties();
261
	}
262

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

    
280
	/**
281
	 * Save the properties to a properties file. Note, the order and comments
282
	 * will be preserved.
283
	 */
284
	public static void persistProperties() throws GeneralPropertyException {
285
		properties.persistProperties();
286
	}
287

    
288
	/**
289
	 * Get the main backup properties file. These are configurable properties
290
	 * that are stored outside the metacat install directories so the user does
291
	 * not need to re-enter all the configuration information every time they do
292
	 * an upgrade.
293
	 * 
294
	 * @return a SortedProperties object with the backup properties
295
	 */
296
	public static SortedProperties getMainBackupProperties() throws GeneralPropertyException {
297
		return properties.getMainBackupProperties();
298
	}
299

    
300
	/**
301
	 * Get the auth backup properties file. These are configurable properties
302
	 * that are stored outside the metacat install directories so the user does
303
	 * not need to re-enter all the configuration information every time they do
304
	 * an upgrade.
305
	 * 
306
	 * @return a SortedProperties object with the backup properties
307
	 */
308
	public static SortedProperties getAuthBackupProperties() throws GeneralPropertyException{
309
		return properties.getAuthBackupProperties();
310
	}
311

    
312
	/**
313
	 * Get the main properties metadata. This is retrieved from an xml file that
314
	 * describes the attributes of configurable properties.
315
	 * 
316
	 * @return a PropertiesMetaData object with the main properties metadata
317
	 */
318
	public static PropertiesMetaData getMainMetaData() throws GeneralPropertyException{
319
		return properties.getMainMetaData();
320
	}
321

    
322
	/**
323
	 * Get the auth properties metadata. This is retrieved from an xml file that
324
	 * describes the attributes of configurable properties.
325
	 * 
326
	 * @return a PropertiesMetaData object with the organization properties
327
	 *         metadata
328
	 */
329
	public static PropertiesMetaData getAuthMetaData() throws GeneralPropertyException{
330
		return properties.getAuthMetaData();
331
	}
332

    
333
	/**
334
	 * Writes out backup configurable properties to a file.
335
	 */
336
	public static void persistMainBackupProperties() throws GeneralPropertyException {
337

    
338
		properties.persistMainBackupProperties();
339
	}
340

    
341
	/**
342
	 * Writes out backup configurable properties to a file.
343
	 */
344
	public static void persistAuthBackupProperties(ServletContext servletContext)
345
			throws GeneralPropertyException {
346

    
347
		properties.persistAuthBackupProperties(servletContext);
348
	}
349

    
350
	/**
351
	 * Reports whether properties are fully configured.
352
	 * 
353
	 * @return a boolean that is true if properties are not unconfigured and
354
	 *         false otherwise
355
	 */
356
	public static boolean arePropertiesConfigured() throws GeneralPropertyException {
357
		return properties.arePropertiesConfigured();
358
	}
359

    
360
	/**
361
	 * Determine if the system is configured to bypass configuration. If so, the
362
	 * system will look for backup configuration files at startup time and use
363
	 * those to configure metacat. The bypass options should only be set by
364
	 * developers. Production code should never bypass confguration.
365
	 * 
366
	 * @return true if dev.runConfiguration is set to true in metacat.properties
367
	 *         and we have not already checked for bypass, false otherwise.
368
	 */
369
	public static boolean doBypass() throws GeneralPropertyException {
370
		return properties.doBypass();
371
	}
372

    
373
	/**
374
	 * Reports whether the metacat configuration utility should be run. Returns
375
	 * false if -- dev.runConfiguration=false and -- backup properties file
376
	 * exists Note that dev.runConfiguration should only be set to false when
377
	 * reinstalling the same version of the application in developement.
378
	 * 
379
	 * @return a boolean that is false if dev.runConfiguration is false and the
380
	 *         backup properties file exists.
381
	 */
382
	public static void bypassConfiguration() throws GeneralPropertyException {
383
		properties.bypassConfiguration();
384
	}
385

    
386
	/**
387
	 * Take input from the user in an HTTP request about an property to be
388
	 * changed and update the metacat property file with that new value if it
389
	 * has changed from the value that was originally set.
390
	 * 
391
	 * @param request
392
	 *            that was generated by the user
393
	 * @param response
394
	 *            to send output back to the user
395
	 * @param propertyName
396
	 *            the name of the property to be checked and set
397
	 */
398
	public static boolean checkAndSetProperty(HttpServletRequest request, String propertyName)
399
			throws GeneralPropertyException {
400
		return properties.checkAndSetProperty(request, propertyName);
401
	}
402

    
403
	/**
404
	 * Sets the recommended external directory. This is populated during
405
	 * initialization time using the SystemUtil.discoverExternalDir() method.
406
	 * This directory will be used to suggest external user directories when the
407
	 * user configures metacat for the first time.
408
	 * 
409
	 */
410
	public static void setRecommendedExternalDir(String extBaseDir) {
411
		RECOMMENDED_EXTERNAL_DIR = extBaseDir;
412
	}
413

    
414
	/**
415
	 * Returns the recommended external base directory. This is populated during
416
	 * initialization time using the SystemUtil.discoverExternalBaseDir()
417
	 * method. This directory will be used to suggest external user directories
418
	 * when the user configures metacat for the first time.
419
	 * 
420
	 * @return a String holding the recommended external directory
421
	 */
422
	public static String getRecommendedExternalDir() {
423
		return RECOMMENDED_EXTERNAL_DIR;
424
	}
425
	
426
	/**
427
	 * The properties on the Setting class isn't synchronized with the change the Metacat properties file. 
428
	 * This method synchronizes (reloads) the properties' change to the Setting class when the property file was modified.
429
	 */
430
	public static void syncToSettings() throws GeneralPropertyException {
431
	    try {
432
	        Settings.getConfiguration();
433
	        Settings.augmentConfiguration(CONFIG_FILE_PATH);
434
	    } catch (ConfigurationException e) {
435
	        e.printStackTrace();
436
	        throw new GeneralPropertyException(e.getMessage());
437
	    }
438
	}
439

    
440
}
(3-3/5)