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-07 13:49:18 -0700 (Mon, 07 Jul 2008) $'
10
 * '$Revision: 4084 $'
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.tomcatDir",
111
						SystemUtil.discoverTomcatDir(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
				// write the backup properties to a location outside the 
189
				// application directories so they will be available after
190
				// the next upgrade
191
				PropertyService.persistMainBackupProperties(
192
						request.getSession().getServletContext());
193

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

    
236
			} catch (ServletException se) {
237
				throw new AdminException("problem forwarding request while "
238
						+ "processing system properties page: " + se.getMessage());
239
			} catch (IOException ioe) {
240
				throw new AdminException("IO problem while processing system "
241
						+ "properties page: " + ioe.getMessage());
242
			} catch (GeneralPropertyException gpe) {
243
				throw new AdminException("problem with properties while "
244
						+ "processing system properties page: " + gpe.getMessage());
245
			}
246
		}
247
	}
248

    
249
	/**
250
	 * Validate the most important configuration options submitted by the user.
251
	 * 
252
	 * @param request
253
	 *            the http request object
254
	 * 
255
	 * @return a vector holding error message for any fields that fail
256
	 *         validation.
257
	 */
258
	protected Vector<String> validateOptions(HttpServletRequest request) {
259
		Vector<String> errorVector = new Vector<String>();
260

    
261
		// Test database connectivity
262
		try {
263
			String dbError = DBAdmin.getInstance().validateDBConnectivity(
264
					request.getParameter("database.driver"),
265
					request.getParameter("database.connectionURI"),
266
					request.getParameter("database.user"),
267
					request.getParameter("database.password"));
268
			if (dbError != null) {
269
				errorVector.add(dbError);
270
			}
271
		} catch (AdminException ae) {
272
			errorVector.add("Could not instantiate database admin: "
273
					+ ae.getMessage());
274
		}
275

    
276
		return errorVector;
277
	}
278
}
(7-7/8)