Project

General

Profile

1
/**
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: leinfelder $'
8
 *     '$Date: 2012-05-18 12:09:33 -0700 (Fri, 18 May 2012) $'
9
 * '$Revision: 7175 $'
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
import org.dataone.service.exceptions.BaseException;
38
import org.dataone.service.types.v1.Node;
39
import org.dataone.service.types.v1.NodeList;
40
import org.dataone.service.types.v1.NodeReference;
41
import org.dataone.service.types.v1.Session;
42

    
43
import edu.ucsb.nceas.metacat.IdentifierManager;
44
import edu.ucsb.nceas.metacat.admin.upgrade.dataone.GenerateORE;
45
import edu.ucsb.nceas.metacat.admin.upgrade.dataone.GenerateSystemMetadata;
46
import edu.ucsb.nceas.metacat.dataone.MNodeService;
47
import edu.ucsb.nceas.metacat.properties.PropertyService;
48
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
49
import edu.ucsb.nceas.metacat.util.RequestUtil;
50
import edu.ucsb.nceas.utilities.GeneralPropertyException;
51
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
52
import edu.ucsb.nceas.utilities.SortedProperties;
53

    
54
/**
55
 * Control the display of the database configuration page and the processing
56
 * of the configuration values.
57
 */
58
public class D1Admin extends MetacatAdmin {
59

    
60
	private static D1Admin instance = null;
61
	private Logger logMetacat = Logger.getLogger(D1Admin.class);
62

    
63
	/**
64
	 * private constructor since this is a singleton
65
	 */
66
	private D1Admin() throws AdminException {
67

    
68
	}
69

    
70
	/**
71
	 * Get the single instance of D1Admin.
72
	 * 
73
	 * @return the single instance of D1Admin
74
	 */
75
	public static D1Admin getInstance() throws AdminException {
76
		if (instance == null) {
77
			instance = new D1Admin();
78
		}
79
		return instance;
80
	}
81

    
82
	/**
83
	 * Handle configuration of the database the first time that Metacat starts
84
	 * or when it is explicitly called. Collect necessary update information
85
	 * from the administrator.
86
	 * 
87
	 * @param request
88
	 *            the http request information
89
	 * @param response
90
	 *            the http response to be sent back to the client
91
	 */
92
	public void configureDataONE(HttpServletRequest request,
93
			HttpServletResponse response) throws AdminException {
94

    
95
		String processForm = request.getParameter("processForm");
96
		String bypass = request.getParameter("bypass");
97
		String formErrors = (String) request.getAttribute("formErrors");
98

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

    
104
			try {
105
				
106
				// get the current configuration values
107
				String nodeName = PropertyService.getProperty("dataone.nodeName");
108
				String nodeDescription = PropertyService.getProperty("dataone.nodeDescription");
109
				String memberNodeId = PropertyService.getProperty("dataone.nodeId");
110
				String nodeSynchronize = PropertyService.getProperty("dataone.nodeSynchronize");
111
				String subject = PropertyService.getProperty("dataone.subject");
112
				String contactSubject = PropertyService.getProperty("dataone.contactSubject");
113
				String certpath = PropertyService.getProperty("D1Client.certificate.file");
114
				
115
				//the synch schedule
116
				String year = PropertyService.getProperty("dataone.nodeSynchronization.schedule.year");
117
				String mon = PropertyService.getProperty("dataone.nodeSynchronization.schedule.mon");
118
				String mday = PropertyService.getProperty("dataone.nodeSynchronization.schedule.mday");
119
				String wday = PropertyService.getProperty("dataone.nodeSynchronization.schedule.wday");
120
				String hour = PropertyService.getProperty("dataone.nodeSynchronization.schedule.hour");
121
				String min = PropertyService.getProperty("dataone.nodeSynchronization.schedule.min");
122
				String sec = PropertyService.getProperty("dataone.nodeSynchronization.schedule.sec");
123
				
124
				// the replication policies
125
                String nodeReplicate = PropertyService.getProperty("dataone.nodeReplicate");
126
                String numReplicas = PropertyService.getProperty("dataone.replicationpolicy.default.numreplicas");
127
                String preferredNodeList = PropertyService.getProperty("dataone.replicationpolicy.default.preferredNodeList");
128
                String blockedNodeList = PropertyService.getProperty("dataone.replicationpolicy.default.blockedNodeList");
129
				
130
				boolean synchronize = false;
131
				if (nodeSynchronize != null) {
132
					synchronize = Boolean.parseBoolean(nodeSynchronize);
133
				}
134
				boolean replicate = false;
135
				if (nodeReplicate != null) {
136
					replicate = Boolean.parseBoolean(nodeReplicate);
137
				}
138
				
139
				request.setAttribute("dataone.nodeName", nodeName);
140
				request.setAttribute("dataone.nodeDescription", nodeDescription);
141
				request.setAttribute("dataone.nodeId", memberNodeId);
142
				request.setAttribute("dataone.nodeSynchronize", Boolean.toString(synchronize));
143
				request.setAttribute("dataone.subject", subject);
144
				request.setAttribute("dataone.contactSubject", contactSubject);
145
				request.setAttribute("D1Client.certificate.file", certpath);
146
				
147
				// synch schedule
148
				request.setAttribute("dataone.nodeSynchronization.schedule.year", year);
149
				request.setAttribute("dataone.nodeSynchronization.schedule.mon", mon);
150
				request.setAttribute("dataone.nodeSynchronization.schedule.mday", mday);
151
				request.setAttribute("dataone.nodeSynchronization.schedule.wday", wday);
152
				request.setAttribute("dataone.nodeSynchronization.schedule.hour", hour);
153
				request.setAttribute("dataone.nodeSynchronization.schedule.min", min);
154
				request.setAttribute("dataone.nodeSynchronization.schedule.sec", sec);
155

    
156
				// replication policies
157
                request.setAttribute("dataone.nodeReplicate", Boolean.toString(replicate));
158
                request.setAttribute("dataone.replicationpolicy.default.numreplicas", numReplicas);
159
                request.setAttribute("dataone.replicationpolicy.default.preferredNodeList", preferredNodeList);
160
                request.setAttribute("dataone.replicationpolicy.default.blockedNodeList", blockedNodeList);
161

    
162

    
163
				// try the backup properties
164
				SortedProperties backupProperties = null;
165
				if ((backupProperties = 
166
						PropertyService.getMainBackupProperties()) != null) {
167
					Vector<String> backupKeys = backupProperties.getPropertyNames();
168
					for (String key : backupKeys) {
169
						String value = backupProperties.getProperty(key);
170
						if (value != null) {
171
							request.setAttribute(key, value);
172
						}
173
					}
174
				}
175
				
176
				// set the configuration state so we know how to render the UI page buttons
177
				// if we have already configured once, we cannot skip this page
178
				request.setAttribute("configutil.dataoneConfigured", PropertyService.getProperty("configutil.dataoneConfigured"));
179
				
180
				// do we know if this is an update, pending verification, or a new registration?
181
				memberNodeId = (String) request.getAttribute("dataone.nodeId");
182
				boolean update = isNodeRegistered(memberNodeId);
183
				request.setAttribute("dataone.isUpdate", Boolean.toString(update));
184
				request.setAttribute("dataone.mn.registration.submitted", PropertyService.getProperty("dataone.mn.registration.submitted"));
185
				
186
				// enable the services?
187
				request.setAttribute("dataone.mn.services.enabled", PropertyService.getProperty("dataone.mn.services.enabled"));
188
				
189
				// Forward the request to the JSP page
190
				RequestUtil.forwardRequest(request, response, "/admin/dataone-configuration.jsp", null);
191
			} catch (GeneralPropertyException gpe) {
192
				throw new AdminException("D1Admin.configureDataONE - Problem getting or " + 
193
						"setting property while initializing system properties page: " + gpe.getMessage());
194
			} catch (MetacatUtilException mue) {
195
				throw new AdminException("D1Admin.configureDataONE - utility problem while initializing "
196
						+ "system properties page:" + mue.getMessage());
197
			} 
198
		} else if (bypass != null && bypass.equals("true")) {
199
			Vector<String> processingErrors = new Vector<String>();
200
			Vector<String> processingSuccess = new Vector<String>();
201
			
202
			// Bypass the D1 configuration. 
203
			// This will keep Metacat running
204
			try {
205
				PropertyService.setProperty("configutil.dataoneConfigured", PropertyService.BYPASSED);
206
				
207
			} catch (GeneralPropertyException gpe) {
208
				String errorMessage = "D1Admin.configureDataONE - Problem getting or setting property while "
209
					+ "processing system properties page: " + gpe.getMessage();
210
				logMetacat.error(errorMessage);
211
				processingErrors.add(errorMessage);
212
			}
213
			try {
214
				if (processingErrors.size() > 0) {
215
					RequestUtil.clearRequestMessages(request);
216
					RequestUtil.setRequestErrors(request, processingErrors);
217
					RequestUtil.forwardRequest(request, response, "/admin", null);
218
				} else {			
219
					// Reload the main metacat configuration page
220
					processingSuccess.add("DataONE configuration successfully bypassed");
221
					RequestUtil.clearRequestMessages(request);
222
					RequestUtil.setRequestSuccess(request, processingSuccess);
223
					RequestUtil.forwardRequest(request, response, 
224
							"/admin?configureType=configure&processForm=false", null);
225
				}
226
			} catch (MetacatUtilException mue) {
227
				throw new AdminException("D1Admin.configureDataONE - utility problem while processing dataone page: "
228
						+ mue.getMessage());
229
			} 
230
		
231
		} else {
232
			// The configuration form is being submitted and needs to be
233
			// processed, setting the properties in the configuration file
234
			// then restart metacat
235

    
236
			// The configuration form is being submitted and needs to be
237
			// processed.
238
			Vector<String> validationErrors = new Vector<String>();
239
			Vector<String> processingErrors = new Vector<String>();
240
			Vector<String> processingSuccess = new Vector<String>();
241

    
242
			try {
243
				// Validate that the options provided are legitimate. Note that
244
				// we've allowed them to persist their entries. As of this point
245
				// there is no other easy way to go back to the configure form
246
				// and preserve their entries.
247
				validationErrors.addAll(validateOptions(request));
248
				
249
				String nodeName = (String)request.getParameter("dataone.nodeName");
250
				String nodeDescription = (String)request.getParameter("dataone.nodeDescription");
251
				String memberNodeId = (String)request.getParameter("dataone.nodeId");
252
				String nodeSynchronize = (String)request.getParameter("dataone.nodeSynchronize");
253
				String subject = (String)request.getParameter("dataone.subject");
254
				String contactSubject = (String)request.getParameter("dataone.contactSubject");
255
				String certpath = (String)request.getParameter("D1Client.certificate.file");
256
				
257
				// the synch schedule
258
				String year = (String) request.getParameter("dataone.nodeSynchronization.schedule.year");
259
				String mon = (String) request.getParameter("dataone.nodeSynchronization.schedule.mon");
260
				String mday = (String) request.getParameter("dataone.nodeSynchronization.schedule.mday");
261
				String wday = (String) request.getParameter("dataone.nodeSynchronization.schedule.wday");
262
				String hour = (String) request.getParameter("dataone.nodeSynchronization.schedule.hour");
263
				String min = (String) request.getParameter("dataone.nodeSynchronization.schedule.min");
264
				String sec = (String) request.getParameter("dataone.nodeSynchronization.schedule.sec");
265
				
266
				// the replication policies
267
                String nodeReplicate = (String)request.getParameter("dataone.nodeReplicate");
268
                String numReplicas = (String)request.getParameter("dataone.replicationpolicy.default.numreplicas");
269
                String preferredNodeList = (String)request.getParameter("dataone.replicationpolicy.default.preferredNodeList");
270
                String blockedNodeList = (String)request.getParameter("dataone.replicationpolicy.default.blockedNodeList");
271

    
272
				boolean synchronize = false;
273
				if (nodeSynchronize != null) {
274
					synchronize = Boolean.parseBoolean(nodeSynchronize);
275
				}
276
				boolean replicate = false;
277
				if (nodeReplicate != null) {
278
					replicate = Boolean.parseBoolean(nodeReplicate);
279
				}
280
				
281
				// enable services as a whole?
282
                boolean servicesEnabled = false;
283
                String servicesEnabledString = (String) request.getParameter("dataone.mn.services.enabled");
284
                if (servicesEnabledString != null) {
285
                	servicesEnabled = Boolean.parseBoolean(servicesEnabledString);
286
                }
287
				
288
				// process the values, checking for nulls etc..
289
				if (nodeName == null ) {
290
					validationErrors.add("nodeName cannot be null");
291
				} else {
292
					
293
					PropertyService.setPropertyNoPersist("dataone.nodeName", nodeName);
294
					PropertyService.setPropertyNoPersist("dataone.nodeDescription", nodeDescription);					
295
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronize", Boolean.toString(synchronize));
296
					PropertyService.setPropertyNoPersist("dataone.subject", subject);
297
					PropertyService.setPropertyNoPersist("dataone.contactSubject", contactSubject);
298
					PropertyService.setPropertyNoPersist("D1Client.certificate.file", certpath);
299
					
300
					// the synch schedule
301
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.year", year);
302
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.mon", mon);
303
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.mday", mday);
304
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.wday", wday);
305
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.hour", hour);
306
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.min", min);
307
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.sec", sec);
308
					
309
					// the replication policies
310
	                PropertyService.setPropertyNoPersist("dataone.nodeReplicate", Boolean.toString(replicate));
311
	                PropertyService.setPropertyNoPersist("dataone.replicationpolicy.default.numreplicas", numReplicas);
312
                    PropertyService.setPropertyNoPersist("dataone.replicationpolicy.default.preferredNodeList", preferredNodeList);
313
                    PropertyService.setPropertyNoPersist("dataone.replicationpolicy.default.blockedNodeList", blockedNodeList);
314

    
315
                    // services
316
					PropertyService.setPropertyNoPersist("dataone.mn.services.enabled", Boolean.toString(servicesEnabled));
317
					
318
					// get the current node id so we know if we updated the value
319
					String existingMemberNodeId = PropertyService.getProperty("dataone.nodeId");
320
					
321
					// update the property value
322
		            PropertyService.setPropertyNoPersist("dataone.nodeId", memberNodeId);
323
					
324
		            // persist them all
325
					PropertyService.persistProperties();
326
					
327
					// save a backup in case the form has errors, we reload from these
328
					PropertyService.persistMainBackupProperties();
329
					
330
			        // Register/update as a DataONE Member Node					
331
					registerDataONEMemberNode();
332
					
333
					// did we end up changing the member node id from what it used to be?
334
					if (!existingMemberNodeId.equals(memberNodeId)) {
335
						// update all existing system Metadata for this node id
336
						IdentifierManager.getInstance().updateAuthoritativeMemberNodeId(existingMemberNodeId, memberNodeId);
337
					}
338
					
339
					// dataone system metadata generation
340
					// NOTE: commented this out - we can generate this after the registration/configuration
341
					// it will be more controlled and deliberate that way -BRL
342
//					boolean smGenerated = Boolean.parseBoolean(PropertyService.getProperty("dataone.systemmetadata.generated"));
343
//					if (!smGenerated) {
344
//						GenerateSystemMetadata systemMetadataUpgrade = new GenerateSystemMetadata();
345
//				        systemMetadataUpgrade.upgrade();
346
//				        // NOTE: this runs in a thread and marks SM generated when thatthread completes
347
//					}
348
//
349
//					// Generate ORE, if we haven't
350
//					boolean oreGenerated = Boolean.parseBoolean(PropertyService.getProperty("dataone.ore.generated"));
351
//					if (!oreGenerated) {
352
//						GenerateORE gore = new GenerateORE();
353
//						gore.upgrade();
354
//						PropertyService.setProperty("dataone.ore.generated", Boolean.TRUE.toString());
355
//					}
356
					
357
					// write the backup properties to a location outside the
358
					// application directories so they will be available after
359
					// the next upgrade
360
					PropertyService.persistMainBackupProperties();
361
					
362
				}
363
			} catch (GeneralPropertyException gpe) {
364
				String errorMessage = "D1Admin.configureDataONE - Problem getting or setting property while "
365
						+ "processing system properties page: " + gpe.getMessage();
366
				logMetacat.error(errorMessage);
367
				processingErrors.add(errorMessage);
368
			} catch (Exception e) {
369
				String errorMessage = "D1Admin.configureDataONE error: " + e.getMessage();
370
				logMetacat.error(errorMessage);
371
				processingErrors.add(errorMessage);
372
			}
373

    
374
			try {
375
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
376
					RequestUtil.clearRequestMessages(request);
377
					RequestUtil.setRequestFormErrors(request, validationErrors);
378
					RequestUtil.setRequestErrors(request, processingErrors);
379
					RequestUtil.forwardRequest(request, response, "/admin", null);
380
				} else {
381
					// Now that the options have been set, change the
382
					// 'dataoneConfigured' option to 'true'
383
					PropertyService.setProperty("configutil.dataoneConfigured",
384
							PropertyService.CONFIGURED);
385
					
386
					// Reload the main metacat configuration page
387
					processingSuccess.add("DataONE successfully configured");
388
					RequestUtil.clearRequestMessages(request);
389
					RequestUtil.setRequestSuccess(request, processingSuccess);
390
					RequestUtil.forwardRequest(request, response, 
391
							"/admin?configureType=configure&processForm=false", null);
392
				}
393
			} catch (MetacatUtilException mue) {
394
				throw new AdminException("D1Admin.configureDataONE - utility problem while processing dataone configuration: "
395
					 + mue.getMessage());
396
			} catch (GeneralPropertyException gpe) {
397
				throw new AdminException("D1Admin.configureDataONE - problem with properties while "
398
						+ "processing geoservices configuration page: " + gpe.getMessage());
399
			}
400
		}
401
	}
402
	
403
	private boolean isNodeRegistered(String nodeId) {
404
		// check if this is new or an update
405
        boolean exists = false;
406
        try {
407
	        NodeList nodes = D1Client.getCN().listNodes();
408
	        for (Node n: nodes.getNodeList()) {
409
	        	if (n.getIdentifier().getValue().equals(nodeId)) {
410
	        		exists = true;
411
	        		break;
412
	        	}
413
	        }
414
        } catch (BaseException e) {
415
            logMetacat.error("Could not check for node with DataONE (" + e.getCode() + "/" + e.getDetail_code() + "): " + e.getDescription());
416
        } 
417
        return exists;
418
	}
419

    
420
	/**
421
	 * Register as a member node on the DataONE network.  The node description is
422
	 * retrieved from the getCapabilities() service, and so this should only be called
423
	 * after the properties have been properly set up in Metacat.
424
	 */
425
	private void registerDataONEMemberNode() throws BaseException, PropertyNotFoundException, GeneralPropertyException {
426
        
427
        logMetacat.debug("Get the Node description.");
428
        Node node = MNodeService.getInstance(null).getCapabilities();
429
        logMetacat.debug("Setting client certificate location.");
430
        String mnCertificatePath = PropertyService.getProperty("D1Client.certificate.file");
431
        CertificateManager.getInstance().setCertificateLocation(mnCertificatePath);
432
        CNode cn = D1Client.getCN();
433
        
434
        // check if this is new or an update
435
        boolean update = isNodeRegistered(node.getIdentifier().getValue());
436
        
437
        // Session is null, because the libclient code automatically sets up an
438
        // SSL session for us using the client certificate provided
439
        Session session = null;
440
        if (update) {
441
        	logMetacat.debug("Updating node with DataONE. " + cn.getNodeBaseServiceUrl());
442
            boolean result = cn.updateNodeCapabilities(session, node.getIdentifier(), node);
443
        } else {
444
            logMetacat.debug("Registering node with DataONE. " + cn.getNodeBaseServiceUrl());
445
            NodeReference mnodeRef = cn.register(session, node);            
446
			
447
			// save that we submitted registration
448
			PropertyService.setPropertyNoPersist("dataone.mn.registration.submitted", Boolean.TRUE.toString());
449
            
450
            // persist the properties
451
            PropertyService.persistProperties();
452
        }
453
        
454
	}
455

    
456
	/**
457
	 * Validate the most important configuration options submitted by the user.
458
	 * 
459
	 * @return a vector holding error message for any fields that fail
460
	 *         validation.
461
	 */
462
	protected Vector<String> validateOptions(HttpServletRequest request) {
463
		Vector<String> errorVector = new Vector<String>();
464

    
465
		// TODO MCD validate options.
466

    
467
		return errorVector;
468
	}
469
}
(4-4/12)