1 |
4080
|
daigle
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A Class that implements main property configuration methods
|
4 |
|
|
* Copyright: 2008 Regents of the University of California and the
|
5 |
|
|
* National Center for Ecological Analysis and Synthesis
|
6 |
|
|
* Authors: Michael Daigle
|
7 |
|
|
*
|
8 |
|
|
* '$Author$'
|
9 |
|
|
* '$Date$'
|
10 |
|
|
* '$Revision$'
|
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 |
5027
|
daigle
|
import edu.ucsb.nceas.metacat.MetacatVersion;
|
37 |
5015
|
daigle
|
import edu.ucsb.nceas.metacat.database.DBVersion;
|
38 |
5030
|
daigle
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
39 |
4795
|
daigle
|
import edu.ucsb.nceas.metacat.service.ServiceService;
|
40 |
5076
|
daigle
|
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
|
41 |
5015
|
daigle
|
import edu.ucsb.nceas.metacat.shared.ServiceException;
|
42 |
4080
|
daigle
|
import edu.ucsb.nceas.metacat.util.RequestUtil;
|
43 |
|
|
import edu.ucsb.nceas.metacat.util.SystemUtil;
|
44 |
|
|
|
45 |
|
|
import edu.ucsb.nceas.utilities.FileUtil;
|
46 |
|
|
import edu.ucsb.nceas.utilities.GeneralPropertyException;
|
47 |
|
|
import edu.ucsb.nceas.utilities.PropertiesMetaData;
|
48 |
|
|
import edu.ucsb.nceas.utilities.SortedProperties;
|
49 |
4950
|
daigle
|
import edu.ucsb.nceas.utilities.UtilException;
|
50 |
4080
|
daigle
|
|
51 |
|
|
/**
|
52 |
|
|
* Control the display of the main properties configuration page and the
|
53 |
|
|
* processing of the configuration values.
|
54 |
|
|
*/
|
55 |
5027
|
daigle
|
public class PropertiesAdmin extends MetacatAdmin {
|
56 |
4080
|
daigle
|
|
57 |
|
|
private static PropertiesAdmin propertiesAdmin = null;
|
58 |
|
|
private static Logger logMetacat = Logger.getLogger(PropertiesAdmin.class);
|
59 |
|
|
|
60 |
|
|
/**
|
61 |
|
|
* private constructor since this is a singleton
|
62 |
|
|
*/
|
63 |
|
|
private PropertiesAdmin() {}
|
64 |
|
|
|
65 |
|
|
/**
|
66 |
|
|
* Get the single instance of the MetaCatConfig.
|
67 |
|
|
*
|
68 |
|
|
* @return the single instance of MetaCatConfig
|
69 |
|
|
*/
|
70 |
|
|
public static PropertiesAdmin getInstance() {
|
71 |
|
|
if (propertiesAdmin == null) {
|
72 |
|
|
propertiesAdmin = new PropertiesAdmin();
|
73 |
|
|
}
|
74 |
|
|
return propertiesAdmin;
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
/**
|
78 |
|
|
* Handle configuration of the main application properties
|
79 |
|
|
*
|
80 |
|
|
* @param request
|
81 |
|
|
* the http request object
|
82 |
|
|
* @param response
|
83 |
|
|
* the http response to be sent back to the client
|
84 |
|
|
*/
|
85 |
|
|
public void configureProperties(HttpServletRequest request,
|
86 |
|
|
HttpServletResponse response) throws AdminException {
|
87 |
|
|
|
88 |
|
|
String processForm = request.getParameter("processForm");
|
89 |
|
|
String formErrors = (String)request.getAttribute("formErrors");
|
90 |
|
|
|
91 |
|
|
if (processForm == null || !processForm.equals("true") || formErrors != null) {
|
92 |
|
|
// The servlet configuration parameters have not been set, or there
|
93 |
|
|
// were form errors on the last attempt to configure, so redirect to
|
94 |
|
|
// the web form for configuring metacat
|
95 |
|
|
|
96 |
|
|
try {
|
97 |
|
|
// Load the properties metadata file so that the JSP page can
|
98 |
|
|
// use the metadata to construct the editing form
|
99 |
|
|
PropertiesMetaData metadata = PropertyService.getMainMetaData();
|
100 |
|
|
request.setAttribute("metadata", metadata);
|
101 |
|
|
|
102 |
4795
|
daigle
|
String externalDir = PropertyService.getRecommendedExternalDir();
|
103 |
4706
|
daigle
|
|
104 |
|
|
if (externalDir == null) {
|
105 |
4795
|
daigle
|
throw new AdminException("Could not initialize property configuration "
|
106 |
|
|
+ "page recommended application backup directory was null");
|
107 |
4706
|
daigle
|
}
|
108 |
|
|
|
109 |
4080
|
daigle
|
// Attempt to discover the following properties. These will show
|
110 |
|
|
// up in the configuration fields if nothing else is provided.
|
111 |
|
|
PropertyService.setPropertyNoPersist("application.context",
|
112 |
4795
|
daigle
|
ServiceService.getRealApplicationContext());
|
113 |
4080
|
daigle
|
PropertyService.setPropertyNoPersist("server.name", SystemUtil
|
114 |
|
|
.discoverServerName(request));
|
115 |
|
|
PropertyService.setPropertyNoPersist("server.httpPort", SystemUtil
|
116 |
|
|
.discoverServerPort(request));
|
117 |
|
|
PropertyService.setPropertyNoPersist("server.httpSSLPort",
|
118 |
|
|
SystemUtil.discoverServerSSLPort(request));
|
119 |
4174
|
daigle
|
PropertyService.setPropertyNoPersist("application.deployDir",
|
120 |
4183
|
daigle
|
SystemUtil.discoverDeployDir(request));
|
121 |
4662
|
daigle
|
PropertyService.setPropertyNoPersist("application.datafilepath",
|
122 |
4706
|
daigle
|
externalDir + FileUtil.getFS() + "data");
|
123 |
4662
|
daigle
|
PropertyService.setPropertyNoPersist("application.inlinedatafilepath",
|
124 |
4706
|
daigle
|
externalDir + FileUtil.getFS() + "inline-data");
|
125 |
4662
|
daigle
|
PropertyService.setPropertyNoPersist("application.documentfilepath",
|
126 |
4706
|
daigle
|
externalDir + FileUtil.getFS() + "documents");
|
127 |
4662
|
daigle
|
PropertyService.setPropertyNoPersist("application.tempDir",
|
128 |
4706
|
daigle
|
externalDir + FileUtil.getFS() + "temporary");
|
129 |
4662
|
daigle
|
PropertyService.setPropertyNoPersist("replication.logdir",
|
130 |
4706
|
daigle
|
externalDir + FileUtil.getFS() + "logs");
|
131 |
4080
|
daigle
|
|
132 |
|
|
PropertyService.persistProperties();
|
133 |
|
|
|
134 |
|
|
// Add the list of properties from metacat.properties to the request
|
135 |
|
|
Vector<String> propertyNames = PropertyService.getPropertyNames();
|
136 |
|
|
for (String propertyName : propertyNames) {
|
137 |
|
|
request.setAttribute(propertyName, PropertyService.getProperty(propertyName));
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
// Check for any backup properties and apply them to the
|
141 |
|
|
// request. These are properties from previous configurations.
|
142 |
|
|
// They keep the user from having to re-enter all values when
|
143 |
|
|
// upgrading. If this is a first time install, getBackupProperties
|
144 |
|
|
// will return null.
|
145 |
|
|
SortedProperties backupProperties = null;
|
146 |
|
|
if ((backupProperties =
|
147 |
|
|
PropertyService.getMainBackupProperties()) != null) {
|
148 |
|
|
Vector<String> backupKeys = backupProperties.getPropertyNames();
|
149 |
|
|
for (String key : backupKeys) {
|
150 |
|
|
String value = backupProperties.getProperty(key);
|
151 |
|
|
if (value != null) {
|
152 |
|
|
request.setAttribute(key, value);
|
153 |
|
|
}
|
154 |
|
|
}
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
// Forward the request to the JSP page
|
158 |
|
|
RequestUtil.forwardRequest(request, response,
|
159 |
5027
|
daigle
|
"/admin/properties-configuration.jsp", null);
|
160 |
4080
|
daigle
|
|
161 |
4084
|
daigle
|
} catch (GeneralPropertyException gpe) {
|
162 |
5030
|
daigle
|
throw new AdminException("PropertiesAdmin.configureProperties - Problem getting or " +
|
163 |
|
|
"setting property while initializing system properties page: " + gpe.getMessage());
|
164 |
5076
|
daigle
|
} catch (MetacatUtilException mue) {
|
165 |
|
|
throw new AdminException("PropertiesAdmin.configureProperties - utility problem while initializing "
|
166 |
|
|
+ "system properties page:" + mue.getMessage());
|
167 |
4795
|
daigle
|
} catch (ServiceException se) {
|
168 |
5030
|
daigle
|
throw new AdminException("PropertiesAdmin.configureProperties - Service problem while initializing "
|
169 |
4795
|
daigle
|
+ "system properties page:" + se.getMessage());
|
170 |
5076
|
daigle
|
}
|
171 |
4080
|
daigle
|
} else {
|
172 |
|
|
// The configuration form is being submitted and needs to be
|
173 |
|
|
// processed.
|
174 |
|
|
Vector<String> validationErrors = new Vector<String>();
|
175 |
|
|
Vector<String> processingErrors = new Vector<String>();
|
176 |
|
|
Vector<String> processingSuccess = new Vector<String>();
|
177 |
|
|
|
178 |
5027
|
daigle
|
MetacatVersion metacatVersion = null;
|
179 |
4080
|
daigle
|
|
180 |
|
|
try {
|
181 |
|
|
metacatVersion = SystemUtil.getMetacatVersion();
|
182 |
|
|
|
183 |
|
|
// For each property, check if it is changed and save it
|
184 |
|
|
Vector<String> propertyNames = PropertyService.getPropertyNames();
|
185 |
|
|
for (String name : propertyNames) {
|
186 |
|
|
PropertyService.checkAndSetProperty(request, name);
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
// we need to write the options from memory to the properties
|
190 |
|
|
// file
|
191 |
|
|
PropertyService.persistProperties();
|
192 |
|
|
|
193 |
|
|
// Validate that the options provided are legitimate. Note that
|
194 |
|
|
// we've allowed them to persist their entries. As of this point
|
195 |
|
|
// there is no other easy way to go back to the configure form
|
196 |
|
|
// and preserve their entries.
|
197 |
|
|
validationErrors.addAll(validateOptions(request));
|
198 |
4183
|
daigle
|
|
199 |
|
|
// Try to create data directories if necessary.
|
200 |
|
|
String dataDir = PropertyService.getProperty("application.datafilepath");
|
201 |
4440
|
daigle
|
try {
|
202 |
|
|
FileUtil.createDirectory(dataDir);
|
203 |
4950
|
daigle
|
} catch (UtilException ue) {
|
204 |
5030
|
daigle
|
String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + dataDir +
|
205 |
4950
|
daigle
|
" : " + ue.getMessage();
|
206 |
4183
|
daigle
|
logMetacat.error(errorString);
|
207 |
|
|
validationErrors.add(errorString);
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
// Try to create inline-data directories if necessary.
|
211 |
|
|
String inlineDataDir = PropertyService.getProperty("application.inlinedatafilepath");
|
212 |
4440
|
daigle
|
try {
|
213 |
|
|
FileUtil.createDirectory(inlineDataDir);
|
214 |
4950
|
daigle
|
} catch (UtilException ue) {
|
215 |
5030
|
daigle
|
String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + inlineDataDir +
|
216 |
4950
|
daigle
|
" : " + ue.getMessage();
|
217 |
4183
|
daigle
|
logMetacat.error(errorString);
|
218 |
|
|
validationErrors.add(errorString);
|
219 |
|
|
}
|
220 |
4428
|
daigle
|
|
221 |
|
|
// Try to create document directories if necessary.
|
222 |
|
|
String documentfilepath = PropertyService.getProperty("application.documentfilepath");
|
223 |
4440
|
daigle
|
try {
|
224 |
|
|
FileUtil.createDirectory(documentfilepath);
|
225 |
4950
|
daigle
|
} catch (UtilException ue) {
|
226 |
5030
|
daigle
|
String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + documentfilepath +
|
227 |
4950
|
daigle
|
" : " + ue.getMessage();
|
228 |
4428
|
daigle
|
logMetacat.error(errorString);
|
229 |
|
|
validationErrors.add(errorString);
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
// Try to create temporary directories if necessary.
|
233 |
|
|
String tempDir = PropertyService.getProperty("application.tempDir");
|
234 |
4440
|
daigle
|
try {
|
235 |
|
|
FileUtil.createDirectory(tempDir);
|
236 |
4950
|
daigle
|
} catch (UtilException ue) {
|
237 |
5030
|
daigle
|
String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + tempDir +
|
238 |
4950
|
daigle
|
" : " + ue.getMessage();
|
239 |
4428
|
daigle
|
logMetacat.error(errorString);
|
240 |
|
|
validationErrors.add(errorString);
|
241 |
|
|
}
|
242 |
4808
|
daigle
|
|
243 |
|
|
// Try to create temporary directories if necessary.
|
244 |
|
|
String replLogDir = PropertyService.getProperty("replication.logdir");
|
245 |
|
|
try {
|
246 |
|
|
FileUtil.createDirectory(replLogDir);
|
247 |
4950
|
daigle
|
} catch (UtilException ue) {
|
248 |
5030
|
daigle
|
String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + replLogDir +
|
249 |
4950
|
daigle
|
" : " + ue.getMessage();
|
250 |
4808
|
daigle
|
logMetacat.error(errorString);
|
251 |
|
|
validationErrors.add(errorString);
|
252 |
|
|
}
|
253 |
4080
|
daigle
|
|
254 |
|
|
// write the backup properties to a location outside the
|
255 |
|
|
// application directories so they will be available after
|
256 |
|
|
// the next upgrade
|
257 |
5028
|
daigle
|
PropertyService.persistMainBackupProperties();
|
258 |
4080
|
daigle
|
|
259 |
|
|
} catch (GeneralPropertyException gpe) {
|
260 |
5030
|
daigle
|
String errorMessage = "PropertiesAdmin.configureProperties - Problem getting or setting property while "
|
261 |
4080
|
daigle
|
+ "processing system properties page: " + gpe.getMessage();
|
262 |
|
|
logMetacat.error(errorMessage);
|
263 |
|
|
processingErrors.add(errorMessage);
|
264 |
4440
|
daigle
|
}
|
265 |
4080
|
daigle
|
|
266 |
|
|
try {
|
267 |
|
|
if (validationErrors.size() > 0 || processingErrors.size() > 0) {
|
268 |
|
|
RequestUtil.clearRequestMessages(request);
|
269 |
|
|
RequestUtil.setRequestFormErrors(request, validationErrors);
|
270 |
|
|
RequestUtil.setRequestErrors(request, processingErrors);
|
271 |
5027
|
daigle
|
RequestUtil.forwardRequest(request, response, "/admin", null);
|
272 |
4080
|
daigle
|
} else {
|
273 |
|
|
// Now that the options have been set, change the
|
274 |
|
|
// 'propertiesConfigured' option to 'true'
|
275 |
|
|
PropertyService.setProperty("configutil.propertiesConfigured",
|
276 |
|
|
PropertyService.CONFIGURED);
|
277 |
|
|
|
278 |
|
|
// if the db version is already the same as the metacat version,
|
279 |
|
|
// update metacat.properties. Have to do this after
|
280 |
|
|
// propertiesConfigured is set to CONFIGURED
|
281 |
|
|
DBVersion dbVersion = DBAdmin.getInstance().getDBVersion();
|
282 |
|
|
if (dbVersion != null && metacatVersion != null &&
|
283 |
|
|
dbVersion.compareTo(metacatVersion) == 0) {
|
284 |
4084
|
daigle
|
PropertyService.setProperty("configutil.databaseConfigured",
|
285 |
4080
|
daigle
|
PropertyService.CONFIGURED);
|
286 |
|
|
}
|
287 |
|
|
|
288 |
|
|
// Reload the main metacat configuration page
|
289 |
|
|
processingSuccess.add("Properties successfully configured");
|
290 |
|
|
RequestUtil.clearRequestMessages(request);
|
291 |
|
|
RequestUtil.setRequestSuccess(request, processingSuccess);
|
292 |
|
|
RequestUtil.forwardRequest(request, response,
|
293 |
5027
|
daigle
|
"/admin?configureType=configure&processForm=false", null);
|
294 |
4080
|
daigle
|
}
|
295 |
5076
|
daigle
|
} catch (MetacatUtilException mue) {
|
296 |
|
|
throw new AdminException("PropertiesAdmin.configureProperties - utility problem while processing system "
|
297 |
|
|
+ "properties page: " + mue.getMessage());
|
298 |
4084
|
daigle
|
} catch (GeneralPropertyException gpe) {
|
299 |
5030
|
daigle
|
throw new AdminException("PropertiesAdmin.configureProperties - problem with properties while "
|
300 |
4084
|
daigle
|
+ "processing system properties page: " + gpe.getMessage());
|
301 |
4080
|
daigle
|
}
|
302 |
|
|
}
|
303 |
|
|
}
|
304 |
|
|
|
305 |
|
|
/**
|
306 |
|
|
* Validate the most important configuration options submitted by the user.
|
307 |
|
|
*
|
308 |
|
|
* @param request
|
309 |
|
|
* the http request object
|
310 |
|
|
*
|
311 |
|
|
* @return a vector holding error message for any fields that fail
|
312 |
|
|
* validation.
|
313 |
|
|
*/
|
314 |
|
|
protected Vector<String> validateOptions(HttpServletRequest request) {
|
315 |
|
|
Vector<String> errorVector = new Vector<String>();
|
316 |
|
|
|
317 |
|
|
// Test database connectivity
|
318 |
|
|
try {
|
319 |
|
|
String dbError = DBAdmin.getInstance().validateDBConnectivity(
|
320 |
|
|
request.getParameter("database.driver"),
|
321 |
|
|
request.getParameter("database.connectionURI"),
|
322 |
|
|
request.getParameter("database.user"),
|
323 |
|
|
request.getParameter("database.password"));
|
324 |
|
|
if (dbError != null) {
|
325 |
|
|
errorVector.add(dbError);
|
326 |
|
|
}
|
327 |
|
|
} catch (AdminException ae) {
|
328 |
|
|
errorVector.add("Could not instantiate database admin: "
|
329 |
|
|
+ ae.getMessage());
|
330 |
|
|
}
|
331 |
|
|
|
332 |
|
|
return errorVector;
|
333 |
|
|
}
|
334 |
|
|
}
|