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
 *    Authors: Michael Daigle
7
 * 
8
 *   '$Author: cjones $'
9
 *     '$Date: 2012-01-23 15:09:48 -0800 (Mon, 23 Jan 2012) $'
10
 * '$Revision: 6941 $'
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.util.Vector;
30

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

    
34
import org.apache.log4j.Logger;
35
import org.dataone.client.CNode;
36
import org.dataone.client.D1Client;
37
import org.dataone.client.auth.CertificateManager;
38
import org.dataone.service.exceptions.BaseException;
39
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
import edu.ucsb.nceas.metacat.IdentifierManager;
45
import edu.ucsb.nceas.metacat.admin.upgrade.GenerateORE;
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.memberNodeId");
110
				String nodeSynchronize = PropertyService.getProperty("dataone.nodeSynchronize");
111
				String nodeReplicate = PropertyService.getProperty("dataone.nodeReplicate");
112
        String subject = PropertyService.getProperty("dataone.subject");
113
				String contactSubject = PropertyService.getProperty("dataone.contactSubject");
114
				String certpath = PropertyService.getProperty("D1Client.certificate.file");
115
				
116
				//the synch schedule
117
				String year = PropertyService.getProperty("dataone.nodeSynchronization.schedule.year");
118
				String mon = PropertyService.getProperty("dataone.nodeSynchronization.schedule.mon");
119
				String mday = PropertyService.getProperty("dataone.nodeSynchronization.schedule.mday");
120
				String wday = PropertyService.getProperty("dataone.nodeSynchronization.schedule.wday");
121
				String hour = PropertyService.getProperty("dataone.nodeSynchronization.schedule.hour");
122
				String min = PropertyService.getProperty("dataone.nodeSynchronization.schedule.min");
123
				String sec = PropertyService.getProperty("dataone.nodeSynchronization.schedule.sec");
124
				
125
				/**
126
				dataone.nodeSynchronization.schedule.year=*
127
				dataone.nodeSynchronization.schedule.mon=*
128
				dataone.nodeSynchronization.schedule.mday=*
129
				dataone.nodeSynchronization.schedule.wday=?
130
				dataone.nodeSynchronization.schedule.hour=*
131
				dataone.nodeSynchronization.schedule.min=0/3
132
				dataone.nodeSynchronization.schedule.sec=10
133
				**/
134
				
135
				boolean synchronize = false;
136
				if (nodeSynchronize != null) {
137
					synchronize = Boolean.parseBoolean(nodeSynchronize);
138
				}
139
				boolean replicate = false;
140
				if (nodeReplicate != null) {
141
					replicate = Boolean.parseBoolean(nodeReplicate);
142
				}
143
				
144
				request.setAttribute("dataone.nodeName", nodeName);
145
				request.setAttribute("dataone.nodeDescription", nodeDescription);
146
				request.setAttribute("dataone.memberNodeId", memberNodeId);
147
				request.setAttribute("dataone.nodeSynchronize", Boolean.toString(synchronize));
148
				request.setAttribute("dataone.nodeReplicate", Boolean.toString(replicate));
149
        request.setAttribute("dataone.subject", subject);
150
				request.setAttribute("dataone.contactSubject", contactSubject);
151
				request.setAttribute("D1Client.certificate.file", certpath);
152
				
153
				// synch schedule
154
				request.setAttribute("dataone.nodeSynchronization.schedule.year", year);
155
				request.setAttribute("dataone.nodeSynchronization.schedule.mon", mon);
156
				request.setAttribute("dataone.nodeSynchronization.schedule.mday", mday);
157
				request.setAttribute("dataone.nodeSynchronization.schedule.wday", wday);
158
				request.setAttribute("dataone.nodeSynchronization.schedule.hour", hour);
159
				request.setAttribute("dataone.nodeSynchronization.schedule.min", min);
160
				request.setAttribute("dataone.nodeSynchronization.schedule.sec", sec);
161

    
162
				// try the backup properties
163
				SortedProperties backupProperties = null;
164
				if ((backupProperties = 
165
						PropertyService.getMainBackupProperties()) != null) {
166
					Vector<String> backupKeys = backupProperties.getPropertyNames();
167
					for (String key : backupKeys) {
168
						String value = backupProperties.getProperty(key);
169
						if (value != null) {
170
							request.setAttribute(key, value);
171
						}
172
					}
173
				}
174
				
175
				// Forward the request to the JSP page
176
				RequestUtil.forwardRequest(request, response, "/admin/dataone-configuration.jsp", null);
177
			} catch (GeneralPropertyException gpe) {
178
				throw new AdminException("D1Admin.configureDataONE - Problem getting or " + 
179
						"setting property while initializing system properties page: " + gpe.getMessage());
180
			} catch (MetacatUtilException mue) {
181
				throw new AdminException("D1Admin.configureDataONE - utility problem while initializing "
182
						+ "system properties page:" + mue.getMessage());
183
			} 
184
		} else if (bypass != null && bypass.equals("true")) {
185
			Vector<String> processingErrors = new Vector<String>();
186
			Vector<String> processingSuccess = new Vector<String>();
187
			
188
			// Bypass the D1 configuration. 
189
			// This will keep Metacat running
190
			try {
191
				PropertyService.setProperty("configutil.dataoneConfigured", PropertyService.BYPASSED);
192
				
193
			} catch (GeneralPropertyException gpe) {
194
				String errorMessage = "D1Admin.configureDataONE - Problem getting or setting property while "
195
					+ "processing system properties page: " + gpe.getMessage();
196
				logMetacat.error(errorMessage);
197
				processingErrors.add(errorMessage);
198
			}
199
			try {
200
				if (processingErrors.size() > 0) {
201
					RequestUtil.clearRequestMessages(request);
202
					RequestUtil.setRequestErrors(request, processingErrors);
203
					RequestUtil.forwardRequest(request, response, "/admin", null);
204
				} else {			
205
					// Reload the main metacat configuration page
206
					processingSuccess.add("DataONE configuration successfully bypassed");
207
					RequestUtil.clearRequestMessages(request);
208
					RequestUtil.setRequestSuccess(request, processingSuccess);
209
					RequestUtil.forwardRequest(request, response, 
210
							"/admin?configureType=configure&processForm=false", null);
211
				}
212
			} catch (MetacatUtilException mue) {
213
				throw new AdminException("D1Admin.configureDataONE - utility problem while processing dataone page: "
214
						+ mue.getMessage());
215
			} 
216
		
217
		} else {
218
			// The configuration form is being submitted and needs to be
219
			// processed, setting the properties in the configuration file
220
			// then restart metacat
221

    
222
			// The configuration form is being submitted and needs to be
223
			// processed.
224
			Vector<String> validationErrors = new Vector<String>();
225
			Vector<String> processingErrors = new Vector<String>();
226
			Vector<String> processingSuccess = new Vector<String>();
227

    
228
			try {
229
				// Validate that the options provided are legitimate. Note that
230
				// we've allowed them to persist their entries. As of this point
231
				// there is no other easy way to go back to the configure form
232
				// and preserve their entries.
233
				validationErrors.addAll(validateOptions(request));
234
				
235
				String nodeName = (String)request.getParameter("dataone.nodeName");
236
				String nodeDescription = (String)request.getParameter("dataone.nodeDescription");
237
				String memberNodeId = (String)request.getParameter("dataone.memberNodeId");
238
				String nodeSynchronize = (String)request.getParameter("dataone.nodeSynchronize");
239
				String nodeReplicate = (String)request.getParameter("dataone.nodeReplicate");
240
        String subject = (String)request.getParameter("dataone.subject");
241
				String contactSubject = (String)request.getParameter("dataone.contactSubject");
242
				String certpath = (String)request.getParameter("D1Client.certificate.file");
243
				
244
				// the synch schedule
245
				String year = (String) request.getParameter("dataone.nodeSynchronization.schedule.year");
246
				String mon = (String) request.getParameter("dataone.nodeSynchronization.schedule.mon");
247
				String mday = (String) request.getParameter("dataone.nodeSynchronization.schedule.mday");
248
				String wday = (String) request.getParameter("dataone.nodeSynchronization.schedule.wday");
249
				String hour = (String) request.getParameter("dataone.nodeSynchronization.schedule.hour");
250
				String min = (String) request.getParameter("dataone.nodeSynchronization.schedule.min");
251
				String sec = (String) request.getParameter("dataone.nodeSynchronization.schedule.sec");
252
				
253
				boolean synchronize = false;
254
				if (nodeSynchronize != null) {
255
					synchronize = Boolean.parseBoolean(nodeSynchronize);
256
				}
257
				boolean replicate = false;
258
				if (nodeReplicate != null) {
259
					replicate = Boolean.parseBoolean(nodeReplicate);
260
				}
261
				
262
				// process the values, checking for nulls etc..
263
				if (nodeName == null ) {
264
					validationErrors.add("nodeName cannot be null");
265
				} else {
266
					
267
					PropertyService.setPropertyNoPersist("dataone.nodeName", nodeName);
268
					PropertyService.setPropertyNoPersist("dataone.nodeDescription", nodeDescription);
269
					
270
					// check if we have updated the memberNodeId
271
					String existingMemberNodeId = PropertyService.getProperty("dataone.memberNodeId");
272
					if (!existingMemberNodeId.equals(memberNodeId)) {
273
						// update all existing system Metadata for this node id
274
						IdentifierManager.getInstance().updateAuthoritativeMemberNodeId(existingMemberNodeId, memberNodeId);
275
					}
276
					// TODO: persist in DB?
277
					PropertyService.setPropertyNoPersist("dataone.memberNodeId", memberNodeId);
278
					
279
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronize", Boolean.toString(synchronize));
280
					PropertyService.setPropertyNoPersist("dataone.nodeReplicate", Boolean.toString(replicate));
281
          PropertyService.setPropertyNoPersist("dataone.subject", subject);
282
					PropertyService.setPropertyNoPersist("dataone.contactSubject", subject); // same as subject for now
283
					PropertyService.setPropertyNoPersist("D1Client.certificate.file", certpath);
284
					
285
					// the synch schedule
286
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.year", year);
287
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.mon", mon);
288
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.mday", mday);
289
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.wday", wday);
290
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.hour", hour);
291
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.min", min);
292
					PropertyService.setPropertyNoPersist("dataone.nodeSynchronization.schedule.sec", sec);
293
					
294
					PropertyService.persistProperties();
295
					
296
			        // Register/update as a DataONE Member Node					
297
					registerDataONEMemberNode();
298

    
299
					// Generate ORE, if we haven't
300
					boolean oreGenerated = Boolean.parseBoolean(PropertyService.getProperty("configutil.dataone.ore.generated"));
301
					if (!oreGenerated) {
302
						GenerateORE gore = new GenerateORE();
303
						gore.upgrade();
304
						PropertyService.setProperty("dataone.ore.generated", Boolean.TRUE.toString());
305

    
306
					}
307
					
308
					// write the backup properties to a location outside the
309
					// application directories so they will be available after
310
					// the next upgrade
311
					PropertyService.persistMainBackupProperties();
312
					
313
				}
314
			} catch (GeneralPropertyException gpe) {
315
				String errorMessage = "D1Admin.configureDataONE - Problem getting or setting property while "
316
						+ "processing system properties page: " + gpe.getMessage();
317
				logMetacat.error(errorMessage);
318
				processingErrors.add(errorMessage);
319
			}
320

    
321
			try {
322
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
323
					RequestUtil.clearRequestMessages(request);
324
					RequestUtil.setRequestFormErrors(request, validationErrors);
325
					RequestUtil.setRequestErrors(request, processingErrors);
326
					RequestUtil.forwardRequest(request, response, "/admin", null);
327
				} else {
328
					// Now that the options have been set, change the
329
					// 'dataoneConfigured' option to 'true'
330
					PropertyService.setProperty("configutil.dataoneConfigured",
331
							PropertyService.CONFIGURED);
332
					
333
					// Reload the main metacat configuration page
334
					processingSuccess.add("DataONE successfully configured");
335
					RequestUtil.clearRequestMessages(request);
336
					RequestUtil.setRequestSuccess(request, processingSuccess);
337
					RequestUtil.forwardRequest(request, response, 
338
							"/admin?configureType=configure&processForm=false", null);
339
				}
340
			} catch (MetacatUtilException mue) {
341
				throw new AdminException("D1Admin.configureDataONE - utility problem while processing dataone configuration: "
342
					 + mue.getMessage());
343
			} catch (GeneralPropertyException gpe) {
344
				throw new AdminException("D1Admin.configureDataONE - problem with properties while "
345
						+ "processing geoservices configuration page: " + gpe.getMessage());
346
			}
347
		}
348
	}
349

    
350
	/**
351
	 * Register as a member node on the DataONE network.  The node description is
352
	 * retrieved from the getCapabilities() service, and so this should only be called
353
	 * after the properties have been properly set up in Metacat.
354
	 */
355
	private void registerDataONEMemberNode() {
356
        CNode cn;
357
        try {
358
            logMetacat.debug("Get the Node description.");
359
            Node node = MNodeService.getInstance(null).getCapabilities();
360
            logMetacat.debug("Setting client certificate location.");
361
            String mnCertificatePath = PropertyService.getProperty("D1Client.certificate.file");
362
            CertificateManager.getInstance().setCertificateLocation(mnCertificatePath);
363
            cn = D1Client.getCN();
364
            // check if this is new or an update
365
            boolean update = false;
366
            NodeList nodes = cn.listNodes();
367
            for (Node n: nodes.getNodeList()) {
368
            	if (n.getIdentifier().getValue().equals(node.getIdentifier().getValue())) {
369
            		update = true;
370
            		break;
371
            	}
372
            }
373
            // Session is null, because the libclient code automatically sets up an
374
            // SSL session for us using the client certificate provided
375
            Session session = null;
376
            if (update) {
377
            	logMetacat.debug("Updating node with DataONE. " + cn.getNodeBaseServiceUrl());
378
	            boolean result = cn.updateNodeCapabilities(session, node.getIdentifier(), node);
379
            } else {
380
	            logMetacat.debug("Registering node with DataONE. " + cn.getNodeBaseServiceUrl());
381
	            NodeReference mnodeRef = cn.register(session, node);
382
	            
383
	            String memberNodeId = mnodeRef.getValue();
384
	            String existingMemberNodeId = PropertyService.getProperty("dataone.memberNodeId");
385
				if (!existingMemberNodeId.equals(memberNodeId)) {
386
					// update all existing system Metadata for this node id
387
					IdentifierManager.getInstance().updateAuthoritativeMemberNodeId(existingMemberNodeId, memberNodeId);
388
				}
389
	            // save this assigned node id
390
	            PropertyService.setProperty("dataone.memberNodeId", memberNodeId);
391
	            
392
            }
393
        } catch (BaseException e) {
394
            logMetacat.warn("Could not register as node with DataONE (" + e.getCode() + "/" + e.getDetail_code() + "): " + e.getDescription());
395
        } catch (PropertyNotFoundException e) {
396
            logMetacat.warn("Could not find the location for client certificates: " + e.getMessage());
397
        } catch (GeneralPropertyException e) {
398
            logMetacat.warn("Could not set the assigned node id: " + e.getMessage());
399
		}
400
	}
401

    
402
	/**
403
	 * Validate the most important configuration options submitted by the user.
404
	 * 
405
	 * @return a vector holding error message for any fields that fail
406
	 *         validation.
407
	 */
408
	protected Vector<String> validateOptions(HttpServletRequest request) {
409
		Vector<String> errorVector = new Vector<String>();
410

    
411
		// TODO MCD validate options.
412

    
413
		return errorVector;
414
	}
415
}
(4-4/12)