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

    
36
import org.apache.commons.logging.Log;
37
import org.apache.commons.logging.LogFactory;
38
import org.dataone.service.exceptions.ServiceFailure;
39
import org.dataone.service.types.v1.Event;
40
import org.dataone.service.types.v1.Identifier;
41
import org.dataone.service.types.v1.SystemMetadata;
42

    
43
import com.hazelcast.core.IMap;
44
import com.hazelcast.core.ISet;
45
import com.hazelcast.core.ItemEvent;
46
import com.hazelcast.core.ItemListener;
47

    
48
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
49
import edu.ucsb.nceas.metacat.index.event.EventlogFactory;
50

    
51
public class SystemMetadataEventListener implements ItemListener<SystemMetadata> {
52
	
53
	private static Log log = LogFactory.getLog(SystemMetadataEventListener.class);
54
	
55
	private SolrIndex solrIndex = null;
56
	        
57
    /**
58
     * Default constructor - caller needs to initialize manually
59
     * @see setSolrIndex()
60
     * @see start()
61
     */
62
    public SystemMetadataEventListener() {
63
    }
64
    
65
    /**
66
     * Sets the SolrIndex instance and initializes the listener 
67
     * @param solrIndex
68
     */
69
    public SystemMetadataEventListener(SolrIndex solrIndex) {
70
    	this.solrIndex = solrIndex;
71
    	try {
72
			start();
73
		} catch (Exception e) {
74
			log.error(e.getMessage(), e);
75
		}
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
	 * @throws ServiceFailure 
97
	 * @throws FileNotFoundException 
98
     */
99
    public void start() throws FileNotFoundException, ServiceFailure {
100
    	
101
        // get shared structures and add listener
102
        IMap<Identifier, String> objectPathMap = DistributedMapsFactory.getObjectPathMap();
103
        ISet<SystemMetadata> indexQueue = DistributedMapsFactory.getIndexQueue();
104
        indexQueue.addItemListener(this, true);
105
        log.info("System Metadata size: " + indexQueue.size());
106
        log.info("Object path size:" + objectPathMap.size());
107
    }
108

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

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

    
157
	public void itemAdded(ItemEvent<SystemMetadata> entryEvent) {
158
	    //System.out.println("===================================calling entryUpdated method ");
159
	    log.info("===================================calling SystemMetadataEventListener.itemAdded method ");
160
		// add to the index
161
		Identifier pid = entryEvent.getItem().getIdentifier();
162
		//System.out.println("===================================update the document "+pid.getValue());
163
		log.info("===================================adding the document "+pid.getValue());
164
		SystemMetadata systemMetadata = entryEvent.getItem();
165
		if(systemMetadata == null) {
166
		    writeEventLog(systemMetadata, pid, "SystemMetadataEventListener.itemAdded -could not get the SystemMetadata");
167
		    return;
168
		}
169
		Identifier obsoletes = systemMetadata.getObsoletes();
170
		List<String> obsoletesChain = null;
171
		if (obsoletes != null) {
172
		    try {
173
				obsoletesChain = getObsoletes(pid.getValue());
174
			} catch (Exception e) {
175
			    String error = "SystemMetadataEventListener.itemAdded -could not look up revision history " + e.getMessage();
176
			    writeEventLog(systemMetadata, pid, error);
177
	            log.error(error, e);
178
	            return;
179
			}
180
		}
181
		String objectPath = null;
182
		try {
183
			objectPath = DistributedMapsFactory.getObjectPathMap().get(pid);
184
		} catch (Exception e) {
185
		    String error = "SystemMetadataEventListener.itemAdded - could not look up object path" + e.getMessage();
186
		    writeEventLog(systemMetadata, pid, error);
187
			log.error(error, e);
188
		}
189
		if(objectPath != null) {
190
		    InputStream data = null;
191
	        try {
192
	            data = new FileInputStream(objectPath);
193
	            solrIndex.update(pid.getValue(), obsoletesChain, systemMetadata, data);
194
	        } catch (Exception e) {
195
	            String error = "SystemMetadataEventListener.itemAdded - could not comit the index into the solr server since " + e.getMessage();
196
	            writeEventLog(systemMetadata, pid, error);
197
	            log.error(error, e);
198
	        }
199
		}
200
		
201
	}
202
	
203
	private void writeEventLog(SystemMetadata systemMetadata, Identifier pid, String error) {
204
	    IndexEvent event = new IndexEvent();
205
        event.setIdentifier(pid);
206
	    event.setDate(Calendar.getInstance().getTime());
207
	    String action = null;
208
	    if (systemMetadata == null ) {
209
	        action = Event.CREATE.xmlValue();
210
            event.setAction(Event.CREATE);
211
	    }
212
	    else if(systemMetadata.getArchived()) {
213
            action = Event.DELETE.xmlValue();
214
            event.setAction(Event.DELETE);
215
        } else {
216
            action = Event.CREATE.xmlValue();
217
            event.setAction(Event.CREATE);
218
        }
219
        event.setDescription("Failed to "+action+"the solr index for the id "+pid.getValue()+" since "+error);
220
        try {
221
            EventlogFactory.createIndexEventLog().write(event);
222
        } catch (Exception ee) {
223
            log.error("SolrIndex.insertToIndex - IndexEventLog can't log the index inserting event :"+ee.getMessage());
224
        }
225
	}
226
    
227
}
(6-6/6)