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-06 13:51:40 -0800 (Fri, 06 Jan 2012) $'
10
 * '$Revision: 6864 $'
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.dataone.MNodeService;
46
import edu.ucsb.nceas.metacat.properties.PropertyService;
47
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
48
import edu.ucsb.nceas.metacat.util.RequestUtil;
49
import edu.ucsb.nceas.utilities.GeneralPropertyException;
50
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
51
import edu.ucsb.nceas.utilities.SortedProperties;
52

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

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

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

    
67
	}
68

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

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

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

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

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

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

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

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

    
291
					// write the backup properties to a location outside the
292
					// application directories so they will be available after
293
					// the next upgrade
294
					PropertyService.persistMainBackupProperties();
295
					
296
			        // Register as a DataONE Member Node
297
					if (replicate) {
298
						registerDataONEMemberNode();
299
					}
300
				}
301
			} catch (GeneralPropertyException gpe) {
302
				String errorMessage = "D1Admin.configureDataONE - Problem getting or setting property while "
303
						+ "processing system properties page: " + gpe.getMessage();
304
				logMetacat.error(errorMessage);
305
				processingErrors.add(errorMessage);
306
			}
307

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

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

    
389
	/**
390
	 * Validate the most important configuration options submitted by the user.
391
	 * 
392
	 * @return a vector holding error message for any fields that fail
393
	 *         validation.
394
	 */
395
	protected Vector<String> validateOptions(HttpServletRequest request) {
396
		Vector<String> errorVector = new Vector<String>();
397

    
398
		// TODO MCD validate options.
399

    
400
		return errorVector;
401
	}
402
}
(4-4/12)