Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000-2011 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author:  $'
7
 *     '$Date:  $'
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 */
23

    
24
package edu.ucsb.nceas.metacat.dataone;
25

    
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.security.NoSuchAlgorithmException;
29
import java.sql.SQLException;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.List;
33
import java.util.Timer;
34

    
35
import javax.servlet.http.HttpServletRequest;
36

    
37
import org.apache.commons.io.IOUtils;
38
import org.apache.log4j.Logger;
39
import org.dataone.client.CNode;
40
import org.dataone.client.D1Client;
41
import org.dataone.client.MNode;
42
import org.dataone.client.auth.CertificateManager;
43
import org.dataone.configuration.Settings;
44
import org.dataone.service.exceptions.BaseException;
45
import org.dataone.service.exceptions.IdentifierNotUnique;
46
import org.dataone.service.exceptions.InsufficientResources;
47
import org.dataone.service.exceptions.InvalidRequest;
48
import org.dataone.service.exceptions.InvalidSystemMetadata;
49
import org.dataone.service.exceptions.InvalidToken;
50
import org.dataone.service.exceptions.NotAuthorized;
51
import org.dataone.service.exceptions.NotFound;
52
import org.dataone.service.exceptions.NotImplemented;
53
import org.dataone.service.exceptions.ServiceFailure;
54
import org.dataone.service.exceptions.SynchronizationFailed;
55
import org.dataone.service.exceptions.UnsupportedType;
56
import org.dataone.service.exceptions.VersionMismatch;
57
import org.dataone.service.mn.tier1.v1.MNCore;
58
import org.dataone.service.mn.tier1.v1.MNRead;
59
import org.dataone.service.mn.tier2.v1.MNAuthorization;
60
import org.dataone.service.mn.tier3.v1.MNStorage;
61
import org.dataone.service.mn.tier4.v1.MNReplication;
62
import org.dataone.service.types.v1.AccessPolicy;
63
import org.dataone.service.types.v1.Checksum;
64
import org.dataone.service.types.v1.DescribeResponse;
65
import org.dataone.service.types.v1.Event;
66
import org.dataone.service.types.v1.Group;
67
import org.dataone.service.types.v1.Identifier;
68
import org.dataone.service.types.v1.Log;
69
import org.dataone.service.types.v1.LogEntry;
70
import org.dataone.service.types.v1.MonitorInfo;
71
import org.dataone.service.types.v1.MonitorList;
72
import org.dataone.service.types.v1.Node;
73
import org.dataone.service.types.v1.NodeList;
74
import org.dataone.service.types.v1.NodeReference;
75
import org.dataone.service.types.v1.NodeState;
76
import org.dataone.service.types.v1.NodeType;
77
import org.dataone.service.types.v1.ObjectFormatIdentifier;
78
import org.dataone.service.types.v1.ObjectList;
79
import org.dataone.service.types.v1.Permission;
80
import org.dataone.service.types.v1.Ping;
81
import org.dataone.service.types.v1.ReplicationStatus;
82
import org.dataone.service.types.v1.Schedule;
83
import org.dataone.service.types.v1.Service;
84
import org.dataone.service.types.v1.Services;
85
import org.dataone.service.types.v1.Session;
86
import org.dataone.service.types.v1.Subject;
87
import org.dataone.service.types.v1.Synchronization;
88
import org.dataone.service.types.v1.SystemMetadata;
89
import org.dataone.service.types.v1.util.ChecksumUtil;
90
import org.dataone.service.util.Constants;
91

    
92
import edu.ucsb.nceas.metacat.DocumentImpl;
93
import edu.ucsb.nceas.metacat.EventLog;
94
import edu.ucsb.nceas.metacat.IdentifierManager;
95
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
96
import edu.ucsb.nceas.metacat.McdbException;
97
import edu.ucsb.nceas.metacat.MetacatHandler;
98
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
99
import edu.ucsb.nceas.metacat.database.DBConnection;
100
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
101
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
102
import edu.ucsb.nceas.metacat.properties.PropertyService;
103
import edu.ucsb.nceas.metacat.shared.ServiceException;
104
import edu.ucsb.nceas.metacat.util.SystemUtil;
105
import edu.ucsb.nceas.utilities.ParseLSIDException;
106
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
107

    
108
/**
109
 * Represents Metacat's implementation of the DataONE Member Node 
110
 * service API. Methods implement the various MN* interfaces, and methods common
111
 * to both Member Node and Coordinating Node interfaces are found in the
112
 * D1NodeService base class.
113
 * 
114
 * Implements:
115
 * MNCore.ping()
116
 * MNCore.getLogRecords()
117
 * MNCore.getObjectStatistics()
118
 * MNCore.getOperationStatistics()
119
 * MNCore.getStatus()
120
 * MNCore.getCapabilities()
121
 * MNRead.get()
122
 * MNRead.getSystemMetadata()
123
 * MNRead.describe()
124
 * MNRead.getChecksum()
125
 * MNRead.listObjects()
126
 * MNRead.synchronizationFailed()
127
 * MNAuthorization.isAuthorized()
128
 * MNAuthorization.setAccessPolicy()
129
 * MNStorage.create()
130
 * MNStorage.update()
131
 * MNStorage.delete()
132
 * MNReplication.replicate()
133
 * 
134
 */
135
public class MNodeService extends D1NodeService 
136
    implements MNAuthorization, MNCore, MNRead, MNReplication, MNStorage {
137

    
138
    /* the logger instance */
139
    private Logger logMetacat = null;
140
    
141
    /* A reference to a remote Memeber Node */
142
    private MNode mn;
143
    
144
    /* A reference to a Coordinating Node */
145
    private CNode cn;
146

    
147

    
148
    /**
149
     * Singleton accessor to get an instance of MNodeService.
150
     * 
151
     * @return instance - the instance of MNodeService
152
     */
153
    public static MNodeService getInstance(HttpServletRequest request) {
154
        return new MNodeService(request);
155
    }
156

    
157
    /**
158
     * Constructor, private for singleton access
159
     */
160
    private MNodeService(HttpServletRequest request) {
161
        super(request);
162
        logMetacat = Logger.getLogger(MNodeService.class);
163
        
164
        // set the Member Node certificate file location
165
        CertificateManager.getInstance().setCertificateLocation(Settings.getConfiguration().getString("D1Client.certificate.file"));
166
    }
167

    
168
    /**
169
     * Deletes an object from the Member Node, where the object is either a 
170
     * data object or a science metadata object.
171
     * 
172
     * @param session - the Session object containing the credentials for the Subject
173
     * @param pid - The object identifier to be deleted
174
     * 
175
     * @return pid - the identifier of the object used for the deletion
176
     * 
177
     * @throws InvalidToken
178
     * @throws ServiceFailure
179
     * @throws NotAuthorized
180
     * @throws NotFound
181
     * @throws NotImplemented
182
     * @throws InvalidRequest
183
     */
184
    @Override
185
    public Identifier delete(Session session, Identifier pid) 
186
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
187

    
188
        String localId = null;
189
        boolean allowed = false;
190
        String username = Constants.SUBJECT_PUBLIC;
191
        String[] groupnames = null;
192
        if (session == null) {
193
        	throw new InvalidToken("1330", "No session has been provided");
194
        } else {
195
            username = session.getSubject().getValue();
196
            if (session.getSubjectInfo() != null) {
197
                List<Group> groupList = session.getSubjectInfo().getGroupList();
198
                if (groupList != null) {
199
                    groupnames = new String[groupList.size()];
200
                    for (int i = 0; i > groupList.size(); i++) {
201
                        groupnames[i] = groupList.get(i).getGroupName();
202
                    }
203
                }
204
            }
205
        }
206

    
207
        // do we have a valid pid?
208
        if (pid == null || pid.getValue().trim().equals("")) {
209
            throw new ServiceFailure("1350", "The provided identifier was invalid.");
210
        }
211

    
212
        // check for the existing identifier
213
        try {
214
            localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
215
        } catch (McdbDocNotFoundException e) {
216
            throw new NotFound("1340", "The object with the provided " + "identifier was not found.");
217
        }
218

    
219
        // does the subject have DELETE (a D1 CHANGE_PERMISSION level) priveleges on the pid?
220
        allowed = isAuthorized(session, pid, Permission.CHANGE_PERMISSION);
221
            
222

    
223
        if (allowed) {
224
            try {
225
                // delete the document
226
                DocumentImpl.delete(localId, username, groupnames, null);
227
                EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), username, localId, Event.DELETE.xmlValue());
228

    
229
                // archive it
230
                SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
231
                sysMeta.setArchived(true);
232
                HazelcastService.getInstance().getSystemMetadataMap().put(pid, sysMeta);
233
                
234
                // remove the system metadata for it
235
                //HazelcastService.getInstance().getSystemMetadataMap().remove(pid);
236
                
237
            } catch (McdbDocNotFoundException e) {
238
                throw new NotFound("1340", "The provided identifier was invalid.");
239

    
240
            } catch (SQLException e) {
241
                throw new ServiceFailure("1350", "There was a problem deleting the object." + "The error message was: " + e.getMessage());
242

    
243
            } catch (InsufficientKarmaException e) {
244
                throw new NotAuthorized("1320", "The provided identity does not have " + "permission to DELETE objects on the Member Node.");
245

    
246
            } catch (Exception e) { // for some reason DocumentImpl throws a general Exception
247
                throw new ServiceFailure("1350", "There was a problem deleting the object." + "The error message was: " + e.getMessage());
248
            }
249

    
250
        } else {
251
            throw new NotAuthorized("1320", "The provided identity does not have " + "permission to DELETE objects on the Member Node.");
252
        }
253

    
254
        return pid;
255
    }
256

    
257
    /**
258
     * Updates an existing object by creating a new object identified by 
259
     * newPid on the Member Node which explicitly obsoletes the object 
260
     * identified by pid through appropriate changes to the SystemMetadata 
261
     * of pid and newPid
262
     * 
263
     * @param session - the Session object containing the credentials for the Subject
264
     * @param pid - The identifier of the object to be updated
265
     * @param object - the new object bytes
266
     * @param sysmeta - the new system metadata describing the object
267
     * 
268
     * @return newPid - the identifier of the new object
269
     * 
270
     * @throws InvalidToken
271
     * @throws ServiceFailure
272
     * @throws NotAuthorized
273
     * @throws NotFound
274
     * @throws NotImplemented
275
     * @throws IdentifierNotUnique
276
     * @throws UnsupportedType
277
     * @throws InsufficientResources
278
     * @throws InvalidSystemMetadata
279
     * @throws InvalidRequest
280
     */
281
    @Override
282
    public Identifier update(Session session, Identifier pid, InputStream object, 
283
        Identifier newPid, SystemMetadata sysmeta) 
284
        throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
285
        UnsupportedType, InsufficientResources, NotFound, 
286
        InvalidSystemMetadata, NotImplemented, InvalidRequest {
287

    
288
        String localId = null;
289
        boolean allowed = false;
290
        boolean isScienceMetadata = false;
291
        
292
        if (session == null) {
293
        	throw new InvalidToken("1210", "No session has been provided");
294
        }
295
        Subject subject = session.getSubject();
296

    
297
        // do we have a valid pid?
298
        if (pid == null || pid.getValue().trim().equals("")) {
299
            throw new InvalidRequest("1202", "The provided identifier was invalid.");
300
            
301
        }
302

    
303
        // check for the existing identifier
304
        try {
305
            localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
306
            
307
        } catch (McdbDocNotFoundException e) {
308
            throw new InvalidRequest("1202", "The object with the provided " + 
309
                "identifier was not found.");
310
            
311
        }
312
        
313
        // set the originating node
314
        NodeReference originMemberNode = this.getCapabilities().getIdentifier();
315
        sysmeta.setOriginMemberNode(originMemberNode);
316
        
317
        // set the submitter to match the certificate
318
        sysmeta.setSubmitter(subject);
319
        // set the dates
320
        Date now = Calendar.getInstance().getTime();
321
        sysmeta.setDateSysMetadataModified(now);
322
        sysmeta.setDateUploaded(now);
323

    
324
        // does the subject have WRITE ( == update) priveleges on the pid?
325
        allowed = isAuthorized(session, pid, Permission.WRITE);
326

    
327
        if (allowed) {
328
        	
329
        	// check quality of SM
330
        	if (sysmeta.getObsoletedBy() != null) {
331
        		throw new InvalidSystemMetadata("1300", "Cannot include obsoletedBy when updating object");
332
        	}
333
        	if (sysmeta.getObsoletes() != null && !sysmeta.getObsoletes().getValue().equals(pid.getValue())) {
334
        		throw new InvalidSystemMetadata("1300", "The identifier provided in obsoletes does not match old Identifier");
335
        	}
336

    
337
            // get the existing system metadata for the object
338
            SystemMetadata existingSysMeta = getSystemMetadata(session, pid);
339

    
340
            // add the newPid to the obsoletedBy list for the existing sysmeta
341
            existingSysMeta.setObsoletedBy(newPid);
342

    
343
            // then update the existing system metadata
344
            updateSystemMetadata(existingSysMeta);
345

    
346
            // prep the new system metadata, add pid to the affected lists
347
            sysmeta.setObsoletes(pid);
348
            //sysmeta.addDerivedFrom(pid);
349

    
350
            isScienceMetadata = isScienceMetadata(sysmeta);
351

    
352
            // do we have XML metadata or a data object?
353
            if (isScienceMetadata) {
354

    
355
                // update the science metadata XML document
356
                // TODO: handle non-XML metadata/data documents (like netCDF)
357
                // TODO: don't put objects into memory using stream to string
358
                String objectAsXML = "";
359
                try {
360
                    objectAsXML = IOUtils.toString(object, "UTF-8");
361
                    localId = insertOrUpdateDocument(objectAsXML, newPid, session, "update");
362
                    // register the newPid and the generated localId
363
                    if (newPid != null) {
364
                        IdentifierManager.getInstance().createMapping(newPid.getValue(), localId);
365

    
366
                    }
367

    
368
                } catch (IOException e) {
369
                    String msg = "The Node is unable to create the object. " + "There was a problem converting the object to XML";
370
                    logMetacat.info(msg);
371
                    throw new ServiceFailure("1310", msg + ": " + e.getMessage());
372

    
373
                }
374

    
375
            } else {
376

    
377
                // update the data object
378
                localId = insertDataObject(object, newPid, session);
379

    
380
            }
381

    
382
            // and insert the new system metadata
383
            insertSystemMetadata(sysmeta);
384

    
385
            // log the update event
386
            EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), subject.getValue(), localId, Event.UPDATE.toString());
387

    
388
        } else {
389
            throw new NotAuthorized("1200", "The provided identity does not have " + "permission to UPDATE the object identified by " + pid.getValue()
390
                    + " on the Member Node.");
391
        }
392

    
393
        return newPid;
394
    }
395

    
396
    public Identifier create(Session session, Identifier pid, InputStream object, SystemMetadata sysmeta) throws InvalidToken, ServiceFailure, NotAuthorized,
397
            IdentifierNotUnique, UnsupportedType, InsufficientResources, InvalidSystemMetadata, NotImplemented, InvalidRequest {
398

    
399
      // check for null session
400
        if (session == null) {
401
          throw new InvalidToken("1110", "Session is required to WRITE to the Node.");
402
        }
403
        // set the submitter to match the certificate
404
        sysmeta.setSubmitter(session.getSubject());
405
        // set the originating node
406
        NodeReference originMemberNode = this.getCapabilities().getIdentifier();
407
        sysmeta.setOriginMemberNode(originMemberNode);
408
        // set the dates
409
        Date now = Calendar.getInstance().getTime();
410
    sysmeta.setDateSysMetadataModified(now);
411
    sysmeta.setDateUploaded(now);
412
        // call the shared impl
413
        return super.create(session, pid, object, sysmeta);
414
    }
415

    
416
    /**
417
     * Called by a Coordinating Node to request that the Member Node create a 
418
     * copy of the specified object by retrieving it from another Member 
419
     * Node and storing it locally so that it can be made accessible to 
420
     * the DataONE system.
421
     * 
422
     * @param session - the Session object containing the credentials for the Subject
423
     * @param sysmeta - Copy of the CN held system metadata for the object
424
     * @param sourceNode - A reference to node from which the content should be 
425
     *                     retrieved. The reference should be resolved by 
426
     *                     checking the CN node registry.
427
     * 
428
     * @return true if the replication succeeds
429
     * 
430
     * @throws ServiceFailure
431
     * @throws NotAuthorized
432
     * @throws NotImplemented
433
     * @throws UnsupportedType
434
     * @throws InsufficientResources
435
     * @throws InvalidRequest
436
     */
437
    @Override
438
    public boolean replicate(Session session, SystemMetadata sysmeta,
439
            NodeReference sourceNode) throws NotImplemented, ServiceFailure,
440
            NotAuthorized, InvalidRequest, InsufficientResources,
441
            UnsupportedType {
442

    
443
        logMetacat.info("MNodeService.replicate() called with parameters: \n"
444
                + "\tSession.Subject      = " + session.getSubject().getValue()
445
                + "\n" + "\tSystemMetadata       = " + sysmeta.toString()
446
                + "\n" + "\tSource NodeReference =" + sourceNode.getValue());
447

    
448
        boolean result = false;
449
        String nodeIdStr = null;
450
        NodeReference nodeId = null;
451

    
452
        // get the referenced object
453
        Identifier pid = sysmeta.getIdentifier();
454

    
455
        // get from the membernode
456
        // TODO: switch credentials for the server retrieval?
457
        this.mn = D1Client.getMN(sourceNode);
458
        this.cn = D1Client.getCN();
459
        InputStream object = null;
460
        Session thisNodeSession = null;
461
        SystemMetadata localSystemMetadata = null;
462
        BaseException failure = null;
463
        String localId = null;
464
        
465
        // TODO: check credentials
466
        // cannot be called by public
467
        if (session == null) {
468
            String msg = "No session was provided.";
469
            failure = new NotAuthorized("2152", msg);
470
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
471
            logMetacat.info(msg);
472
            return true;
473
        }
474

    
475

    
476
        // get the local node id
477
        try {
478
            nodeIdStr = PropertyService.getProperty("dataone.memberNodeId");
479
            nodeId = new NodeReference();
480
            nodeId.setValue(nodeIdStr);
481

    
482
        } catch (PropertyNotFoundException e1) {
483
            String msg = "Couldn't get dataone.memberNodeId property: " + e1.getMessage();
484
            failure = new ServiceFailure("2151", msg);
485
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
486
            logMetacat.error(msg);
487
            return true;
488

    
489
        }
490
        
491

    
492
        try {
493
            // do we already have a replica?
494
            try {
495
                localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
496

    
497
            } catch (McdbDocNotFoundException e) {
498
                logMetacat.info("No replica found. Continuing.");
499
                
500
            }
501

    
502
            String msg = "Can't read the object bytes properly, replica is invalid.";
503
            ServiceFailure serviceFailure = new ServiceFailure("2151", msg);
504
            
505
            try {
506
                object = MetacatHandler.read(localId);
507
            } catch (PropertyNotFoundException e) {
508
                setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, serviceFailure);  
509
                
510
            } catch (ParseLSIDException e) {
511
                setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, serviceFailure);  
512
                
513
            } catch (McdbException e) {
514
                setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, serviceFailure);  
515
                
516
            } catch (SQLException e) {
517
                setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, serviceFailure);  
518
                
519
            } catch (ClassNotFoundException e) {
520
                setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, serviceFailure);  
521
                
522
            } catch (IOException e) {
523
                setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, serviceFailure);  
524
               
525
            }
526

    
527
            // no local replica, get a replica
528
            if ( object == null ) {
529
                // session should be null to use the default certificate
530
                // location set in the Certificate manager
531
                object = mn.getReplica(thisNodeSession, pid);
532
                logMetacat.info("MNodeService.replicate() called for identifier "
533
                                + pid.getValue());
534

    
535
            }
536

    
537
        } catch (InvalidToken e) {            
538
            String msg = "Could not retrieve object to replicate (InvalidToken): "+ e.getMessage();
539
            failure = new ServiceFailure("2151", msg);
540
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
541
            logMetacat.error(msg);
542
            throw new ServiceFailure("2151", msg);
543

    
544
        } catch (NotFound e) {
545
            String msg = "Could not retrieve object to replicate (NotFound): "+ e.getMessage();
546
            failure = new ServiceFailure("2151", msg);
547
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
548
            logMetacat.error(msg);
549
            throw new ServiceFailure("2151", msg);
550

    
551
        }
552

    
553
        // verify checksum on the object, if supported
554
        if (object.markSupported()) {
555
            Checksum givenChecksum = sysmeta.getChecksum();
556
            Checksum computedChecksum = null;
557
            try {
558
                computedChecksum = ChecksumUtil.checksum(object,
559
                        givenChecksum.getAlgorithm());
560
                object.reset();
561
            } catch (Exception e) {
562
                String msg = "Error computing checksum on replica: "
563
                        + e.getMessage();
564
                ServiceFailure sf = new ServiceFailure("2151", msg);
565
                sf.initCause(e);
566
                throw sf;
567
            }
568
            if (!givenChecksum.getValue().equals(computedChecksum)) {
569
                throw new ServiceFailure("2151",
570
                        "Computed checksum does not match declared checksum");
571
            }
572
        }
573

    
574
        // add it to local store
575
        Identifier retPid;
576
        try {
577
            // skip the MN.create -- this mutates the system metadata and we
578
            // dont want it to
579
            if ( localId == null ) {
580
                
581
                retPid = super.create(session, pid, object, sysmeta);
582
                result = (retPid.getValue().equals(pid.getValue()));
583
            }
584
            
585
        } catch (InvalidToken e) {
586
            String msg = "Could not save object to local store (InvalidToken): " + e.getMessage();
587
            failure = new ServiceFailure("2151", msg);
588
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
589
            logMetacat.error(msg);
590
            throw new ServiceFailure("2151", msg);
591
        
592
        } catch (IdentifierNotUnique e) {
593
            String msg = "Could not save object to local store (IdentifierNotUnique): " + e.getMessage();
594
            failure = new ServiceFailure("2151", msg);
595
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
596
            logMetacat.error(msg);
597
            throw new ServiceFailure("2151", msg);
598
        
599
        } catch (InvalidSystemMetadata e) {
600
            String msg = "Could not save object to local store (InvalidSystemMetadata): " + e.getMessage();
601
            failure = new ServiceFailure("2151", msg);
602
            setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.FAILED, failure);
603
            logMetacat.error(msg);
604
            throw new ServiceFailure("2151", msg);
605
            
606
        }
607

    
608
        // finish by setting the replication status
609
        setReplicationStatus(thisNodeSession, pid, nodeId, ReplicationStatus.COMPLETED, null);
610
        return result;
611

    
612
    }
613

    
614
    /**
615
     * Return the object identified by the given object identifier
616
     * 
617
     * @param session - the Session object containing the credentials for the Subject
618
     * @param pid - the object identifier for the given object
619
     * 
620
     * @return inputStream - the input stream of the given object
621
     * 
622
     * @throws InvalidToken
623
     * @throws ServiceFailure
624
     * @throws NotAuthorized
625
     * @throws InvalidRequest
626
     * @throws NotImplemented
627
     */
628
    @Override
629
    public InputStream get(Session session, Identifier pid) 
630
    throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
631

    
632
        return super.get(session, pid);
633

    
634
    }
635

    
636
    /**
637
     * Returns a Checksum for the specified object using an accepted hashing algorithm
638
     * 
639
     * @param session - the Session object containing the credentials for the Subject
640
     * @param pid - the object identifier for the given object
641
     * @param algorithm -  the name of an algorithm that will be used to compute 
642
     *                     a checksum of the bytes of the object
643
     * 
644
     * @return checksum - the checksum of the given object
645
     * 
646
     * @throws InvalidToken
647
     * @throws ServiceFailure
648
     * @throws NotAuthorized
649
     * @throws NotFound
650
     * @throws InvalidRequest
651
     * @throws NotImplemented
652
     */
653
    @Override
654
    public Checksum getChecksum(Session session, Identifier pid, String algorithm) 
655
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
656
        InvalidRequest, NotImplemented {
657

    
658
        Checksum checksum = null;
659

    
660
        InputStream inputStream = get(session, pid);
661

    
662
        try {
663
            checksum = ChecksumUtil.checksum(inputStream, algorithm);
664

    
665
        } catch (NoSuchAlgorithmException e) {
666
            throw new ServiceFailure("1410", "The checksum for the object specified by " + pid.getValue() + "could not be returned due to an internal error: "
667
                    + e.getMessage());
668
        } catch (IOException e) {
669
            throw new ServiceFailure("1410", "The checksum for the object specified by " + pid.getValue() + "could not be returned due to an internal error: "
670
                    + e.getMessage());
671
        }
672

    
673
        if (checksum == null) {
674
            throw new ServiceFailure("1410", "The checksum for the object specified by " + pid.getValue() + "could not be returned.");
675
        }
676

    
677
        return checksum;
678
    }
679

    
680
    /**
681
     * Return the system metadata for a given object
682
     * 
683
     * @param session - the Session object containing the credentials for the Subject
684
     * @param pid - the object identifier for the given object
685
     * 
686
     * @return inputStream - the input stream of the given system metadata object
687
     * 
688
     * @throws InvalidToken
689
     * @throws ServiceFailure
690
     * @throws NotAuthorized
691
     * @throws NotFound
692
     * @throws InvalidRequest
693
     * @throws NotImplemented
694
     */
695
    @Override
696
    public SystemMetadata getSystemMetadata(Session session, Identifier pid) 
697
        throws InvalidToken, ServiceFailure, NotAuthorized, NotFound,
698
        NotImplemented {
699

    
700
        return super.getSystemMetadata(session, pid);
701
    }
702

    
703
    /**
704
     * Retrieve the list of objects present on the MN that match the calling parameters
705
     * 
706
     * @param session - the Session object containing the credentials for the Subject
707
     * @param startTime - Specifies the beginning of the time range from which 
708
     *                    to return object (>=)
709
     * @param endTime - Specifies the beginning of the time range from which 
710
     *                  to return object (>=)
711
     * @param objectFormat - Restrict results to the specified object format
712
     * @param replicaStatus - Indicates if replicated objects should be returned in the list
713
     * @param start - The zero-based index of the first value, relative to the 
714
     *                first record of the resultset that matches the parameters.
715
     * @param count - The maximum number of entries that should be returned in 
716
     *                the response. The Member Node may return less entries 
717
     *                than specified in this value.
718
     * 
719
     * @return objectList - the list of objects matching the criteria
720
     * 
721
     * @throws InvalidToken
722
     * @throws ServiceFailure
723
     * @throws NotAuthorized
724
     * @throws InvalidRequest
725
     * @throws NotImplemented
726
     */
727
    @Override
728
    public ObjectList listObjects(Session session, Date startTime, Date endTime, ObjectFormatIdentifier objectFormatId, Boolean replicaStatus, Integer start,
729
            Integer count) throws NotAuthorized, InvalidRequest, NotImplemented, ServiceFailure, InvalidToken {
730

    
731
        ObjectList objectList = null;
732

    
733
        try {
734
            objectList = IdentifierManager.getInstance().querySystemMetadata(startTime, endTime, objectFormatId, replicaStatus, start, count);
735
        } catch (Exception e) {
736
            throw new ServiceFailure("1580", "Error querying system metadata: " + e.getMessage());
737
        }
738

    
739
        return objectList;
740
    }
741

    
742
    /**
743
     * Return a description of the node's capabilities and services.
744
     * 
745
     * @return node - the technical capabilities of the Member Node
746
     * 
747
     * @throws ServiceFailure
748
     * @throws NotAuthorized
749
     * @throws InvalidRequest
750
     * @throws NotImplemented
751
     */
752
    @Override
753
    public Node getCapabilities() 
754
        throws NotImplemented, ServiceFailure {
755

    
756
        String nodeName = null;
757
        String nodeId = null;
758
        String subject = null;
759
        String nodeDesc = null;
760
        String nodeTypeString = null;
761
        NodeType nodeType = null;
762
        String mnCoreServiceVersion = null;
763
        String mnReadServiceVersion = null;
764
        String mnAuthorizationServiceVersion = null;
765
        String mnStorageServiceVersion = null;
766
        String mnReplicationServiceVersion = null;
767

    
768
        boolean nodeSynchronize = false;
769
        boolean nodeReplicate = false;
770
        boolean mnCoreServiceAvailable = false;
771
        boolean mnReadServiceAvailable = false;
772
        boolean mnAuthorizationServiceAvailable = false;
773
        boolean mnStorageServiceAvailable = false;
774
        boolean mnReplicationServiceAvailable = false;
775

    
776
        try {
777
            // get the properties of the node based on configuration information
778
            nodeName = PropertyService.getProperty("dataone.nodeName");
779
            nodeId = PropertyService.getProperty("dataone.memberNodeId");
780
            subject = PropertyService.getProperty("dataone.subject");
781
            nodeDesc = PropertyService.getProperty("dataone.nodeDescription");
782
            nodeTypeString = PropertyService.getProperty("dataone.nodeType");
783
            nodeType = NodeType.convert(nodeTypeString);
784
            nodeSynchronize = new Boolean(PropertyService.getProperty("dataone.nodeSynchronize")).booleanValue();
785
            nodeReplicate = new Boolean(PropertyService.getProperty("dataone.nodeReplicate")).booleanValue();
786

    
787
            mnCoreServiceVersion = PropertyService.getProperty("dataone.mnCore.serviceVersion");
788
            mnReadServiceVersion = PropertyService.getProperty("dataone.mnRead.serviceVersion");
789
            mnAuthorizationServiceVersion = PropertyService.getProperty("dataone.mnAuthorization.serviceVersion");
790
            mnStorageServiceVersion = PropertyService.getProperty("dataone.mnStorage.serviceVersion");
791
            mnReplicationServiceVersion = PropertyService.getProperty("dataone.mnReplication.serviceVersion");
792

    
793
            mnCoreServiceAvailable = new Boolean(PropertyService.getProperty("dataone.mnCore.serviceAvailable")).booleanValue();
794
            mnReadServiceAvailable = new Boolean(PropertyService.getProperty("dataone.mnRead.serviceAvailable")).booleanValue();
795
            mnAuthorizationServiceAvailable = new Boolean(PropertyService.getProperty("dataone.mnAuthorization.serviceAvailable")).booleanValue();
796
            mnStorageServiceAvailable = new Boolean(PropertyService.getProperty("dataone.mnStorage.serviceAvailable")).booleanValue();
797
            mnReplicationServiceAvailable = new Boolean(PropertyService.getProperty("dataone.mnReplication.serviceAvailable")).booleanValue();
798

    
799
            // Set the properties of the node based on configuration information and
800
            // calls to current status methods
801
            String serviceName = SystemUtil.getContextURL() + "/" + PropertyService.getProperty("dataone.serviceName");
802
            Node node = new Node();
803
            node.setBaseURL(serviceName + "/" + nodeTypeString);
804
            node.setDescription(nodeDesc);
805

    
806
            // set the node's health information
807
            node.setState(NodeState.UP);
808
            
809
            // set the ping response to the current value
810
            Ping canPing = new Ping();
811
            canPing.setSuccess(false);
812
            try {
813
            	Date pingDate = ping();
814
                canPing.setSuccess(pingDate != null);
815
            } catch (BaseException e) {
816
                e.printStackTrace();
817
                // guess it can't be pinged
818
            }
819
            
820
            node.setPing(canPing);
821

    
822
            NodeReference identifier = new NodeReference();
823
            identifier.setValue(nodeId);
824
            node.setIdentifier(identifier);
825
            Subject s = new Subject();
826
            s.setValue(subject);
827
            node.addSubject(s);
828
            node.setName(nodeName);
829
            node.setReplicate(nodeReplicate);
830
            node.setSynchronize(nodeSynchronize);
831

    
832
            // services: MNAuthorization, MNCore, MNRead, MNReplication, MNStorage
833
            Services services = new Services();
834

    
835
            Service sMNCore = new Service();
836
            sMNCore.setName("MNCore");
837
            sMNCore.setVersion(mnCoreServiceVersion);
838
            sMNCore.setAvailable(mnCoreServiceAvailable);
839

    
840
            Service sMNRead = new Service();
841
            sMNRead.setName("MNRead");
842
            sMNRead.setVersion(mnReadServiceVersion);
843
            sMNRead.setAvailable(mnReadServiceAvailable);
844

    
845
            Service sMNAuthorization = new Service();
846
            sMNAuthorization.setName("MNAuthorization");
847
            sMNAuthorization.setVersion(mnAuthorizationServiceVersion);
848
            sMNAuthorization.setAvailable(mnAuthorizationServiceAvailable);
849

    
850
            Service sMNStorage = new Service();
851
            sMNStorage.setName("MNStorage");
852
            sMNStorage.setVersion(mnStorageServiceVersion);
853
            sMNStorage.setAvailable(mnStorageServiceAvailable);
854

    
855
            Service sMNReplication = new Service();
856
            sMNReplication.setName("MNReplication");
857
            sMNReplication.setVersion(mnReplicationServiceVersion);
858
            sMNReplication.setAvailable(mnReplicationServiceAvailable);
859

    
860
            services.addService(sMNRead);
861
            services.addService(sMNCore);
862
            services.addService(sMNAuthorization);
863
            services.addService(sMNStorage);
864
            services.addService(sMNReplication);
865
            node.setServices(services);
866

    
867
            // Set the schedule for synchronization
868
            Synchronization synchronization = new Synchronization();
869
            Schedule schedule = new Schedule();
870
            Date now = new Date();
871
            schedule.setYear(PropertyService.getProperty("dataone.nodeSynchronization.schedule.year"));
872
            schedule.setMon(PropertyService.getProperty("dataone.nodeSynchronization.schedule.mon"));
873
            schedule.setMday(PropertyService.getProperty("dataone.nodeSynchronization.schedule.mday"));
874
            schedule.setWday(PropertyService.getProperty("dataone.nodeSynchronization.schedule.wday"));
875
            schedule.setHour(PropertyService.getProperty("dataone.nodeSynchronization.schedule.hour"));
876
            schedule.setMin(PropertyService.getProperty("dataone.nodeSynchronization.schedule.min"));
877
            schedule.setSec(PropertyService.getProperty("dataone.nodeSynchronization.schedule.sec"));
878
            synchronization.setSchedule(schedule);
879
            synchronization.setLastHarvested(now);
880
            synchronization.setLastCompleteHarvest(now);
881
            node.setSynchronization(synchronization);
882

    
883
            node.setType(nodeType);
884
            return node;
885

    
886
        } catch (PropertyNotFoundException pnfe) {
887
            String msg = "MNodeService.getCapabilities(): " + "property not found: " + pnfe.getMessage();
888
            logMetacat.error(msg);
889
            throw new ServiceFailure("2162", msg);
890
        }
891
    }
892

    
893
    /**
894
     * Returns the number of operations that have been serviced by the node 
895
     * over time periods of one and 24 hours.
896
     * 
897
     * @param session - the Session object containing the credentials for the Subject
898
     * @param period - An ISO8601 compatible DateTime range specifying the time 
899
     *                 range for which to return operation statistics.
900
     * @param requestor - Limit to operations performed by given requestor identity.
901
     * @param event -  Enumerated value indicating the type of event being examined
902
     * @param format - Limit to events involving objects of the specified format
903
     * 
904
     * @return the desired log records
905
     * 
906
     * @throws InvalidToken
907
     * @throws ServiceFailure
908
     * @throws NotAuthorized
909
     * @throws InvalidRequest
910
     * @throws NotImplemented
911
     */
912
    public MonitorList getOperationStatistics(Session session, Date startTime, 
913
        Date endTime, Subject requestor, Event event, ObjectFormatIdentifier formatId)
914
        throws NotImplemented, ServiceFailure, NotAuthorized, InsufficientResources, UnsupportedType {
915

    
916
        MonitorList monitorList = new MonitorList();
917

    
918
        try {
919

    
920
            // get log records first
921
            Log logs = getLogRecords(session, startTime, endTime, event, 0, null);
922

    
923
            // TODO: aggregate by day or hour -- needs clarification
924
            int count = 1;
925
            for (LogEntry logEntry : logs.getLogEntryList()) {
926
                Identifier pid = logEntry.getIdentifier();
927
                Date logDate = logEntry.getDateLogged();
928
                // if we are filtering by format
929
                if (formatId != null) {
930
                    SystemMetadata sysmeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
931
                    if (!sysmeta.getFormatId().getValue().equals(formatId.getValue())) {
932
                        // does not match
933
                        continue;
934
                    }
935
                }
936
                MonitorInfo item = new MonitorInfo();
937
                item.setCount(count);
938
                item.setDate(new java.sql.Date(logDate.getTime()));
939
                monitorList.addMonitorInfo(item);
940

    
941
            }
942
        } catch (Exception e) {
943
            e.printStackTrace();
944
            throw new ServiceFailure("2081", "Could not retrieve statistics: " + e.getMessage());
945
        }
946

    
947
        return monitorList;
948

    
949
    }
950

    
951
    /**
952
     * A callback method used by a CN to indicate to a MN that it cannot 
953
     * complete synchronization of the science metadata identified by pid.  Log
954
     * the event in the metacat event log.
955
     * 
956
     * @param session
957
     * @param syncFailed
958
     * 
959
     * @throws ServiceFailure
960
     * @throws NotAuthorized
961
     * @throws NotImplemented
962
     */
963
    @Override
964
    public void synchronizationFailed(Session session, SynchronizationFailed syncFailed) 
965
        throws NotImplemented, ServiceFailure, NotAuthorized {
966

    
967
        String localId;
968

    
969
        try {
970
            localId = IdentifierManager.getInstance().getLocalId(syncFailed.getPid());
971
        } catch (McdbDocNotFoundException e) {
972
            throw new ServiceFailure("2161", "The identifier specified by " + syncFailed.getPid() + " was not found on this node.");
973

    
974
        }
975
        // TODO: update the CN URL below when the CNRead.SynchronizationFailed
976
        // method is changed to include the URL as a parameter
977
        logMetacat.debug("Synchronization for the object identified by " + syncFailed.getPid() + " failed from " + syncFailed.getNodeId()
978
                + " Logging the event to the Metacat EventLog as a 'syncFailed' event.");
979
        // TODO: use the event type enum when the SYNCHRONIZATION_FAILED event is added
980
        String principal = Constants.SUBJECT_PUBLIC;
981
        if (session != null && session.getSubject() != null) {
982
          principal = session.getSubject().getValue();
983
        }
984
        try {
985
          EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), principal, localId, "synchronization_failed");
986
        } catch (Exception e) {
987
            throw new ServiceFailure("2161", "Could not log the error for: " + syncFailed.getPid());
988
    }
989
        //EventLog.getInstance().log("CN URL WILL GO HERE", 
990
        //  session.getSubject().getValue(), localId, Event.SYNCHRONIZATION_FAILED);
991

    
992
    }
993

    
994
    /**
995
     * Essentially a get() but with different logging behavior
996
     */
997
    @Override
998
    public InputStream getReplica(Session session, Identifier pid) 
999
        throws NotAuthorized, NotImplemented, ServiceFailure, InvalidToken {
1000

    
1001
        logMetacat.info("MNodeService.getReplica() called.");
1002

    
1003
        // cannot be called by public
1004
        if (session == null) {
1005
        	throw new InvalidToken("2183", "No session was provided.");
1006
        }
1007
        
1008
        logMetacat.info("MNodeService.getReplica() called with parameters: \n" +
1009
             "\tSession.Subject      = " + session.getSubject().getValue() + "\n" +
1010
             "\tIdentifier           = " + pid.getValue());
1011

    
1012
        InputStream inputStream = null; // bytes to be returned
1013
        handler = new MetacatHandler(new Timer());
1014
        boolean allowed = false;
1015
        String localId; // the metacat docid for the pid
1016

    
1017
        // get the local docid from Metacat
1018
        try {
1019
            localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1020
        } catch (McdbDocNotFoundException e) {
1021
            throw new ServiceFailure("2181", "The object specified by " + 
1022
                    pid.getValue() + " does not exist at this node.");
1023
            
1024
        }
1025

    
1026
        Subject targetNodeSubject = session.getSubject();
1027

    
1028
        // check for authorization to replicate, null session to act as this source MN
1029
        try {
1030
            allowed = D1Client.getCN().isNodeAuthorized(null, targetNodeSubject, pid);
1031
        } catch (InvalidToken e1) {
1032
            throw new ServiceFailure("2181", "Could not determine if node is authorized: " 
1033
                + e1.getMessage());
1034
            
1035
        } catch (NotFound e1) {
1036
            throw new ServiceFailure("2181", "Could not determine if node is authorized: " 
1037
                    + e1.getMessage());
1038

    
1039
        } catch (InvalidRequest e1) {
1040
            throw new ServiceFailure("2181", "Could not determine if node is authorized: " 
1041
                    + e1.getMessage());
1042

    
1043
        }
1044

    
1045
        logMetacat.info("Called D1Client.isNodeAuthorized(). Allowed = " + allowed +
1046
            " for identifier " + pid.getValue());
1047

    
1048
        // if the person is authorized, perform the read
1049
        if (allowed) {
1050
            try {
1051
                inputStream = handler.read(localId);
1052
            } catch (Exception e) {
1053
                throw new ServiceFailure("1020", "The object specified by " + 
1054
                    pid.getValue() + "could not be returned due to error: " + e.getMessage());
1055
            }
1056
        }
1057

    
1058
        // if we fail to set the input stream
1059
        if (inputStream == null) {
1060
            throw new ServiceFailure("2181", "The object specified by " + 
1061
                pid.getValue() + "does not exist at this node.");
1062
        }
1063

    
1064
        // log the replica event
1065
        String principal = null;
1066
        if (session.getSubject() != null) {
1067
            principal = session.getSubject().getValue();
1068
        }
1069
        EventLog.getInstance().log(request.getRemoteAddr(), 
1070
            request.getHeader("User-Agent"), principal, localId, "replicate");
1071

    
1072
        return inputStream;
1073
    }
1074

    
1075
    /**
1076
     * A method to notify the Member Node that the authoritative copy of 
1077
     * system metadata on the Coordinating Nodes has changed.
1078
     * 
1079
     * @param session   Session information that contains the identity of the 
1080
     *                  calling user as retrieved from the X.509 certificate 
1081
     *                  which must be traceable to the CILogon service.
1082
     * @param serialVersion   The serialVersion of the system metadata
1083
     * @param dateSysMetaLastModified  The time stamp for when the system metadata was changed
1084
     * @throws NotImplemented
1085
     * @throws ServiceFailure
1086
     * @throws NotAuthorized
1087
     * @throws InvalidRequest
1088
     * @throws InvalidToken
1089
     */
1090
    public void systemMetadataChanged(Session session, Identifier pid,
1091
        long serialVersion, Date dateSysMetaLastModified) 
1092
        throws NotImplemented, ServiceFailure, NotAuthorized, InvalidRequest,
1093
        InvalidToken {
1094
        
1095
        SystemMetadata currentLocalSysMeta = null;
1096
        SystemMetadata newSysMeta = null;
1097
        CNode cn = D1Client.getCN();
1098
        NodeList nodeList = null;
1099
        Subject callingSubject = null;
1100
        boolean allowed = false;
1101
        
1102
        // are we allowed to call this?
1103
        callingSubject = session.getSubject();
1104
        nodeList = cn.listNodes();
1105
        
1106
        for(Node node : nodeList.getNodeList()) {
1107
            // must be a CN
1108
            if ( node.getType().equals(NodeType.CN)) {
1109
               List<Subject> subjectList = node.getSubjectList();
1110
               // the calling subject must be in the subject list
1111
               if ( subjectList.contains(callingSubject)) {
1112
                   allowed = true;
1113
                   
1114
               }
1115
               
1116
            }
1117
        }
1118
        
1119
        if (!allowed ) {
1120
            String msg = "The subject identified by " + callingSubject.getValue() +
1121
              " is not authorized to call this service.";
1122
            throw new NotAuthorized("1331", msg);
1123
            
1124
        }
1125
        
1126
        // compare what we have locally to what is sent in the change notification
1127
        try {
1128
            currentLocalSysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
1129
             
1130
        } catch (RuntimeException e) {
1131
            String msg = "SystemMetadata for pid " + pid.getValue() +
1132
              " couldn't be updated because it couldn't be found locally: " +
1133
              e.getMessage();
1134
            logMetacat.error(msg);
1135
            ServiceFailure sf = new ServiceFailure("1333", msg);
1136
            sf.initCause(e);
1137
            throw sf; 
1138
        }
1139
        
1140
        if (currentLocalSysMeta.getSerialVersion().longValue() < serialVersion ) {
1141
            try {
1142
                newSysMeta = cn.getSystemMetadata(null, pid);
1143
            } catch (NotFound e) {
1144
                // huh? you just said you had it
1145
            	String msg = "On updating the local copy of system metadata " + 
1146
                "for pid " + pid.getValue() +", the CN reports it is not found." +
1147
                " The error message was: " + e.getMessage();
1148
                logMetacat.error(msg);
1149
                ServiceFailure sf = new ServiceFailure("1333", msg);
1150
                sf.initCause(e);
1151
                throw sf;
1152
            }
1153
            
1154
            // update the local copy of system metadata for the pid
1155
            try {
1156
                HazelcastService.getInstance().getSystemMetadataMap().put(newSysMeta.getIdentifier(), newSysMeta);
1157
                logMetacat.info("Updated local copy of system metadata for pid " +
1158
                    pid.getValue() + " after change notification from the CN.");
1159
                
1160
            } catch (RuntimeException e) {
1161
                String msg = "SystemMetadata for pid " + pid.getValue() +
1162
                  " couldn't be updated: " +
1163
                  e.getMessage();
1164
                logMetacat.error(msg);
1165
                ServiceFailure sf = new ServiceFailure("1333", msg);
1166
                sf.initCause(e);
1167
                throw sf;
1168
            }
1169
        }
1170
        
1171
    }
1172
    
1173
    /*
1174
     * Set the replication status for the object on the Coordinating Node
1175
     * 
1176
     * @param session - the session for the this target node
1177
     * @param pid - the identifier of the object being updated
1178
     * @param nodeId - the identifier of this target node
1179
     * @param status - the replication status to set
1180
     * @param failure - the exception to include, if any
1181
     */
1182
    private void setReplicationStatus(Session session, Identifier pid, 
1183
        NodeReference nodeId, ReplicationStatus status, BaseException failure) 
1184
        throws ServiceFailure, NotImplemented, NotAuthorized, 
1185
        InvalidRequest {
1186
        
1187
        // call the CN as the MN to set the replication status
1188
        try {
1189
            this.cn = D1Client.getCN();
1190
            this.cn.setReplicationStatus(session, pid, nodeId,
1191
                    status, failure);
1192
            
1193
        } catch (InvalidToken e) {
1194
            throw new ServiceFailure("2151",
1195
                    "Could not set the replication status on the CN (InvalidToken): " + 
1196
                    e.getMessage());
1197
            
1198
        } catch (NotFound e) {
1199
            throw new ServiceFailure("2151",
1200
                    "Could not set the replication status on the CN (NotFound): " + 
1201
                    e.getMessage());
1202
            
1203
        } catch (VersionMismatch e) {
1204
            throw new ServiceFailure("2151",
1205
                    "Could not set the replication status on the CN (VersionMismatch): " + 
1206
                    e.getMessage());
1207
            
1208
        }
1209

    
1210

    
1211
    }
1212
    
1213
}
(3-3/5)