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
					&& DataONEConfigUtil.isDataOneConfigured()
67
					&& isEZIDConfigured();
68
		} catch (MetacatUtilException ue) {
69
			logMetacat.error("Could not determine if metacat is configured due to utility exception: "
70
					+ ue.getMessage());
71
		} catch (GeneralPropertyException gpe) {
72
			logMetacat.error("Could not determine if metacat is configured due to property exception: "
73
					+ gpe.getMessage());
74
		}
75

    
76
		return metacatConfigured;
77
	}
78

    
79
	/**
80
	 * Check if the application.backupDir property is populated in
81
	 * metacat.properties and that it points to a writable directory.
82
	 * 
83
	 * @return false if the application.backupDir property does not point to a
84
	 *         writable directory.
85
	 */
86
	public static boolean isBackupDirConfigured() throws MetacatUtilException, PropertyNotFoundException {
87
		String backupDir = PropertyService.getProperty("application.backupDir");
88
		if (backupDir == null || backupDir.equals("")) {
89
			return false;
90
		}		
91
		if (FileUtil.getFileStatus(backupDir) < FileUtil.EXISTS_READ_WRITABLE) {
92
			return false;
93
		}	
94
		return true;
95
	}
96
	
97
	public static boolean isEZIDConfigured() throws MetacatUtilException {
98
        String ezidConfiguredString = PropertyService.UNCONFIGURED;
99
        try {
100
            ezidConfiguredString = PropertyService.getProperty("configutil.ezidConfigured");
101
        } catch (PropertyNotFoundException pnfe) {
102
            throw new MetacatUtilException("Could not determine if the ezid service are configured: "
103
                    + pnfe.getMessage());
104
        }
105
        // geoserver is configured if not unconfigured
106
        return !ezidConfiguredString.equals(PropertyService.UNCONFIGURED);
107
    }
108
		
109
	/**
110
	 * Reports whether the metacat configuration utility should be run. Returns
111
	 * false if 
112
	 *   -- dev.runConfiguration=false and 
113
	 *   -- backup properties file exists 
114
	 * Note that dev.runConfiguration should only be set to false when reinstalling the 
115
	 * same version of the application in development.
116
	 * 
117
	 * @return a boolean that is false if dev.runConfiguration is false and the
118
	 *         backup properties file exists.
119
	 */
120
	public static boolean bypassConfiguration() throws MetacatUtilException, ServiceException {
121
		try {
122
			// If the system is not configured to do bypass, return false.
123
			if (!PropertyService.doBypass()) {
124
				return false;
125
			}
126

    
127
			// Get the most likely backup files.  If these cannot be found, we 
128
			// cannot do the configuration bypass.
129
			String ExternalBaseDir = SystemUtil.discoverExternalDir();
130
			if (ExternalBaseDir == null) {
131
				logMetacat.error("bypassConfiguration: Could not find backup directory.");
132
				// could not find backup files ... force the configuration
133
				return false;
134
			}
135
			String realContext = ServiceService.getRealApplicationContext();
136
			PropertyService.setRecommendedExternalDir(ExternalBaseDir);
137
			PropertyService.setProperty("application.backupDir", 
138
					ExternalBaseDir + FileUtil.getFS() + "." + realContext);
139

    
140
			// Refresh the property service and skin property service.  This will pick up 
141
			// the backup directory and populate backup properties in caches.
142
			ServiceService.refreshService("PropertyService");
143
			ServiceService.refreshService("SkinPropertyService");
144

    
145
			// Call bypassConfiguration to make sure backup properties get persisted 
146
			// to active properties for both main and skin properties.
147
			PropertyService.bypassConfiguration();
148
			SkinPropertyService.bypassConfiguration();
149

    
150
			return true;
151
		} catch (GeneralPropertyException gpe) {
152
			throw new MetacatUtilException("Property error while discovering backup directory: "
153
					+ gpe.getMessage());
154
		} catch (MetacatUtilException mue) {
155
			throw new MetacatUtilException("Utility error while discovering backup directory: "
156
					+ mue.getMessage());
157
		}
158

    
159
	}
160
		
161
}
(2-2/18)