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: 2008-10-10 17:11:10 -0700 (Fri, 10 Oct 2008) $'
10
 * '$Revision: 4440 $'
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.DBVersion;
39
import edu.ucsb.nceas.metacat.MetaCatVersion;
40
import edu.ucsb.nceas.metacat.service.PropertyService;
41
import edu.ucsb.nceas.metacat.util.RequestUtil;
42
import edu.ucsb.nceas.metacat.util.SystemUtil;
43

    
44
import edu.ucsb.nceas.utilities.FileUtil;
45
import edu.ucsb.nceas.utilities.GeneralPropertyException;
46
import edu.ucsb.nceas.utilities.PropertiesMetaData;
47
import edu.ucsb.nceas.utilities.SortedProperties;
48

    
49
/**
50
 * Control the display of the main properties configuration page and the 
51
 * processing of the configuration values.
52
 */
53
public class PropertiesAdmin extends MetaCatAdmin {
54

    
55
	private static PropertiesAdmin propertiesAdmin = null;
56
	private static Logger logMetacat = Logger.getLogger(PropertiesAdmin.class);
57

    
58
	/**
59
	 * private constructor since this is a singleton
60
	 */
61
	private PropertiesAdmin() {}
62

    
63
	/**
64
	 * Get the single instance of the MetaCatConfig.
65
	 * 
66
	 * @return the single instance of MetaCatConfig
67
	 */
68
	public static PropertiesAdmin getInstance() {
69
		if (propertiesAdmin == null) {
70
			propertiesAdmin = new PropertiesAdmin();
71
		}
72
		return propertiesAdmin;
73
	}
74
	
75
	/**
76
	 * Handle configuration of the main application properties
77
	 * 
78
	 * @param request
79
	 *            the http request object
80
	 * @param response
81
	 *            the http response to be sent back to the client
82
	 */
83
	public void configureProperties(HttpServletRequest request,
84
			HttpServletResponse response) throws AdminException {
85

    
86
		String processForm = request.getParameter("processForm");
87
		String formErrors = (String)request.getAttribute("formErrors");
88

    
89
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
90
			// The servlet configuration parameters have not been set, or there
91
			// were form errors on the last attempt to configure, so redirect to
92
			// the web form for configuring metacat
93

    
94
			try {
95
				// Load the properties metadata file so that the JSP page can
96
				// use the metadata to construct the editing form
97
				PropertiesMetaData metadata = PropertyService.getMainMetaData();
98
				request.setAttribute("metadata", metadata);
99

    
100
				// Attempt to discover the following properties.  These will show
101
				// up in the configuration fields if nothing else is provided.
102
				PropertyService.setPropertyNoPersist("application.context",
103
						SystemUtil.discoverApplicationContext(request));
104
				PropertyService.setPropertyNoPersist("server.name", SystemUtil
105
						.discoverServerName(request));
106
				PropertyService.setPropertyNoPersist("server.httpPort", SystemUtil
107
						.discoverServerPort(request));
108
				PropertyService.setPropertyNoPersist("server.httpSSLPort",
109
						SystemUtil.discoverServerSSLPort(request));
110
				PropertyService.setPropertyNoPersist("application.deployDir",
111
						SystemUtil.discoverDeployDir(request));
112

    
113
				PropertyService.persistProperties();
114

    
115
				// Add the list of properties from metacat.properties to the request
116
				Vector<String> propertyNames = PropertyService.getPropertyNames();
117
				for (String propertyName : propertyNames) {
118
					request.setAttribute(propertyName, PropertyService.getProperty(propertyName));
119
				}
120

    
121
				// Check for any backup properties and apply them to the
122
				// request. These are properties from previous configurations. 
123
				// They keep the user from having to re-enter all values when 
124
				// upgrading. If this is a first time install, getBackupProperties 
125
				// will return null.
126
				SortedProperties backupProperties = null;
127
				if ((backupProperties = 
128
						PropertyService.getMainBackupProperties()) != null) {
129
					Vector<String> backupKeys = backupProperties.getPropertyNames();
130
					for (String key : backupKeys) {
131
						String value = backupProperties.getProperty(key);
132
						if (value != null) {
133
							request.setAttribute(key, value);
134
						}
135
					}
136
				}
137

    
138
				// Forward the request to the JSP page
139
				RequestUtil.forwardRequest(request, response,
140
						"/admin/properties-configuration.jsp");
141

    
142
			} catch (GeneralPropertyException gpe) {
143
				throw new AdminException("Problem getting or setting property while " 
144
						+ "initializing system properties page: " + gpe.getMessage());
145
			} catch (IOException ioe) {
146
				throw new AdminException("IO problem while initializing "
147
						+ "system properties page:" + ioe.getMessage());
148
			} catch (ServletException se) {
149
				throw new AdminException("problem forwarding request while " 
150
						+ "initializing system properties page: " + se.getMessage());
151
			}
152
		} else {
153
			// The configuration form is being submitted and needs to be
154
			// processed.
155
			Vector<String> validationErrors = new Vector<String>();
156
			Vector<String> processingErrors = new Vector<String>();
157
			Vector<String> processingSuccess = new Vector<String>();
158

    
159
			MetaCatVersion metacatVersion = null;
160
			
161
			try {
162
				metacatVersion = SystemUtil.getMetacatVersion();
163
				
164
				// For each property, check if it is changed and save it
165
				Vector<String> propertyNames = PropertyService.getPropertyNames();
166
				for (String name : propertyNames) {
167
					PropertyService.checkAndSetProperty(request, name);
168
				}
169

    
170
				// we need to write the options from memory to the properties
171
				// file
172
				PropertyService.persistProperties();
173

    
174
				// Validate that the options provided are legitimate. Note that
175
				// we've allowed them to persist their entries. As of this point
176
				// there is no other easy way to go back to the configure form
177
				// and preserve their entries.
178
				validationErrors.addAll(validateOptions(request));
179

    
180
				// Try to create backup directories if necessary.
181
				String backupDir = PropertyService.getBackupDir();
182
				try {
183
					FileUtil.createDirectory(backupDir);
184
				} catch (IOException ioe) {
185
					String errorString = "Could not create directory: " + backupDir +
186
						" : " + ioe.getMessage();
187
					logMetacat.error(errorString);
188
					validationErrors.add(errorString);
189
				}
190
				
191
				// Try to create data directories if necessary.
192
				String dataDir = PropertyService.getProperty("application.datafilepath");
193
				try {
194
					FileUtil.createDirectory(dataDir);
195
				} catch (IOException ioe) {
196
					String errorString = "Could not create directory: " + dataDir +
197
						" : " + ioe.getMessage();
198
					logMetacat.error(errorString);
199
					validationErrors.add(errorString);
200
				}
201
				
202
				// Try to create inline-data directories if necessary.
203
				String inlineDataDir = PropertyService.getProperty("application.inlinedatafilepath");
204
				try {
205
					FileUtil.createDirectory(inlineDataDir);
206
				} catch (IOException ioe) {
207
					String errorString = "Could not create directory: " + inlineDataDir +
208
						" : " + ioe.getMessage();
209
					logMetacat.error(errorString);
210
					validationErrors.add(errorString);
211
				}
212
				
213
				// Try to create document directories if necessary.
214
				String documentfilepath = PropertyService.getProperty("application.documentfilepath");
215
				try {
216
					FileUtil.createDirectory(documentfilepath);
217
				} catch (IOException ioe) {	
218
					String errorString = "Could not create directory: " + documentfilepath +
219
						" : " + ioe.getMessage();
220
					logMetacat.error(errorString);
221
					validationErrors.add(errorString);
222
				}
223
				
224
				// Try to create temporary directories if necessary.
225
				String tempDir = PropertyService.getProperty("application.tempDir");
226
				try {
227
					FileUtil.createDirectory(tempDir);
228
				} catch (IOException ioe) {		
229
					String errorString = "Could not create directory: " + tempDir +
230
						" : " + ioe.getMessage();
231
					logMetacat.error(errorString);
232
					validationErrors.add(errorString);
233
				}
234

    
235
				// write the backup properties to a location outside the 
236
				// application directories so they will be available after
237
				// the next upgrade
238
				PropertyService.persistMainBackupProperties(
239
						request.getSession().getServletContext());
240

    
241
			} catch (GeneralPropertyException gpe) {
242
				String errorMessage = "Problem getting or setting property while "
243
						+ "processing system properties page: " + gpe.getMessage();
244
				logMetacat.error(errorMessage);
245
				processingErrors.add(errorMessage);
246
			} 
247
			
248
			try {
249
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
250
					RequestUtil.clearRequestMessages(request);
251
					RequestUtil.setRequestFormErrors(request, validationErrors);
252
					RequestUtil.setRequestErrors(request, processingErrors);
253
					RequestUtil.forwardRequest(request, response, "/admin");
254
				} else {
255
					// Now that the options have been set, change the
256
					// 'propertiesConfigured' option to 'true'
257
					PropertyService.setProperty("configutil.propertiesConfigured",
258
							PropertyService.CONFIGURED);
259
					
260
					// if the db version is already the same as the metacat version,
261
					// update metacat.properties. Have to do this after
262
					// propertiesConfigured is set to CONFIGURED
263
					DBVersion dbVersion = DBAdmin.getInstance().getDBVersion();
264
					if (dbVersion != null && metacatVersion != null && 
265
							dbVersion.compareTo(metacatVersion) == 0) {
266
						PropertyService.setProperty("configutil.databaseConfigured", 
267
								PropertyService.CONFIGURED);
268
					}
269
					
270
					// Reload the main metacat configuration page
271
					processingSuccess.add("Properties successfully configured");
272
					RequestUtil.clearRequestMessages(request);
273
					RequestUtil.setRequestSuccess(request, processingSuccess);
274
					RequestUtil.forwardRequest(request, response, 
275
							"/admin?configureType=configure&processForm=false");
276
				}
277

    
278
			} catch (ServletException se) {
279
				throw new AdminException("problem forwarding request while "
280
						+ "processing system properties page: " + se.getMessage());
281
			} catch (IOException ioe) {
282
				throw new AdminException("IO problem while processing system "
283
						+ "properties page: " + ioe.getMessage());
284
			} catch (GeneralPropertyException gpe) {
285
				throw new AdminException("problem with properties while "
286
						+ "processing system properties page: " + gpe.getMessage());
287
			}
288
		}
289
	}
290

    
291
	/**
292
	 * Validate the most important configuration options submitted by the user.
293
	 * 
294
	 * @param request
295
	 *            the http request object
296
	 * 
297
	 * @return a vector holding error message for any fields that fail
298
	 *         validation.
299
	 */
300
	protected Vector<String> validateOptions(HttpServletRequest request) {
301
		Vector<String> errorVector = new Vector<String>();
302

    
303
		// Test database connectivity
304
		try {
305
			String dbError = DBAdmin.getInstance().validateDBConnectivity(
306
					request.getParameter("database.driver"),
307
					request.getParameter("database.connectionURI"),
308
					request.getParameter("database.user"),
309
					request.getParameter("database.password"));
310
			if (dbError != null) {
311
				errorVector.add(dbError);
312
			}
313
		} catch (AdminException ae) {
314
			errorVector.add("Could not instantiate database admin: "
315
					+ ae.getMessage());
316
		}
317

    
318
		return errorVector;
319
	}
320
}
(9-9/10)