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 String hzIndexQueue = null;
62
    private static int waitingTime = IndexGenerator.WAITTIME;
63
    private static int maxAttempts = IndexGenerator.MAXWAITNUMBER;
64
    private static IMap<Identifier, SystemMetadata> systemMetadataMap = null;
65
    private static IMap<Identifier, String> objectPathMap = null;
66
    private static ISet<SystemMetadata> indexQueue = null;
67
    /* The name of the identifiers set */
68
    private static String identifiersSetName = IDENTIFIERSETNAME;
69
    /* The Hazelcast distributed identifiers set */
70
    private static ISet<Identifier> identifiersSet = null;
71
    
72
    /*
73
     * Start the hazel cast client
74
     */
75
    private static void startHazelCastClient() throws FileNotFoundException, ServiceFailure{
76
        
77
        try {
78
            waitingTime = Settings.getConfiguration().getInt(IndexGenerator.WAITIMEPOPERTYNAME);
79
            maxAttempts = Settings.getConfiguration().getInt(IndexGenerator.MAXATTEMPTSPROPERTYNAME);
80
        } catch (Exception e) {
81
            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");
82
            waitingTime = IndexGenerator.WAITTIME;
83
            maxAttempts = IndexGenerator.MAXWAITNUMBER;
84
        }
85
        try {
86
            identifiersSetName = Settings.getConfiguration().getString("dataone.hazelcast.storageCluster.identifiersSet");
87
        } catch (Exception e) {
88
            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");
89
            identifiersSetName = IDENTIFIERSETNAME;
90
        }
91
        
92
        // the index queue name to listen to
93
        hzIndexQueue = Settings.getConfiguration().getString("index.hazelcast.indexqueue");        
94
        
95
        // get config values
96
        hzSystemMetadata = Settings.getConfiguration().getString(
97
                "dataone.hazelcast.storageCluster.systemMetadataMap");
98
        hzObjectPath = Settings.getConfiguration().getString(
99
                "dataone.hazelcast.storageCluster.objectPathMap");
100
        String configFileName = Settings.getConfiguration().getString(
101
                "dataone.hazelcast.configFilePath");;
102
        Config hzConfig = null;
103
        try {
104
            hzConfig = new FileSystemXmlConfig(configFileName);
105
        } catch (FileNotFoundException e) {
106
            log.error("could not load hazelcast configuration file from: " + configFileName, e);
107
            throw e;
108
        }
109
        
110
        String hzGroupName = hzConfig.getGroupConfig().getName();
111
        String hzGroupPassword = hzConfig.getGroupConfig().getPassword();
112
        String hzAddress = hzConfig.getNetworkConfig().getInterfaces().getInterfaces().iterator().next() + ":" + hzConfig.getNetworkConfig().getPort();
113

    
114
        log.info("starting index entry listener...");
115
        log.info("System Metadata value: " + hzSystemMetadata);
116
        log.info("Object path value: " + hzObjectPath);
117
        log.info("Group Name: " + hzGroupName);
118
        log.info("Group Password: " + "*****"); // don't show value
119
        log.info("HZ Address: " + hzAddress);
120

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

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