Project

General

Profile

1
/**
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.io.IOException;
30
import java.util.Vector;
31

    
32
import javax.servlet.ServletException;
33
import javax.servlet.http.HttpServletRequest;
34
import javax.servlet.http.HttpServletResponse;
35

    
36
import org.apache.log4j.Logger;
37

    
38
import edu.ucsb.nceas.metacat.service.PropertyService;
39
import edu.ucsb.nceas.metacat.service.ServiceException;
40
import edu.ucsb.nceas.metacat.service.ServiceService;
41
import edu.ucsb.nceas.metacat.util.RequestUtil;
42
import edu.ucsb.nceas.metacat.util.SystemUtil;
43
import edu.ucsb.nceas.utilities.FileUtil;
44
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
45
import edu.ucsb.nceas.utilities.GeneralPropertyException;
46

    
47
/**
48
 * Control the display of the login page 
49
 */
50
public class BackupAdmin extends MetaCatAdmin {
51

    
52
	private static BackupAdmin Admin = null;
53
	private static Logger logMetacat = Logger.getLogger(BackupAdmin.class);
54
	
55
	// <base_dir>/metacat/.metacat/metacat.properties.backup exists
56
	public static final String HIDDEN_EXISTS_POPULATED = "hiddenExistsPopulated";
57
	// <base_dir>/metacat/.metacat exists writable and is empty dir
58
	public static final String HIDDEN_EXISTS_UNPOPULATED = "hiddenExistsUnPopulated";	
59
	// <base_dir> exists writable without metacat subdirectory
60
	public static final String BASE_EXISTS_ONLY = "baseExistsOnly";
61
	// we couldn't determine the status of <base_dir>
62
	public static final String UNKNOWN = "unknown";
63

    
64
	/**
65
	 * private constructor since this is a singleton
66
	 */
67
	private BackupAdmin() {}
68

    
69
	/**
70
	 * Get the single instance of BackupAdmin.
71
	 * 
72
	 * @return the single instance of BackupAdmin
73
	 */
74
	public static BackupAdmin getInstance() {
75
		if (Admin == null) {
76
			Admin = new BackupAdmin();
77
		}
78
		return Admin;
79
	}
80
	
81
	/**
82
	 * Handle configuration of the backup directory
83
	 * 
84
	 * @param request
85
	 *            the http request information
86
	 * @param response
87
	 *            the http response to be sent back to the client
88
	 */
89
	public void configureBackup(HttpServletRequest request,
90
			HttpServletResponse response) throws AdminException {
91

    
92
		String processForm = request.getParameter("processForm");
93
		String formErrors = (String) request.getAttribute("formErrors");
94

    
95
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
96
			// The servlet configuration parameters have not been set, or there
97
			// were form errors on the last attempt to configure, so redirect to
98
			// the web form for configuring metacat
99
			
100
			try {
101
				String backupBaseDir = SystemUtil.discoverExternalBaseDir();
102
				logMetacat.debug("Backup dir discovered as: " + backupBaseDir);
103
				String backupDirStatus = getBackupDirStatus(backupBaseDir);
104
				logMetacat.debug("Status of discovered backup dir: " + backupDirStatus);
105
				
106
				if (backupBaseDir != null) {
107
					request.setAttribute("backupBaseDir", backupBaseDir);
108
				} else {
109
					request.setAttribute("backupBaseDir", "");
110
				}
111
				request.setAttribute("backupDirStatus", backupDirStatus);
112
				
113
				// Forward the request to the JSP page
114
				RequestUtil.forwardRequest(request, response,
115
						"/admin/backup-configuration.jsp");
116
			} catch (IOException ioe) {
117
				throw new AdminException("IO problem while initializing "
118
						+ "backup configuration page:" + ioe.getMessage());
119
			} catch (ServletException se) {
120
				throw new AdminException("Problem forwarding request while "
121
						+ "initializing backup configuration page: " + se.getMessage());
122
			} catch (PropertyNotFoundException pnfe) {
123
				throw new AdminException("Problem discovering backup directory while "
124
						+ "initializing backup configuration page: " + pnfe.getMessage());
125
			}
126
		} else {
127
			// The configuration form is being submitted and needs to be
128
			// processed.
129
			Vector<String> processingSuccess = new Vector<String>();
130
			Vector<String> processingErrors = new Vector<String>();
131
			Vector<String> validationErrors = new Vector<String>();
132
			
133
			String backupDir = request.getParameter("backup-dir");
134
			String hiddenBackupDir = backupDir + FileUtil.getFS() + ".metacat";
135

    
136
			// Validate that the options provided are legitimate.
137
			validationErrors.addAll(validateOptions(request));
138

    
139
			if (validationErrors.size() == 0) {
140
				try {
141
					FileUtil.createDirectory(hiddenBackupDir);
142
					
143
					PropertyService.setProperty("application.backupDir", backupDir);
144
					ServiceService.refreshService("PropertyService");
145
					ServiceService.refreshService("SkinPropertyService");
146
					SystemUtil.storeExternalDirLocation(backupDir);
147
				} catch (IOException ue) {
148
					String errorMessage = "Could not create directory: " + hiddenBackupDir
149
							+ " : " + ue.getMessage() + ". Please try again";
150
					processingErrors.add(errorMessage);
151
					logMetacat.error(errorMessage);
152
				} catch (GeneralPropertyException gpe) {
153
					String errorMessage = "Could not set application.backupDir property "
154
							+ " to " + backupDir + " : " + gpe.getMessage() + ".";
155
					processingErrors.add(errorMessage);
156
					logMetacat.error(errorMessage);
157
				} catch (ServiceException se) {
158
					String errorMessage = "Could not refresh service : " + se.getMessage() + ".";
159
					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
					RequestUtil.forwardRequest(request, response, "/admin");
170
				} 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
							"/admin?configureType=configure&processForm=false");
177
				}
178
			} catch (IOException ioe) {
179
				throw new AdminException("IO problem while processing login page: " 
180
						+ ioe.getMessage());
181
			} catch (ServletException se) {
182
				throw new AdminException("problem forwarding request while "
183
						+ "processing login page: " + se.getMessage());
184
			}
185
		}
186
	}
187
	
188
	/**
189
	 * Find the status of the backup base directory.  The possible statuses are:
190
	 *  -- HIDDEN_EXISTS_POPULATED
191
	 *  -- HIDDEN_EXISTS_UNPOPULATED
192
	 *  -- METACAT_EXISTS_ONLY
193
	 *  -- BASE_EXISTS_ONLY
194
	 *  -- UNKNOWN
195
	 *  
196
	 * @param backupBaseDir the directory we want to check
197
	 * @return a string corresponding to one of the statuses shown above.
198
	 */
199
	protected String getBackupDirStatus(String backupBaseDir) {
200
		if (backupBaseDir == null) {
201
			return UNKNOWN;
202
		}
203
		
204
		if (FileUtil.getFileStatus(backupBaseDir + FileUtil.getFS() + ".metacat" + FileUtil.getFS()
205
				+ "metacat.properties.backup") >= FileUtil.EXISTS_READ_WRITABLE) {
206
			return HIDDEN_EXISTS_POPULATED;
207
		}
208
		
209
		if (FileUtil.getFileStatus(backupBaseDir + FileUtil.getFS() + ".metacat") >= FileUtil.EXISTS_READ_WRITABLE) {
210
			return HIDDEN_EXISTS_UNPOPULATED;
211
		}
212
		
213
		if (FileUtil.getFileStatus(backupBaseDir) >= FileUtil.EXISTS_READ_WRITABLE) {
214
			return BASE_EXISTS_ONLY;
215
		}
216
		
217
		return UNKNOWN;
218
		
219
	}
220
	
221
	/**
222
	 * Validate the most important configuration options submitted by the user.
223
	 * 
224
	 * @return a vector holding error message for any fields that fail
225
	 *         validation.
226
	 */
227
	protected Vector<String> validateOptions(HttpServletRequest request) {
228
		Vector<String> errorVector = new Vector<String>();
229
		
230
		String backupDir = request.getParameter("backup-dir");
231
		String hiddenBackupDir = backupDir + FileUtil.getFS() + ".metacat";
232
		if (FileUtil.getFileStatus(hiddenBackupDir) > FileUtil.DOES_NOT_EXIST
233
				&& !FileUtil.isDirectory(hiddenBackupDir)) {
234
			errorVector.add(hiddenBackupDir + " exists, but is not a directory.");
235
		}
236

    
237
		String deployDir = SystemUtil.discoverDeployDir(request);
238
		if (backupDir.startsWith(deployDir)) {
239
			errorVector.add("Backup location must be outside of the application directory: "
240
							+ deployDir);
241
		}
242

    
243

    
244
		return errorVector;
245
	}
246
}
(3-3/10)