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-12-09 14:58:00 -0800 (Tue, 09 Dec 2008) $'
10
 * '$Revision: 4662 $'
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
				PropertyService.setPropertyNoPersist("application.datafilepath",
113
						SystemUtil.discoverExternalDir() + FileUtil.getFS() + "data");
114
				PropertyService.setPropertyNoPersist("application.inlinedatafilepath",
115
						SystemUtil.discoverExternalDir() + FileUtil.getFS() + "inline-data");
116
				PropertyService.setPropertyNoPersist("application.documentfilepath",
117
						SystemUtil.discoverExternalDir() + FileUtil.getFS() + "documents");
118
				PropertyService.setPropertyNoPersist("application.tempDir",
119
						SystemUtil.discoverExternalDir() + FileUtil.getFS() + "temporary");
120
				PropertyService.setPropertyNoPersist("replication.logdir",
121
						SystemUtil.discoverExternalDir() + FileUtil.getFS() + "logs");
122

    
123
				PropertyService.persistProperties();
124

    
125
				// Add the list of properties from metacat.properties to the request
126
				Vector<String> propertyNames = PropertyService.getPropertyNames();
127
				for (String propertyName : propertyNames) {
128
					request.setAttribute(propertyName, PropertyService.getProperty(propertyName));
129
				}
130

    
131
				// Check for any backup properties and apply them to the
132
				// request. These are properties from previous configurations. 
133
				// They keep the user from having to re-enter all values when 
134
				// upgrading. If this is a first time install, getBackupProperties 
135
				// will return null.
136
				SortedProperties backupProperties = null;
137
				if ((backupProperties = 
138
						PropertyService.getMainBackupProperties()) != null) {
139
					Vector<String> backupKeys = backupProperties.getPropertyNames();
140
					for (String key : backupKeys) {
141
						String value = backupProperties.getProperty(key);
142
						if (value != null) {
143
							request.setAttribute(key, value);
144
						}
145
					}
146
				}
147

    
148
				// Forward the request to the JSP page
149
				RequestUtil.forwardRequest(request, response,
150
						"/admin/properties-configuration.jsp");
151

    
152
			} catch (GeneralPropertyException gpe) {
153
				throw new AdminException("Problem getting or setting property while " 
154
						+ "initializing system properties page: " + gpe.getMessage());
155
			} catch (IOException ioe) {
156
				throw new AdminException("IO problem while initializing "
157
						+ "system properties page:" + ioe.getMessage());
158
			} catch (ServletException se) {
159
				throw new AdminException("problem forwarding request while " 
160
						+ "initializing system properties page: " + se.getMessage());
161
			}
162
		} else {
163
			// The configuration form is being submitted and needs to be
164
			// processed.
165
			Vector<String> validationErrors = new Vector<String>();
166
			Vector<String> processingErrors = new Vector<String>();
167
			Vector<String> processingSuccess = new Vector<String>();
168

    
169
			MetaCatVersion metacatVersion = null;
170
			
171
			try {
172
				metacatVersion = SystemUtil.getMetacatVersion();
173
				
174
				// For each property, check if it is changed and save it
175
				Vector<String> propertyNames = PropertyService.getPropertyNames();
176
				for (String name : propertyNames) {
177
					PropertyService.checkAndSetProperty(request, name);
178
				}
179

    
180
				// we need to write the options from memory to the properties
181
				// file
182
				PropertyService.persistProperties();
183

    
184
				// Validate that the options provided are legitimate. Note that
185
				// we've allowed them to persist their entries. As of this point
186
				// there is no other easy way to go back to the configure form
187
				// and preserve their entries.
188
				validationErrors.addAll(validateOptions(request));
189

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

    
245
				// write the backup properties to a location outside the 
246
				// application directories so they will be available after
247
				// the next upgrade
248
				PropertyService.persistMainBackupProperties(
249
						request.getSession().getServletContext());
250

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

    
288
			} catch (ServletException se) {
289
				throw new AdminException("problem forwarding request while "
290
						+ "processing system properties page: " + se.getMessage());
291
			} catch (IOException ioe) {
292
				throw new AdminException("IO problem while processing system "
293
						+ "properties page: " + ioe.getMessage());
294
			} catch (GeneralPropertyException gpe) {
295
				throw new AdminException("problem with properties while "
296
						+ "processing system properties page: " + gpe.getMessage());
297
			}
298
		}
299
	}
300

    
301
	/**
302
	 * Validate the most important configuration options submitted by the user.
303
	 * 
304
	 * @param request
305
	 *            the http request object
306
	 * 
307
	 * @return a vector holding error message for any fields that fail
308
	 *         validation.
309
	 */
310
	protected Vector<String> validateOptions(HttpServletRequest request) {
311
		Vector<String> errorVector = new Vector<String>();
312

    
313
		// Test database connectivity
314
		try {
315
			String dbError = DBAdmin.getInstance().validateDBConnectivity(
316
					request.getParameter("database.driver"),
317
					request.getParameter("database.connectionURI"),
318
					request.getParameter("database.user"),
319
					request.getParameter("database.password"));
320
			if (dbError != null) {
321
				errorVector.add(dbError);
322
			}
323
		} catch (AdminException ae) {
324
			errorVector.add("Could not instantiate database admin: "
325
					+ ae.getMessage());
326
		}
327

    
328
		return errorVector;
329
	}
330
}
(9-9/10)