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-26 13:16:10 -0800 (Fri, 26 Dec 2008) $'
10
 * '$Revision: 4706 $'
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
				String externalDir = PropertyService.getProperty("application.backupDir");
101
				
102
				if (externalDir == null) {
103
					throw new AdminException("Could not initialize property configuration"
104
									+ "page since application.backupDir was null");
105
				}
106
				
107
				// Attempt to discover the following properties.  These will show
108
				// up in the configuration fields if nothing else is provided.
109
				PropertyService.setPropertyNoPersist("application.context",
110
						SystemUtil.discoverApplicationContext(request));
111
				PropertyService.setPropertyNoPersist("server.name", SystemUtil
112
						.discoverServerName(request));
113
				PropertyService.setPropertyNoPersist("server.httpPort", SystemUtil
114
						.discoverServerPort(request));
115
				PropertyService.setPropertyNoPersist("server.httpSSLPort",
116
						SystemUtil.discoverServerSSLPort(request));
117
				PropertyService.setPropertyNoPersist("application.deployDir",
118
						SystemUtil.discoverDeployDir(request));
119
				PropertyService.setPropertyNoPersist("application.datafilepath",
120
						externalDir + FileUtil.getFS() + "data");
121
				PropertyService.setPropertyNoPersist("application.inlinedatafilepath",
122
						externalDir + FileUtil.getFS() + "inline-data");
123
				PropertyService.setPropertyNoPersist("application.documentfilepath",
124
						externalDir + FileUtil.getFS() + "documents");
125
				PropertyService.setPropertyNoPersist("application.tempDir",
126
						externalDir + FileUtil.getFS() + "temporary");
127
				PropertyService.setPropertyNoPersist("replication.logdir",
128
						externalDir + FileUtil.getFS() + "logs");
129

    
130
				PropertyService.persistProperties();
131

    
132
				// Add the list of properties from metacat.properties to the request
133
				Vector<String> propertyNames = PropertyService.getPropertyNames();
134
				for (String propertyName : propertyNames) {
135
					request.setAttribute(propertyName, PropertyService.getProperty(propertyName));
136
				}
137

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

    
155
				// Forward the request to the JSP page
156
				RequestUtil.forwardRequest(request, response,
157
						"/admin/properties-configuration.jsp");
158

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

    
176
			MetaCatVersion metacatVersion = null;
177
			
178
			try {
179
				metacatVersion = SystemUtil.getMetacatVersion();
180
				
181
				// For each property, check if it is changed and save it
182
				Vector<String> propertyNames = PropertyService.getPropertyNames();
183
				for (String name : propertyNames) {
184
					PropertyService.checkAndSetProperty(request, name);
185
				}
186

    
187
				// we need to write the options from memory to the properties
188
				// file
189
				PropertyService.persistProperties();
190

    
191
				// Validate that the options provided are legitimate. Note that
192
				// we've allowed them to persist their entries. As of this point
193
				// there is no other easy way to go back to the configure form
194
				// and preserve their entries.
195
				validationErrors.addAll(validateOptions(request));
196
				
197
				// Try to create data directories if necessary.
198
				String dataDir = PropertyService.getProperty("application.datafilepath");
199
				try {
200
					FileUtil.createDirectory(dataDir);
201
				} catch (IOException ioe) {
202
					String errorString = "Could not create directory: " + dataDir +
203
						" : " + ioe.getMessage();
204
					logMetacat.error(errorString);
205
					validationErrors.add(errorString);
206
				}
207
				
208
				// Try to create inline-data directories if necessary.
209
				String inlineDataDir = PropertyService.getProperty("application.inlinedatafilepath");
210
				try {
211
					FileUtil.createDirectory(inlineDataDir);
212
				} catch (IOException ioe) {
213
					String errorString = "Could not create directory: " + inlineDataDir +
214
						" : " + ioe.getMessage();
215
					logMetacat.error(errorString);
216
					validationErrors.add(errorString);
217
				}
218
				
219
				// Try to create document directories if necessary.
220
				String documentfilepath = PropertyService.getProperty("application.documentfilepath");
221
				try {
222
					FileUtil.createDirectory(documentfilepath);
223
				} catch (IOException ioe) {	
224
					String errorString = "Could not create directory: " + documentfilepath +
225
						" : " + ioe.getMessage();
226
					logMetacat.error(errorString);
227
					validationErrors.add(errorString);
228
				}
229
				
230
				// Try to create temporary directories if necessary.
231
				String tempDir = PropertyService.getProperty("application.tempDir");
232
				try {
233
					FileUtil.createDirectory(tempDir);
234
				} catch (IOException ioe) {		
235
					String errorString = "Could not create directory: " + tempDir +
236
						" : " + ioe.getMessage();
237
					logMetacat.error(errorString);
238
					validationErrors.add(errorString);
239
				}
240

    
241
				// write the backup properties to a location outside the 
242
				// application directories so they will be available after
243
				// the next upgrade
244
				PropertyService.persistMainBackupProperties(
245
						request.getSession().getServletContext());
246

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

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

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

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

    
324
		return errorVector;
325
	}
326
}
(9-9/10)