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: leinfelder $'
9
 *     '$Date: 2011-11-10 23:36:42 -0800 (Thu, 10 Nov 2011) $'
10
 * '$Revision: 6635 $'
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.File;
30
import java.util.Vector;
31

    
32
import javax.servlet.http.HttpServletRequest;
33
import javax.servlet.http.HttpServletResponse;
34

    
35
import org.apache.log4j.Logger;
36
import org.dataone.client.CNode;
37
import org.dataone.client.D1Client;
38
import org.dataone.client.auth.CertificateManager;
39
import org.dataone.service.exceptions.IdentifierNotUnique;
40
import org.dataone.service.exceptions.InvalidRequest;
41
import org.dataone.service.exceptions.NotAuthorized;
42
import org.dataone.service.exceptions.NotImplemented;
43
import org.dataone.service.exceptions.ServiceFailure;
44
import org.dataone.service.types.v1.Node;
45
import org.dataone.service.types.v1.NodeReference;
46
import org.dataone.service.types.v1.Session;
47

    
48
import edu.ucsb.nceas.metacat.MetacatVersion;
49
import edu.ucsb.nceas.metacat.database.DBVersion;
50
import edu.ucsb.nceas.metacat.dataone.MNodeService;
51
import edu.ucsb.nceas.metacat.properties.PropertyService;
52
import edu.ucsb.nceas.metacat.service.ServiceService;
53
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
54
import edu.ucsb.nceas.metacat.shared.ServiceException;
55
import edu.ucsb.nceas.metacat.util.RequestUtil;
56
import edu.ucsb.nceas.metacat.util.SystemUtil;
57
import edu.ucsb.nceas.utilities.FileUtil;
58
import edu.ucsb.nceas.utilities.GeneralPropertyException;
59
import edu.ucsb.nceas.utilities.PropertiesMetaData;
60
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
61
import edu.ucsb.nceas.utilities.SortedProperties;
62
import edu.ucsb.nceas.utilities.UtilException;
63

    
64
/**
65
 * Control the display of the main properties configuration page and the 
66
 * processing of the configuration values.
67
 */
68
public class PropertiesAdmin extends MetacatAdmin {
69

    
70
	private static PropertiesAdmin propertiesAdmin = null;
71
	private static Logger logMetacat = Logger.getLogger(PropertiesAdmin.class);
72

    
73
	/**
74
	 * private constructor since this is a singleton
75
	 */
76
	private PropertiesAdmin() {}
77

    
78
	/**
79
	 * Get the single instance of the MetaCatConfig.
80
	 * 
81
	 * @return the single instance of MetaCatConfig
82
	 */
83
	public static PropertiesAdmin getInstance() {
84
		if (propertiesAdmin == null) {
85
			propertiesAdmin = new PropertiesAdmin();
86
		}
87
		return propertiesAdmin;
88
	}
89
	
90
	/**
91
	 * Handle configuration of the main application properties
92
	 * 
93
	 * @param request
94
	 *            the http request object
95
	 * @param response
96
	 *            the http response to be sent back to the client
97
	 */
98
	public void configureProperties(HttpServletRequest request,
99
			HttpServletResponse response) throws AdminException {
100

    
101
		String processForm = request.getParameter("processForm");
102
		String formErrors = (String)request.getAttribute("formErrors");
103

    
104
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
105
			// The servlet configuration parameters have not been set, or there
106
			// were form errors on the last attempt to configure, so redirect to
107
			// the web form for configuring metacat
108

    
109
			try {
110
				// Load the properties metadata file so that the JSP page can
111
				// use the metadata to construct the editing form
112
				PropertiesMetaData metadata = PropertyService.getMainMetaData();
113
				request.setAttribute("metadata", metadata);
114

    
115
				String externalDir = PropertyService.getRecommendedExternalDir();
116
				
117
				if (externalDir == null) {
118
					throw new AdminException("Could not initialize property configuration "
119
									+ "page recommended application backup directory was null");
120
				}
121
				
122
				// Attempt to discover the following properties.  These will show
123
				// up in the configuration fields if nothing else is provided.
124
				PropertyService.setPropertyNoPersist("application.context",
125
						ServiceService.getRealApplicationContext());
126
				PropertyService.setPropertyNoPersist("server.name", SystemUtil
127
						.discoverServerName(request));
128
				PropertyService.setPropertyNoPersist("server.httpPort", SystemUtil
129
						.discoverServerPort(request));
130
				PropertyService.setPropertyNoPersist("server.httpSSLPort",
131
						SystemUtil.discoverServerSSLPort(request));
132
				PropertyService.setPropertyNoPersist("application.deployDir",
133
						SystemUtil.discoverDeployDir(request));
134
				PropertyService.setPropertyNoPersist("application.datafilepath",
135
						externalDir + FileUtil.getFS() + "data");
136
				PropertyService.setPropertyNoPersist("application.inlinedatafilepath",
137
						externalDir + FileUtil.getFS() + "inline-data");
138
				PropertyService.setPropertyNoPersist("application.documentfilepath",
139
						externalDir + FileUtil.getFS() + "documents");
140
				PropertyService.setPropertyNoPersist("application.tempDir",
141
						externalDir + FileUtil.getFS() + "temporary");
142
				PropertyService.setPropertyNoPersist("replication.logdir",
143
						externalDir + FileUtil.getFS() + "logs");
144

    
145
				PropertyService.persistProperties();
146

    
147
				// Add the list of properties from metacat.properties to the request
148
				Vector<String> propertyNames = PropertyService.getPropertyNames();
149
				for (String propertyName : propertyNames) {
150
					request.setAttribute(propertyName, PropertyService.getProperty(propertyName));
151
				}
152

    
153
				// Check for any backup properties and apply them to the
154
				// request. These are properties from previous configurations. 
155
				// They keep the user from having to re-enter all values when 
156
				// upgrading. If this is a first time install, getBackupProperties 
157
				// will return null.
158
				SortedProperties backupProperties = null;
159
				if ((backupProperties = 
160
						PropertyService.getMainBackupProperties()) != null) {
161
					Vector<String> backupKeys = backupProperties.getPropertyNames();
162
					for (String key : backupKeys) {
163
						String value = backupProperties.getProperty(key);
164
						if (value != null) {
165
							request.setAttribute(key, value);
166
						}
167
					}
168
				}
169

    
170
				// Forward the request to the JSP page
171
				RequestUtil.forwardRequest(request, response,
172
						"/admin/properties-configuration.jsp", null);
173

    
174
			} catch (GeneralPropertyException gpe) {
175
				throw new AdminException("PropertiesAdmin.configureProperties - Problem getting or " + 
176
						"setting property while initializing system properties page: " + gpe.getMessage());
177
			} catch (MetacatUtilException mue) {
178
				throw new AdminException("PropertiesAdmin.configureProperties - utility problem while initializing "
179
						+ "system properties page:" + mue.getMessage());
180
			} catch (ServiceException se) {
181
				throw new AdminException("PropertiesAdmin.configureProperties - Service problem while initializing "
182
						+ "system properties page:" + se.getMessage());
183
			} 
184
		} else {
185
			// The configuration form is being submitted and needs to be
186
			// processed.
187
			Vector<String> validationErrors = new Vector<String>();
188
			Vector<String> processingErrors = new Vector<String>();
189
			Vector<String> processingSuccess = new Vector<String>();
190

    
191
			MetacatVersion metacatVersion = null;
192
			
193
			try {
194
				metacatVersion = SystemUtil.getMetacatVersion();
195
				
196
				// For each property, check if it is changed and save it
197
				Vector<String> propertyNames = PropertyService.getPropertyNames();
198
				for (String name : propertyNames) {
199
					PropertyService.checkAndSetProperty(request, name);
200
				}
201

    
202
				// we need to write the options from memory to the properties
203
				// file
204
				PropertyService.persistProperties();
205

    
206
				// Validate that the options provided are legitimate. Note that
207
				// we've allowed them to persist their entries. As of this point
208
				// there is no other easy way to go back to the configure form
209
				// and preserve their entries.
210
				validationErrors.addAll(validateOptions(request));
211
				
212
				// Try to create data directories if necessary.
213
				String dataDir = PropertyService.getProperty("application.datafilepath");
214
				try {
215
					FileUtil.createDirectory(dataDir);
216
				} catch (UtilException ue) {
217
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + dataDir +
218
					" : " + ue.getMessage();
219
					logMetacat.error(errorString);
220
					validationErrors.add(errorString);
221
				}
222
				
223
				// Try to create inline-data directories if necessary.
224
				String inlineDataDir = PropertyService.getProperty("application.inlinedatafilepath");
225
				try {
226
					FileUtil.createDirectory(inlineDataDir);
227
				} catch (UtilException ue) {
228
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + inlineDataDir +
229
						" : " + ue.getMessage();
230
					logMetacat.error(errorString);
231
					validationErrors.add(errorString);
232
				}
233
				
234
				// Try to create document directories if necessary.
235
				String documentfilepath = PropertyService.getProperty("application.documentfilepath");
236
				try {
237
					FileUtil.createDirectory(documentfilepath);
238
				} catch (UtilException ue) {	
239
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + documentfilepath +
240
						" : " + ue.getMessage();
241
					logMetacat.error(errorString);
242
					validationErrors.add(errorString);
243
				}
244
				
245
				// Try to create temporary directories if necessary.
246
				String tempDir = PropertyService.getProperty("application.tempDir");
247
				try {
248
					FileUtil.createDirectory(tempDir);
249
				} catch (UtilException ue) {		
250
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + tempDir +
251
						" : " + ue.getMessage();
252
					logMetacat.error(errorString);
253
					validationErrors.add(errorString);
254
				}
255
				
256
				// Try to create temporary directories if necessary.
257
				String replLogDir = PropertyService.getProperty("replication.logdir");
258
				try {
259
					FileUtil.createDirectory(replLogDir);
260
				} catch (UtilException ue) {		
261
					String errorString = "PropertiesAdmin.configureProperties - Could not create directory: " + replLogDir +
262
						" : " + ue.getMessage();
263
					logMetacat.error(errorString);
264
					validationErrors.add(errorString);
265
				}
266
				
267
				// write the backup properties to a location outside the 
268
				// application directories so they will be available after
269
				// the next upgrade
270
				PropertyService.persistMainBackupProperties();
271

    
272
			} catch (GeneralPropertyException gpe) {
273
				String errorMessage = "PropertiesAdmin.configureProperties - Problem getting or setting property while "
274
						+ "processing system properties page: " + gpe.getMessage();
275
				logMetacat.error(errorMessage);
276
				processingErrors.add(errorMessage);
277
			} 
278
			
279
			try {
280
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
281
					RequestUtil.clearRequestMessages(request);
282
					RequestUtil.setRequestFormErrors(request, validationErrors);
283
					RequestUtil.setRequestErrors(request, processingErrors);
284
					RequestUtil.forwardRequest(request, response, "/admin", null);
285
				} else {
286
					// Now that the options have been set, change the
287
					// 'propertiesConfigured' option to 'true'
288
					PropertyService.setProperty("configutil.propertiesConfigured",
289
							PropertyService.CONFIGURED);
290
					
291
					// if the db version is already the same as the metacat version,
292
					// update metacat.properties. Have to do this after
293
					// propertiesConfigured is set to CONFIGURED
294
					DBVersion dbVersion = DBAdmin.getInstance().getDBVersion();
295
					if (dbVersion != null && metacatVersion != null && 
296
							dbVersion.compareTo(metacatVersion) == 0) {
297
						PropertyService.setProperty("configutil.databaseConfigured", 
298
								PropertyService.CONFIGURED);
299
					}
300
					
301
					// Reload the main metacat configuration page
302
					processingSuccess.add("Properties successfully configured");
303
					RequestUtil.clearRequestMessages(request);
304
					RequestUtil.setRequestSuccess(request, processingSuccess);
305
					RequestUtil.forwardRequest(request, response, 
306
							"/admin?configureType=configure&processForm=false", null);
307
				}
308
			} catch (MetacatUtilException mue) {
309
				throw new AdminException("PropertiesAdmin.configureProperties - utility problem while processing system "
310
						+ "properties page: " + mue.getMessage());
311
			} catch (GeneralPropertyException gpe) {
312
				throw new AdminException("PropertiesAdmin.configureProperties - problem with properties while "
313
						+ "processing system properties page: " + gpe.getMessage());
314
			}
315
		}
316
	}
317

    
318
	/**
319
	 * Validate the most important configuration options submitted by the user.
320
	 * 
321
	 * @param request
322
	 *            the http request object
323
	 * 
324
	 * @return a vector holding error message for any fields that fail
325
	 *         validation.
326
	 */
327
	protected Vector<String> validateOptions(HttpServletRequest request) {
328
		Vector<String> errorVector = new Vector<String>();
329

    
330
		// Test database connectivity
331
		try {
332
			String dbError = DBAdmin.getInstance().validateDBConnectivity(
333
					request.getParameter("database.driver"),
334
					request.getParameter("database.connectionURI"),
335
					request.getParameter("database.user"),
336
					request.getParameter("database.password"));
337
			if (dbError != null) {
338
				errorVector.add(dbError);
339
			}
340
		} catch (AdminException ae) {
341
			errorVector.add("Could not instantiate database admin: "
342
					+ ae.getMessage());
343
		}
344

    
345
		return errorVector;
346
	}
347
	
348

    
349
}
(10-10/12)