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: 2009-08-24 14:34:17 -0700 (Mon, 24 Aug 2009) $'
10
 * '$Revision: 5030 $'
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 set a property value in memory. This will NOT cause the
248
	 * property to be written to disk. Use this method to set multiple
249
	 * properties in a row without causing excessive I/O. You must call
250
	 * persistProperties() once you're done setting properties to have them
251
	 * written to disk.
252
	 * 
253
	 * @param propertyName
254
	 *            the name of the property requested
255
	 * @param newValue
256
	 *            the new value for the property
257
	 */
258
	public static void setPropertyNoPersist(String propertyName, String newValue)
259
			throws GeneralPropertyException {
260
		properties.setPropertyNoPersist(propertyName, newValue);
261
	}
262

    
263
	/**
264
	 * Save the properties to a properties file. Note, the order and comments
265
	 * will be preserved.
266
	 */
267
	public static void persistProperties() throws GeneralPropertyException {
268
		properties.persistProperties();
269
	}
270

    
271
	/**
272
	 * Get the main backup properties file. These are configurable properties
273
	 * that are stored outside the metacat install directories so the user does
274
	 * not need to re-enter all the configuration information every time they do
275
	 * an upgrade.
276
	 * 
277
	 * @return a SortedProperties object with the backup properties
278
	 */
279
	public static SortedProperties getMainBackupProperties() throws GeneralPropertyException {
280
		return properties.getMainBackupProperties();
281
	}
282

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

    
295
	/**
296
	 * Get the main properties metadata. This is retrieved from an xml file that
297
	 * describes the attributes of configurable properties.
298
	 * 
299
	 * @return a PropertiesMetaData object with the main properties metadata
300
	 */
301
	public static PropertiesMetaData getMainMetaData() throws GeneralPropertyException{
302
		return properties.getMainMetaData();
303
	}
304

    
305
	/**
306
	 * Get the auth properties metadata. This is retrieved from an xml file that
307
	 * describes the attributes of configurable properties.
308
	 * 
309
	 * @return a PropertiesMetaData object with the organization properties
310
	 *         metadata
311
	 */
312
	public static PropertiesMetaData getAuthMetaData() throws GeneralPropertyException{
313
		return properties.getAuthMetaData();
314
	}
315

    
316
	/**
317
	 * Writes out backup configurable properties to a file.
318
	 */
319
	public static void persistMainBackupProperties() throws GeneralPropertyException {
320

    
321
		properties.persistMainBackupProperties();
322
	}
323

    
324
	/**
325
	 * Writes out backup configurable properties to a file.
326
	 */
327
	public static void persistAuthBackupProperties(ServletContext servletContext)
328
			throws GeneralPropertyException {
329

    
330
		properties.persistAuthBackupProperties(servletContext);
331
	}
332

    
333
	/**
334
	 * Reports whether properties are fully configured.
335
	 * 
336
	 * @return a boolean that is true if properties are not unconfigured and
337
	 *         false otherwise
338
	 */
339
	public static boolean arePropertiesConfigured() throws GeneralPropertyException {
340
		return properties.arePropertiesConfigured();
341
	}
342

    
343
	/**
344
	 * Determine if the system is configured to bypass configuration. If so, the
345
	 * system will look for backup configuration files at startup time and use
346
	 * those to configure metacat. The bypass options should only be set by
347
	 * developers. Production code should never bypass confguration.
348
	 * 
349
	 * @return true if dev.runConfiguration is set to true in metacat.properties
350
	 *         and we have not already checked for bypass, false otherwise.
351
	 */
352
	public static boolean doBypass() throws GeneralPropertyException {
353
		return properties.doBypass();
354
	}
355

    
356
	/**
357
	 * Reports whether the metacat configuration utility should be run. Returns
358
	 * false if -- dev.runConfiguration=false and -- backup properties file
359
	 * exists Note that dev.runConfiguration should only be set to false when
360
	 * reinstalling the same version of the application in developement.
361
	 * 
362
	 * @return a boolean that is false if dev.runConfiguration is false and the
363
	 *         backup properties file exists.
364
	 */
365
	public static void bypassConfiguration() throws GeneralPropertyException {
366
		properties.bypassConfiguration();
367
	}
368

    
369
	/**
370
	 * Take input from the user in an HTTP request about an property to be
371
	 * changed and update the metacat property file with that new value if it
372
	 * has changed from the value that was originally set.
373
	 * 
374
	 * @param request
375
	 *            that was generated by the user
376
	 * @param response
377
	 *            to send output back to the user
378
	 * @param propertyName
379
	 *            the name of the property to be checked and set
380
	 */
381
	public static void checkAndSetProperty(HttpServletRequest request, String propertyName)
382
			throws GeneralPropertyException {
383
		properties.checkAndSetProperty(request, propertyName);
384
	}
385

    
386
	/**
387
	 * Sets the recommended external directory. This is populated during
388
	 * initialization time using the SystemUtil.discoverExternalDir() method.
389
	 * This directory will be used to suggest external user directories when the
390
	 * user configures metacat for the first time.
391
	 * 
392
	 */
393
	public static void setRecommendedExternalDir(String extBaseDir) {
394
		RECOMMENDED_EXTERNAL_DIR = extBaseDir;
395
	}
396

    
397
	/**
398
	 * Returns the recommended external base directory. This is populated during
399
	 * initialization time using the SystemUtil.discoverExternalBaseDir()
400
	 * method. This directory will be used to suggest external user directories
401
	 * when the user configures metacat for the first time.
402
	 * 
403
	 * @return a String holding the recommended external directory
404
	 */
405
	public static String getRecommendedExternalDir() {
406
		return RECOMMENDED_EXTERNAL_DIR;
407
	}
408

    
409
}
    (1-1/1)