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.InputStream;
|
31
|
|
32
|
import org.apache.commons.logging.Log;
|
33
|
import org.apache.commons.logging.LogFactory;
|
34
|
import org.dataone.configuration.Settings;
|
35
|
import org.dataone.service.types.v1.Identifier;
|
36
|
import org.dataone.service.types.v1.SystemMetadata;
|
37
|
|
38
|
import com.hazelcast.client.ClientConfig;
|
39
|
import com.hazelcast.client.HazelcastClient;
|
40
|
import com.hazelcast.core.EntryEvent;
|
41
|
import com.hazelcast.core.EntryListener;
|
42
|
import com.hazelcast.core.IMap;
|
43
|
|
44
|
public class SystemMetadataEventListener implements EntryListener<Identifier, SystemMetadata> {
|
45
|
|
46
|
private static Log log = LogFactory.getLog(SystemMetadataEventListener.class);
|
47
|
|
48
|
private SolrIndex solrIndex = null;
|
49
|
|
50
|
private HazelcastClient hzClient;
|
51
|
|
52
|
private IMap<Identifier, SystemMetadata> systemMetadataMap;
|
53
|
|
54
|
private IMap<Identifier, String> objectPathMap;
|
55
|
|
56
|
/**
|
57
|
* Default constructor - caller needs to initialize manually
|
58
|
* @see setSolrIndex()
|
59
|
* @see start()
|
60
|
*/
|
61
|
public SystemMetadataEventListener() {
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* Sets the SolrIndex instance and initializes the listener
|
66
|
* @param solrIndex
|
67
|
*/
|
68
|
public SystemMetadataEventListener(SolrIndex solrIndex) {
|
69
|
this.solrIndex = solrIndex;
|
70
|
start();
|
71
|
}
|
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
|
*/
|
92
|
public void start() {
|
93
|
|
94
|
// get config values
|
95
|
String hzSystemMetadata = Settings.getConfiguration().getString(
|
96
|
"dataone.hazelcast.systemMetadata");
|
97
|
String hzObjectPath = Settings.getConfiguration().getString(
|
98
|
"dataone.hazelcast.objectPath");
|
99
|
String hzGroupName = Settings.getConfiguration().getString(
|
100
|
"dataone.hazelcast.groupName");
|
101
|
String hzGroupPassword = Settings.getConfiguration().getString(
|
102
|
"dataone.hazelcast.groupPassword");
|
103
|
String hzAddress = Settings.getConfiguration().getString(
|
104
|
"dataone.hazelcast.address");
|
105
|
|
106
|
log.info("starting index entry listener...");
|
107
|
log.info("System Metadata value: " + hzSystemMetadata);
|
108
|
log.info("Object path value: " + hzObjectPath);
|
109
|
log.info("Group Name: " + hzGroupName);
|
110
|
log.info("Group Password: " + "*****"); // don't show value
|
111
|
log.info("HZ Address: " + hzAddress);
|
112
|
|
113
|
// connect to the HZ cluster
|
114
|
ClientConfig cc = new ClientConfig();
|
115
|
cc.getGroupConfig().setName(hzGroupName);
|
116
|
cc.getGroupConfig().setPassword(hzGroupPassword);
|
117
|
cc.addAddress(hzAddress );
|
118
|
try {
|
119
|
this.hzClient = HazelcastClient.newHazelcastClient(cc);
|
120
|
} catch (Exception e) {
|
121
|
log.error("Unable to create hazelcast client: ", e);
|
122
|
e.printStackTrace();
|
123
|
}
|
124
|
|
125
|
// get shared structures and add listener
|
126
|
this.systemMetadataMap = this.hzClient.getMap(hzSystemMetadata);
|
127
|
this.objectPathMap = this.hzClient.getMap(hzObjectPath);
|
128
|
this.systemMetadataMap.addEntryListener(this, true);
|
129
|
|
130
|
log.info("System Metadata size: " + this.systemMetadataMap.size());
|
131
|
log.info("Object path size:" + this.objectPathMap.size());
|
132
|
}
|
133
|
|
134
|
/**
|
135
|
* Removes this instance as a system metadata map event listener
|
136
|
*/
|
137
|
public void stop() {
|
138
|
log.info("stopping index entry listener...");
|
139
|
this.systemMetadataMap.removeEntryListener(this);
|
140
|
}
|
141
|
|
142
|
public void entryAdded(EntryEvent<Identifier, SystemMetadata> entryEvent) {
|
143
|
// use the same implementation for insert/update for now
|
144
|
this.entryUpdated(entryEvent);
|
145
|
|
146
|
}
|
147
|
|
148
|
public void entryEvicted(EntryEvent<Identifier, SystemMetadata> entryEvent) {
|
149
|
// remove from the index for now, this may be a temporary eviction
|
150
|
this.entryRemoved(entryEvent);
|
151
|
|
152
|
}
|
153
|
|
154
|
public void entryRemoved(EntryEvent<Identifier, SystemMetadata> entryEvent) {
|
155
|
// remove from the index
|
156
|
Identifier pid = entryEvent.getKey();
|
157
|
try {
|
158
|
solrIndex.remove(pid.getValue());
|
159
|
} catch (Exception e) {
|
160
|
// TODO: need to track errors, retry later
|
161
|
log.error(e.getMessage(), e);
|
162
|
}
|
163
|
|
164
|
}
|
165
|
|
166
|
public void entryUpdated(EntryEvent<Identifier, SystemMetadata> entryEvent) {
|
167
|
// add to the index
|
168
|
Identifier pid = entryEvent.getKey();
|
169
|
SystemMetadata systemMetadata = entryEvent.getValue();
|
170
|
|
171
|
String objectPath = objectPathMap.get(pid);
|
172
|
InputStream data = null;
|
173
|
try {
|
174
|
data = new FileInputStream(objectPath);
|
175
|
solrIndex.insert(pid.getValue(), systemMetadata, data);
|
176
|
} catch (Exception e) {
|
177
|
// TODO: need to track errors, retry later
|
178
|
log.error(e.getMessage(), e);
|
179
|
}
|
180
|
}
|
181
|
|
182
|
}
|