Project

General

Profile

« Previous | Next » 

Revision 9291

Added by Jing Tao over 8 years ago

A new class is used to get the version of service for a given node.

View differences:

src/edu/ucsb/nceas/metacat/dataone/D1NodeVersionChecker.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000-2015 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
package edu.ucsb.nceas.metacat.dataone;
24

  
25
import java.util.List;
26

  
27
import org.dataone.client.v2.CNode;
28
import org.dataone.client.v2.itk.D1Client;
29
import org.dataone.service.exceptions.InvalidRequest;
30
import org.dataone.service.exceptions.NotImplemented;
31
import org.dataone.service.exceptions.ServiceFailure;
32
import org.dataone.service.types.v1.Node;
33
import org.dataone.service.types.v1.NodeList;
34
import org.dataone.service.types.v1.NodeReference;
35
import org.dataone.service.types.v1.Service;
36
import org.dataone.service.types.v1.Services;
37
/**
38
 * This class will contact the CN to figure out the version of the service
39
 * for the given node
40
 * @author tao
41
 *
42
 */
43
public class D1NodeVersionChecker {
44
    
45
    public static final String V1 = "v1";
46
    public static final String V2 = "v2";
47
    public static final String HIGHESTVERSION = V2;
48
    public static final String SECONDHIGHESTVERSION = V1;
49
    private NodeReference nodeId = null;
50
    
51
    /**
52
     * Constructor
53
     * @param nodeId
54
     */
55
    public D1NodeVersionChecker(NodeReference nodeId) {
56
        this.nodeId = nodeId;
57
    }
58
    
59
    
60
    /**
61
     * Get the version of the service name for the node.
62
     * If we can't find the service for the node, a NotFound exception will return.
63
     * The null maybe return.
64
     * @param serviceName
65
     * @return the version of the service
66
     * @throws NotImplemented 
67
     * @throws ServiceFailure 
68
     */
69
    public String getVersion(String serviceName) throws ServiceFailure, NotImplemented {
70
        String version = null;
71
        if(nodeId != null && serviceName != null) {
72
            Node node = null;
73
            CNode cnV2 = D1Client.getCN();
74
            try {
75
                //try v2  first.
76
                node = cnV2.getNodeCapabilities(nodeId);
77
            } catch (Exception e) {
78
                //try v1 api
79
                org.dataone.client.v1.CNode cnV1 = org.dataone.client.v1.itk.D1Client.getCN();
80
                NodeList nodeList = cnV1.listNodes();
81
                if(nodeList != null) {
82
                    List<Node> list =nodeList.getNodeList();
83
                    if(list != null) {
84
                        for(Node node1 : list) {
85
                            if(node1 != null && node1.getIdentifier() != null && node1.getIdentifier().equals(nodeId)) {
86
                                node = node1;
87
                                break;
88
                            }
89
                        }
90
                    }
91
                }
92
            }
93
            if(node != null) {
94
                Services services = node.getServices();
95
                if(services != null) {
96
                   
97
                   List<Service> list = services.getServiceList();
98
                   if(list != null) {
99
                       for(Service service : list) {
100
                           if(service != null && service.getName() != null && service.getName().equals(serviceName) && 
101
                                   service.getVersion() != null && service.getVersion().equalsIgnoreCase(HIGHESTVERSION) && service.getAvailable() == true ) {
102
                               version = HIGHESTVERSION;
103
                               break;
104
                           }  else if(service != null && service.getName() != null && service.getName().equals(serviceName) && 
105
                                   service.getVersion() != null && service.getVersion().equalsIgnoreCase(SECONDHIGHESTVERSION) && service.getAvailable() == true ) {
106
                               version = SECONDHIGHESTVERSION;
107
                           } 
108
                       }
109
                   }
110
                }
111
            }
112
        }
113
        return version;
114
    }
115
}

Also available in: Unified diff