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 10:45:36 -0800 (Fri, 06 Jan 2012) $'
10
 * '$Revision: 6862 $'
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

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

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

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

    
66
	}
67

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

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

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

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

    
102
			try {
103
				
104
				// get the current configuration values
105
				String nodeName = PropertyService.getProperty("dataone.nodeName");
106
				String nodeDescription = PropertyService.getProperty("dataone.nodeDescription");
107
				String memberNodeId = PropertyService.getProperty("dataone.memberNodeId");
108
				String nodeSynchronize = PropertyService.getProperty("dataone.nodeSynchronize");
109
				String nodeReplicate = PropertyService.getProperty("dataone.nodeReplicate");
110
				String subject = PropertyService.getProperty("dataone.subject");
111
				String certpath = PropertyService.getProperty("D1Client.certificate.file");
112
				
113
				//the synch schedule
114
				String year = PropertyService.getProperty("dataone.nodeSynchronization.schedule.year");
115
				String mon = PropertyService.getProperty("dataone.nodeSynchronization.schedule.mon");
116
				String mday = PropertyService.getProperty("dataone.nodeSynchronization.schedule.mday");
117
				String wday = PropertyService.getProperty("dataone.nodeSynchronization.schedule.wday");
118
				String hour = PropertyService.getProperty("dataone.nodeSynchronization.schedule.hour");
119
				String min = PropertyService.getProperty("dataone.nodeSynchronization.schedule.min");
120
				String sec = PropertyService.getProperty("dataone.nodeSynchronization.schedule.sec");
121
				
122
				/**
123
				dataone.nodeSynchronization.schedule.year=*
124
				dataone.nodeSynchronization.schedule.mon=*
125
				dataone.nodeSynchronization.schedule.mday=*
126
				dataone.nodeSynchronization.schedule.wday=?
127
				dataone.nodeSynchronization.schedule.hour=*
128
				dataone.nodeSynchronization.schedule.min=0/3
129
				dataone.nodeSynchronization.schedule.sec=10
130
				**/
131
				
132
				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
				
141
				request.setAttribute("dataone.nodeName", nodeName);
142
				request.setAttribute("dataone.nodeDescription", nodeDescription);
143
				request.setAttribute("dataone.memberNodeId", memberNodeId);
144
				request.setAttribute("dataone.nodeSynchronize", synchronize);
145
				request.setAttribute("dataone.nodeReplicate", replicate);
146
				request.setAttribute("dataone.subject", subject);
147
				request.setAttribute("D1Client.certificate.file", certpath);
148
				
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

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

    
205
			// The configuration form is being submitted and needs to be
206
			// processed.
207
			Vector<String> validationErrors = new Vector<String>();
208
			Vector<String> processingErrors = new Vector<String>();
209
			Vector<String> processingSuccess = new Vector<String>();
210

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

    
277
					// write the backup properties to a location outside the
278
					// application directories so they will be available after
279
					// the next upgrade
280
					PropertyService.persistMainBackupProperties();
281
					
282
			        // Register as a DataONE Member Node
283
					if (replicate) {
284
						registerDataONEMemberNode();
285
					}
286
				}
287
			} catch (GeneralPropertyException gpe) {
288
				String errorMessage = "D1Admin.configureDataONE - Problem getting or setting property while "
289
						+ "processing system properties page: " + gpe.getMessage();
290
				logMetacat.error(errorMessage);
291
				processingErrors.add(errorMessage);
292
			}
293

    
294
			try {
295
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
296
					RequestUtil.clearRequestMessages(request);
297
					RequestUtil.setRequestFormErrors(request, validationErrors);
298
					RequestUtil.setRequestErrors(request, processingErrors);
299
					RequestUtil.forwardRequest(request, response, "/admin", null);
300
				} else {
301
					// Now that the options have been set, change the
302
					// 'dataoneConfigured' option to 'true'
303
					PropertyService.setProperty("configutil.dataoneConfigured",
304
							PropertyService.CONFIGURED);
305
					
306
					// Reload the main metacat configuration page
307
					processingSuccess.add("DataONE successfully configured");
308
					RequestUtil.clearRequestMessages(request);
309
					RequestUtil.setRequestSuccess(request, processingSuccess);
310
					RequestUtil.forwardRequest(request, response, 
311
							"/admin?configureType=configure&processForm=false", null);
312
				}
313
			} catch (MetacatUtilException mue) {
314
				throw new AdminException("D1Admin.configureDataONE - utility problem while processing geoservices "
315
						+ "geoservices page: " + mue.getMessage());
316
			} catch (GeneralPropertyException gpe) {
317
				throw new AdminException("D1Admin.configureDataONE - problem with properties while "
318
						+ "processing geoservices configuration page: " + gpe.getMessage());
319
			}
320
		}
321
	}
322

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

    
375
	/**
376
	 * Validate the most important configuration options submitted by the user.
377
	 * 
378
	 * @return a vector holding error message for any fields that fail
379
	 *         validation.
380
	 */
381
	protected Vector<String> validateOptions(HttpServletRequest request) {
382
		Vector<String> errorVector = new Vector<String>();
383

    
384
		// TODO MCD validate options.
385

    
386
		return errorVector;
387
	}
388
}
(4-4/12)