Project

General

Profile

1 6635 leinfelder
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements database configuration methods
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *
7
 *   '$Author$'
8
 *     '$Date$'
9
 * '$Revision$'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25
26
package edu.ucsb.nceas.metacat.admin;
27
28
import java.util.Vector;
29
30
import javax.servlet.http.HttpServletRequest;
31
import javax.servlet.http.HttpServletResponse;
32
33
import org.apache.log4j.Logger;
34
import org.dataone.client.CNode;
35
import org.dataone.client.D1Client;
36
import org.dataone.client.auth.CertificateManager;
37 8759 leinfelder
import org.dataone.configuration.Settings;
38 6803 leinfelder
import org.dataone.service.exceptions.BaseException;
39 6635 leinfelder
import org.dataone.service.types.v1.Node;
40
import org.dataone.service.types.v1.NodeList;
41
import org.dataone.service.types.v1.NodeReference;
42
import org.dataone.service.types.v1.Session;
43
44 6862 leinfelder
import edu.ucsb.nceas.metacat.IdentifierManager;
45 6999 leinfelder
import edu.ucsb.nceas.metacat.admin.upgrade.dataone.GenerateORE;
46
import edu.ucsb.nceas.metacat.admin.upgrade.dataone.GenerateSystemMetadata;
47 6635 leinfelder
import edu.ucsb.nceas.metacat.dataone.MNodeService;
48
import edu.ucsb.nceas.metacat.properties.PropertyService;
49
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
50
import edu.ucsb.nceas.metacat.util.RequestUtil;
51
import edu.ucsb.nceas.utilities.GeneralPropertyException;
52
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
53 6864 leinfelder
import edu.ucsb.nceas.utilities.SortedProperties;
54 6635 leinfelder
55
/**
56
 * Control the display of the database configuration page and the processing
57
 * of the configuration values.
58
 */
59
public class D1Admin extends MetacatAdmin {
60
61
	private static D1Admin instance = null;
62
	private Logger logMetacat = Logger.getLogger(D1Admin.class);
63
64
	/**
65
	 * private constructor since this is a singleton
66
	 */
67
	private D1Admin() throws AdminException {
68
69
	}
70
71
	/**
72
	 * Get the single instance of D1Admin.
73
	 *
74
	 * @return the single instance of D1Admin
75
	 */
76
	public static D1Admin getInstance() throws AdminException {
77
		if (instance == null) {
78
			instance = new D1Admin();
79
		}
80
		return instance;
81
	}
82
83
	/**
84
	 * Handle configuration of the database the first time that Metacat starts
85
	 * or when it is explicitly called. Collect necessary update information
86
	 * from the administrator.
87
	 *
88
	 * @param request
89
	 *            the http request information
90
	 * @param response
91
	 *            the http response to be sent back to the client
92
	 */
93
	public void configureDataONE(HttpServletRequest request,
94
			HttpServletResponse response) throws AdminException {
95
96
		String processForm = request.getParameter("processForm");
97
		String bypass = request.getParameter("bypass");
98
		String formErrors = (String) request.getAttribute("formErrors");
99
100
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
101
			// The servlet configuration parameters have not been set, or there
102
			// were form errors on the last attempt to configure, so redirect to
103
			// the web form for configuring metacat
104
105
			try {
106
107
				// get the current configuration values
108 8749 tao
				String cnURL = PropertyService.getProperty("D1Client.CN_URL");
109 6635 leinfelder
				String nodeName = PropertyService.getProperty("dataone.nodeName");
110
				String nodeDescription = PropertyService.getProperty("dataone.nodeDescription");
111 7030 cjones
				String memberNodeId = PropertyService.getProperty("dataone.nodeId");
112 6635 leinfelder
				String nodeSynchronize = PropertyService.getProperty("dataone.nodeSynchronize");
113 6984 jones
				String subject = PropertyService.getProperty("dataone.subject");
114 6941 cjones
				String contactSubject = PropertyService.getProperty("dataone.contactSubject");
115 6635 leinfelder
				String certpath = PropertyService.getProperty("D1Client.certificate.file");
116
117 6690 leinfelder
				//the synch schedule
118
				String year = PropertyService.getProperty("dataone.nodeSynchronization.schedule.year");
119
				String mon = PropertyService.getProperty("dataone.nodeSynchronization.schedule.mon");
120
				String mday = PropertyService.getProperty("dataone.nodeSynchronization.schedule.mday");
121
				String wday = PropertyService.getProperty("dataone.nodeSynchronization.schedule.wday");
122
				String hour = PropertyService.getProperty("dataone.nodeSynchronization.schedule.hour");
123
				String min = PropertyService.getProperty("dataone.nodeSynchronization.schedule.min");
124
				String sec = PropertyService.getProperty("dataone.nodeSynchronization.schedule.sec");
125
126 6987 jones
				// the replication policies
127
                String nodeReplicate = PropertyService.getProperty("dataone.nodeReplicate");
128
                String numReplicas = PropertyService.getProperty("dataone.replicationpolicy.default.numreplicas");
129
                String preferredNodeList = PropertyService.getProperty("dataone.replicationpolicy.default.preferredNodeList");
130
                String blockedNodeList = PropertyService.getProperty("dataone.replicationpolicy.default.blockedNodeList");
131 6690 leinfelder
132 6635 leinfelder
				boolean synchronize = false;
133
				if (nodeSynchronize != null) {
134
					synchronize = Boolean.parseBoolean(nodeSynchronize);
135
				}
136
				boolean replicate = false;
137
				if (nodeReplicate != null) {
138
					replicate = Boolean.parseBoolean(nodeReplicate);
139
				}
140 8749 tao
				request.setAttribute("D1Client.CN_URL", cnURL);
141 6635 leinfelder
				request.setAttribute("dataone.nodeName", nodeName);
142
				request.setAttribute("dataone.nodeDescription", nodeDescription);
143 7030 cjones
				request.setAttribute("dataone.nodeId", memberNodeId);
144 6864 leinfelder
				request.setAttribute("dataone.nodeSynchronize", Boolean.toString(synchronize));
145 6984 jones
				request.setAttribute("dataone.subject", subject);
146 6941 cjones
				request.setAttribute("dataone.contactSubject", contactSubject);
147 6635 leinfelder
				request.setAttribute("D1Client.certificate.file", certpath);
148 6690 leinfelder
149
				// synch schedule
150
				request.setAttribute("dataone.nodeSynchronization.schedule.year", year);
151
				request.setAttribute("dataone.nodeSynchronization.schedule.mon", mon);
152
				request.setAttribute("dataone.nodeSynchronization.schedule.mday", mday);
153
				request.setAttribute("dataone.nodeSynchronization.schedule.wday", wday);
154
				request.setAttribute("dataone.nodeSynchronization.schedule.hour", hour);
155
				request.setAttribute("dataone.nodeSynchronization.schedule.min", min);
156
				request.setAttribute("dataone.nodeSynchronization.schedule.sec", sec);
157 6635 leinfelder
158 6987 jones
				// replication policies
159
                request.setAttribute("dataone.nodeReplicate", Boolean.toString(replicate));
160
                request.setAttribute("dataone.replicationpolicy.default.numreplicas", numReplicas);
161
                request.setAttribute("dataone.replicationpolicy.default.preferredNodeList", preferredNodeList);
162
                request.setAttribute("dataone.replicationpolicy.default.blockedNodeList", blockedNodeList);
163
164
165 6864 leinfelder
				// try the backup properties
166
				SortedProperties backupProperties = null;
167
				if ((backupProperties =
168
						PropertyService.getMainBackupProperties()) != null) {
169
					Vector<String> backupKeys = backupProperties.getPropertyNames();
170
					for (String key : backupKeys) {
171
						String value = backupProperties.getProperty(key);
172
						if (value != null) {
173
							request.setAttribute(key, value);
174
						}
175
					}
176
				}
177
178 6994 leinfelder
				// set the configuration state so we know how to render the UI page buttons
179
				// if we have already configured once, we cannot skip this page
180
				request.setAttribute("configutil.dataoneConfigured", PropertyService.getProperty("configutil.dataoneConfigured"));
181
182
				// do we know if this is an update, pending verification, or a new registration?
183 7030 cjones
				memberNodeId = (String) request.getAttribute("dataone.nodeId");
184 6992 leinfelder
				boolean update = isNodeRegistered(memberNodeId);
185
				request.setAttribute("dataone.isUpdate", Boolean.toString(update));
186 6994 leinfelder
				request.setAttribute("dataone.mn.registration.submitted", PropertyService.getProperty("dataone.mn.registration.submitted"));
187 6992 leinfelder
188 6994 leinfelder
				// enable the services?
189
				request.setAttribute("dataone.mn.services.enabled", PropertyService.getProperty("dataone.mn.services.enabled"));
190
191 6635 leinfelder
				// Forward the request to the JSP page
192
				RequestUtil.forwardRequest(request, response, "/admin/dataone-configuration.jsp", null);
193
			} catch (GeneralPropertyException gpe) {
194
				throw new AdminException("D1Admin.configureDataONE - Problem getting or " +
195
						"setting property while initializing system properties page: " + gpe.getMessage());
196
			} catch (MetacatUtilException mue) {
197
				throw new AdminException("D1Admin.configureDataONE - utility problem while initializing "
198
						+ "system properties page:" + mue.getMessage());
199
			}
200
		} else if (bypass != null && bypass.equals("true")) {
201
			Vector<String> processingErrors = new Vector<String>();
202
			Vector<String> processingSuccess = new Vector<String>();
203
204
			// Bypass the D1 configuration.
205
			// This will keep Metacat running
206
			try {
207
				PropertyService.setProperty("configutil.dataoneConfigured", PropertyService.BYPASSED);
208
209
			} catch (GeneralPropertyException gpe) {
210
				String errorMessage = "D1Admin.configureDataONE - Problem getting or setting property while "
211
					+ "processing system properties page: " + gpe.getMessage();
212
				logMetacat.error(errorMessage);
213
				processingErrors.add(errorMessage);
214
			}
215
			try {
216
				if (processingErrors.size() > 0) {
217
					RequestUtil.clearRequestMessages(request);
218
					RequestUtil.setRequestErrors(request, processingErrors);
219
					RequestUtil.forwardRequest(request, response, "/admin", null);
220
				} else {
221
					// Reload the main metacat configuration page
222
					processingSuccess.add("DataONE configuration successfully bypassed");
223
					RequestUtil.clearRequestMessages(request);
224
					RequestUtil.setRequestSuccess(request, processingSuccess);
225
					RequestUtil.forwardRequest(request, response,
226
							"/admin?configureType=configure&processForm=false", null);
227
				}
228
			} catch (MetacatUtilException mue) {
229
				throw new AdminException("D1Admin.configureDataONE - utility problem while processing dataone page: "
230
						+ mue.getMessage());
231
			}
232
233
		} else {
234
			// The configuration form is being submitted and needs to be
235
			// processed, setting the properties in the configuration file
236
			// then restart metacat
237
238
			// The configuration form is being submitted and needs to be
239
			// processed.
240
			Vector<String> validationErrors = new Vector<String>();
241
			Vector<String> processingErrors = new Vector<String>();
242
			Vector<String> processingSuccess = new Vector<String>();
243
244
			try {
245
				// Validate that the options provided are legitimate. Note that
246
				// we've allowed them to persist their entries. As of this point
247
				// there is no other easy way to go back to the configure form
248
				// and preserve their entries.
249
				validationErrors.addAll(validateOptions(request));
250
251 8749 tao
				String cnURL = (String)request.getParameter("D1Client.CN_URL");
252 6635 leinfelder
				String nodeName = (String)request.getParameter("dataone.nodeName");
253
				String nodeDescription = (String)request.getParameter("dataone.nodeDescription");
254 7030 cjones
				String memberNodeId = (String)request.getParameter("dataone.nodeId");
255 6635 leinfelder
				String nodeSynchronize = (String)request.getParameter("dataone.nodeSynchronize");
256 6984 jones
				String subject = (String)request.getParameter("dataone.subject");
257 6941 cjones
				String contactSubject = (String)request.getParameter("dataone.contactSubject");
258 6635 leinfelder
				String certpath = (String)request.getParameter("D1Client.certificate.file");
259
260 6690 leinfelder
				// the synch schedule
261
				String year = (String) request.getParameter("dataone.nodeSynchronization.schedule.year");
262
				String mon = (String) request.getParameter("dataone.nodeSynchronization.schedule.mon");
263
				String mday = (String) request.getParameter("dataone.nodeSynchronization.schedule.mday");
264
				String wday = (String) request.getParameter("dataone.nodeSynchronization.schedule.wday");
265
				String hour = (String) request.getParameter("dataone.nodeSynchronization.schedule.hour");
266
				String min = (String) request.getParameter("dataone.nodeSynchronization.schedule.min");
267
				String sec = (String) request.getParameter("dataone.nodeSynchronization.schedule.sec");
268
269 6987 jones
				// the replication policies
270
                String nodeReplicate = (String)request.getParameter("dataone.nodeReplicate");
271
                String numReplicas = (String)request.getParameter("dataone.replicationpolicy.default.numreplicas");
272
                String preferredNodeList = (String)request.getParameter("dataone.replicationpolicy.default.preferredNodeList");
273
                String blockedNodeList = (String)request.getParameter("dataone.replicationpolicy.default.blockedNodeList");
274
275 6635 leinfelder
				boolean synchronize = false;
276
				if (nodeSynchronize != null) {
277
					synchronize = Boolean.parseBoolean(nodeSynchronize);
278
				}
279
				boolean replicate = false;
280
				if (nodeReplicate != null) {
281
					replicate = Boolean.parseBoolean(nodeReplicate);
282
				}
283
284 6994 leinfelder
				// enable services as a whole?
285
                boolean servicesEnabled = false;
286
                String servicesEnabledString = (String) request.getParameter("dataone.mn.services.enabled");
287
                if (servicesEnabledString != null) {
288
                	servicesEnabled = Boolean.parseBoolean(servicesEnabledString);
289
                }
290
291 6635 leinfelder
				// process the values, checking for nulls etc..
292
				if (nodeName == null ) {
293
					validationErrors.add("nodeName cannot be null");
294
				} else {
295
296 8749 tao
					PropertyService.setProperty("D1Client.CN_URL", cnURL);
297 8759 leinfelder
					Settings.getConfiguration().setProperty("D1Client.CN_URL", cnURL);
298 6635 leinfelder
					PropertyService.setPropertyNoPersist("dataone.nodeName", nodeName);
299 6987 jones
					PropertyService.setPropertyNoPersist("dataone.nodeDescription", nodeDescription);
300 6635 leinfelder
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronize", Boolean.toString(synchronize));
301 6984 jones
					PropertyService.setPropertyNoPersist("dataone.subject", subject);
302
					PropertyService.setPropertyNoPersist("dataone.contactSubject", contactSubject);
303 6635 leinfelder
					PropertyService.setPropertyNoPersist("D1Client.certificate.file", certpath);
304
305 6690 leinfelder
					// the synch schedule
306
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.year", year);
307
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.mon", mon);
308
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.mday", mday);
309
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.wday", wday);
310
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.hour", hour);
311
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.min", min);
312
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.sec", sec);
313
314 6987 jones
					// the replication policies
315
	                PropertyService.setPropertyNoPersist("dataone.nodeReplicate", Boolean.toString(replicate));
316
	                PropertyService.setPropertyNoPersist("dataone.replicationpolicy.default.numreplicas", numReplicas);
317
                    PropertyService.setPropertyNoPersist("dataone.replicationpolicy.default.preferredNodeList", preferredNodeList);
318
                    PropertyService.setPropertyNoPersist("dataone.replicationpolicy.default.blockedNodeList", blockedNodeList);
319
320 6994 leinfelder
                    // services
321
					PropertyService.setPropertyNoPersist("dataone.mn.services.enabled", Boolean.toString(servicesEnabled));
322 7002 leinfelder
323 7003 leinfelder
					// get the current node id so we know if we updated the value
324 7030 cjones
					String existingMemberNodeId = PropertyService.getProperty("dataone.nodeId");
325 7003 leinfelder
326
					// update the property value
327 7030 cjones
		            PropertyService.setPropertyNoPersist("dataone.nodeId", memberNodeId);
328 7002 leinfelder
329 7003 leinfelder
		            // persist them all
330 6635 leinfelder
					PropertyService.persistProperties();
331 6935 leinfelder
332 7129 leinfelder
					// save a backup in case the form has errors, we reload from these
333
					PropertyService.persistMainBackupProperties();
334
335 6935 leinfelder
			        // Register/update as a DataONE Member Node
336
					registerDataONEMemberNode();
337 7003 leinfelder
338
					// did we end up changing the member node id from what it used to be?
339
					if (!existingMemberNodeId.equals(memberNodeId)) {
340
						// update all existing system Metadata for this node id
341
						IdentifierManager.getInstance().updateAuthoritativeMemberNodeId(existingMemberNodeId, memberNodeId);
342
					}
343
344 6996 leinfelder
					// dataone system metadata generation
345 7175 leinfelder
					// NOTE: commented this out - we can generate this after the registration/configuration
346
					// it will be more controlled and deliberate that way -BRL
347
//					boolean smGenerated = Boolean.parseBoolean(PropertyService.getProperty("dataone.systemmetadata.generated"));
348
//					if (!smGenerated) {
349
//						GenerateSystemMetadata systemMetadataUpgrade = new GenerateSystemMetadata();
350
//				        systemMetadataUpgrade.upgrade();
351
//				        // NOTE: this runs in a thread and marks SM generated when thatthread completes
352
//					}
353
//
354
//					// Generate ORE, if we haven't
355
//					boolean oreGenerated = Boolean.parseBoolean(PropertyService.getProperty("dataone.ore.generated"));
356
//					if (!oreGenerated) {
357
//						GenerateORE gore = new GenerateORE();
358
//						gore.upgrade();
359
//						PropertyService.setProperty("dataone.ore.generated", Boolean.TRUE.toString());
360
//					}
361 6935 leinfelder
362 6635 leinfelder
					// write the backup properties to a location outside the
363
					// application directories so they will be available after
364
					// the next upgrade
365
					PropertyService.persistMainBackupProperties();
366
367
				}
368
			} catch (GeneralPropertyException gpe) {
369
				String errorMessage = "D1Admin.configureDataONE - Problem getting or setting property while "
370
						+ "processing system properties page: " + gpe.getMessage();
371
				logMetacat.error(errorMessage);
372
				processingErrors.add(errorMessage);
373 6994 leinfelder
			} catch (Exception e) {
374
				String errorMessage = "D1Admin.configureDataONE error: " + e.getMessage();
375
				logMetacat.error(errorMessage);
376
				processingErrors.add(errorMessage);
377 6635 leinfelder
			}
378
379
			try {
380
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
381
					RequestUtil.clearRequestMessages(request);
382
					RequestUtil.setRequestFormErrors(request, validationErrors);
383
					RequestUtil.setRequestErrors(request, processingErrors);
384
					RequestUtil.forwardRequest(request, response, "/admin", null);
385
				} else {
386
					// Now that the options have been set, change the
387
					// 'dataoneConfigured' option to 'true'
388
					PropertyService.setProperty("configutil.dataoneConfigured",
389
							PropertyService.CONFIGURED);
390
391
					// Reload the main metacat configuration page
392
					processingSuccess.add("DataONE successfully configured");
393
					RequestUtil.clearRequestMessages(request);
394
					RequestUtil.setRequestSuccess(request, processingSuccess);
395
					RequestUtil.forwardRequest(request, response,
396
							"/admin?configureType=configure&processForm=false", null);
397
				}
398
			} catch (MetacatUtilException mue) {
399 6935 leinfelder
				throw new AdminException("D1Admin.configureDataONE - utility problem while processing dataone configuration: "
400
					 + mue.getMessage());
401 6635 leinfelder
			} catch (GeneralPropertyException gpe) {
402
				throw new AdminException("D1Admin.configureDataONE - problem with properties while "
403
						+ "processing geoservices configuration page: " + gpe.getMessage());
404
			}
405
		}
406
	}
407 6992 leinfelder
408
	private boolean isNodeRegistered(String nodeId) {
409
		// check if this is new or an update
410
        boolean exists = false;
411
        try {
412
	        NodeList nodes = D1Client.getCN().listNodes();
413
	        for (Node n: nodes.getNodeList()) {
414
	        	if (n.getIdentifier().getValue().equals(nodeId)) {
415
	        		exists = true;
416
	        		break;
417
	        	}
418
	        }
419
        } catch (BaseException e) {
420
            logMetacat.error("Could not check for node with DataONE (" + e.getCode() + "/" + e.getDetail_code() + "): " + e.getDescription());
421
        }
422
        return exists;
423
	}
424 6862 leinfelder
425 6635 leinfelder
	/**
426
	 * Register as a member node on the DataONE network.  The node description is
427
	 * retrieved from the getCapabilities() service, and so this should only be called
428
	 * after the properties have been properly set up in Metacat.
429
	 */
430 6994 leinfelder
	private void registerDataONEMemberNode() throws BaseException, PropertyNotFoundException, GeneralPropertyException {
431
432
        logMetacat.debug("Get the Node description.");
433
        Node node = MNodeService.getInstance(null).getCapabilities();
434
        logMetacat.debug("Setting client certificate location.");
435
        String mnCertificatePath = PropertyService.getProperty("D1Client.certificate.file");
436
        CertificateManager.getInstance().setCertificateLocation(mnCertificatePath);
437 8759 leinfelder
        CNode cn = D1Client.getCN(PropertyService.getProperty("D1Client.CN_URL"));
438 6994 leinfelder
439
        // check if this is new or an update
440
        boolean update = isNodeRegistered(node.getIdentifier().getValue());
441
442
        // Session is null, because the libclient code automatically sets up an
443
        // SSL session for us using the client certificate provided
444
        Session session = null;
445
        if (update) {
446
        	logMetacat.debug("Updating node with DataONE. " + cn.getNodeBaseServiceUrl());
447
            boolean result = cn.updateNodeCapabilities(session, node.getIdentifier(), node);
448
        } else {
449
            logMetacat.debug("Registering node with DataONE. " + cn.getNodeBaseServiceUrl());
450 7002 leinfelder
            NodeReference mnodeRef = cn.register(session, node);
451 6994 leinfelder
452
			// save that we submitted registration
453
			PropertyService.setPropertyNoPersist("dataone.mn.registration.submitted", Boolean.TRUE.toString());
454 6992 leinfelder
455 6994 leinfelder
            // persist the properties
456
            PropertyService.persistProperties();
457
        }
458
459 6635 leinfelder
	}
460
461
	/**
462
	 * Validate the most important configuration options submitted by the user.
463
	 *
464
	 * @return a vector holding error message for any fields that fail
465
	 *         validation.
466
	 */
467
	protected Vector<String> validateOptions(HttpServletRequest request) {
468
		Vector<String> errorVector = new Vector<String>();
469
470
		// TODO MCD validate options.
471
472
		return errorVector;
473
	}
474
}