Project

General

Profile

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
    private static int waitingTime = IndexGenerator.WAITTIME;
58
    private static int maxAttempts = IndexGenerator.MAXWAITNUMBER;
59
    
60
    /*
61
     * Start the hazel cast client
62
     */
63
    private static void startHazelCastClient() throws FileNotFoundException, ServiceFailure{
64
        
65
        try {
66
            waitingTime = Settings.getConfiguration().getInt(IndexGenerator.WAITIMEPOPERTYNAME);
67
            maxAttempts = Settings.getConfiguration().getInt(IndexGenerator.MAXATTEMPTSPROPERTYNAME);
68
        } catch (Exception e) {
69
            log.warn("DistributedMapFactory.startHazelCastClient - couldn't read the waiting time or maxattempts from the metacat.properties file since : "+e.getMessage()+". Default values will be used");
70
            waitingTime = IndexGenerator.WAITTIME;
71
            maxAttempts = IndexGenerator.MAXWAITNUMBER;
72
        }
73
     // get config values
74
        hzSystemMetadata = Settings.getConfiguration().getString(
75
                "dataone.hazelcast.storageCluster.systemMetadataMap");
76
        hzObjectPath = Settings.getConfiguration().getString(
77
                "dataone.hazelcast.storageCluster.objectPathMap");
78
        String configFileName = Settings.getConfiguration().getString(
79
                "dataone.hazelcast.configFilePath");;
80
        Config hzConfig = null;
81
        try {
82
            hzConfig = new FileSystemXmlConfig(configFileName);
83
        } catch (FileNotFoundException e) {
84
            log.error("could not load hazelcast configuration file from: " + configFileName, e);
85
            throw e;
86
        }
87
        
88
        String hzGroupName = hzConfig.getGroupConfig().getName();
89
        String hzGroupPassword = hzConfig.getGroupConfig().getPassword();
90
        String hzAddress = hzConfig.getNetworkConfig().getInterfaces().getInterfaces().iterator().next() + ":" + hzConfig.getNetworkConfig().getPort();
91

    
92
        log.info("starting index entry listener...");
93
        log.info("System Metadata value: " + hzSystemMetadata);
94
        log.info("Object path value: " + hzObjectPath);
95
        log.info("Group Name: " + hzGroupName);
96
        log.info("Group Password: " + "*****"); // don't show value
97
        log.info("HZ Address: " + hzAddress);
98

    
99
        // connect to the HZ cluster
100
        ClientConfig cc = new ClientConfig();
101
        cc.getGroupConfig().setName(hzGroupName);
102
        cc.getGroupConfig().setPassword(hzGroupPassword);
103
        cc.addAddress(hzAddress);
104
        
105
        int times = 0;
106
        while(true) {
107
            //System.out.println("here ==================");
108
            try {
109
                hzClient = HazelcastClient.newHazelcastClient(cc);
110
                break;
111
            } catch (Exception e) {
112
                    if(times <= maxAttempts) {
113
                        log.warn("DistributedMapFactory.startHazelCastClient - the hazelcast service is not ready : "
114
                                         +e.getMessage()+"\nWe will try to access it "+waitingTime/1000+" seconds later ");
115
                        try {
116
                            Thread.sleep(waitingTime);
117
                        } catch (Exception ee) {
118
                            log.warn("DistributedMapFactory.startHazelCastClient - the thread can't sleep for "+waitingTime/1000+" seconds to wait the hazelcast service");
119
                        }
120
                       
121
                    } else {
122
                        throw new ServiceFailure("0000", "DistributedMapFactory.startHazelCastClient - the hazelcast service is not ready even though Metacat-index wailted for "+
123
                                        maxAttempts*waitingTime/1000+" seconds. We can't get the system metadata from it and the building index can't happen this time");
124
                    }
125
             }
126
             times++;
127
             //System.out.println("here ==================2");
128
        }
129
        
130

    
131
    }
132
    
133
    /**
134
     * Get the system metadata map
135
     * @return
136
     * @throws FileNotFoundException
137
     * @throws ServiceFailure
138
     */
139
    public static IMap<Identifier, SystemMetadata> getSystemMetadataMap() throws FileNotFoundException, ServiceFailure {
140
        if(hzClient == null) {
141
            startHazelCastClient();
142
        }
143
        return hzClient.getMap(hzSystemMetadata);
144
    }
145
    
146
    /**
147
     * Get the distributed object path map from the haszel cast client.
148
     * @return
149
     * @throws FileNotFoundException
150
     * @throws ServiceFailure
151
     */
152
    public static IMap<Identifier, String> getObjectPathMap() throws FileNotFoundException, ServiceFailure {
153
        if(hzClient == null) {
154
            startHazelCastClient();
155
        }
156
        return hzClient.getMap(hzObjectPath);
157
    }
158
    
159
}
(2-2/6)