Project

General

Profile

1 4080 daigle
/**
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$'
9
 *     '$Date$'
10
 * '$Revision$'
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 5030 daigle
package edu.ucsb.nceas.metacat.properties;
28 4080 daigle
29 4107 daigle
import java.util.Map;
30 4080 daigle
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 5015 daigle
import edu.ucsb.nceas.metacat.shared.BaseService;
38
import edu.ucsb.nceas.metacat.shared.ServiceException;
39 4080 daigle
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 4442 daigle
public class PropertyService extends BaseService {
49 5030 daigle
50 4080 daigle
	private static PropertyService propertyService = null;
51 5030 daigle
52
	private static PropertiesInterface properties = null;
53
54 4080 daigle
	// system is configured
55 5030 daigle
	public static final String CONFIGURED = PropertiesInterface.CONFIGURED;
56 4080 daigle
	// system has never been configured
57 5030 daigle
	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 4080 daigle
65 5030 daigle
	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 4080 daigle
	private static Logger logMetacat = Logger.getLogger(PropertyService.class);
73
74
	/**
75
	 * private constructor since this is a singleton
76
	 *
77 5030 daigle
	 * @param servletContext
78
	 *            the context we will use to get relative paths
79 4080 daigle
	 */
80 5030 daigle
	private PropertyService() throws ServiceException {
81 5015 daigle
		_serviceName = "PropertyService";
82
83 5030 daigle
		initialize();
84 4080 daigle
	}
85 5030 daigle
86 4080 daigle
	/**
87 4429 daigle
	 * Get the single instance of PropertyService.
88 4080 daigle
	 *
89 4429 daigle
	 * @return the single instance of PropertyService
90 4080 daigle
	 */
91 5030 daigle
	public static PropertyService getInstance(ServletContext context)
92
			throws ServiceException {
93 4080 daigle
		if (propertyService == null) {
94 4711 daigle
95 5030 daigle
			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 4711 daigle
			propertyService = new PropertyService();
111 4080 daigle
		}
112
		return propertyService;
113
	}
114 5030 daigle
115
	 /**
116
	 * Get the single instance of PropertyService for test purposes. In this
117 4719 daigle
	 * case, we allow the configuration directory to be passed in.
118 5030 daigle
	 *
119 4719 daigle
	 * @param configDir the configuration directory we need to look in
120
	 * @return the single instance of PropertyService
121
	 */
122 5030 daigle
	 public static PropertyService getInstance(String testConfigFileDir)
123
			throws ServiceException {
124 4719 daigle
		if (propertyService == null) {
125 5030 daigle
			CONFIG_FILE_DIR = testConfigFileDir;
126
127
			PROPERTY_CLASS_NAME = DEFAULT_PROPERTY_CLASS_NAME;
128
129 4719 daigle
			propertyService = new PropertyService();
130
		}
131
		return propertyService;
132
	}
133 5030 daigle
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 4442 daigle
	public boolean refreshable() {
151 4711 daigle
		return true;
152 4442 daigle
	}
153 5030 daigle
154 4981 daigle
	public void doRefresh() throws ServiceException {
155 5030 daigle
		// initialize();
156 4442 daigle
	}
157 5030 daigle
158 4981 daigle
	public void stop() throws ServiceException {
159
		return;
160
	}
161 5030 daigle
162 4080 daigle
	/**
163
	 * Initialize the singleton.
164
	 *
165 5030 daigle
	 * @param servletContext
166
	 *            the context we will use to get relative paths
167 4080 daigle
	 */
168 4795 daigle
	private void initialize() throws ServiceException {
169 5030 daigle
170 4080 daigle
		logMetacat.debug("Initializing PropertyService");
171 4711 daigle
172 5030 daigle
		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 4719 daigle
		}
185 4080 daigle
	}
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 5030 daigle
		return properties.getProperty(propertyName);
197 4080 daigle
	}
198 5030 daigle
199 4080 daigle
	/**
200 5030 daigle
	 * 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 4080 daigle
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 5030 daigle
	public static Vector<String> getPropertyNamesByGroup(String groupName) {
216
		return properties.getPropertyNamesByGroup(groupName);
217
	}
218
219 4107 daigle
	/**
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 5030 daigle
	public static Map<String, String> getPropertiesByGroup(String groupName)
227
			throws PropertyNotFoundException {
228
		return properties.getPropertiesByGroup(groupName);
229
	}
230 4080 daigle
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 5030 daigle
	public static void setProperty(String propertyName, String newValue)
241
			throws GeneralPropertyException {
242
		properties.setProperty(propertyName, newValue);
243
		properties.persistProperties();
244 4080 daigle
	}
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 5030 daigle
	public static void setPropertyNoPersist(String propertyName, String newValue)
259
			throws GeneralPropertyException {
260
		properties.setPropertyNoPersist(propertyName, newValue);
261 4080 daigle
	}
262
263
	/**
264 5030 daigle
	 * Save the properties to a properties file. Note, the order and comments
265
	 * will be preserved.
266 4080 daigle
	 */
267 4085 daigle
	public static void persistProperties() throws GeneralPropertyException {
268 5030 daigle
		properties.persistProperties();
269 4080 daigle
	}
270 5030 daigle
271 4080 daigle
	/**
272 5030 daigle
	 * 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 4080 daigle
	 *
277
	 * @return a SortedProperties object with the backup properties
278
	 */
279 5030 daigle
	public static SortedProperties getMainBackupProperties() throws GeneralPropertyException {
280
		return properties.getMainBackupProperties();
281 4080 daigle
	}
282 5030 daigle
283 4080 daigle
	/**
284 5030 daigle
	 * 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 4159 daigle
	 *
289
	 * @return a SortedProperties object with the backup properties
290
	 */
291 5030 daigle
	public static SortedProperties getAuthBackupProperties() throws GeneralPropertyException{
292
		return properties.getAuthBackupProperties();
293 4159 daigle
	}
294 5030 daigle
295 4159 daigle
	/**
296 4080 daigle
	 * 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 5030 daigle
	public static PropertiesMetaData getMainMetaData() throws GeneralPropertyException{
302
		return properties.getMainMetaData();
303 4080 daigle
	}
304 5030 daigle
305 4080 daigle
	/**
306 5030 daigle
	 * Get the auth properties metadata. This is retrieved from an xml file that
307
	 * describes the attributes of configurable properties.
308 4159 daigle
	 *
309
	 * @return a PropertiesMetaData object with the organization properties
310
	 *         metadata
311
	 */
312 5030 daigle
	public static PropertiesMetaData getAuthMetaData() throws GeneralPropertyException{
313
		return properties.getAuthMetaData();
314 4159 daigle
	}
315 5030 daigle
316 4159 daigle
	/**
317 4080 daigle
	 * Writes out backup configurable properties to a file.
318
	 */
319 5030 daigle
	public static void persistMainBackupProperties() throws GeneralPropertyException {
320 4080 daigle
321 5030 daigle
		properties.persistMainBackupProperties();
322
	}
323 4766 daigle
324 4080 daigle
	/**
325
	 * Writes out backup configurable properties to a file.
326
	 */
327 4589 daigle
	public static void persistAuthBackupProperties(ServletContext servletContext)
328 4159 daigle
			throws GeneralPropertyException {
329 4080 daigle
330 5030 daigle
		properties.persistAuthBackupProperties(servletContext);
331
	}
332 4159 daigle
333 4080 daigle
	/**
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 5030 daigle
	public static boolean arePropertiesConfigured() throws GeneralPropertyException {
340
		return properties.arePropertiesConfigured();
341 4080 daigle
	}
342 5030 daigle
343 4080 daigle
	/**
344 4711 daigle
	 * 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 5030 daigle
	public static boolean doBypass() throws GeneralPropertyException {
353
		return properties.doBypass();
354 4711 daigle
	}
355 5030 daigle
356 4711 daigle
	/**
357 5030 daigle
	 * 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 4154 daigle
	 * reinstalling the same version of the application in developement.
361
	 *
362 5030 daigle
	 * @return a boolean that is false if dev.runConfiguration is false and the
363
	 *         backup properties file exists.
364 4154 daigle
	 */
365 5030 daigle
	public static void bypassConfiguration() throws GeneralPropertyException {
366
		properties.bypassConfiguration();
367
	}
368 4154 daigle
369
	/**
370 5030 daigle
	 * 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 4080 daigle
	 *
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 5030 daigle
	public static void checkAndSetProperty(HttpServletRequest request, String propertyName)
382 4080 daigle
			throws GeneralPropertyException {
383 5030 daigle
		properties.checkAndSetProperty(request, propertyName);
384 4080 daigle
	}
385 5030 daigle
386 4795 daigle
	/**
387 5030 daigle
	 * 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 4795 daigle
	 *
392
	 */
393
	public static void setRecommendedExternalDir(String extBaseDir) {
394 5030 daigle
		RECOMMENDED_EXTERNAL_DIR = extBaseDir;
395 4795 daigle
	}
396 5030 daigle
397 4795 daigle
	/**
398 5030 daigle
	 * 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 4795 daigle
	 *
403
	 * @return a String holding the recommended external directory
404
	 */
405
	public static String getRecommendedExternalDir() {
406 5030 daigle
		return RECOMMENDED_EXTERNAL_DIR;
407 4795 daigle
	}
408 4080 daigle
409
}