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
import java.util.ArrayList;
33
import java.util.Calendar;
34
import java.util.Date;
35
import java.util.List;
36
import java.util.TimerTask;
37

    
38
import org.apache.commons.logging.Log;
39
import org.apache.commons.logging.LogFactory;
40
import org.apache.solr.client.solrj.SolrServerException;
41
import org.dataone.configuration.Settings;
42
import org.dataone.service.exceptions.InvalidRequest;
43
import org.dataone.service.exceptions.InvalidToken;
44
import org.dataone.service.exceptions.NotAuthorized;
45
import org.dataone.service.exceptions.NotImplemented;
46
import org.dataone.service.exceptions.ServiceFailure;
47
import org.dataone.service.types.v1.Event;
48
import org.dataone.service.types.v1.Identifier;
49
import org.dataone.service.types.v1.ObjectFormatIdentifier;
50
import org.dataone.service.types.v1.SystemMetadata;
51

    
52
import com.hazelcast.core.IMap;
53
import com.hazelcast.core.ISet;
54

    
55
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
56
import edu.ucsb.nceas.metacat.index.event.EventlogFactory;
57
import edu.ucsb.nceas.metacat.index.event.IndexEventLogException;
58

    
59

    
60
/**
61
 * A class represents the object to generate massive solr indexes.
62
 * This can happen during an update of Metacat (generating index for all existing documents)
63
 * or regenerate index for those documents
64
 * failing to build index during the insert or update.
65
 * 
66
 * @author tao
67
 *
68
 */
69
public class IndexGenerator extends TimerTask {
70
    
71
    private static final int FIRST =0;
72
    private static final int SECOND =1;
73
    public static final int WAITTIME = 10000;
74
    public static final int MAXWAITNUMBER = 180;
75
    private static final String HTTP = "http://";
76
    private static final String MNAPPENDIX = "/d1/mn";
77
    private static final String RESOURCEMAPPROPERYNAME = "index.resourcemap.namespace";
78
    public static final String WAITIMEPOPERTYNAME = "index.regenerate.start.waitingtime";
79
    public static final String MAXATTEMPTSPROPERTYNAME = "index.regenerate.start.maxattempts";
80
    
81
    private static int waitingTime = WAITTIME;
82
    private static int maxAttempts = MAXWAITNUMBER;
83
    
84
    private SolrIndex solrIndex = null;
85
    //private SystemMetadataEventListener systemMetadataListener = null;
86
    private IMap<Identifier, SystemMetadata> systemMetadataMap;
87
    private IMap<Identifier, String> objectPathMap;
88
    private Log log = LogFactory.getLog(IndexGenerator.class);
89
    //private MNode mNode = null;
90
    private static List<String> resourceMapNamespaces = null;
91
    
92
    /**
93
     * Constructor
94
     * @param solrIndex
95
     * @param systemMetadataListener
96
     */
97
    public IndexGenerator(SolrIndex solrIndex) {
98
        this.solrIndex = solrIndex;
99
        resourceMapNamespaces = Settings.getConfiguration().getList(RESOURCEMAPPROPERYNAME);
100
        //this.systemMetadataListener = systemMetadataListener;
101
        //this.mNode = new MNode(buildMNBaseURL());
102
        try {
103
            waitingTime = Settings.getConfiguration().getInt(WAITIMEPOPERTYNAME);
104
            maxAttempts = Settings.getConfiguration().getInt(MAXATTEMPTSPROPERTYNAME);
105
        } catch (Exception e) {
106
            log.warn("IndexGenerator.constructor - couldn't read the waiting time or maxattempts from the metacat.properties file since : "+e.getMessage()+". Default values will be used");
107
            waitingTime = WAITTIME;
108
            maxAttempts = MAXWAITNUMBER;
109
        }
110
    }
111
    
112
    /**
113
     * Build the index for all documents in Metacat without overwriting.
114
     * @throws SolrServerException 
115
     * @throws ServiceFailure 
116
     * @throws NotImplemented 
117
     * @throws NotAuthorized 
118
     * @throws InvalidToken 
119
     * @throws InvalidRequest 
120
     * @throws IndexEventLogException 
121
     * @throws IllegalAccessException 
122
     * @throws InstantiationException 
123
     * @throws ClassNotFoundException 
124
     */
125
    /*public void indexAll() throws InvalidRequest, InvalidToken, NotAuthorized, 
126
                            NotImplemented, ServiceFailure, SolrServerException, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException {
127
        boolean force = false;
128
        indexAll(force);
129
    }*/
130
    
131
    /**
132
     * Build the index for all documents.
133
     * @throws SolrServerException 
134
     * @throws ServiceFailure 
135
     * @throws NotImplemented 
136
     * @throws NotAuthorized 
137
     * @throws InvalidToken 
138
     * @throws InvalidRequest 
139
     * @throws IndexEventLogException 
140
     * @throws IllegalAccessException 
141
     * @throws InstantiationException 
142
     * @throws ClassNotFoundException 
143
     */
144
    public void indexAll() throws InvalidRequest, InvalidToken,
145
                NotAuthorized, NotImplemented, ServiceFailure, SolrServerException, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException {
146
        Date since = null;
147
        Date until = null;
148
        index(since, until);
149
    }
150
    
151
    /**
152
     * Build the index for the docs which have been modified since the specified date.
153
     * @param since
154
     * @throws SolrServerException 
155
     * @throws ServiceFailure 
156
     * @throws NotImplemented 
157
     * @throws NotAuthorized 
158
     * @throws InvalidToken 
159
     * @throws InvalidRequest 
160
     * @throws IndexEventLogException 
161
     * @throws IllegalAccessException 
162
     * @throws InstantiationException 
163
     * @throws ClassNotFoundException 
164
     */
165
    public void index(Date since) throws InvalidRequest, InvalidToken, 
166
                    NotAuthorized, NotImplemented, ServiceFailure, SolrServerException, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException {
167
        Date until = null;
168
        index(since, until);
169
    }
170
    
171
    /**
172
     *  Build the index for the docs which have been modified between the specified date.s
173
     * @param since
174
     * @param until
175
     * @throws SolrServerException 
176
     * @throws ServiceFailure 
177
     * @throws NotImplemented 
178
     * @throws NotAuthorized 
179
     * @throws InvalidToken 
180
     * @throws InvalidRequest 
181
     * @throws FileNotFoundException 
182
     * @throws IndexEventLogException 
183
     * @throws IllegalAccessException 
184
     * @throws InstantiationException 
185
     * @throws ClassNotFoundException 
186
     */
187
    public void index(Date since, Date until) throws SolrServerException, InvalidRequest, 
188
                                                InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, IndexEventLogException {
189
        Date processedDate = null;
190
        List<String> solrIds = null;
191
        initSystemMetadataMap();
192
        initObjectPathMap();
193
        List[] metacatIds = getMetacatIds(since, until);
194
        List<String> otherMetacatIds = metacatIds[FIRST];
195
        List<String> resourceMapIds =  metacatIds[SECOND];
196
        
197
        //figure out the procesedDate by comparing the last element of otherMetacatIds and resourceMapIds.
198
        Date latestOtherId = null;
199
        if (otherMetacatIds != null && !otherMetacatIds.isEmpty()) {
200
            int size = otherMetacatIds.size();
201
            String id = otherMetacatIds.get(size-1);
202
            SystemMetadata sysmeta = getSystemMetadata(id);
203
            latestOtherId = sysmeta.getDateSysMetadataModified();
204
        }
205
        Date latestResourceId = null;
206
        if (resourceMapIds != null && !resourceMapIds.isEmpty()) {
207
            int size = resourceMapIds.size();
208
            String id = resourceMapIds.get(size-1);
209
            SystemMetadata sysmeta = getSystemMetadata(id);
210
            latestResourceId = sysmeta.getDateSysMetadataModified();
211
        }
212
        if(latestOtherId != null && latestResourceId != null && latestOtherId.getTime() > latestResourceId.getTime()) {
213
            processedDate = latestOtherId;
214
        } else if (latestOtherId != null && latestResourceId != null && latestOtherId.getTime()  <= latestResourceId.getTime()) {
215
            processedDate = latestResourceId;
216
        } else if (latestOtherId == null && latestResourceId != null) {
217
            processedDate = latestResourceId;
218
        } else if (latestOtherId != null && latestResourceId == null) {
219
            processedDate = latestOtherId;
220
        }
221
        
222
        //add the failedPids 
223
        List<IndexEvent> failedEvents = EventlogFactory.createIndexEventLog().getEvents(null, null, null, null);
224
        List<IndexEvent> failedOtherIds = new ArrayList<IndexEvent>();
225
        List<IndexEvent> failedResourceMapIds = new ArrayList<IndexEvent>();
226
        if(failedEvents != null) {
227
            for(IndexEvent event : failedEvents) {
228
            	String id = event.getIdentifier().getValue();
229
                SystemMetadata sysmeta = getSystemMetadata(id);
230
                if(sysmeta != null) {
231
                    ObjectFormatIdentifier formatId =sysmeta.getFormatId();
232
                    if(formatId != null && formatId.getValue() != null && resourceMapNamespaces != null && isResourceMap(formatId)) {
233
                        failedResourceMapIds.add(event);
234
                    } else {
235
                        failedOtherIds.add(event);
236
                    }
237
                }
238
            }
239
        }
240
        indexFailedIds(failedOtherIds);
241
        indexFailedIds(failedResourceMapIds);
242
        
243
        /*if(!failedOtherIds.isEmpty()) {
244
            failedOtherIds.addAll(otherMetacatIds);
245
        } else {
246
            failedOtherIds = otherMetacatIds;
247
        }
248
        
249
        if(!failedResourceMapIds.isEmpty()) {
250
            failedResourceMapIds.addAll(resourceMapIds);
251
        } else {
252
            failedResourceMapIds = resourceMapIds;
253
        }*/
254
        
255
        log.info("the metacat ids (except the resource map ids)-----------------------------"+otherMetacatIds);
256
        log.info("the metacat resroucemap ids -----------------------------"+resourceMapIds);
257
        index(otherMetacatIds);
258
        index(resourceMapIds);
259
       
260
        //record the timed index.
261
        if(processedDate != null) {
262
            EventlogFactory.createIndexEventLog().setLastProcessDate(processedDate);
263
        }
264
        
265
    }
266
    
267
    /*
268
     * Doing index
269
     */
270
    private void index(List<String> metacatIds) {
271
        if(metacatIds != null) {
272
            for(String metacatId : metacatIds) {
273
                if(metacatId != null) {
274
                        try {
275
                            generateIndex(metacatId);
276
                        } catch (Exception e) {
277
                            IndexEvent event = new IndexEvent();
278
                            Identifier pid = new Identifier();
279
                            pid.setValue(metacatId);
280
                            event.setIdentifier(pid);
281
                            event.setDate(Calendar.getInstance().getTime());
282
                            event.setAction(Event.CREATE);
283
                            String error = "IndexGenerator.index - Metacat Index couldn't generate the index for the id - "+metacatId+" because "+e.getMessage();
284
                            event.setDescription(error);
285
                            try {
286
                                EventlogFactory.createIndexEventLog().write(event);
287
                            } catch (Exception ee) {
288
                                log.error("SolrIndex.insertToIndex - IndexEventLog can't log the index inserting event :"+ee.getMessage());
289
                            }
290
                            log.error(error);
291
                        }
292
                        
293
                   
294
                }
295
            }
296
        }
297
    }
298
    
299
    /*
300
     * Index those ids which failed in the process (We got them from the EventLog)
301
     */
302
    private void indexFailedIds(List<IndexEvent> events) {
303
        if(events != null) {
304
            for(IndexEvent event : events) {
305
                if(event != null) {
306
                    Identifier identifier = event.getIdentifier();
307
                    if(identifier != null) {
308
                        String id = identifier.getValue();
309
                        if(id != null) {
310
                            Event action = event.getAction();
311
                            if (action != null && action.equals(Event.CREATE)) {
312
                                try {
313
                                    generateIndex(id);
314
                                    EventlogFactory.createIndexEventLog().remove(identifier);
315
                                } catch (Exception e) {
316
                                    log.error("IndexGenerator.indexFailedIds - Metacat Index couldn't generate the index for the id - "+id+" because "+e.getMessage());
317
                                }
318
                            } else if (action != null && action.equals(Event.DELETE)) {
319
                                try {
320
                                    removeIndex(id);
321
                                    EventlogFactory.createIndexEventLog().remove(identifier);
322
                                } catch (Exception e) {
323
                                    log.error("IndexGenerator.indexFailedIds - Metacat Index couldn't remove the index for the id - "+id+" because "+e.getMessage());
324
                                }
325
                            }
326
                        }
327
                    }
328
                }
329
            }
330
        }
331
    }
332
    
333
    public void run() {
334
        /*IndexEvent event = new IndexEvent();
335
        event.setDate(Calendar.getInstance().getTime());
336
        event.setType(IndexEvent.STARTTIMEDINDEX);
337
        event.setDescription("Start the timed index job");
338
        try {
339
            EventlogFactory.createIndexEventLog().write(event);
340
        } catch (Exception e) {
341
            log.error("IndexGenerator.run - IndexEventLog can't log the timed indexing start event :"+e.getMessage());
342
        }*/
343
        try {
344
            Date since = EventlogFactory.createIndexEventLog().getLastProcessDate();
345
            index(since);
346
        } catch (InvalidRequest e) {
347
            // TODO Auto-generated catch block
348
            //e.printStackTrace();
349
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
350
        } catch (InvalidToken e) {
351
            // TODO Auto-generated catch block
352
            //e.printStackTrace();
353
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
354
        } catch (NotAuthorized e) {
355
            // TODO Auto-generated catch block
356
            //e.printStackTrace();
357
        } catch (NotImplemented e) {
358
            // TODO Auto-generated catch block
359
            //e.printStackTrace();
360
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
361
        } catch (ServiceFailure e) {
362
            // TODO Auto-generated catch block
363
            //e.printStackTrace();
364
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
365
        } catch (SolrServerException e) {
366
            // TODO Auto-generated catch block
367
            //e.printStackTrace();
368
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
369
        } catch (FileNotFoundException e) {
370
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
371
        }
372
        /*event.setDate(Calendar.getInstance().getTime());
373
        event.setType(IndexEvent.FINISHTIMEDINDEX);
374
        event.setDescription("Finish the timed index job");
375
        try {
376
            EventlogFactory.createIndexEventLog().write(event);
377
        } catch (Exception e) {
378
            log.error("IndexGenerator.run - IndexEventLog can't log the timed indexing finish event :"+e.getMessage());
379
        }*/ catch (ClassNotFoundException e) {
380
            // TODO Auto-generated catch block
381
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
382
        } catch (InstantiationException e) {
383
            // TODO Auto-generated catch block
384
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
385
        } catch (IllegalAccessException e) {
386
            // TODO Auto-generated catch block
387
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
388
        } catch (IndexEventLogException e) {
389
            // TODO Auto-generated catch block
390
            log.error("IndexGenerator.run - Metadata-Index couldn't generate indexes for those documents which haven't been indexed : "+e.getMessage());
391
        }
392
    }
393
    
394
    /*
395
     * Get the indexed ids list from the solr server.
396
     * An empty list will be returned if there is no ids.
397
     */
398
    private List<String> getSolrDocIds() throws SolrServerException {
399
        List<String> ids = solrIndex.getSolrIds();
400
        return ids;
401
    }
402
    
403
    /*
404
     * Get an array of the list of ids of the metacat which has the systemmetadata modification in the range.
405
     * 
406
     * If since and util are null, it will return all of them.
407
     * The first element of the list is the ids except the resource map. The second elements of the list is the ids of the resource map.
408
     * The reason to split them is when we index the resource map, we need the index of the documents in the resource map ready.
409
     * The last element in the each list has the latest SystemMetadata modification date. But they are not sorted.
410
     */
411
    private List[] getMetacatIds(Date since, Date until) throws InvalidRequest, 
412
                        InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, FileNotFoundException {
413
        
414
        List<String> resourceMapIds = new ArrayList();
415
        List<String> otherIds = new ArrayList();
416
        List[] ids = new List[2];
417
        ids[FIRST]= otherIds;
418
        ids[SECOND] = resourceMapIds;
419
        ISet<Identifier> metacatIds = DistributedMapsFactory.getIdentifiersSet();
420
        Date otherPreviousDate = null;
421
        Date resourceMapPreviousDate = null;
422
        if(metacatIds != null) {
423
            for(Identifier identifier : metacatIds) {
424
                if(identifier != null && identifier.getValue() != null && !identifier.getValue().equals("")) {
425
                    SystemMetadata sysmeta = getSystemMetadata(identifier.getValue());
426
                    if(sysmeta != null && !sysmeta.getArchived()) {
427
                        ObjectFormatIdentifier formatId =sysmeta.getFormatId();
428
                        //System.out.println("the object format id is "+formatId.getValue());
429
                        //System.out.println("the ============ resourcMapNamespaces"+resourceMapNamespaces);
430
                        boolean correctTimeRange = false;
431
                        Date sysDate = sysmeta.getDateSysMetadataModified();
432
                        if(since == null && until == null) {
433
                            correctTimeRange = true;
434
                        } else if (since != null && until == null) {
435
                            if(sysDate.getTime() >= since.getTime()) {
436
                                correctTimeRange = true;
437
                            }
438
                        } else if (since == null && until != null) {
439
                            if(sysDate.getTime() <= until.getTime()) {
440
                                correctTimeRange = true;
441
                            }
442
                        } else if (since != null && until != null) {
443
                            if(sysDate.getTime() >= since.getTime() && sysDate.getTime() <= until.getTime()) {
444
                                correctTimeRange = true;
445
                            }
446
                        }
447
                        if(correctTimeRange && formatId != null && formatId.getValue() != null && resourceMapNamespaces != null && isResourceMap(formatId)) {
448
                            //for the resource map
449
                            if(!resourceMapIds.isEmpty()) {
450
                                if(sysDate.getTime() > resourceMapPreviousDate.getTime()) {
451
                                    resourceMapIds.add(identifier.getValue());//append to the end of the list if current is later than the previous one
452
                                    resourceMapPreviousDate = sysDate;//reset resourceMapPreviousDate to the bigger one
453
                                } else {
454
                                    int size = resourceMapIds.size();//
455
                                    resourceMapIds.add(size -1, identifier.getValue());//keep the previous one at the end of the list.
456
                                }
457
                            } else {
458
                                resourceMapIds.add(identifier.getValue());
459
                                resourceMapPreviousDate = sysDate;//init resourcemapPreviousDate
460
                            }
461
                            
462
                        } else if (correctTimeRange) {
463
                            if(!otherIds.isEmpty()) {
464
                                if(sysDate.getTime() > otherPreviousDate.getTime()) {
465
                                    otherIds.add(identifier.getValue());
466
                                    otherPreviousDate = sysDate;//reset otherPreviousDate to the bigger one
467
                                } else {
468
                                    int size = otherIds.size();
469
                                    otherIds.add(size-1, identifier.getValue());
470
                                }
471
                            } else {
472
                                otherIds.add(identifier.getValue());
473
                                otherPreviousDate = sysDate;//init otherPreviousDate
474
                            }
475
                            
476
                        }
477
                        
478
                    }
479
                }
480
            }
481
        }
482
        return ids;
483
    }
484
    
485
    /*
486
     * If the specified ObjectFormatIdentifier is a resrouce map namespace.
487
     */
488
    public static boolean isResourceMap(ObjectFormatIdentifier formatId) {
489
        boolean isResourceMap = false;
490
        if(formatId != null && resourceMapNamespaces != null) {
491
            for(String namespace : resourceMapNamespaces) {
492
                if(namespace != null && formatId.getValue() != null && !formatId.getValue().trim().equals("") && formatId.getValue().equals(namespace)) {
493
                    isResourceMap = true;
494
                    break;
495
                }
496
            }
497
        }
498
        return isResourceMap;
499
    }
500
    
501
   
502
    
503
    /*
504
     * Generate index for the id.
505
     */
506
    private void generateIndex(String id) throws Exception {
507
        if(id != null)  {
508
                SystemMetadata sysmeta = getSystemMetadata(id);
509
                //only update none-archived id.
510
                if(sysmeta != null && !sysmeta.getArchived()) {
511
                        InputStream data = getDataObject(id);
512
                        Identifier obsolete = sysmeta.getObsoletes();
513
                        List<String> obsoleteChain = null;
514
                        if(obsolete != null) {
515
                            obsoleteChain = getObsoletes(id);
516
                        } 
517
                        solrIndex.update(id, obsoleteChain, sysmeta, data);
518
                } else {
519
                    throw new Exception("IndexGenerator.generate - there is no found SystemMetadata associated with the id "+id);
520
                }
521
           
522
        }
523
    }
524
    
525
    /*
526
     * Remove the index for the id
527
     */
528
    private void removeIndex(String id) throws Exception {
529
        if(id != null) {
530
            solrIndex.remove(id);
531
        }
532
    }
533
    
534
    /*
535
     * Initialize the system metadata map
536
     */
537
    private void initSystemMetadataMap() throws FileNotFoundException, ServiceFailure{
538
        int times = 0;
539
        if(systemMetadataMap == null) {
540
            systemMetadataMap = DistributedMapsFactory.getSystemMetadataMap();
541
            /*while(true) {
542
                try {
543
                    systemMetadataMap = DistributedMapsFactory.getSystemMetadataMap();
544
                    break;
545
                } catch (FileNotFoundException e) {
546
                    throw e;
547
                } catch (ServiceFailure e) {
548
                    if(times <= maxAttempts) {
549
                        log.warn("IndexGenerator.initSystemMetadataMap - the hazelcast service is not ready : "
550
                                         +e.getMessage()+"\nWe will try to access it "+waitingTime/1000+" seconds later ");
551
                        try {
552
                            Thread.sleep(waitingTime);
553
                        } catch (Exception ee) {
554
                            log.warn("IndexGenerator.initSystemMetadataMap - the thread can't sleep for "+waitingTime/1000+" seconds to wait the hazelcast service");
555
                        }
556
                       
557
                    } else {
558
                        throw new ServiceFailure("0000", "IndexGenerator.initSystemMetadataMap - the hazelcast service is not ready even though Metacat-index wailted for "+maxAttempts*waitingTime/1000+" seconds. We can't get the system metadata from it and the building index can't happen this time");
559
                    }
560
                }
561
                times++;
562
            }*/
563
        }
564
    }
565
    
566
    /*
567
     * We should call this method after calling initSystemMetadataMap since this method doesn't have the mechanism to wait the readiness of the hazelcast service
568
     */
569
    private void initObjectPathMap() throws FileNotFoundException, ServiceFailure {
570
        if(objectPathMap == null) {
571
            objectPathMap = DistributedMapsFactory.getObjectPathMap();
572
        }
573
    }
574
    /**
575
     * Get an InputStream as the data object for the specific pid.
576
     * @param pid
577
     * @return
578
     * @throws FileNotFoundException
579
     */
580
    private InputStream getDataObject(String pid) throws FileNotFoundException {
581
        Identifier identifier = new Identifier();
582
        identifier.setValue(pid);
583
        String objectPath = objectPathMap.get(identifier);
584
        InputStream data = null;
585
        data = new FileInputStream(objectPath);
586
        return data;
587

    
588
    }
589
    
590
    /**
591
     * Get the SystemMetadata for the specified id from the distributed Map.
592
     * The null maybe is returned if there is no system metadata found.
593
     * @param id  the specified id.
594
     * @return the SystemMetadata associated with the id.
595
     */
596
    private SystemMetadata getSystemMetadata(String id) {
597
        SystemMetadata metadata = null;
598
        if(systemMetadataMap != null && id != null) {
599
            Identifier identifier = new Identifier();
600
            identifier.setValue(id);
601
            metadata = systemMetadataMap.get(identifier);
602
        }
603
        return metadata;
604
    }
605
    
606
    /**
607
     * Get the obsoletes chain of the specified id. The returned list doesn't include
608
     * the specified id itself. The newer version has the lower index number in the list.
609
     * Empty list will be returned if there is no document to be obsoleted by this id.
610
     * @param id
611
     * @return
612
     */
613
    private List<String> getObsoletes(String id) {
614
        List<String> obsoletes = new ArrayList<String>();
615
        while (id != null) {
616
            SystemMetadata metadata = getSystemMetadata(id);
617
            id = null;//set it to be null in order to stop the while loop if the id can't be assinged to a new value in the following code.
618
            if(metadata != null) {
619
                Identifier identifier = metadata.getObsoletes();
620
                if(identifier != null && identifier.getValue() != null && !identifier.getValue().trim().equals("")) {
621
                    obsoletes.add(identifier.getValue());
622
                    id = identifier.getValue();
623
                } 
624
            } 
625
        }
626
        return obsoletes;
627
    }
628
    
629
    /**
630
     * Overwrite and do nothing
631
     */
632
    public boolean cancel() {
633
        return true;
634
    }
635

    
636
}
(3-3/6)