Project

General

Profile

« Previous | Next » 

Revision 7854

Added by Jing Tao over 11 years ago

Add the EventLog code.

View differences:

metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/SystemMetadataEventListener.java
30 30
import java.io.FileNotFoundException;
31 31
import java.io.InputStream;
32 32
import java.util.ArrayList;
33
import java.util.Calendar;
33 34
import java.util.List;
34 35

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

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

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

  
46 51
public class SystemMetadataEventListener implements ItemListener<SystemMetadata> {
47 52
	
48 53
	private static Log log = LogFactory.getLog(SystemMetadataEventListener.class);
......
151 156

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

Also available in: Unified diff