Project

General

Profile

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