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-10-06 10:55:18 -0700 (Tue, 06 Oct 2009) $'
10
 * '$Revision: 5076 $'
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
import edu.ucsb.nceas.metacat.MetacatVersion;
37
import edu.ucsb.nceas.metacat.database.DBVersion;
38
import edu.ucsb.nceas.metacat.properties.PropertyService;
39
import edu.ucsb.nceas.metacat.service.ServiceService;
40
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
41
import edu.ucsb.nceas.metacat.shared.ServiceException;
42
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
import edu.ucsb.nceas.utilities.UtilException;
50

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

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

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

    
161
			} catch (GeneralPropertyException gpe) {
162
				throw new AdminException("PropertiesAdmin.configureProperties - Problem getting or " + 
163
						"setting property while initializing system properties page: " + gpe.getMessage());
164
			} catch (MetacatUtilException mue) {
165
				throw new AdminException("PropertiesAdmin.configureProperties - utility problem while initializing "
166
						+ "system properties page:" + mue.getMessage());
167
			} catch (ServiceException se) {
168
				throw new AdminException("PropertiesAdmin.configureProperties - Service problem while initializing "
169
						+ "system properties page:" + se.getMessage());
170
			} 
171
		} 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
			MetacatVersion metacatVersion = null;
179
			
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
				
199
				// Try to create data directories if necessary.
200
				String dataDir = PropertyService.getProperty("application.datafilepath");
201
				try {
202
					FileUtil.createDirectory(dataDir);
203
				} catch (UtilException ue) {
204
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + dataDir +
205
					" : " + ue.getMessage();
206
					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
				try {
213
					FileUtil.createDirectory(inlineDataDir);
214
				} catch (UtilException ue) {
215
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + inlineDataDir +
216
						" : " + ue.getMessage();
217
					logMetacat.error(errorString);
218
					validationErrors.add(errorString);
219
				}
220
				
221
				// Try to create document directories if necessary.
222
				String documentfilepath = PropertyService.getProperty("application.documentfilepath");
223
				try {
224
					FileUtil.createDirectory(documentfilepath);
225
				} catch (UtilException ue) {	
226
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + documentfilepath +
227
						" : " + ue.getMessage();
228
					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
				try {
235
					FileUtil.createDirectory(tempDir);
236
				} catch (UtilException ue) {		
237
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + tempDir +
238
						" : " + ue.getMessage();
239
					logMetacat.error(errorString);
240
					validationErrors.add(errorString);
241
				}
242
				
243
				// Try to create temporary directories if necessary.
244
				String replLogDir = PropertyService.getProperty("replication.logdir");
245
				try {
246
					FileUtil.createDirectory(replLogDir);
247
				} catch (UtilException ue) {		
248
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + replLogDir +
249
						" : " + ue.getMessage();
250
					logMetacat.error(errorString);
251
					validationErrors.add(errorString);
252
				}
253

    
254
				// write the backup properties to a location outside the 
255
				// application directories so they will be available after
256
				// the next upgrade
257
				PropertyService.persistMainBackupProperties();
258

    
259
			} catch (GeneralPropertyException gpe) {
260
				String errorMessage = "PropertiesAdmin.configureProperties - Problem getting or setting property while "
261
						+ "processing system properties page: " + gpe.getMessage();
262
				logMetacat.error(errorMessage);
263
				processingErrors.add(errorMessage);
264
			} 
265
			
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
					RequestUtil.forwardRequest(request, response, "/admin", null);
272
				} 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
						PropertyService.setProperty("configutil.databaseConfigured", 
285
								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
							"/admin?configureType=configure&processForm=false", null);
294
				}
295
			} catch (MetacatUtilException mue) {
296
				throw new AdminException("PropertiesAdmin.configureProperties - utility problem while processing system "
297
						+ "properties page: " + mue.getMessage());
298
			} catch (GeneralPropertyException gpe) {
299
				throw new AdminException("PropertiesAdmin.configureProperties - problem with properties while "
300
						+ "processing system properties page: " + gpe.getMessage());
301
			}
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
}
(9-9/10)