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.List;
34

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

    
41
import com.hazelcast.client.ClientConfig;
42
import com.hazelcast.client.HazelcastClient;
43
import com.hazelcast.config.Config;
44
import com.hazelcast.config.FileSystemXmlConfig;
45
import com.hazelcast.core.EntryEvent;
46
import com.hazelcast.core.EntryListener;
47
import com.hazelcast.core.IMap;
48

    
49
public class SystemMetadataEventListener implements EntryListener<Identifier, SystemMetadata> {
50
	
51
	private static Log log = LogFactory.getLog(SystemMetadataEventListener.class);
52
	
53
	private SolrIndex solrIndex = null;
54
	
55
	//private HazelcastClient hzClient;
56

    
57
    private IMap<Identifier, SystemMetadata> systemMetadataMap;
58
    
59
    private IMap<Identifier, String> objectPathMap;
60
    
61
    /**
62
     * Default constructor - caller needs to initialize manually
63
     * @see setSolrIndex()
64
     * @see start()
65
     */
66
    public SystemMetadataEventListener() {
67
    }
68
    
69
    /**
70
     * Sets the SolrIndex instance and initializes the listener 
71
     * @param solrIndex
72
     */
73
    public SystemMetadataEventListener(SolrIndex solrIndex) {
74
    	this.solrIndex = solrIndex;
75
    	start();
76
    }
77
    
78
    /**
79
     * Get the SolrIndex that this listener communicates with
80
     * @return a SolrIndex instance that indexes the content being listened to
81
     */
82
    public SolrIndex getSolrIndex() {
83
		return solrIndex;
84
	}
85

    
86
    /**
87
     * Set the SolrIndex instance that the listener modifies
88
     * @param solrIndex
89
     */
90
	public void setSolrIndex(SolrIndex solrIndex) {
91
		this.solrIndex = solrIndex;
92
	}
93

    
94
	/**
95
     * Registers this instance as a system metadata map event listener
96
     */
97
    public void start() {
98
    	
99
        try {
100
            // get shared structures and add listener
101
            this.systemMetadataMap =  DistributedMapsFactory.getSystemMetadataMap();
102
            this.objectPathMap = DistributedMapsFactory.getObjectPathMap();
103

    
104
        } catch (Exception e) {
105
            log.error("Unable to create hazelcast client: ", e);
106
            e.printStackTrace();
107
        }
108
        
109
        this.systemMetadataMap.addEntryListener(this, true);
110
        log.info("System Metadata size: " + this.systemMetadataMap.size());
111
        log.info("Object path size:" + this.objectPathMap.size());
112
    }
113

    
114
    /**
115
     * Removes this instance as a system metadata map event listener
116
     */
117
    public void stop() {
118
    	log.info("stopping index entry listener...");
119
        this.systemMetadataMap.removeEntryListener(this);
120
    }
121
    
122
    /**
123
     * Get the SystemMetadata for the specified id from the distributed Map.
124
     * The null maybe is returned if there is no system metadata found.
125
     * @param id  the specified id.
126
     * @return the SystemMetadata associated with the id.
127
     */
128
    private SystemMetadata getSystemMetadata(String id) {
129
        SystemMetadata metadata = null;
130
        if(systemMetadataMap != null && id != null) {
131
            Identifier identifier = new Identifier();
132
            identifier.setValue(id);
133
            metadata = systemMetadataMap.get(identifier);
134
        }
135
        return metadata;
136
    }
137
    
138
    /**
139
     * Get the obsoletes chain of the specified id. The returned list doesn't include
140
     * the specified id itself. The newer version has the lower index number in the list.
141
     * Empty list will be returned if there is no document to be obsoleted by this id.
142
     * @param id
143
     * @return
144
     */
145
    private List<String> getObsoletes(String id) {
146
        List<String> obsoletes = new ArrayList<String>();
147
        while (id != null) {
148
            SystemMetadata metadata = getSystemMetadata(id);
149
            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.
150
            if(metadata != null) {
151
                Identifier identifier = metadata.getObsoletes();
152
                if(identifier != null && identifier.getValue() != null && !identifier.getValue().trim().equals("")) {
153
                    obsoletes.add(identifier.getValue());
154
                    id = identifier.getValue();
155
                } 
156
            } 
157
        }
158
        return obsoletes;
159
    }
160
    
161
    
162
  
163
	public void entryAdded(EntryEvent<Identifier, SystemMetadata> entryEvent) {
164
	    //System.out.println("===================================calling entryAdded method ");
165
	    log.info("===================================calling entryAdded method ");
166
		// use the same implementation for insert/update for now
167
		this.entryUpdated(entryEvent);
168

    
169
	}
170

    
171
	public void entryEvicted(EntryEvent<Identifier, SystemMetadata> entryEvent) {
172
		// remove from the index for now, this may be a temporary eviction
173
		this.entryRemoved(entryEvent);
174
		
175
	}
176

    
177
	public void entryRemoved(EntryEvent<Identifier, SystemMetadata> entryEvent) {
178
		// remove from the index
179
		Identifier pid = entryEvent.getKey();		
180
		try {
181
			solrIndex.remove(pid.getValue());
182
		} catch (Exception e) {
183
			// TODO: need to track errors, retry later
184
			log.error(e.getMessage(), e);
185
		}
186
		
187
	}
188

    
189
	public void entryUpdated(EntryEvent<Identifier, SystemMetadata> entryEvent) {
190
	    //System.out.println("===================================calling entryUpdated method ");
191
	    log.info("===================================calling entryUpdated method ");
192
		// add to the index
193
		Identifier pid = entryEvent.getKey();
194
		//System.out.println("===================================update the document "+pid.getValue());
195
		log.info("===================================update the document "+pid.getValue());
196
		SystemMetadata systemMetadata = entryEvent.getValue();
197
		Identifier obsoletes = systemMetadata.getObsoletes();
198
		List<String> obsoletesChain = null;
199
		if(obsoletes != null) {
200
		    obsoletesChain= getObsoletes(pid.getValue());
201
		}
202
		String objectPath = objectPathMap.get(pid);
203
		if(objectPath != null) {
204
		    InputStream data = null;
205
	        try {
206
	            data = new FileInputStream(objectPath);
207
	            solrIndex.update(pid.getValue(), obsoletesChain, systemMetadata, data);
208
	        } catch (Exception e) {
209
	            // TODO: need to track errors, retry later
210
	            log.error(e.getMessage(), e);
211
	        }
212
		}
213
		
214
	}
215
    
216
}
(6-6/6)