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: leinfelder $'
9
 *     '$Date: 2012-01-23 12:53:51 -0800 (Mon, 23 Jan 2012) $'
10
 * '$Revision: 6935 $'
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 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
				/**
125
				dataone.nodeSynchronization.schedule.year=*
126
				dataone.nodeSynchronization.schedule.mon=*
127
				dataone.nodeSynchronization.schedule.mday=*
128
				dataone.nodeSynchronization.schedule.wday=?
129
				dataone.nodeSynchronization.schedule.hour=*
130
				dataone.nodeSynchronization.schedule.min=0/3
131
				dataone.nodeSynchronization.schedule.sec=10
132
				**/
133
				
134
				boolean synchronize = false;
135
				if (nodeSynchronize != null) {
136
					synchronize = Boolean.parseBoolean(nodeSynchronize);
137
				}
138
				boolean replicate = false;
139
				if (nodeReplicate != null) {
140
					replicate = Boolean.parseBoolean(nodeReplicate);
141
				}
142
				
143
				request.setAttribute("dataone.nodeName", nodeName);
144
				request.setAttribute("dataone.nodeDescription", nodeDescription);
145
				request.setAttribute("dataone.memberNodeId", memberNodeId);
146
				request.setAttribute("dataone.nodeSynchronize", Boolean.toString(synchronize));
147
				request.setAttribute("dataone.nodeReplicate", Boolean.toString(replicate));
148
				request.setAttribute("dataone.subject", subject);
149
				request.setAttribute("D1Client.certificate.file", certpath);
150
				
151
				// synch schedule
152
				request.setAttribute("dataone.nodeSynchronization.schedule.year", year);
153
				request.setAttribute("dataone.nodeSynchronization.schedule.mon", mon);
154
				request.setAttribute("dataone.nodeSynchronization.schedule.mday", mday);
155
				request.setAttribute("dataone.nodeSynchronization.schedule.wday", wday);
156
				request.setAttribute("dataone.nodeSynchronization.schedule.hour", hour);
157
				request.setAttribute("dataone.nodeSynchronization.schedule.min", min);
158
				request.setAttribute("dataone.nodeSynchronization.schedule.sec", sec);
159

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

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

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

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

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

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

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

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

    
407
		// TODO MCD validate options.
408

    
409
		return errorVector;
410
	}
411
}
(4-4/12)