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.FileInputStream;
30
import java.io.FileNotFoundException;
31
import java.io.InputStream;
32

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

    
40
import com.hazelcast.client.ClientConfig;
41
import com.hazelcast.client.HazelcastClient;
42
import com.hazelcast.config.Config;
43
import com.hazelcast.config.FileSystemXmlConfig;
44
import com.hazelcast.core.IMap;
45
import com.hazelcast.core.ISet;
46

    
47

    
48
/**
49
 * A factory to get distributed maps from the haszel cast client.
50
 * @author tao
51
 *
52
 */
53
public class DistributedMapsFactory {
54
    
55
    private static final String IDENTIFIERSETNAME = "hzIdentifiers";
56
    private static Log log = LogFactory.getLog(DistributedMapsFactory.class);
57
    
58
    private static HazelcastClient hzClient = null;
59
    private static String hzSystemMetadata = null;
60
    private static String hzObjectPath = null;
61
    private static int waitingTime = IndexGenerator.WAITTIME;
62
    private static int maxAttempts = IndexGenerator.MAXWAITNUMBER;
63
    private static IMap<Identifier, SystemMetadata> systemMetadataMap = null;
64
    private static IMap<Identifier, String> objectPathMap = null;
65
    /* The name of the identifiers set */
66
    private static String identifiersSetName = IDENTIFIERSETNAME;
67
    /* The Hazelcast distributed identifiers set */
68
    private static ISet<Identifier> identifiersSet = null;
69
    
70
    /*
71
     * Start the hazel cast client
72
     */
73
    private static void startHazelCastClient() throws FileNotFoundException, ServiceFailure{
74
        
75
        try {
76
            waitingTime = Settings.getConfiguration().getInt(IndexGenerator.WAITIMEPOPERTYNAME);
77
            maxAttempts = Settings.getConfiguration().getInt(IndexGenerator.MAXATTEMPTSPROPERTYNAME);
78
        } catch (Exception e) {
79
            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");
80
            waitingTime = IndexGenerator.WAITTIME;
81
            maxAttempts = IndexGenerator.MAXWAITNUMBER;
82
        }
83
        try {
84
            identifiersSetName = Settings.getConfiguration().getString("dataone.hazelcast.storageCluster.identifiersSet");
85
        } catch (Exception e) {
86
            log.warn("DistributedMapFactory.startHazelCastClient - couldn't read the name of the identifiersSet from the metacat.properties file since : "+e.getMessage()+". Default values will be used");
87
            identifiersSetName = IDENTIFIERSETNAME;
88
        }
89
     // get config values
90
        hzSystemMetadata = Settings.getConfiguration().getString(
91
                "dataone.hazelcast.storageCluster.systemMetadataMap");
92
        hzObjectPath = Settings.getConfiguration().getString(
93
                "dataone.hazelcast.storageCluster.objectPathMap");
94
        String configFileName = Settings.getConfiguration().getString(
95
                "dataone.hazelcast.configFilePath");;
96
        Config hzConfig = null;
97
        try {
98
            hzConfig = new FileSystemXmlConfig(configFileName);
99
        } catch (FileNotFoundException e) {
100
            log.error("could not load hazelcast configuration file from: " + configFileName, e);
101
            throw e;
102
        }
103
        
104
        String hzGroupName = hzConfig.getGroupConfig().getName();
105
        String hzGroupPassword = hzConfig.getGroupConfig().getPassword();
106
        String hzAddress = hzConfig.getNetworkConfig().getInterfaces().getInterfaces().iterator().next() + ":" + hzConfig.getNetworkConfig().getPort();
107

    
108
        log.info("starting index entry listener...");
109
        log.info("System Metadata value: " + hzSystemMetadata);
110
        log.info("Object path value: " + hzObjectPath);
111
        log.info("Group Name: " + hzGroupName);
112
        log.info("Group Password: " + "*****"); // don't show value
113
        log.info("HZ Address: " + hzAddress);
114

    
115
        // connect to the HZ cluster
116
        ClientConfig cc = new ClientConfig();
117
        cc.getGroupConfig().setName(hzGroupName);
118
        cc.getGroupConfig().setPassword(hzGroupPassword);
119
        cc.addAddress(hzAddress);
120
        
121
        int times = 0;
122
        while(true) {
123
            //System.out.println("here ==================");
124
            try {
125
                hzClient = HazelcastClient.newHazelcastClient(cc);
126
                break;
127
            } catch (Exception e) {
128
                    if(times <= maxAttempts) {
129
                        log.warn("DistributedMapFactory.startHazelCastClient - the hazelcast service is not ready : "
130
                                         +e.getMessage()+"\nWe will try to access it "+waitingTime/1000+" seconds later ");
131
                        try {
132
                            Thread.sleep(waitingTime);
133
                        } catch (Exception ee) {
134
                            log.warn("DistributedMapFactory.startHazelCastClient - the thread can't sleep for "+waitingTime/1000+" seconds to wait the hazelcast service");
135
                        }
136
                       
137
                    } else {
138
                        throw new ServiceFailure("0000", "DistributedMapFactory.startHazelCastClient - the hazelcast service is not ready even though Metacat-index wailted for "+
139
                                        maxAttempts*waitingTime/1000+" seconds. We can't get the system metadata from it and the building index can't happen this time");
140
                    }
141
             }
142
             times++;
143
             //System.out.println("here ==================2");
144
        }
145
        
146

    
147
    }
148
    
149
    /**
150
     * Get the system metadata map
151
     * @return
152
     * @throws FileNotFoundException
153
     * @throws ServiceFailure
154
     */
155
    public static IMap<Identifier, SystemMetadata> getSystemMetadataMap() throws FileNotFoundException, ServiceFailure {
156
        if(hzClient == null) {
157
            startHazelCastClient();
158
        }
159
        systemMetadataMap = hzClient.getMap(hzSystemMetadata);
160
        return systemMetadataMap;
161
    }
162
    
163
    /**
164
     * Get the distributed object path map from the haszel cast client.
165
     * @return
166
     * @throws FileNotFoundException
167
     * @throws ServiceFailure
168
     */
169
    public static IMap<Identifier, String> getObjectPathMap() throws FileNotFoundException, ServiceFailure {
170
        if(hzClient == null) {
171
            startHazelCastClient();
172
        }
173
        objectPathMap = hzClient.getMap(hzObjectPath);
174
        return objectPathMap;
175
    }
176
    
177
    /**
178
     * Get the SystemMetadata for the specified id. The null will be returned if there is no SystemMetadata found for this id
179
     * @param id the specified id.
180
     * @return the SystemMetadata for the id
181
     * @throws FileNotFoundException
182
     * @throws ServiceFailure
183
     */
184
    public static SystemMetadata getSystemMetadata(String id) throws FileNotFoundException, ServiceFailure {
185
        if(systemMetadataMap == null) {
186
            getSystemMetadataMap();
187
        }
188
        SystemMetadata metadata = null;
189
        if(systemMetadataMap != null && id != null) {
190
            Identifier identifier = new Identifier();
191
            identifier.setValue(id);
192
            metadata = systemMetadataMap.get(identifier);
193
        }
194
        return metadata;
195
    }
196
    
197
    /**
198
     * Get the DataObject for the specified id. The null will be returned if not data object is found.
199
     * @param id the specified id
200
     * @return the InputStream of the data object for the specified id.
201
     * @throws FileNotFoundException
202
     * @throws ServiceFailure
203
     */
204
    public static InputStream getDataObject(String id) throws FileNotFoundException, ServiceFailure {
205
        if(objectPathMap == null) {
206
            getObjectPathMap();
207
        }
208
        InputStream data = null;
209
        if(objectPathMap != null && id != null) {
210
            Identifier identifier = new Identifier();
211
            identifier.setValue(id);
212
            String objectPath = objectPathMap.get(identifier);
213
            if(objectPath != null) {
214
                data = new FileInputStream(objectPath);
215
            }
216
        }
217
        return data;
218
    }
219
    
220
    /**
221
     * Get the identifiers set in the hazelcast client. The null may be returned if we can't find the set.
222
     * @return the identifiersSet
223
     * @throws FileNotFoundException
224
     * @throws ServiceFailure
225
     */
226
    public static ISet<Identifier> getIdentifiersSet() throws FileNotFoundException, ServiceFailure {
227
        if(hzClient== null) {
228
            startHazelCastClient();
229
        }
230
        identifiersSet= hzClient.getSet(identifiersSetName);
231
        return identifiersSet;
232
    }
233
    
234
}
(2-2/6)