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-07-30 13:56:06 -0700 (Wed, 30 Jul 2008) $'
10
 * '$Revision: 4183 $'
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
				// write the backup properties to a location outside the 
205
				// application directories so they will be available after
206
				// the next upgrade
207
				PropertyService.persistMainBackupProperties(
208
						request.getSession().getServletContext());
209

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

    
252
			} catch (ServletException se) {
253
				throw new AdminException("problem forwarding request while "
254
						+ "processing system properties page: " + se.getMessage());
255
			} catch (IOException ioe) {
256
				throw new AdminException("IO problem while processing system "
257
						+ "properties page: " + ioe.getMessage());
258
			} catch (GeneralPropertyException gpe) {
259
				throw new AdminException("problem with properties while "
260
						+ "processing system properties page: " + gpe.getMessage());
261
			}
262
		}
263
	}
264

    
265
	/**
266
	 * Validate the most important configuration options submitted by the user.
267
	 * 
268
	 * @param request
269
	 *            the http request object
270
	 * 
271
	 * @return a vector holding error message for any fields that fail
272
	 *         validation.
273
	 */
274
	protected Vector<String> validateOptions(HttpServletRequest request) {
275
		Vector<String> errorVector = new Vector<String>();
276

    
277
		// Test database connectivity
278
		try {
279
			String dbError = DBAdmin.getInstance().validateDBConnectivity(
280
					request.getParameter("database.driver"),
281
					request.getParameter("database.connectionURI"),
282
					request.getParameter("database.user"),
283
					request.getParameter("database.password"));
284
			if (dbError != null) {
285
				errorVector.add(dbError);
286
			}
287
		} catch (AdminException ae) {
288
			errorVector.add("Could not instantiate database admin: "
289
					+ ae.getMessage());
290
		}
291

    
292
		return errorVector;
293
	}
294
}
(9-9/10)