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: cjones $'
9
 *     '$Date: 2011-06-07 09:53:46 -0700 (Tue, 07 Jun 2011) $'
10
 * '$Revision: 6124 $'
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.log4j.Logger;
36

    
37
import edu.ucsb.nceas.metacat.shared.BaseService;
38
import edu.ucsb.nceas.metacat.shared.ServiceException;
39
import edu.ucsb.nceas.utilities.FileUtil;
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 PropertyService extends BaseService {
49

    
50
	private static PropertyService propertyService = null;
51

    
52
	private static PropertiesInterface properties = null;
53

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

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

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

    
70
	private static String RECOMMENDED_EXTERNAL_DIR = null;
71

    
72
	private static Logger logMetacat = Logger.getLogger(PropertyService.class);
73

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

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

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

    
110
			propertyService = new PropertyService();
111
		}
112
		return propertyService;
113
	}
114

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

    
127
			PROPERTY_CLASS_NAME = DEFAULT_PROPERTY_CLASS_NAME;
128

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

    
150
	public boolean refreshable() {
151
		return true;
152
	}
153

    
154
	public void doRefresh() throws ServiceException {
155
		// initialize();
156
	}
157

    
158
	public void stop() throws ServiceException {
159
		return;
160
	}
161

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
336
		properties.persistMainBackupProperties();
337
	}
338

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

    
345
		properties.persistAuthBackupProperties(servletContext);
346
	}
347

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

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

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

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

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

    
412
	/**
413
	 * Returns the recommended external base directory. This is populated during
414
	 * initialization time using the SystemUtil.discoverExternalBaseDir()
415
	 * method. This directory will be used to suggest external user directories
416
	 * when the user configures metacat for the first time.
417
	 * 
418
	 * @return a String holding the recommended external directory
419
	 */
420
	public static String getRecommendedExternalDir() {
421
		return RECOMMENDED_EXTERNAL_DIR;
422
	}
423

    
424
}
(3-3/5)