Project

General

Profile

1
/**
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: daigle $'
9
 *     '$Date: 2009-08-24 14:34:17 -0700 (Mon, 24 Aug 2009) $'
10
 * '$Revision: 5030 $'
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.MetacatVersion;
39
import edu.ucsb.nceas.metacat.database.DBVersion;
40
import edu.ucsb.nceas.metacat.properties.PropertyService;
41
import edu.ucsb.nceas.metacat.service.ServiceService;
42
import edu.ucsb.nceas.metacat.shared.ServiceException;
43
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
import edu.ucsb.nceas.utilities.UtilException;
51

    
52
/**
53
 * Control the display of the main properties configuration page and the 
54
 * processing of the configuration values.
55
 */
56
public class PropertiesAdmin extends MetacatAdmin {
57

    
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
				String externalDir = PropertyService.getRecommendedExternalDir();
104
				
105
				if (externalDir == null) {
106
					throw new AdminException("Could not initialize property configuration "
107
									+ "page recommended application backup directory was null");
108
				}
109
				
110
				// 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
						ServiceService.getRealApplicationContext());
114
				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
				PropertyService.setPropertyNoPersist("application.deployDir",
121
						SystemUtil.discoverDeployDir(request));
122
				PropertyService.setPropertyNoPersist("application.datafilepath",
123
						externalDir + FileUtil.getFS() + "data");
124
				PropertyService.setPropertyNoPersist("application.inlinedatafilepath",
125
						externalDir + FileUtil.getFS() + "inline-data");
126
				PropertyService.setPropertyNoPersist("application.documentfilepath",
127
						externalDir + FileUtil.getFS() + "documents");
128
				PropertyService.setPropertyNoPersist("application.tempDir",
129
						externalDir + FileUtil.getFS() + "temporary");
130
				PropertyService.setPropertyNoPersist("replication.logdir",
131
						externalDir + FileUtil.getFS() + "logs");
132

    
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
						"/admin/properties-configuration.jsp", null);
161

    
162
			} catch (GeneralPropertyException gpe) {
163
				throw new AdminException("PropertiesAdmin.configureProperties - Problem getting or " + 
164
						"setting property while initializing system properties page: " + gpe.getMessage());
165
			} catch (IOException ioe) {
166
				throw new AdminException("PropertiesAdmin.configureProperties - IO problem while initializing "
167
						+ "system properties page:" + ioe.getMessage());
168
			} catch (ServiceException se) {
169
				throw new AdminException("PropertiesAdmin.configureProperties - Service problem while initializing "
170
						+ "system properties page:" + se.getMessage());
171
			} catch (ServletException se) {
172
				throw new AdminException("PropertiesAdmin.configureProperties - 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
			MetacatVersion metacatVersion = null;
183
			
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
				
203
				// Try to create data directories if necessary.
204
				String dataDir = PropertyService.getProperty("application.datafilepath");
205
				try {
206
					FileUtil.createDirectory(dataDir);
207
				} catch (UtilException ue) {
208
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + dataDir +
209
					" : " + ue.getMessage();
210
					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
				try {
217
					FileUtil.createDirectory(inlineDataDir);
218
				} catch (UtilException ue) {
219
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + inlineDataDir +
220
						" : " + ue.getMessage();
221
					logMetacat.error(errorString);
222
					validationErrors.add(errorString);
223
				}
224
				
225
				// Try to create document directories if necessary.
226
				String documentfilepath = PropertyService.getProperty("application.documentfilepath");
227
				try {
228
					FileUtil.createDirectory(documentfilepath);
229
				} catch (UtilException ue) {	
230
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + documentfilepath +
231
						" : " + ue.getMessage();
232
					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
				try {
239
					FileUtil.createDirectory(tempDir);
240
				} catch (UtilException ue) {		
241
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + tempDir +
242
						" : " + ue.getMessage();
243
					logMetacat.error(errorString);
244
					validationErrors.add(errorString);
245
				}
246
				
247
				// Try to create temporary directories if necessary.
248
				String replLogDir = PropertyService.getProperty("replication.logdir");
249
				try {
250
					FileUtil.createDirectory(replLogDir);
251
				} catch (UtilException ue) {		
252
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + replLogDir +
253
						" : " + ue.getMessage();
254
					logMetacat.error(errorString);
255
					validationErrors.add(errorString);
256
				}
257

    
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

    
263
			} catch (GeneralPropertyException gpe) {
264
				String errorMessage = "PropertiesAdmin.configureProperties - Problem getting or setting property while "
265
						+ "processing system properties page: " + gpe.getMessage();
266
				logMetacat.error(errorMessage);
267
				processingErrors.add(errorMessage);
268
			} 
269
			
270
			try {
271
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
272
					RequestUtil.clearRequestMessages(request);
273
					RequestUtil.setRequestFormErrors(request, validationErrors);
274
					RequestUtil.setRequestErrors(request, processingErrors);
275
					RequestUtil.forwardRequest(request, response, "/admin", null);
276
				} else {
277
					// Now that the options have been set, change the
278
					// 'propertiesConfigured' option to 'true'
279
					PropertyService.setProperty("configutil.propertiesConfigured",
280
							PropertyService.CONFIGURED);
281
					
282
					// if the db version is already the same as the metacat version,
283
					// update metacat.properties. Have to do this after
284
					// propertiesConfigured is set to CONFIGURED
285
					DBVersion dbVersion = DBAdmin.getInstance().getDBVersion();
286
					if (dbVersion != null && metacatVersion != null && 
287
							dbVersion.compareTo(metacatVersion) == 0) {
288
						PropertyService.setProperty("configutil.databaseConfigured", 
289
								PropertyService.CONFIGURED);
290
					}
291
					
292
					// Reload the main metacat configuration page
293
					processingSuccess.add("Properties successfully configured");
294
					RequestUtil.clearRequestMessages(request);
295
					RequestUtil.setRequestSuccess(request, processingSuccess);
296
					RequestUtil.forwardRequest(request, response, 
297
							"/admin?configureType=configure&processForm=false", null);
298
				}
299

    
300
			} catch (ServletException se) {
301
				throw new AdminException("PropertiesAdmin.configureProperties - problem forwarding request while "
302
						+ "processing system properties page: " + se.getMessage());
303
			} catch (IOException ioe) {
304
				throw new AdminException("PropertiesAdmin.configureProperties - IO problem while processing system "
305
						+ "properties page: " + ioe.getMessage());
306
			} catch (GeneralPropertyException gpe) {
307
				throw new AdminException("PropertiesAdmin.configureProperties - problem with properties while "
308
						+ "processing system properties page: " + gpe.getMessage());
309
			}
310
		}
311
	}
312

    
313
	/**
314
	 * Validate the most important configuration options submitted by the user.
315
	 * 
316
	 * @param request
317
	 *            the http request object
318
	 * 
319
	 * @return a vector holding error message for any fields that fail
320
	 *         validation.
321
	 */
322
	protected Vector<String> validateOptions(HttpServletRequest request) {
323
		Vector<String> errorVector = new Vector<String>();
324

    
325
		// Test database connectivity
326
		try {
327
			String dbError = DBAdmin.getInstance().validateDBConnectivity(
328
					request.getParameter("database.driver"),
329
					request.getParameter("database.connectionURI"),
330
					request.getParameter("database.user"),
331
					request.getParameter("database.password"));
332
			if (dbError != null) {
333
				errorVector.add(dbError);
334
			}
335
		} catch (AdminException ae) {
336
			errorVector.add("Could not instantiate database admin: "
337
					+ ae.getMessage());
338
		}
339

    
340
		return errorVector;
341
	}
342
}
(9-9/10)