1
|
/**
|
2
|
* Copyright: 2013 Regents of the University of California and the
|
3
|
* 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
|
import java.io.FileNotFoundException;
|
22
|
import java.util.List;
|
23
|
import java.util.Map;
|
24
|
|
25
|
import org.apache.commons.logging.Log;
|
26
|
import org.apache.commons.logging.LogFactory;
|
27
|
import org.dataone.service.exceptions.ServiceFailure;
|
28
|
import org.dataone.service.types.v1.Identifier;
|
29
|
import org.dataone.service.types.v2.SystemMetadata;
|
30
|
|
31
|
import com.hazelcast.core.EntryEvent;
|
32
|
import com.hazelcast.core.EntryListener;
|
33
|
import com.hazelcast.core.IMap;
|
34
|
|
35
|
import edu.ucsb.nceas.metacat.common.index.IndexTask;
|
36
|
|
37
|
public class SystemMetadataEventListener implements EntryListener<Identifier, IndexTask>, Runnable {
|
38
|
|
39
|
private static Log log = LogFactory.getLog(SystemMetadataEventListener.class);
|
40
|
|
41
|
private SolrIndex solrIndex = null;
|
42
|
|
43
|
private IMap<Identifier, IndexTask> source = null;
|
44
|
|
45
|
/**
|
46
|
* Default constructor - caller needs to initialize manually
|
47
|
* @see setSolrIndex()
|
48
|
* @see start()
|
49
|
*/
|
50
|
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
|
try {
|
60
|
run();
|
61
|
} catch (Exception e) {
|
62
|
log.error(e.getMessage(), e);
|
63
|
}
|
64
|
}
|
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
|
* @throws ServiceFailure
|
85
|
* @throws FileNotFoundException
|
86
|
*/
|
87
|
public void run() {
|
88
|
|
89
|
// get shared structures and add listener
|
90
|
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
|
}
|
101
|
|
102
|
/**
|
103
|
* Removes this instance as a system metadata map event listener
|
104
|
* @throws ServiceFailure
|
105
|
* @throws FileNotFoundException
|
106
|
*/
|
107
|
public void stop() throws FileNotFoundException, ServiceFailure {
|
108
|
log.info("stopping index entry listener...");
|
109
|
DistributedMapsFactory.getIndexQueue().removeEntryListener(this);
|
110
|
}
|
111
|
|
112
|
public void entryRemoved(EntryEvent<Identifier, IndexTask> entryEvent) {
|
113
|
// do nothing
|
114
|
}
|
115
|
public void entryEvicted(EntryEvent<Identifier, IndexTask> entryEvent) {
|
116
|
// do nothing
|
117
|
}
|
118
|
public void entryAdded(EntryEvent<Identifier, IndexTask> entryEvent) {
|
119
|
entryUpdated(entryEvent);
|
120
|
}
|
121
|
|
122
|
public void entryUpdated(EntryEvent<Identifier, IndexTask> entryEvent) {
|
123
|
//System.out.println("===================================calling entryUpdated method ");
|
124
|
log.info("===================================calling SystemMetadataEventListener.itemAdded method ");
|
125
|
// add to the index
|
126
|
Identifier pid = entryEvent.getKey();
|
127
|
//System.out.println("===================================update the document "+pid.getValue());
|
128
|
log.info("===================================adding the document " + pid.getValue());
|
129
|
|
130
|
// 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
|
/*if(systemMetadata == null) {
|
136
|
writeEventLog(systemMetadata, pid, "SystemMetadataEventListener.itemAdded -could not get the SystemMetadata");
|
137
|
return;
|
138
|
}*/
|
139
|
|
140
|
// 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
|
}
|
144
|
|
145
|
if (systemMetadata != null) {
|
146
|
solrIndex.update(pid, systemMetadata);
|
147
|
|
148
|
}
|
149
|
if (fields != null) {
|
150
|
solrIndex.insertFields(pid, fields);
|
151
|
}
|
152
|
|
153
|
}
|
154
|
|
155
|
|
156
|
|
157
|
}
|