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-09 09:52:41 -0700 (Thu, 09 Oct 2008) $'
10
 * '$Revision: 4428 $'
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
				if (!FileUtil.createDirectory(backupDir)) {
183
					String errorString = "Could not create directory: " + backupDir;
184
					logMetacat.error(errorString);
185
					validationErrors.add(errorString);
186
				}
187
				
188
				// Try to create data directories if necessary.
189
				String dataDir = PropertyService.getProperty("application.datafilepath");
190
				if (!FileUtil.createDirectory(dataDir)) {
191
					String errorString = "Could not create directory: " + dataDir;
192
					logMetacat.error(errorString);
193
					validationErrors.add(errorString);
194
				}
195
				
196
				// Try to create inline-data directories if necessary.
197
				String inlineDataDir = PropertyService.getProperty("application.inlinedatafilepath");
198
				if (!FileUtil.createDirectory(inlineDataDir)) {
199
					String errorString = "Could not create directory: " + inlineDataDir;
200
					logMetacat.error(errorString);
201
					validationErrors.add(errorString);
202
				}
203
				
204
				// Try to create document directories if necessary.
205
				String documentfilepath = PropertyService.getProperty("application.documentfilepath");
206
				if (!FileUtil.createDirectory(documentfilepath)) {
207
					String errorString = "Could not create directory: " + documentfilepath;
208
					logMetacat.error(errorString);
209
					validationErrors.add(errorString);
210
				}
211
				
212
				// Try to create temporary directories if necessary.
213
				String tempDir = PropertyService.getProperty("application.tempDir");
214
				if (!FileUtil.createDirectory(tempDir)) {
215
					String errorString = "Could not create directory: " + tempDir;
216
					logMetacat.error(errorString);
217
					validationErrors.add(errorString);
218
				}
219

    
220
				// write the backup properties to a location outside the 
221
				// application directories so they will be available after
222
				// the next upgrade
223
				PropertyService.persistMainBackupProperties(
224
						request.getSession().getServletContext());
225

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

    
268
			} catch (ServletException se) {
269
				throw new AdminException("problem forwarding request while "
270
						+ "processing system properties page: " + se.getMessage());
271
			} catch (IOException ioe) {
272
				throw new AdminException("IO problem while processing system "
273
						+ "properties page: " + ioe.getMessage());
274
			} catch (GeneralPropertyException gpe) {
275
				throw new AdminException("problem with properties while "
276
						+ "processing system properties page: " + gpe.getMessage());
277
			}
278
		}
279
	}
280

    
281
	/**
282
	 * Validate the most important configuration options submitted by the user.
283
	 * 
284
	 * @param request
285
	 *            the http request object
286
	 * 
287
	 * @return a vector holding error message for any fields that fail
288
	 *         validation.
289
	 */
290
	protected Vector<String> validateOptions(HttpServletRequest request) {
291
		Vector<String> errorVector = new Vector<String>();
292

    
293
		// Test database connectivity
294
		try {
295
			String dbError = DBAdmin.getInstance().validateDBConnectivity(
296
					request.getParameter("database.driver"),
297
					request.getParameter("database.connectionURI"),
298
					request.getParameter("database.user"),
299
					request.getParameter("database.password"));
300
			if (dbError != null) {
301
				errorVector.add(dbError);
302
			}
303
		} catch (AdminException ae) {
304
			errorVector.add("Could not instantiate database admin: "
305
					+ ae.getMessage());
306
		}
307

    
308
		return errorVector;
309
	}
310
}
(9-9/10)