1 |
7542
|
tao
|
/**
|
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 |
7550
|
leinfelder
|
import java.io.FileInputStream;
|
30 |
7562
|
leinfelder
|
import java.io.FileNotFoundException;
|
31 |
7550
|
leinfelder
|
import java.io.InputStream;
|
32 |
7602
|
tao
|
import java.util.ArrayList;
|
33 |
|
|
import java.util.List;
|
34 |
7550
|
leinfelder
|
|
35 |
|
|
import org.apache.commons.logging.Log;
|
36 |
|
|
import org.apache.commons.logging.LogFactory;
|
37 |
7811
|
leinfelder
|
import org.dataone.service.exceptions.ServiceFailure;
|
38 |
7550
|
leinfelder
|
import org.dataone.service.types.v1.Identifier;
|
39 |
|
|
import org.dataone.service.types.v1.SystemMetadata;
|
40 |
|
|
|
41 |
|
|
import com.hazelcast.core.IMap;
|
42 |
7812
|
leinfelder
|
import com.hazelcast.core.ISet;
|
43 |
|
|
import com.hazelcast.core.ItemEvent;
|
44 |
|
|
import com.hazelcast.core.ItemListener;
|
45 |
7550
|
leinfelder
|
|
46 |
7812
|
leinfelder
|
public class SystemMetadataEventListener implements ItemListener<SystemMetadata> {
|
47 |
7550
|
leinfelder
|
|
48 |
|
|
private static Log log = LogFactory.getLog(SystemMetadataEventListener.class);
|
49 |
|
|
|
50 |
|
|
private SolrIndex solrIndex = null;
|
51 |
7811
|
leinfelder
|
|
52 |
7550
|
leinfelder
|
/**
|
53 |
7557
|
leinfelder
|
* Default constructor - caller needs to initialize manually
|
54 |
|
|
* @see setSolrIndex()
|
55 |
|
|
* @see start()
|
56 |
7550
|
leinfelder
|
*/
|
57 |
7557
|
leinfelder
|
public SystemMetadataEventListener() {
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
/**
|
61 |
|
|
* Sets the SolrIndex instance and initializes the listener
|
62 |
|
|
* @param solrIndex
|
63 |
|
|
*/
|
64 |
|
|
public SystemMetadataEventListener(SolrIndex solrIndex) {
|
65 |
|
|
this.solrIndex = solrIndex;
|
66 |
7811
|
leinfelder
|
try {
|
67 |
|
|
start();
|
68 |
|
|
} catch (Exception e) {
|
69 |
|
|
log.error(e.getMessage(), e);
|
70 |
|
|
}
|
71 |
7557
|
leinfelder
|
}
|
72 |
|
|
|
73 |
|
|
/**
|
74 |
|
|
* Get the SolrIndex that this listener communicates with
|
75 |
|
|
* @return a SolrIndex instance that indexes the content being listened to
|
76 |
|
|
*/
|
77 |
|
|
public SolrIndex getSolrIndex() {
|
78 |
|
|
return solrIndex;
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/**
|
82 |
|
|
* Set the SolrIndex instance that the listener modifies
|
83 |
|
|
* @param solrIndex
|
84 |
|
|
*/
|
85 |
|
|
public void setSolrIndex(SolrIndex solrIndex) {
|
86 |
|
|
this.solrIndex = solrIndex;
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
/**
|
90 |
|
|
* Registers this instance as a system metadata map event listener
|
91 |
7811
|
leinfelder
|
* @throws ServiceFailure
|
92 |
|
|
* @throws FileNotFoundException
|
93 |
7557
|
leinfelder
|
*/
|
94 |
7811
|
leinfelder
|
public void start() throws FileNotFoundException, ServiceFailure {
|
95 |
7550
|
leinfelder
|
|
96 |
7811
|
leinfelder
|
// get shared structures and add listener
|
97 |
|
|
IMap<Identifier, String> objectPathMap = DistributedMapsFactory.getObjectPathMap();
|
98 |
7812
|
leinfelder
|
ISet<SystemMetadata> indexQueue = DistributedMapsFactory.getIndexQueue();
|
99 |
|
|
indexQueue.addItemListener(this, true);
|
100 |
|
|
log.info("System Metadata size: " + indexQueue.size());
|
101 |
7811
|
leinfelder
|
log.info("Object path size:" + objectPathMap.size());
|
102 |
7550
|
leinfelder
|
}
|
103 |
|
|
|
104 |
|
|
/**
|
105 |
7557
|
leinfelder
|
* Removes this instance as a system metadata map event listener
|
106 |
7811
|
leinfelder
|
* @throws ServiceFailure
|
107 |
|
|
* @throws FileNotFoundException
|
108 |
7550
|
leinfelder
|
*/
|
109 |
7811
|
leinfelder
|
public void stop() throws FileNotFoundException, ServiceFailure {
|
110 |
7550
|
leinfelder
|
log.info("stopping index entry listener...");
|
111 |
7812
|
leinfelder
|
DistributedMapsFactory.getIndexQueue().removeItemListener(this);
|
112 |
7550
|
leinfelder
|
}
|
113 |
7602
|
tao
|
|
114 |
|
|
/**
|
115 |
|
|
* Get the obsoletes chain of the specified id. The returned list doesn't include
|
116 |
|
|
* the specified id itself. The newer version has the lower index number in the list.
|
117 |
|
|
* Empty list will be returned if there is no document to be obsoleted by this id.
|
118 |
|
|
* @param id
|
119 |
|
|
* @return
|
120 |
7811
|
leinfelder
|
* @throws ServiceFailure
|
121 |
|
|
* @throws FileNotFoundException
|
122 |
7602
|
tao
|
*/
|
123 |
7811
|
leinfelder
|
private List<String> getObsoletes(String id) throws FileNotFoundException, ServiceFailure {
|
124 |
7602
|
tao
|
List<String> obsoletes = new ArrayList<String>();
|
125 |
|
|
while (id != null) {
|
126 |
7811
|
leinfelder
|
SystemMetadata metadata = DistributedMapsFactory.getSystemMetadata(id);
|
127 |
7602
|
tao
|
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.
|
128 |
|
|
if(metadata != null) {
|
129 |
|
|
Identifier identifier = metadata.getObsoletes();
|
130 |
|
|
if(identifier != null && identifier.getValue() != null && !identifier.getValue().trim().equals("")) {
|
131 |
|
|
obsoletes.add(identifier.getValue());
|
132 |
|
|
id = identifier.getValue();
|
133 |
|
|
}
|
134 |
|
|
}
|
135 |
|
|
}
|
136 |
|
|
return obsoletes;
|
137 |
|
|
}
|
138 |
7604
|
tao
|
|
139 |
7550
|
leinfelder
|
|
140 |
7812
|
leinfelder
|
public void itemRemoved(ItemEvent<SystemMetadata> entryEvent) {
|
141 |
7556
|
leinfelder
|
// remove from the index
|
142 |
7812
|
leinfelder
|
Identifier pid = entryEvent.getItem().getIdentifier();
|
143 |
7556
|
leinfelder
|
try {
|
144 |
|
|
solrIndex.remove(pid.getValue());
|
145 |
|
|
} catch (Exception e) {
|
146 |
|
|
// TODO: need to track errors, retry later
|
147 |
|
|
log.error(e.getMessage(), e);
|
148 |
|
|
}
|
149 |
7550
|
leinfelder
|
|
150 |
|
|
}
|
151 |
|
|
|
152 |
7812
|
leinfelder
|
public void itemAdded(ItemEvent<SystemMetadata> entryEvent) {
|
153 |
7684
|
tao
|
//System.out.println("===================================calling entryUpdated method ");
|
154 |
|
|
log.info("===================================calling entryUpdated method ");
|
155 |
7556
|
leinfelder
|
// add to the index
|
156 |
7812
|
leinfelder
|
Identifier pid = entryEvent.getItem().getIdentifier();
|
157 |
7684
|
tao
|
//System.out.println("===================================update the document "+pid.getValue());
|
158 |
|
|
log.info("===================================update the document "+pid.getValue());
|
159 |
7812
|
leinfelder
|
SystemMetadata systemMetadata = entryEvent.getItem();
|
160 |
7627
|
tao
|
Identifier obsoletes = systemMetadata.getObsoletes();
|
161 |
|
|
List<String> obsoletesChain = null;
|
162 |
7811
|
leinfelder
|
if (obsoletes != null) {
|
163 |
|
|
try {
|
164 |
|
|
obsoletesChain = getObsoletes(pid.getValue());
|
165 |
|
|
} catch (Exception e) {
|
166 |
|
|
log.error("Could not look up revision history" + e.getMessage(), e);
|
167 |
|
|
}
|
168 |
7627
|
tao
|
}
|
169 |
7811
|
leinfelder
|
String objectPath = null;
|
170 |
|
|
try {
|
171 |
|
|
objectPath = DistributedMapsFactory.getObjectPathMap().get(pid);
|
172 |
|
|
} catch (Exception e) {
|
173 |
|
|
log.error("Could not look up object path" + e.getMessage(), e);
|
174 |
|
|
}
|
175 |
7691
|
tao
|
if(objectPath != null) {
|
176 |
|
|
InputStream data = null;
|
177 |
|
|
try {
|
178 |
|
|
data = new FileInputStream(objectPath);
|
179 |
|
|
solrIndex.update(pid.getValue(), obsoletesChain, systemMetadata, data);
|
180 |
|
|
} catch (Exception e) {
|
181 |
|
|
// TODO: need to track errors, retry later
|
182 |
|
|
log.error(e.getMessage(), e);
|
183 |
|
|
}
|
184 |
7556
|
leinfelder
|
}
|
185 |
7691
|
tao
|
|
186 |
7550
|
leinfelder
|
}
|
187 |
|
|
|
188 |
7542
|
tao
|
}
|