1
|
/**
|
2
|
* This work was created by participants in the DataONE project, and is
|
3
|
* jointly copyrighted by participating institutions in DataONE. For
|
4
|
* more information on DataONE, see our web site at http://dataone.org.
|
5
|
*
|
6
|
* Copyright ${year}
|
7
|
*
|
8
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
* you may not use this file except in compliance with the License.
|
10
|
* You may obtain a copy of the License at
|
11
|
*
|
12
|
* http://www.apache.org/licenses/LICENSE-2.0
|
13
|
*
|
14
|
* Unless required by applicable law or agreed to in writing, software
|
15
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
17
|
* See the License for the specific language governing permissions and
|
18
|
* limitations under the License.
|
19
|
*/
|
20
|
|
21
|
package edu.ucsb.nceas.metacat.dataone.hazelcast;
|
22
|
|
23
|
import java.io.InputStream;
|
24
|
import java.io.Serializable;
|
25
|
import java.util.concurrent.Callable;
|
26
|
|
27
|
import org.dataone.client.D1Client;
|
28
|
import org.dataone.configuration.Settings;
|
29
|
import org.dataone.service.exceptions.NotFound;
|
30
|
import org.dataone.service.types.v1.Identifier;
|
31
|
import org.dataone.service.types.v1.Node;
|
32
|
import org.dataone.service.types.v1.Permission;
|
33
|
import org.dataone.service.types.v1.SystemMetadata;
|
34
|
|
35
|
import com.hazelcast.core.Hazelcast;
|
36
|
import com.hazelcast.core.IMap;
|
37
|
|
38
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
39
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
40
|
import edu.ucsb.nceas.metacat.dataone.CNodeService;
|
41
|
|
42
|
/**
|
43
|
* A single CN replication task to be executed by the CN Replication Service. This
|
44
|
* applies to replication of system metadata, science metadata, and resource maps
|
45
|
* across CNs.
|
46
|
*
|
47
|
* The task is built when a change occurs in the hzSystemMetadata map in the
|
48
|
* storage cluster initiated by Metacat for a given PID. If the change involves
|
49
|
* only modifications to system metadata, those changes will occur in the shared
|
50
|
* map and in the backing store of the member that owns the map entry. Other
|
51
|
* members must be updated using this task. Likewise, changes to the system
|
52
|
* metadata map that involve science metadata creation or changes will be
|
53
|
* distributed to all members via this task. The same is for OAI-ORE resource maps.
|
54
|
*
|
55
|
* @author cjones
|
56
|
*
|
57
|
*/
|
58
|
public class CNReplicationTask implements Serializable, Callable<Identifier> {
|
59
|
|
60
|
/* The identifier of this task */
|
61
|
private String taskid;
|
62
|
|
63
|
/* The identifier of the object to replicate */
|
64
|
private Identifier pid;
|
65
|
|
66
|
/* The object format type stated in the system metadata (DATA/METADATA/RESOURCE) */
|
67
|
private String formatType;
|
68
|
|
69
|
/* The target Node object */
|
70
|
private Node targetNode;
|
71
|
|
72
|
/* The originating Node object */
|
73
|
private Node originatingNode;
|
74
|
|
75
|
/* The subject of the target node, extracted from the Node object */
|
76
|
private String targetNodeSubject;
|
77
|
|
78
|
/* The subject of the originating node, extracted from the Node object */
|
79
|
private String originatingNodeSubject;
|
80
|
|
81
|
/* The permission to be executed (in this case, always 'replicate') */
|
82
|
String permission;
|
83
|
|
84
|
/**
|
85
|
* Constructor - create an empty replication task instance
|
86
|
*/
|
87
|
public CNReplicationTask() {
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Constructor - create a replication task instance
|
92
|
*
|
93
|
* @param taskid
|
94
|
* @param pid
|
95
|
* @param targetNode
|
96
|
*/
|
97
|
public CNReplicationTask(String taskid, Identifier pid, String formatType,
|
98
|
Node originatingNode, Node targetNode,
|
99
|
Permission replicatePermission) {
|
100
|
|
101
|
this.taskid = taskid;
|
102
|
this.pid = pid;
|
103
|
this.formatType = formatType;
|
104
|
this.originatingNode = originatingNode;
|
105
|
this.targetNode = targetNode;
|
106
|
this.originatingNodeSubject = originatingNode.getSubject(0).getValue();
|
107
|
this.targetNodeSubject = targetNode.getSubject(0).getValue();
|
108
|
this.permission = replicatePermission.name();
|
109
|
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* Get the task identifier for this task
|
114
|
* @return the taskid
|
115
|
*/
|
116
|
public String getTaskid() {
|
117
|
return taskid;
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* Set the task identifier for this task
|
122
|
* @param taskid the taskid to set
|
123
|
*/
|
124
|
public void setTaskid(String taskid) {
|
125
|
this.taskid = taskid;
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* Get the object identifier to be replicated
|
130
|
* @return the pid
|
131
|
*/
|
132
|
public Identifier getPid() {
|
133
|
return pid;
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* Set the object identifier to be replicated
|
138
|
* @param pid the pid to set
|
139
|
*/
|
140
|
public void setPid(Identifier pid) {
|
141
|
this.pid = pid;
|
142
|
}
|
143
|
|
144
|
|
145
|
/**
|
146
|
* Get the format type of the object to be replicated
|
147
|
* @return the pid
|
148
|
*/
|
149
|
public String getFormatType() {
|
150
|
return this.formatType;
|
151
|
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* Set the format type of the object to be replicated
|
156
|
* @param pid the pid to set
|
157
|
*/
|
158
|
public void setFormatType(String formatType) {
|
159
|
this.formatType = formatType;
|
160
|
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* Get the target node
|
165
|
* @return the targetNode
|
166
|
*/
|
167
|
public Node getTargetNode() {
|
168
|
return targetNode;
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
* Set the target node
|
173
|
* @param targetNode the targetNode to set
|
174
|
*/
|
175
|
public void setTargetNode(Node targetNode) {
|
176
|
this.targetNode = targetNode;
|
177
|
}
|
178
|
|
179
|
/**
|
180
|
* Get the originating node
|
181
|
* @return the originatingNode
|
182
|
*/
|
183
|
public Node getOriginatingNode() {
|
184
|
return originatingNode;
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* Set the originating node
|
189
|
* @param originatingNode the originatingNode to set
|
190
|
*/
|
191
|
public void setOriginatingNode(Node originatingNode) {
|
192
|
this.originatingNode = originatingNode;
|
193
|
}
|
194
|
|
195
|
/**
|
196
|
* For the given Replication task, return the Subject listed in the target
|
197
|
* node. Usually used in authorizing a replication event.
|
198
|
*
|
199
|
* @return subject - the subject listed in the target Node object as a string
|
200
|
*/
|
201
|
public String getTargetNodeSubject() {
|
202
|
|
203
|
return this.targetNodeSubject;
|
204
|
|
205
|
}
|
206
|
|
207
|
/**
|
208
|
* Set the target node subject identifying the node
|
209
|
* @param subject the targetNode subject
|
210
|
*/
|
211
|
public void setTargetNodeSubject(String subject) {
|
212
|
this.targetNodeSubject = subject;
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* For the given Replication task, return the Subject listed in the target
|
217
|
* node. Usually used in authorizing a replication event.
|
218
|
*
|
219
|
* @return subject - the subject listed in the target Node object as a string
|
220
|
*/
|
221
|
public String getOriginatingNodeSubject() {
|
222
|
|
223
|
return this.originatingNodeSubject;
|
224
|
|
225
|
}
|
226
|
|
227
|
/**
|
228
|
* Set the target node subject identifying the node
|
229
|
* @param subject the targetNode subject
|
230
|
*/
|
231
|
public void setOriginatingNodeSubject(String subject) {
|
232
|
this.originatingNodeSubject = subject;
|
233
|
}
|
234
|
|
235
|
/**
|
236
|
* Get the permission being allowed for this task
|
237
|
*
|
238
|
* @return subject - the subject listed in the target Node object
|
239
|
*/
|
240
|
public String getPermission() {
|
241
|
return this.permission;
|
242
|
|
243
|
}
|
244
|
|
245
|
/**
|
246
|
* Set the permission being allowed for this task
|
247
|
* @param subject the targetNode subject
|
248
|
*/
|
249
|
public void setPermission(Permission permission) {
|
250
|
this.permission = permission.name();
|
251
|
|
252
|
}
|
253
|
|
254
|
/**
|
255
|
* Implement the Callable interface, providing code that initiates replication.
|
256
|
*
|
257
|
* @return pid - the identifier of the replicated object upon success
|
258
|
*/
|
259
|
public Identifier call() {
|
260
|
|
261
|
// Get the D1 Hazelcast configuration parameters
|
262
|
String hzSystemMetadata =
|
263
|
Settings.getConfiguration().getString("dataone.hazelcast.systemMetadata");
|
264
|
|
265
|
// get the system metadata for the pid
|
266
|
IMap<Identifier, SystemMetadata> sysMetaMap = Hazelcast.getMap(hzSystemMetadata);
|
267
|
|
268
|
// get the systemMetadata
|
269
|
SystemMetadata sm = sysMetaMap.get(pid);
|
270
|
|
271
|
|
272
|
|
273
|
// only system metadata - no data replicated
|
274
|
boolean isData = formatType.equals("??");
|
275
|
if (!isData) {
|
276
|
// TODO: get the science metadata/ORE from somewhere
|
277
|
InputStream sciMetaORE = null;
|
278
|
try {
|
279
|
sciMetaORE = CNodeService.getInstance().get(null, pid);
|
280
|
} catch (NotFound nf) {
|
281
|
try {
|
282
|
sciMetaORE = D1Client.getCN().get(null, pid);
|
283
|
// save it locally
|
284
|
CNodeService.getInstance().create(null, pid, sciMetaORE, sm);
|
285
|
} catch (Exception e) {
|
286
|
// TODO Auto-generated catch block
|
287
|
e.printStackTrace();
|
288
|
}
|
289
|
} catch (Exception e) {
|
290
|
e.printStackTrace();
|
291
|
}
|
292
|
} else {
|
293
|
// just system metadata
|
294
|
try {
|
295
|
if (!IdentifierManager.getInstance().identifierExists(pid.getValue())) {
|
296
|
IdentifierManager.getInstance().createSystemMetadata(sm);
|
297
|
} else {
|
298
|
IdentifierManager.getInstance().updateSystemMetadata(sm);
|
299
|
}
|
300
|
} catch (McdbDocNotFoundException e) {
|
301
|
// TODO Auto-generated catch block
|
302
|
e.printStackTrace();
|
303
|
return null;
|
304
|
}
|
305
|
}
|
306
|
|
307
|
return pid;
|
308
|
}
|
309
|
|
310
|
}
|