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-22 14:20:45 -0700 (Tue, 22 Jul 2008) $'
10
 * '$Revision: 4154 $'
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
				// TODO MCD change this to handle deploy dir
111
				PropertyService.setPropertyNoPersist("application.tomcatDir",
112
						SystemUtil.discoverTomcatDir(request));
113

    
114
				PropertyService.persistProperties();
115

    
116
				// Add the list of properties from metacat.properties to the request
117
				Vector<String> propertyNames = PropertyService.getPropertyNames();
118
				for (String propertyName : propertyNames) {
119
					request.setAttribute(propertyName, PropertyService.getProperty(propertyName));
120
				}
121

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

    
139
				// Forward the request to the JSP page
140
				RequestUtil.forwardRequest(request, response,
141
						"/admin/properties-configuration.jsp");
142

    
143
			} catch (GeneralPropertyException gpe) {
144
				throw new AdminException("Problem getting or setting property while " 
145
						+ "initializing system properties page: " + gpe.getMessage());
146
			} catch (IOException ioe) {
147
				throw new AdminException("IO problem while initializing "
148
						+ "system properties page:" + ioe.getMessage());
149
			} catch (ServletException se) {
150
				throw new AdminException("problem forwarding request while " 
151
						+ "initializing system properties page: " + se.getMessage());
152
			}
153
		} else {
154
			// The configuration form is being submitted and needs to be
155
			// processed.
156
			Vector<String> validationErrors = new Vector<String>();
157
			Vector<String> processingErrors = new Vector<String>();
158
			Vector<String> processingSuccess = new Vector<String>();
159

    
160
			MetaCatVersion metacatVersion = null;
161
			
162
			try {
163
				metacatVersion = SystemUtil.getMetacatVersion();
164
				
165
				// For each property, check if it is changed and save it
166
				Vector<String> propertyNames = PropertyService.getPropertyNames();
167
				for (String name : propertyNames) {
168
					PropertyService.checkAndSetProperty(request, name);
169
				}
170

    
171
				// we need to write the options from memory to the properties
172
				// file
173
				PropertyService.persistProperties();
174

    
175
				// Validate that the options provided are legitimate. Note that
176
				// we've allowed them to persist their entries. As of this point
177
				// there is no other easy way to go back to the configure form
178
				// and preserve their entries.
179
				validationErrors.addAll(validateOptions(request));
180

    
181
				// Try to create backup directories if necessary.
182
				String backupDir = PropertyService.getBackupDir();
183
				if (!FileUtil.createDirectory(backupDir)) {
184
					String errorString = "Could not create directory: " + backupDir;
185
					logMetacat.error(errorString);
186
					validationErrors.add(errorString);
187
				}
188

    
189
				// write the backup properties to a location outside the 
190
				// application directories so they will be available after
191
				// the next upgrade
192
				PropertyService.persistMainBackupProperties(
193
						request.getSession().getServletContext());
194

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

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

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

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

    
277
		return errorVector;
278
	}
279
}
(8-8/9)