Project

General

Profile

« Previous | Next » 

Revision 7689

Added by Jing Tao over 11 years ago

Add a class to get distributed maps such as the system metadata map.

View differences:

metacat-index/src/test/java/edu/ucsb/nceas/metacat/index/DistributedMapsFactoryIT.java
1
package edu.ucsb.nceas.metacat.index;
2

  
3
import static org.junit.Assert.assertTrue;
4

  
5
import org.dataone.service.types.v1.Identifier;
6
import org.dataone.service.types.v1.SystemMetadata;
7
import org.junit.Test;
8

  
9
import com.hazelcast.core.IMap;
10

  
11
public class DistributedMapsFactoryIT {
12
    /**
13
     * Test to get the system metadata map
14
     */
15
    @Test
16
    public void testGetSystemMetadataMap() throws Exception {
17
        IMap<Identifier, SystemMetadata> map = DistributedMapsFactory.getSystemMetadataMap();
18
        System.out.println("the size of the map is "+map.size());
19
        assertTrue("The size of the system metadata map should equal or be greater than 0", map.size() >= 0);
20
    }
21
    
22
    /**
23
     * Test to get the system metadata map
24
     */
25
    @Test
26
    public void testGetObjectPathMap() throws Exception {
27
        IMap<Identifier, String> map = DistributedMapsFactory.getObjectPathMap();
28
        System.out.println("the size of the map is "+map.size());
29
        assertTrue("The size of the object path map should equal or be greater than 0", map.size() >= 0);
30
    }
31
}
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/DistributedMapsFactory.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class that gets Accession Number, check for uniqueness
4
 *             and register it into db
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Jivka Bojilova, Matt Jones
8
 *
9
 *   '$Author: leinfelder $'
10
 *     '$Date: 2011-11-02 20:40:12 -0700 (Wed, 02 Nov 2011) $'
11
 * '$Revision: 6595 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
package edu.ucsb.nceas.metacat.index;
28

  
29
import java.io.FileNotFoundException;
30

  
31
import org.apache.commons.logging.Log;
32
import org.apache.commons.logging.LogFactory;
33
import org.dataone.configuration.Settings;
34
import org.dataone.service.exceptions.ServiceFailure;
35
import org.dataone.service.types.v1.Identifier;
36
import org.dataone.service.types.v1.SystemMetadata;
37

  
38
import com.hazelcast.client.ClientConfig;
39
import com.hazelcast.client.HazelcastClient;
40
import com.hazelcast.config.Config;
41
import com.hazelcast.config.FileSystemXmlConfig;
42
import com.hazelcast.core.IMap;
43

  
44

  
45
/**
46
 * A factory to get distributed maps from the haszel cast client.
47
 * @author tao
48
 *
49
 */
50
public class DistributedMapsFactory {
51
    
52
    private static Log log = LogFactory.getLog(DistributedMapsFactory.class);
53
    
54
    private static HazelcastClient hzClient = null;
55
    private static String hzSystemMetadata = null;
56
    private static String hzObjectPath = null;
57
    
58
    /*
59
     * Start the hazel cast client
60
     */
61
    private static void startHazelCastClient() throws FileNotFoundException, ServiceFailure{
62
     // get config values
63
        hzSystemMetadata = Settings.getConfiguration().getString(
64
                "dataone.hazelcast.storageCluster.systemMetadataMap");
65
        hzObjectPath = Settings.getConfiguration().getString(
66
                "dataone.hazelcast.storageCluster.objectPathMap");
67
        String configFileName = Settings.getConfiguration().getString(
68
                "dataone.hazelcast.configFilePath");;
69
        Config hzConfig = null;
70
        try {
71
            hzConfig = new FileSystemXmlConfig(configFileName);
72
        } catch (FileNotFoundException e) {
73
            log.error("could not load hazelcast configuration file from: " + configFileName, e);
74
            throw e;
75
        }
76
        
77
        String hzGroupName = hzConfig.getGroupConfig().getName();
78
        String hzGroupPassword = hzConfig.getGroupConfig().getPassword();
79
        String hzAddress = hzConfig.getNetworkConfig().getInterfaces().getInterfaces().iterator().next() + ":" + hzConfig.getNetworkConfig().getPort();
80

  
81
        log.info("starting index entry listener...");
82
        log.info("System Metadata value: " + hzSystemMetadata);
83
        log.info("Object path value: " + hzObjectPath);
84
        log.info("Group Name: " + hzGroupName);
85
        log.info("Group Password: " + "*****"); // don't show value
86
        log.info("HZ Address: " + hzAddress);
87

  
88
        // connect to the HZ cluster
89
        ClientConfig cc = new ClientConfig();
90
        cc.getGroupConfig().setName(hzGroupName);
91
        cc.getGroupConfig().setPassword(hzGroupPassword);
92
        cc.addAddress(hzAddress);
93
        try {
94
            hzClient = HazelcastClient.newHazelcastClient(cc);
95
        } catch (Exception e) {
96
            log.error("Unable to create hazelcast client: ", e);
97
            throw new ServiceFailure("0000","DistributedMapsFactory.startHazelCastClient - can't start the hazel cast client since "+e.getMessage());
98
        }
99
    }
100
    
101
    /**
102
     * Get the system metadata map
103
     * @return
104
     * @throws FileNotFoundException
105
     * @throws ServiceFailure
106
     */
107
    public static IMap<Identifier, SystemMetadata> getSystemMetadataMap() throws FileNotFoundException, ServiceFailure {
108
        if(hzClient == null) {
109
            startHazelCastClient();
110
        }
111
        return hzClient.getMap(hzSystemMetadata);
112
    }
113
    
114
    /**
115
     * Get the distributed object path map from the haszel cast client.
116
     * @return
117
     * @throws FileNotFoundException
118
     * @throws ServiceFailure
119
     */
120
    public static IMap<Identifier, String> getObjectPathMap() throws FileNotFoundException, ServiceFailure {
121
        if(hzClient == null) {
122
            startHazelCastClient();
123
        }
124
        return hzClient.getMap(hzObjectPath);
125
    }
126
    
127
}

Also available in: Unified diff