Project

General

Profile

1 7542 tao
/**
2 8138 tao
 *  Copyright: 2013 Regents of the University of California and the
3 7542 tao
 *             National Center for Ecological Analysis and Synthesis
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package edu.ucsb.nceas.metacat.index;
20
21 7562 leinfelder
import java.io.FileNotFoundException;
22 7602 tao
import java.util.List;
23 8464 leinfelder
import java.util.Map;
24 7550 leinfelder
25
import org.apache.commons.logging.Log;
26
import org.apache.commons.logging.LogFactory;
27 7811 leinfelder
import org.dataone.service.exceptions.ServiceFailure;
28 7550 leinfelder
import org.dataone.service.types.v1.Identifier;
29
import org.dataone.service.types.v1.SystemMetadata;
30
31 8464 leinfelder
import com.hazelcast.core.EntryEvent;
32
import com.hazelcast.core.EntryListener;
33 7550 leinfelder
import com.hazelcast.core.IMap;
34
35 8464 leinfelder
import edu.ucsb.nceas.metacat.common.index.IndexTask;
36 7854 tao
37 8464 leinfelder
public class SystemMetadataEventListener implements EntryListener<Identifier, IndexTask>, Runnable {
38 7550 leinfelder
39
	private static Log log = LogFactory.getLog(SystemMetadataEventListener.class);
40
41
	private SolrIndex solrIndex = null;
42 8283 leinfelder
43 8464 leinfelder
	private IMap<Identifier, IndexTask> source = null;
44 7811 leinfelder
45 7550 leinfelder
    /**
46 7557 leinfelder
     * Default constructor - caller needs to initialize manually
47
     * @see setSolrIndex()
48
     * @see start()
49 7550 leinfelder
     */
50 7557 leinfelder
    public SystemMetadataEventListener() {
51
    }
52
53
    /**
54
     * Sets the SolrIndex instance and initializes the listener
55
     * @param solrIndex
56
     */
57
    public SystemMetadataEventListener(SolrIndex solrIndex) {
58
    	this.solrIndex = solrIndex;
59 7811 leinfelder
    	try {
60 8464 leinfelder
			run();
61 7811 leinfelder
		} catch (Exception e) {
62
			log.error(e.getMessage(), e);
63
		}
64 7557 leinfelder
    }
65
66
    /**
67
     * Get the SolrIndex that this listener communicates with
68
     * @return a SolrIndex instance that indexes the content being listened to
69
     */
70
    public SolrIndex getSolrIndex() {
71
		return solrIndex;
72
	}
73
74
    /**
75
     * Set the SolrIndex instance that the listener modifies
76
     * @param solrIndex
77
     */
78
	public void setSolrIndex(SolrIndex solrIndex) {
79
		this.solrIndex = solrIndex;
80
	}
81
82
	/**
83
     * Registers this instance as a system metadata map event listener
84 7811 leinfelder
	 * @throws ServiceFailure
85
	 * @throws FileNotFoundException
86 7557 leinfelder
     */
87 8464 leinfelder
    public void run() {
88 7550 leinfelder
89 7811 leinfelder
        // get shared structures and add listener
90 8464 leinfelder
    	try {
91
	        IMap<Identifier, String> objectPathMap = DistributedMapsFactory.getObjectPathMap();
92
	        IMap<Identifier, IndexTask> indexQueue = DistributedMapsFactory.getIndexQueue();
93
	        indexQueue.addEntryListener(this, true);
94
	        this.source = indexQueue;
95
	        log.info("System Metadata size: " + indexQueue.size());
96
	        log.info("Object path size:" + objectPathMap.size());
97
    	} catch (Exception e) {
98
    		log.error("Cannot start SystemMetadata listener" , e);
99
    	}
100 7550 leinfelder
    }
101
102
    /**
103 7557 leinfelder
     * Removes this instance as a system metadata map event listener
104 7811 leinfelder
     * @throws ServiceFailure
105
     * @throws FileNotFoundException
106 7550 leinfelder
     */
107 7811 leinfelder
    public void stop() throws FileNotFoundException, ServiceFailure {
108 7550 leinfelder
    	log.info("stopping index entry listener...");
109 8464 leinfelder
    	DistributedMapsFactory.getIndexQueue().removeEntryListener(this);
110 7550 leinfelder
    }
111 8464 leinfelder
112
    public void entryRemoved(EntryEvent<Identifier, IndexTask> entryEvent) {
113
    	// do nothing
114 7602 tao
    }
115 8464 leinfelder
    public void entryEvicted(EntryEvent<Identifier, IndexTask> entryEvent) {
116
    	// do nothing
117
    }
118
    public void entryAdded(EntryEvent<Identifier, IndexTask> entryEvent) {
119
    	entryUpdated(entryEvent);
120
    }
121 7604 tao
122 8464 leinfelder
	public void entryUpdated(EntryEvent<Identifier, IndexTask> entryEvent) {
123 7684 tao
	    //System.out.println("===================================calling entryUpdated method ");
124 7854 tao
	    log.info("===================================calling SystemMetadataEventListener.itemAdded method ");
125 7556 leinfelder
		// add to the index
126 8464 leinfelder
		Identifier pid = entryEvent.getKey();
127 7684 tao
		//System.out.println("===================================update the document "+pid.getValue());
128 8464 leinfelder
		log.info("===================================adding the document " + pid.getValue());
129 8343 tao
130 8464 leinfelder
		// what do we have to index?
131
		IndexTask task = entryEvent.getValue();
132
		SystemMetadata systemMetadata = task.getSystemMetadata();
133
		Map<String, List<Object>> fields = task.getFields();
134
135 8343 tao
		/*if(systemMetadata == null) {
136 7854 tao
		    writeEventLog(systemMetadata, pid, "SystemMetadataEventListener.itemAdded -could not get the SystemMetadata");
137
		    return;
138 8343 tao
		}*/
139 8283 leinfelder
140 8464 leinfelder
		// make sure we remove this task so that it can be re-added in the future
141
		if (source != null && pid != null) {
142
			source.remove(pid);
143 8283 leinfelder
		}
144 8343 tao
145 8464 leinfelder
		if (systemMetadata != null) {
146
			solrIndex.update(pid, systemMetadata);
147
		}
148
		if (fields != null) {
149
			solrIndex.insertFields(pid, fields);
150
		}
151 7691 tao
152 7550 leinfelder
	}
153 7854 tao
154 8343 tao
155 7550 leinfelder
156 7542 tao
}