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.service.exceptions.ServiceFailure;
38
import org.dataone.service.types.v1.Identifier;
39
import org.dataone.service.types.v1.SystemMetadata;
40

    
41
import com.hazelcast.core.IMap;
42
import com.hazelcast.core.ISet;
43
import com.hazelcast.core.ItemEvent;
44
import com.hazelcast.core.ItemListener;
45

    
46
public class SystemMetadataEventListener implements ItemListener<SystemMetadata> {
47
	
48
	private static Log log = LogFactory.getLog(SystemMetadataEventListener.class);
49
	
50
	private SolrIndex solrIndex = null;
51
	        
52
    /**
53
     * Default constructor - caller needs to initialize manually
54
     * @see setSolrIndex()
55
     * @see start()
56
     */
57
    public SystemMetadataEventListener() {
58
    }
59
    
60
    /**
61
     * Sets the SolrIndex instance and initializes the listener 
62
     * @param solrIndex
63
     */
64
    public SystemMetadataEventListener(SolrIndex solrIndex) {
65
    	this.solrIndex = solrIndex;
66
    	try {
67
			start();
68
		} catch (Exception e) {
69
			log.error(e.getMessage(), e);
70
		}
71
    }
72
    
73
    /**
74
     * Get the SolrIndex that this listener communicates with
75
     * @return a SolrIndex instance that indexes the content being listened to
76
     */
77
    public SolrIndex getSolrIndex() {
78
		return solrIndex;
79
	}
80

    
81
    /**
82
     * Set the SolrIndex instance that the listener modifies
83
     * @param solrIndex
84
     */
85
	public void setSolrIndex(SolrIndex solrIndex) {
86
		this.solrIndex = solrIndex;
87
	}
88

    
89
	/**
90
     * Registers this instance as a system metadata map event listener
91
	 * @throws ServiceFailure 
92
	 * @throws FileNotFoundException 
93
     */
94
    public void start() throws FileNotFoundException, ServiceFailure {
95
    	
96
        // get shared structures and add listener
97
        IMap<Identifier, String> objectPathMap = DistributedMapsFactory.getObjectPathMap();
98
        ISet<SystemMetadata> indexQueue = DistributedMapsFactory.getIndexQueue();
99
        indexQueue.addItemListener(this, true);
100
        log.info("System Metadata size: " + indexQueue.size());
101
        log.info("Object path size:" + objectPathMap.size());
102
    }
103

    
104
    /**
105
     * Removes this instance as a system metadata map event listener
106
     * @throws ServiceFailure 
107
     * @throws FileNotFoundException 
108
     */
109
    public void stop() throws FileNotFoundException, ServiceFailure {
110
    	log.info("stopping index entry listener...");
111
    	DistributedMapsFactory.getIndexQueue().removeItemListener(this);
112
    }
113
    
114
    /**
115
     * Get the obsoletes chain of the specified id. The returned list doesn't include
116
     * the specified id itself. The newer version has the lower index number in the list.
117
     * Empty list will be returned if there is no document to be obsoleted by this id.
118
     * @param id
119
     * @return
120
     * @throws ServiceFailure
121
     * @throws FileNotFoundException 
122
     */
123
    private List<String> getObsoletes(String id) throws FileNotFoundException, ServiceFailure {
124
        List<String> obsoletes = new ArrayList<String>();
125
        while (id != null) {
126
            SystemMetadata metadata = DistributedMapsFactory.getSystemMetadata(id);
127
            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.
128
            if(metadata != null) {
129
                Identifier identifier = metadata.getObsoletes();
130
                if(identifier != null && identifier.getValue() != null && !identifier.getValue().trim().equals("")) {
131
                    obsoletes.add(identifier.getValue());
132
                    id = identifier.getValue();
133
                } 
134
            } 
135
        }
136
        return obsoletes;
137
    }
138
    
139

    
140
	public void itemRemoved(ItemEvent<SystemMetadata> entryEvent) {
141
		// remove from the index
142
		Identifier pid = entryEvent.getItem().getIdentifier();		
143
		try {
144
			solrIndex.remove(pid.getValue());
145
		} catch (Exception e) {
146
			// TODO: need to track errors, retry later
147
			log.error(e.getMessage(), e);
148
		}
149
		
150
	}
151

    
152
	public void itemAdded(ItemEvent<SystemMetadata> entryEvent) {
153
	    //System.out.println("===================================calling entryUpdated method ");
154
	    log.info("===================================calling entryUpdated method ");
155
		// add to the index
156
		Identifier pid = entryEvent.getItem().getIdentifier();
157
		//System.out.println("===================================update the document "+pid.getValue());
158
		log.info("===================================update the document "+pid.getValue());
159
		SystemMetadata systemMetadata = entryEvent.getItem();
160
		Identifier obsoletes = systemMetadata.getObsoletes();
161
		List<String> obsoletesChain = null;
162
		if (obsoletes != null) {
163
		    try {
164
				obsoletesChain = getObsoletes(pid.getValue());
165
			} catch (Exception e) {
166
	            log.error("Could not look up revision history" + e.getMessage(), e);
167
			}
168
		}
169
		String objectPath = null;
170
		try {
171
			objectPath = DistributedMapsFactory.getObjectPathMap().get(pid);
172
		} catch (Exception e) {
173
			log.error("Could not look up object path" + e.getMessage(), e);
174
		}
175
		if(objectPath != null) {
176
		    InputStream data = null;
177
	        try {
178
	            data = new FileInputStream(objectPath);
179
	            solrIndex.update(pid.getValue(), obsoletesChain, systemMetadata, data);
180
	        } catch (Exception e) {
181
	            // TODO: need to track errors, retry later
182
	            log.error(e.getMessage(), e);
183
	        }
184
		}
185
		
186
	}
187
    
188
}
(6-6/6)