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
		if(pid != null) {
149
		    try {
150
	            solrIndex.remove(pid.getValue());
151
	        } catch (Exception e) {
152
	            String error = "SystemMetadataEventListener.itemRemoved - couldn't remove the index for the pid "+pid.getValue()+" since "+e.getMessage();
153
	            SystemMetadata systemMetadata = entryEvent.getItem();
154
	            writeEventLog(systemMetadata, pid, error);
155
	            log.error(error, e);
156
	        }
157
		}
158
		
159
		
160
	}
161

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