Project

General

Profile

1 4703 daigle
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements login methods
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    ors: Michael Daigle
7
 *
8
 *   '$or: daigle $'
9
 *     '$Date: 2008-11-25 09:54:32 -0800 (Tue, 25 Nov 2008) $'
10
 * '$Revision: 4628 $'
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.admin;
28
29
import java.util.Vector;
30
31
import javax.servlet.http.HttpServletRequest;
32
import javax.servlet.http.HttpServletResponse;
33
34
import org.apache.log4j.Logger;
35
36 5030 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
37 4703 daigle
import edu.ucsb.nceas.metacat.service.ServiceService;
38 5015 daigle
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
39
import edu.ucsb.nceas.metacat.shared.ServiceException;
40 4703 daigle
import edu.ucsb.nceas.metacat.util.RequestUtil;
41
import edu.ucsb.nceas.metacat.util.SystemUtil;
42
import edu.ucsb.nceas.utilities.FileUtil;
43
import edu.ucsb.nceas.utilities.GeneralPropertyException;
44 4854 daigle
import edu.ucsb.nceas.utilities.UtilException;
45 4703 daigle
46
/**
47
 * Control the display of the login page
48
 */
49 5027 daigle
public class BackupAdmin extends MetacatAdmin {
50 4703 daigle
51
	private static BackupAdmin Admin = null;
52
	private static Logger logMetacat = Logger.getLogger(BackupAdmin.class);
53
54
	// <base_dir>/metacat/.metacat/metacat.properties.backup exists
55
	public static final String HIDDEN_EXISTS_POPULATED = "hiddenExistsPopulated";
56
	// <base_dir>/metacat/.metacat exists writable and is empty dir
57
	public static final String HIDDEN_EXISTS_UNPOPULATED = "hiddenExistsUnPopulated";
58
	// <base_dir> exists writable without metacat subdirectory
59
	public static final String BASE_EXISTS_ONLY = "baseExistsOnly";
60
	// we couldn't determine the status of <base_dir>
61
	public static final String UNKNOWN = "unknown";
62
63
	/**
64
	 * private constructor since this is a singleton
65
	 */
66
	private BackupAdmin() {}
67
68
	/**
69
	 * Get the single instance of BackupAdmin.
70
	 *
71
	 * @return the single instance of BackupAdmin
72
	 */
73
	public static BackupAdmin getInstance() {
74
		if (Admin == null) {
75
			Admin = new BackupAdmin();
76
		}
77
		return Admin;
78
	}
79
80
	/**
81
	 * Handle configuration of the backup directory
82
	 *
83
	 * @param request
84
	 *            the http request information
85
	 * @param response
86
	 *            the http response to be sent back to the client
87
	 */
88
	public void configureBackup(HttpServletRequest request,
89
			HttpServletResponse response) throws AdminException {
90
91
		String processForm = request.getParameter("processForm");
92
		String formErrors = (String) request.getAttribute("formErrors");
93
94
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
95
			// The servlet configuration parameters have not been set, or there
96
			// were form errors on the last attempt to configure, so redirect to
97
			// the web form for configuring metacat
98
99
			try {
100 4795 daigle
				String backupBaseDir = SystemUtil.discoverExternalDir();
101 5030 daigle
				logMetacat.debug("BackupAdmin.configureBackup - Backup dir discovered as: " + backupBaseDir);
102 4703 daigle
				String backupDirStatus = getBackupDirStatus(backupBaseDir);
103 5030 daigle
				logMetacat.debug("BackupAdmin.configureBackup - Status of discovered backup dir: " + backupDirStatus);
104 4703 daigle
105 4760 daigle
				if (backupBaseDir != null) {
106
					request.setAttribute("backupBaseDir", backupBaseDir);
107
				} else {
108
					request.setAttribute("backupBaseDir", "");
109
				}
110 4703 daigle
				request.setAttribute("backupDirStatus", backupDirStatus);
111
112
				// Forward the request to the JSP page
113
				RequestUtil.forwardRequest(request, response,
114 5027 daigle
						"/admin/backup-configuration.jsp", null);
115 4950 daigle
			} catch (MetacatUtilException mue) {
116 5030 daigle
				throw new AdminException("BackupAdmin.configureBackup - Problem discovering backup directory while "
117 4950 daigle
						+ "initializing backup configuration page: " + mue.getMessage());
118 4703 daigle
			}
119
		} else {
120
			// The configuration form is being submitted and needs to be
121
			// processed.
122
			Vector<String> processingSuccess = new Vector<String>();
123
			Vector<String> processingErrors = new Vector<String>();
124
			Vector<String> validationErrors = new Vector<String>();
125
126
			// Validate that the options provided are legitimate.
127
			validationErrors.addAll(validateOptions(request));
128 4795 daigle
			String backupDir = null;
129
			String realApplicationContext = null;
130
			String hiddenBackupDir = null;
131
132 4703 daigle
			if (validationErrors.size() == 0) {
133
				try {
134 4795 daigle
					backupDir = request.getParameter("backup-dir");
135
					realApplicationContext = ServiceService.getRealApplicationContext();
136
					hiddenBackupDir =
137
						backupDir + FileUtil.getFS() + "." + realApplicationContext;
138
139 4703 daigle
					FileUtil.createDirectory(hiddenBackupDir);
140
141
					PropertyService.setProperty("application.backupDir", backupDir);
142
					ServiceService.refreshService("PropertyService");
143 4795 daigle
					PropertyService.setRecommendedExternalDir(backupDir);
144
145 4703 daigle
					ServiceService.refreshService("SkinPropertyService");
146
					SystemUtil.storeExternalDirLocation(backupDir);
147 4950 daigle
				} catch (UtilException ue) {
148 5030 daigle
					String errorMessage = "BackupAdmin.configureBackup - Could not create directory: " + hiddenBackupDir
149 4703 daigle
							+ " : " + ue.getMessage() + ". Please try again";
150
					processingErrors.add(errorMessage);
151
					logMetacat.error(errorMessage);
152
				} catch (GeneralPropertyException gpe) {
153 5030 daigle
					String errorMessage = "BackupAdmin.configureBackup - Could not set application.backupDir property "
154 4703 daigle
							+ " to " + backupDir + " : " + gpe.getMessage() + ".";
155
					processingErrors.add(errorMessage);
156
					logMetacat.error(errorMessage);
157
				} catch (ServiceException se) {
158 5030 daigle
					String errorMessage = "BackupAdmin.configureBackup - Could not refresh service : " + se.getMessage() + ".";
159 4703 daigle
					processingErrors.add(errorMessage);
160
					logMetacat.error(errorMessage);
161
				}
162
			}
163
164
			try {
165
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
166
					RequestUtil.clearRequestMessages(request);
167
					RequestUtil.setRequestFormErrors(request, validationErrors);
168
					RequestUtil.setRequestErrors(request, processingErrors);
169 5027 daigle
					RequestUtil.forwardRequest(request, response, "/admin", null);
170 4703 daigle
				} else {
171
					// Reload the main metacat configuration page
172
					processingSuccess.add("Directory: " + backupDir + " configured.");
173
					RequestUtil.clearRequestMessages(request);
174
					RequestUtil.setRequestSuccess(request, processingSuccess);
175
					RequestUtil.forwardRequest(request, response,
176 5027 daigle
							"/admin?configureType=configure&processForm=false", null);
177 4703 daigle
				}
178 5076 daigle
			} catch (MetacatUtilException mue) {
179
				throw new AdminException("BackupAdmin.configureBackup - utility problem while processing login page: "
180
						+ mue.getMessage());
181
			}
182 4703 daigle
		}
183
	}
184
185
	/**
186
	 * Find the status of the backup base directory.  The possible statuses are:
187
	 *  -- HIDDEN_EXISTS_POPULATED
188
	 *  -- HIDDEN_EXISTS_UNPOPULATED
189
	 *  -- METACAT_EXISTS_ONLY
190
	 *  -- BASE_EXISTS_ONLY
191
	 *  -- UNKNOWN
192
	 *
193
	 * @param backupBaseDir the directory we want to check
194
	 * @return a string corresponding to one of the statuses shown above.
195
	 */
196
	protected String getBackupDirStatus(String backupBaseDir) {
197 4760 daigle
		if (backupBaseDir == null) {
198
			return UNKNOWN;
199
		}
200
201 4703 daigle
		if (FileUtil.getFileStatus(backupBaseDir + FileUtil.getFS() + ".metacat" + FileUtil.getFS()
202
				+ "metacat.properties.backup") >= FileUtil.EXISTS_READ_WRITABLE) {
203
			return HIDDEN_EXISTS_POPULATED;
204
		}
205
206
		if (FileUtil.getFileStatus(backupBaseDir + FileUtil.getFS() + ".metacat") >= FileUtil.EXISTS_READ_WRITABLE) {
207
			return HIDDEN_EXISTS_UNPOPULATED;
208
		}
209
210
		if (FileUtil.getFileStatus(backupBaseDir) >= FileUtil.EXISTS_READ_WRITABLE) {
211
			return BASE_EXISTS_ONLY;
212
		}
213
214
		return UNKNOWN;
215
216
	}
217
218
	/**
219
	 * Validate the most important configuration options submitted by the user.
220
	 *
221
	 * @return a vector holding error message for any fields that fail
222
	 *         validation.
223
	 */
224
	protected Vector<String> validateOptions(HttpServletRequest request) {
225
		Vector<String> errorVector = new Vector<String>();
226
227
		String backupDir = request.getParameter("backup-dir");
228
		String hiddenBackupDir = backupDir + FileUtil.getFS() + ".metacat";
229
		if (FileUtil.getFileStatus(hiddenBackupDir) > FileUtil.DOES_NOT_EXIST
230
				&& !FileUtil.isDirectory(hiddenBackupDir)) {
231
			errorVector.add(hiddenBackupDir + " exists, but is not a directory.");
232
		}
233
234
		String deployDir = SystemUtil.discoverDeployDir(request);
235
		if (backupDir.startsWith(deployDir)) {
236
			errorVector.add("Backup location must be outside of the application directory: "
237
							+ deployDir);
238
		}
239
240
241
		return errorVector;
242
	}
243
}