Project

General

Profile

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;
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.client.MNode;
29
import org.dataone.configuration.Settings;
30
import org.dataone.service.exceptions.InsufficientResources;
31
import org.dataone.service.exceptions.InvalidRequest;
32
import org.dataone.service.exceptions.InvalidToken;
33
import org.dataone.service.exceptions.NotAuthorized;
34
import org.dataone.service.exceptions.NotFound;
35
import org.dataone.service.exceptions.NotImplemented;
36
import org.dataone.service.exceptions.ServiceFailure;
37
import org.dataone.service.exceptions.UnsupportedType;
38
import org.dataone.service.types.v1.Identifier;
39
import org.dataone.service.types.v1.Node;
40
import org.dataone.service.types.v1.Permission;
41
import org.dataone.service.types.v1.Session;
42
import org.dataone.service.types.v1.Subject;
43
import org.dataone.service.types.v1.SystemMetadata;
44

    
45
import quicktime.std.qtcomponents.SCInfo;
46

    
47
import com.hazelcast.core.Hazelcast;
48
import com.hazelcast.core.HazelcastInstance;
49
import com.hazelcast.core.IMap;
50

    
51
import edu.ucsb.nceas.metacat.IdentifierManager;
52
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
53

    
54
/**
55
 * A single CN replication task to be executed by the CN Replication Service. This 
56
 * applies to replication of system metadata, science metadata, and resource maps 
57
 * across CNs.
58
 * 
59
 * The task is built when a change occurs in the hzSystemMetadata map in the
60
 * storage cluster initiated by Metacat for a given PID. If the change involves
61
 * only modifications to system metadata, those changes will occur in the shared
62
 * map and in the backing store of the member that owns the map entry. Other 
63
 * members must be updated using this task.  Likewise, changes to the system
64
 * metadata map that involve science metadata creation or changes will be
65
 * distributed to all members via this task.  The same is for OAI-ORE resource maps.
66
 * 
67
 * @author cjones
68
 *
69
 */
70
public class CNReplicationTask implements Serializable, Callable<Identifier> {
71

    
72
  /* The identifier of this task */
73
  private String taskid;
74
  
75
  /* The identifier of the object to replicate */
76
  private Identifier pid;
77
  
78
  /* The object format type stated in the system metadata (DATA/METADATA/RESOURCE) */
79
  private String formatType;
80
  
81
  /* The target Node object */
82
  private Node targetNode;
83
  
84
  /* The originating Node object */
85
  private Node originatingNode;
86

    
87
  /* The subject of the target node, extracted from the Node object */
88
  private String targetNodeSubject;
89
  
90
  /* The subject of the originating node, extracted from the Node object */
91
  private String originatingNodeSubject;
92
  
93
  /* The permission to be executed (in this case, always 'replicate') */
94
  String permission;
95

    
96
  /**
97
   * Constructor - create an empty replication task instance
98
   */
99
  public CNReplicationTask() {
100
  }
101

    
102
  /**
103
   * Constructor - create a replication task instance
104
   * 
105
   * @param taskid
106
   * @param pid
107
   * @param targetNode
108
   */
109
  public CNReplicationTask(String taskid, Identifier pid, String formatType,
110
    Node originatingNode, Node targetNode,
111
    Permission replicatePermission) {
112
    
113
    this.taskid = taskid;
114
    this.pid = pid;
115
    this.formatType = formatType;
116
    this.originatingNode = originatingNode;
117
    this.targetNode = targetNode;
118
    this.originatingNodeSubject = originatingNode.getSubject(0).getValue();
119
    this.targetNodeSubject = targetNode.getSubject(0).getValue();
120
    this.permission = replicatePermission.name();
121
    
122
  }
123

    
124
  /**
125
   * Get the task identifier for this task
126
   * @return the taskid
127
   */
128
  public String getTaskid() {
129
    return taskid;
130
  }
131

    
132
  /**
133
   * Set the task identifier for this task
134
   * @param taskid the taskid to set
135
   */
136
  public void setTaskid(String taskid) {
137
    this.taskid = taskid;
138
  }
139

    
140
  /**
141
   * Get the object identifier to be replicated
142
   * @return the pid
143
   */
144
  public Identifier getPid() {
145
    return pid;
146
  }
147

    
148
  /**
149
   * Set the object identifier to be replicated
150
   * @param pid the pid to set
151
   */
152
  public void setPid(Identifier pid) {
153
    this.pid = pid;
154
  }
155

    
156

    
157
  /**
158
   * Get the format type of the object to be replicated
159
   * @return the pid
160
   */
161
  public String getFormatType() {
162
    return this.formatType;
163
    
164
  }
165

    
166
  /**
167
   * Set the format type of the object to be replicated
168
   * @param pid the pid to set
169
   */
170
  public void setFormatType(String formatType) {
171
    this.formatType = formatType;
172
    
173
  }
174

    
175
  /**
176
   * Get the target node
177
   * @return the targetNode
178
   */
179
  public Node getTargetNode() {
180
    return targetNode;
181
  }
182

    
183
  /**
184
   * Set the target node
185
   * @param targetNode the targetNode to set
186
   */
187
  public void setTargetNode(Node targetNode) {
188
    this.targetNode = targetNode;
189
  }
190

    
191
  /**
192
   * Get the originating node
193
   * @return the originatingNode
194
   */
195
  public Node getOriginatingNode() {
196
    return originatingNode;
197
  }
198

    
199
  /**
200
   * Set the originating node
201
   * @param originatingNode the originatingNode to set
202
   */
203
  public void setOriginatingNode(Node originatingNode) {
204
    this.originatingNode = originatingNode;
205
  }
206

    
207
  /**
208
   * For the given Replication task, return the Subject listed in the target
209
   * node.  Usually used in authorizing a replication event.
210
   * 
211
   * @return subject - the subject listed in the target Node object as a string
212
   */
213
  public String getTargetNodeSubject() {
214
    
215
    return this.targetNodeSubject;
216
    
217
  }
218
  
219
  /**
220
   * Set the target node subject identifying the node
221
   * @param subject the targetNode subject
222
   */
223
  public void setTargetNodeSubject(String subject) {
224
    this.targetNodeSubject = subject;
225
  }
226
  
227
  /**
228
   * For the given Replication task, return the Subject listed in the target
229
   * node.  Usually used in authorizing a replication event.
230
   * 
231
   * @return subject - the subject listed in the target Node object as a string
232
   */
233
  public String getOriginatingNodeSubject() {
234
    
235
    return this.originatingNodeSubject;
236
    
237
  }
238
  
239
  /**
240
   * Set the target node subject identifying the node
241
   * @param subject the targetNode subject
242
   */
243
  public void setOriginatingNodeSubject(String subject) {
244
    this.originatingNodeSubject = subject;
245
  }
246
  
247
  /**
248
   * Get the permission being allowed for this task
249
   * 
250
   * @return subject - the subject listed in the target Node object
251
   */
252
  public String getPermission() {
253
    return this.permission;
254
    
255
  }
256
  
257
  /**
258
   * Set the permission being allowed for this task
259
   * @param subject the targetNode subject
260
   */
261
  public void setPermission(Permission permission) {
262
    this.permission = permission.name();
263
    
264
  }
265
 
266
  /**
267
   * Implement the Callable interface, providing code that initiates replication.
268
   * 
269
   * @return pid - the identifier of the replicated object upon success
270
   */
271
  public Identifier call() {
272
		
273
	// Get the D1 Hazelcast configuration parameters
274
	String hzSystemMetadata = 
275
		Settings.getConfiguration().getString("dataone.hazelcast.systemMetadata");
276
	
277
	// get the system metadata for the pid	
278
	IMap<Identifier, SystemMetadata> sysMetaMap = Hazelcast.getMap(hzSystemMetadata);
279
	
280
	// get the systemMetadata
281
	SystemMetadata sm = sysMetaMap.get(pid);
282
	
283
	
284
  	
285
  	// only system metadata - no data replicated
286
  	boolean isData = formatType.equals("??");
287
  	if (!isData) {
288
  		// TODO: get the science metadata/ORE from somewhere
289
  		InputStream sciMetaORE = null;
290
		try {
291
			sciMetaORE = CNodeService.getInstance().get(null, pid);
292
		} catch (NotFound nf) {
293
			try {
294
				sciMetaORE = D1Client.getCN().get(null, pid);
295
				// save it locally
296
		  		CNodeService.getInstance().create(null, pid, sciMetaORE, sm);
297
			} catch (Exception e) {
298
				// TODO Auto-generated catch block
299
				e.printStackTrace();
300
			}
301
		} catch (Exception e) {
302
			e.printStackTrace();
303
		}
304
  	} else {
305
  		// just system metadata
306
  		try {
307
  			if (!IdentifierManager.getInstance().identifierExists(pid.getValue())) {
308
  				IdentifierManager.getInstance().createSystemMetadata(sm);
309
  			} else {
310
  				IdentifierManager.getInstance().updateSystemMetadata(sm);
311
  			}  
312
  	  	} catch (McdbDocNotFoundException e) {
313
  			// TODO Auto-generated catch block
314
  			e.printStackTrace();
315
  			return null;
316
  		}
317
  	}
318
  	
319
    return pid;
320
  }
321

    
322
}
(1-1/7)