Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements utility methods for a metadata catalog
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Matt Jones, Jivka Bojilova
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2009-08-04 14:32:58 -0700 (Tue, 04 Aug 2009) $'
10
 * '$Revision: 5015 $'
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.util;
28

    
29
import org.apache.log4j.Logger;
30

    
31
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
32
import edu.ucsb.nceas.metacat.properties.PropertyService;
33
import edu.ucsb.nceas.metacat.properties.SkinPropertyService;
34
import edu.ucsb.nceas.metacat.service.ServiceService;
35
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
36
import edu.ucsb.nceas.metacat.shared.ServiceException;
37
import edu.ucsb.nceas.utilities.GeneralPropertyException;
38
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
39
import edu.ucsb.nceas.utilities.FileUtil;
40

    
41
/**
42
 * A suite of utility classes for the metadata catalog server
43
 */
44
public class ConfigurationUtil
45
{
46

    
47
    public static AbstractDatabase dbAdapter;
48
    
49
    private static Logger logMetacat = Logger.getLogger(ConfigurationUtil.class);
50

    
51
	/**
52
	 * Reports whether metacat is fully configured.
53
	 * 
54
	 * @return a boolean that is true if all sections are configured and 
55
	 * false otherwise
56
	 */
57
	public static boolean isMetacatConfigured() {
58
		boolean metacatConfigured = false;
59
		try {			
60
			metacatConfigured = PropertyService.arePropertiesConfigured()
61
					&& AuthUtil.isAuthConfigured()
62
					&& SkinUtil.areSkinsConfigured()
63
					&& DatabaseUtil.isDatabaseConfigured()
64
					&& GeoserverUtil.isGeoserverConfigured()
65
					&& isBackupDirConfigured();
66
		} catch (MetacatUtilException ue) {
67
			logMetacat.error("Could not determine if metacat is configured due to utility exception: "
68
					+ ue.getMessage());
69
		} catch (GeneralPropertyException gpe) {
70
			logMetacat.error("Could not determine if metacat is configured due to property exception: "
71
					+ gpe.getMessage());
72
		}
73

    
74
		return metacatConfigured;
75
	}
76

    
77
	/**
78
	 * Check if the application.backupDir property is populated in
79
	 * metacat.properties and that it points to a writable directory.
80
	 * 
81
	 * @return false if the application.backupDir property does not point to a
82
	 *         writable directory.
83
	 */
84
	public static boolean isBackupDirConfigured() throws MetacatUtilException, PropertyNotFoundException {
85
		String backupDir = PropertyService.getProperty("application.backupDir");
86
		if (backupDir == null || backupDir.equals("")) {
87
			return false;
88
		}		
89
		if (FileUtil.getFileStatus(backupDir) < FileUtil.EXISTS_READ_WRITABLE) {
90
			return false;
91
		}	
92
		return true;
93
	}
94
		
95
	/**
96
	 * Reports whether the metacat configuration utility should be run. Returns
97
	 * false if 
98
	 *   -- dev.runConfiguration=false and 
99
	 *   -- backup properties file exists 
100
	 * Note that dev.runConfiguration should only be set to false when reinstalling the 
101
	 * same version of the application in development.
102
	 * 
103
	 * @return a boolean that is false if dev.runConfiguration is false and the
104
	 *         backup properties file exists.
105
	 */
106
	public static boolean bypassConfiguration() throws MetacatUtilException, ServiceException {
107
		try {
108
			// If the system is not configured to do bypass, return false.
109
			if (!PropertyService.doBypass()) {
110
				return false;
111
			}
112

    
113
			// Get the most likely backup files.  If these cannot be found, we 
114
			// cannot do the configuration bypass.
115
			String ExternalBaseDir = SystemUtil.discoverExternalDir();
116
			if (ExternalBaseDir == null) {
117
				logMetacat.error("bypassConfiguration: Could not find backup directory.");
118
				// could not find backup files ... force the configuration
119
				return false;
120
			}
121
			String realContext = ServiceService.getRealApplicationContext();
122
			PropertyService.setRecommendedExternalDir(ExternalBaseDir);
123
			PropertyService.setProperty("application.backupDir", 
124
					ExternalBaseDir + FileUtil.getFS() + "." + realContext);
125

    
126
			// Refresh the property service and skin property service.  This will pick up 
127
			// the backup directory and populate backup properties in caches.
128
			ServiceService.refreshService("PropertyService");
129
			ServiceService.refreshService("SkinPropertyService");
130

    
131
			// Call bypassConfiguration to make sure backup properties get persisted 
132
			// to active properties for both main and skin properties.
133
			PropertyService.bypassConfiguration();
134
			SkinPropertyService.bypassConfiguration();
135

    
136
			return true;
137
		} catch (GeneralPropertyException gpe) {
138
			throw new MetacatUtilException("Property error while discovering backup directory: "
139
					+ gpe.getMessage());
140
		} catch (MetacatUtilException mue) {
141
			throw new MetacatUtilException("Utility error while discovering backup directory: "
142
					+ mue.getMessage());
143
		}
144

    
145
	}
146
		
147
}
(2-2/15)