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