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