Revision 4703
Added by daigle about 16 years ago
src/edu/ucsb/nceas/metacat/admin/BackupAdmin.java | ||
---|---|---|
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 |
String backupDirStatus = getBackupDirStatus(backupBaseDir); |
|
103 |
|
|
104 |
request.setAttribute("backupBaseDir", backupBaseDir); |
|
105 |
request.setAttribute("backupDirStatus", backupDirStatus); |
|
106 |
|
|
107 |
// Forward the request to the JSP page |
|
108 |
RequestUtil.forwardRequest(request, response, |
|
109 |
"/admin/backup-configuration.jsp"); |
|
110 |
} catch (IOException ioe) { |
|
111 |
throw new AdminException("IO problem while initializing " |
|
112 |
+ "backup configuration page:" + ioe.getMessage()); |
|
113 |
} catch (ServletException se) { |
|
114 |
throw new AdminException("Problem forwarding request while " |
|
115 |
+ "initializing backup configuration page: " + se.getMessage()); |
|
116 |
} catch (PropertyNotFoundException pnfe) { |
|
117 |
throw new AdminException("Problem discovering backup directory while " |
|
118 |
+ "initializing backup configuration page: " + pnfe.getMessage()); |
|
119 |
} |
|
120 |
} else { |
|
121 |
// The configuration form is being submitted and needs to be |
|
122 |
// processed. |
|
123 |
Vector<String> processingSuccess = new Vector<String>(); |
|
124 |
Vector<String> processingErrors = new Vector<String>(); |
|
125 |
Vector<String> validationErrors = new Vector<String>(); |
|
126 |
|
|
127 |
String backupDir = request.getParameter("backup-dir"); |
|
128 |
String hiddenBackupDir = backupDir + FileUtil.getFS() + ".metacat"; |
|
129 |
|
|
130 |
// Validate that the options provided are legitimate. |
|
131 |
validationErrors.addAll(validateOptions(request)); |
|
132 |
|
|
133 |
if (validationErrors.size() == 0) { |
|
134 |
try { |
|
135 |
FileUtil.createDirectory(hiddenBackupDir); |
|
136 |
|
|
137 |
PropertyService.setProperty("application.backupDir", backupDir); |
|
138 |
ServiceService.refreshService("PropertyService"); |
|
139 |
ServiceService.refreshService("SkinPropertyService"); |
|
140 |
SystemUtil.storeExternalDirLocation(backupDir); |
|
141 |
} catch (IOException ue) { |
|
142 |
String errorMessage = "Could not create directory: " + hiddenBackupDir |
|
143 |
+ " : " + ue.getMessage() + ". Please try again"; |
|
144 |
processingErrors.add(errorMessage); |
|
145 |
logMetacat.error(errorMessage); |
|
146 |
} catch (GeneralPropertyException gpe) { |
|
147 |
String errorMessage = "Could not set application.backupDir property " |
|
148 |
+ " to " + backupDir + " : " + gpe.getMessage() + "."; |
|
149 |
processingErrors.add(errorMessage); |
|
150 |
logMetacat.error(errorMessage); |
|
151 |
} catch (ServiceException se) { |
|
152 |
String errorMessage = "Could not refresh service : " + se.getMessage() + "."; |
|
153 |
processingErrors.add(errorMessage); |
|
154 |
logMetacat.error(errorMessage); |
|
155 |
} |
|
156 |
} |
|
157 |
|
|
158 |
try { |
|
159 |
if (validationErrors.size() > 0 || processingErrors.size() > 0) { |
|
160 |
RequestUtil.clearRequestMessages(request); |
|
161 |
RequestUtil.setRequestFormErrors(request, validationErrors); |
|
162 |
RequestUtil.setRequestErrors(request, processingErrors); |
|
163 |
RequestUtil.forwardRequest(request, response, "/admin"); |
|
164 |
} else { |
|
165 |
// Reload the main metacat configuration page |
|
166 |
processingSuccess.add("Directory: " + backupDir + " configured."); |
|
167 |
RequestUtil.clearRequestMessages(request); |
|
168 |
RequestUtil.setRequestSuccess(request, processingSuccess); |
|
169 |
RequestUtil.forwardRequest(request, response, |
|
170 |
"/admin?configureType=configure&processForm=false"); |
|
171 |
} |
|
172 |
} catch (IOException ioe) { |
|
173 |
throw new AdminException("IO problem while processing login page: " |
|
174 |
+ ioe.getMessage()); |
|
175 |
} catch (ServletException se) { |
|
176 |
throw new AdminException("problem forwarding request while " |
|
177 |
+ "processing login page: " + se.getMessage()); |
|
178 |
} |
|
179 |
} |
|
180 |
} |
|
181 |
|
|
182 |
/** |
|
183 |
* Find the status of the backup base directory. The possible statuses are: |
|
184 |
* -- HIDDEN_EXISTS_POPULATED |
|
185 |
* -- HIDDEN_EXISTS_UNPOPULATED |
|
186 |
* -- METACAT_EXISTS_ONLY |
|
187 |
* -- BASE_EXISTS_ONLY |
|
188 |
* -- UNKNOWN |
|
189 |
* |
|
190 |
* @param backupBaseDir the directory we want to check |
|
191 |
* @return a string corresponding to one of the statuses shown above. |
|
192 |
*/ |
|
193 |
protected String getBackupDirStatus(String backupBaseDir) { |
|
194 |
if (FileUtil.getFileStatus(backupBaseDir + FileUtil.getFS() + ".metacat" + FileUtil.getFS() |
|
195 |
+ "metacat.properties.backup") >= FileUtil.EXISTS_READ_WRITABLE) { |
|
196 |
return HIDDEN_EXISTS_POPULATED; |
|
197 |
} |
|
198 |
|
|
199 |
if (FileUtil.getFileStatus(backupBaseDir + FileUtil.getFS() + ".metacat") >= FileUtil.EXISTS_READ_WRITABLE) { |
|
200 |
return HIDDEN_EXISTS_UNPOPULATED; |
|
201 |
} |
|
202 |
|
|
203 |
if (FileUtil.getFileStatus(backupBaseDir) >= FileUtil.EXISTS_READ_WRITABLE) { |
|
204 |
return BASE_EXISTS_ONLY; |
|
205 |
} |
|
206 |
|
|
207 |
return UNKNOWN; |
|
208 |
|
|
209 |
} |
|
210 |
|
|
211 |
/** |
|
212 |
* Validate the most important configuration options submitted by the user. |
|
213 |
* |
|
214 |
* @return a vector holding error message for any fields that fail |
|
215 |
* validation. |
|
216 |
*/ |
|
217 |
protected Vector<String> validateOptions(HttpServletRequest request) { |
|
218 |
Vector<String> errorVector = new Vector<String>(); |
|
219 |
|
|
220 |
String backupDir = request.getParameter("backup-dir"); |
|
221 |
String hiddenBackupDir = backupDir + FileUtil.getFS() + ".metacat"; |
|
222 |
if (FileUtil.getFileStatus(hiddenBackupDir) > FileUtil.DOES_NOT_EXIST |
|
223 |
&& !FileUtil.isDirectory(hiddenBackupDir)) { |
|
224 |
errorVector.add(hiddenBackupDir + " exists, but is not a directory."); |
|
225 |
} |
|
226 |
|
|
227 |
String deployDir = SystemUtil.discoverDeployDir(request); |
|
228 |
if (backupDir.startsWith(deployDir)) { |
|
229 |
errorVector.add("Backup location must be outside of the application directory: " |
|
230 |
+ deployDir); |
|
231 |
} |
|
232 |
|
|
233 |
|
|
234 |
return errorVector; |
|
235 |
} |
|
236 |
} |
|
0 | 237 |
Also available in: Unified diff
Class to support backup directory configuration