22 |
22 |
*/
|
23 |
23 |
package edu.ucsb.nceas.metacat.dataone;
|
24 |
24 |
|
|
25 |
import java.io.ByteArrayInputStream;
|
|
26 |
import java.io.IOException;
|
25 |
27 |
import java.io.InputStream;
|
|
28 |
import java.sql.SQLException;
|
26 |
29 |
import java.util.Date;
|
|
30 |
import java.util.HashMap;
|
27 |
31 |
|
28 |
32 |
import org.apache.log4j.Logger;
|
|
33 |
import org.dataone.cn.batch.utils.TypeMarshaller;
|
29 |
34 |
import org.dataone.service.cn.CNCore;
|
30 |
35 |
import org.dataone.service.exceptions.IdentifierNotUnique;
|
31 |
36 |
import org.dataone.service.exceptions.InsufficientResources;
|
... | ... | |
46 |
51 |
import org.dataone.service.types.ObjectFormatList;
|
47 |
52 |
import org.dataone.service.types.Session;
|
48 |
53 |
import org.dataone.service.types.SystemMetadata;
|
|
54 |
import org.jibx.runtime.JiBXException;
|
49 |
55 |
|
|
56 |
import edu.ucsb.nceas.metacat.DBUtil;
|
|
57 |
import edu.ucsb.nceas.metacat.DocumentImpl;
|
50 |
58 |
import edu.ucsb.nceas.metacat.EventLog;
|
51 |
59 |
import edu.ucsb.nceas.metacat.IdentifierManager;
|
|
60 |
import edu.ucsb.nceas.metacat.McdbException;
|
|
61 |
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
52 |
62 |
import edu.ucsb.nceas.metacat.replication.ForceReplicationSystemMetadataHandler;
|
|
63 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
53 |
64 |
|
54 |
65 |
/**
|
55 |
66 |
*
|
... | ... | |
59 |
70 |
*/
|
60 |
71 |
public class CNCoreImpl implements CNCore
|
61 |
72 |
{
|
62 |
|
private static CNCoreImpl instance = null;
|
|
73 |
private static CNCoreImpl instance = null;
|
63 |
74 |
|
64 |
|
private Logger logMetacat = null;
|
65 |
|
|
66 |
|
/**
|
67 |
|
* singleton accessor
|
68 |
|
*/
|
69 |
|
public static CNCoreImpl getInstance()
|
70 |
|
{
|
71 |
|
if (instance == null) {
|
72 |
|
instance = new CNCoreImpl();
|
73 |
|
}
|
74 |
|
|
75 |
|
return instance;
|
|
75 |
private Logger logMetacat = null;
|
|
76 |
|
|
77 |
/* The scope of the object formats docid used as the metacat identifier */
|
|
78 |
private final String OBJECT_FORMAT_DOCID = "OBJECT_FORMAT_LIST.1";
|
|
79 |
|
|
80 |
/* The revision of the object formats document */
|
|
81 |
private int rev;
|
|
82 |
|
|
83 |
/* The separator of the object formats document */
|
|
84 |
private String separator = ".";
|
|
85 |
|
|
86 |
/* The accession number of the object formats document */
|
|
87 |
private String accNumber = null;
|
|
88 |
|
|
89 |
/* The list of object formats */
|
|
90 |
private ObjectFormatList objectFormatList = null;
|
|
91 |
|
|
92 |
/* the searchable map of object formats */
|
|
93 |
private static HashMap<String, ObjectFormat> objectFormatMap;
|
|
94 |
|
|
95 |
/**
|
|
96 |
* singleton accessor
|
|
97 |
*/
|
|
98 |
public static CNCoreImpl getInstance()
|
|
99 |
{
|
|
100 |
if (instance == null) {
|
|
101 |
instance = new CNCoreImpl();
|
76 |
102 |
}
|
77 |
103 |
|
78 |
|
/**
|
79 |
|
* Constructor, private for singleton access
|
80 |
|
*/
|
81 |
|
private CNCoreImpl() {
|
82 |
|
logMetacat = Logger.getLogger(CNCoreImpl.class);
|
|
104 |
return instance;
|
|
105 |
}
|
|
106 |
|
|
107 |
/**
|
|
108 |
* Constructor, private for singleton access
|
|
109 |
*/
|
|
110 |
private CNCoreImpl() {
|
|
111 |
logMetacat = Logger.getLogger(CNCoreImpl.class);
|
83 |
112 |
|
84 |
|
}
|
|
113 |
}
|
85 |
114 |
|
86 |
115 |
/**
|
87 |
116 |
* register System Metadata without data/metadata object
|
88 |
117 |
*/
|
89 |
|
@Override
|
90 |
|
public boolean registerSystemMetaData(Session session, Identifier guid,
|
91 |
|
SystemMetadata sysmeta) throws NotImplemented, NotAuthorized,
|
92 |
|
ServiceFailure, InvalidRequest {
|
93 |
|
logMetacat.debug("Starting CrudService.create()...");
|
94 |
|
|
95 |
|
// TODO: control who can call this?
|
96 |
|
if (session == null) {
|
97 |
|
//TODO: many of the thrown exceptions do not use the correct error codes
|
98 |
|
//check these against the docs and correct them
|
99 |
|
throw new NotAuthorized("4861", "No Session - could not authorize to registration." +
|
100 |
|
" If you are not logged in, please do so and retry the request.");
|
101 |
|
}
|
102 |
|
|
103 |
|
// verify that guid == SystemMetadata.getIdentifier()
|
104 |
|
logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + sysmeta.getIdentifier().getValue());
|
105 |
|
if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
|
106 |
|
throw new InvalidRequest("4863",
|
107 |
|
"GUID in method call (" + guid.getValue() + ") does not match GUID in system metadata (" +
|
108 |
|
sysmeta.getIdentifier().getValue() + ").");
|
109 |
|
}
|
|
118 |
@Override
|
|
119 |
public boolean registerSystemMetadata(Session session, Identifier guid,
|
|
120 |
SystemMetadata sysmeta) throws NotImplemented, NotAuthorized,
|
|
121 |
ServiceFailure, InvalidRequest, InvalidSystemMetadata {
|
|
122 |
|
|
123 |
// TODO: control who can call this?
|
|
124 |
if (session == null) {
|
|
125 |
//TODO: many of the thrown exceptions do not use the correct error codes
|
|
126 |
//check these against the docs and correct them
|
|
127 |
throw new NotAuthorized("4861", "No Session - could not authorize to registration." +
|
|
128 |
" If you are not logged in, please do so and retry the request.");
|
|
129 |
}
|
|
130 |
|
|
131 |
// verify that guid == SystemMetadata.getIdentifier()
|
|
132 |
logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + sysmeta.getIdentifier().getValue());
|
|
133 |
if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
|
|
134 |
throw new InvalidRequest("4863",
|
|
135 |
"GUID in method call (" + guid.getValue() + ") does not match GUID in system metadata (" +
|
|
136 |
sysmeta.getIdentifier().getValue() + ").");
|
|
137 |
}
|
110 |
138 |
|
111 |
|
logMetacat.debug("Checking if identifier exists...");
|
112 |
|
// Check that the identifier does not already exist
|
113 |
|
IdentifierManager im = IdentifierManager.getInstance();
|
114 |
|
if (im.identifierExists(guid.getValue())) {
|
115 |
|
throw new InvalidRequest("4863",
|
116 |
|
"GUID is already in use by an existing object.");
|
117 |
|
}
|
|
139 |
logMetacat.debug("Checking if identifier exists...");
|
|
140 |
// Check that the identifier does not already exist
|
|
141 |
IdentifierManager im = IdentifierManager.getInstance();
|
|
142 |
if (im.identifierExists(guid.getValue())) {
|
|
143 |
throw new InvalidRequest("4863",
|
|
144 |
"GUID is already in use by an existing object.");
|
|
145 |
}
|
118 |
146 |
|
119 |
|
// insert the system metadata into the object store
|
120 |
|
logMetacat.debug("Starting to insert SystemMetadata...");
|
121 |
|
sysmeta.setDateSysMetadataModified(new Date());
|
122 |
|
try {
|
123 |
|
IdentifierManager.getInstance().createSystemMetadata(sysmeta);
|
124 |
|
IdentifierManager.getInstance().updateSystemMetadata(sysmeta);
|
125 |
|
// force replication of this record
|
126 |
|
ForceReplicationSystemMetadataHandler forceReplication = new ForceReplicationSystemMetadataHandler(guid.getValue(), null);
|
127 |
|
} catch (Exception e) {
|
128 |
|
throw new ServiceFailure("4862", "Error inserting system metadata: " + e.getClass() + ": " + e.getMessage());
|
129 |
|
}
|
130 |
|
|
131 |
|
logMetacat.debug("Returning from registerSystemMetadata");
|
132 |
|
EventLog.getInstance().log(null, session.getSubject().getValue(), guid.getValue(), "registerSystemMetadata");
|
133 |
|
return true;
|
|
147 |
// insert the system metadata into the object store
|
|
148 |
logMetacat.debug("Starting to insert SystemMetadata...");
|
|
149 |
sysmeta.setDateSysMetadataModified(new Date());
|
|
150 |
try {
|
|
151 |
IdentifierManager.getInstance().createSystemMetadata(sysmeta);
|
|
152 |
IdentifierManager.getInstance().updateSystemMetadata(sysmeta);
|
|
153 |
// force replication of this record
|
|
154 |
ForceReplicationSystemMetadataHandler forceReplication =
|
|
155 |
new ForceReplicationSystemMetadataHandler(guid.getValue(), null);
|
|
156 |
} catch (Exception e) {
|
|
157 |
throw new ServiceFailure("4862", "Error inserting system metadata: " + e.getClass() + ": " + e.getMessage());
|
|
158 |
}
|
|
159 |
|
|
160 |
logMetacat.debug("Returning from registerSystemMetadata");
|
|
161 |
EventLog.getInstance().log(null, session.getSubject().getValue(), guid.getValue(), "registerSystemMetadata");
|
|
162 |
return true;
|
|
163 |
|
134 |
164 |
}
|
135 |
165 |
|
136 |
|
|
137 |
166 |
@Override
|
138 |
167 |
public Log getLogRecords(Session arg0, Date arg1, Date arg2,
|
139 |
168 |
Event arg3) throws InvalidToken, InvalidRequest, ServiceFailure,
|
140 |
169 |
NotAuthorized, NotImplemented {
|
141 |
170 |
// TODO Auto-generated method stub
|
142 |
171 |
return null;
|
143 |
|
}
|
144 |
|
@Override
|
145 |
|
public ObjectFormat getFormat(ObjectFormatIdentifier arg0)
|
146 |
|
throws InvalidRequest, ServiceFailure, NotFound, InsufficientResources,
|
147 |
|
NotImplemented {
|
148 |
|
// TODO Auto-generated method stub
|
149 |
|
return null;
|
|
172 |
}
|
|
173 |
|
|
174 |
/**
|
|
175 |
* Return the object format based on the given object format identifier
|
|
176 |
* @param fmtid - the object format identifier to look up
|
|
177 |
* @return objectFormat - the desired object format
|
|
178 |
*/
|
|
179 |
@Override
|
|
180 |
public ObjectFormat getFormat(ObjectFormatIdentifier fmtid)
|
|
181 |
throws InvalidRequest, ServiceFailure, NotFound, InsufficientResources,
|
|
182 |
NotImplemented {
|
|
183 |
|
|
184 |
logMetacat.debug("CNCoreImpl.getFormat() called.");
|
|
185 |
|
|
186 |
ObjectFormat objectFormat = null;
|
|
187 |
|
|
188 |
// look up the format in the object format map
|
|
189 |
objectFormat = getObjectFormatMap().get(fmtid.getValue());
|
|
190 |
|
|
191 |
// refresh the object format list if the format is null
|
|
192 |
if ( objectFormat == null ) {
|
|
193 |
getCachedList();
|
|
194 |
objectFormat = getObjectFormatMap().get(fmtid.getValue());
|
|
195 |
|
|
196 |
// the object format isn't registered
|
|
197 |
if ( objectFormat == null ) {
|
|
198 |
throw new NotFound("4848", "The format specified by " + fmtid.getValue() +
|
|
199 |
" does not exist at this node.");
|
|
200 |
|
|
201 |
}
|
|
202 |
|
|
203 |
}
|
|
204 |
|
|
205 |
return objectFormat;
|
150 |
206 |
}
|
151 |
207 |
|
152 |
208 |
@Override
|
... | ... | |
155 |
211 |
return null;
|
156 |
212 |
}
|
157 |
213 |
|
|
214 |
/**
|
|
215 |
* Return the list of object formats registered from the Coordinating Node.
|
|
216 |
* @return objectFormatList - the list of object formats
|
|
217 |
*/
|
158 |
218 |
@Override
|
159 |
219 |
public ObjectFormatList listFormats() throws InvalidRequest, ServiceFailure,
|
160 |
220 |
NotFound, InsufficientResources, NotImplemented {
|
161 |
|
// TODO Auto-generated method stub
|
162 |
|
return null;
|
|
221 |
|
|
222 |
try {
|
|
223 |
objectFormatList = getInstance().getCachedList();
|
|
224 |
|
|
225 |
} catch (ServiceFailure sfe) {
|
|
226 |
throw sfe;
|
|
227 |
|
|
228 |
} catch (NotFound nfe) {
|
|
229 |
throw nfe;
|
|
230 |
|
|
231 |
}
|
|
232 |
|
|
233 |
return objectFormatList;
|
163 |
234 |
}
|
164 |
235 |
|
165 |
236 |
@Override
|
... | ... | |
180 |
251 |
return null;
|
181 |
252 |
}
|
182 |
253 |
|
|
254 |
/*
|
|
255 |
* Return the hash containing the fmtid and format mapping
|
|
256 |
*
|
|
257 |
* @return objectFormatMap - the hash of fmtid/format pairs
|
|
258 |
*/
|
|
259 |
private HashMap<String, ObjectFormat> getObjectFormatMap() {
|
|
260 |
|
|
261 |
if ( objectFormatMap == null ) {
|
|
262 |
objectFormatMap = new HashMap<String, ObjectFormat>();
|
|
263 |
|
|
264 |
}
|
|
265 |
return objectFormatMap;
|
|
266 |
|
|
267 |
}
|
|
268 |
|
|
269 |
/**
|
|
270 |
* Get the object format list cached in Metacat
|
|
271 |
*/
|
|
272 |
private ObjectFormatList getCachedList()
|
|
273 |
throws NotFound, ServiceFailure {
|
|
274 |
|
|
275 |
ObjectFormatList objectFormatList = null;
|
|
276 |
|
|
277 |
try {
|
|
278 |
|
|
279 |
// reset the accession number separator in case it is
|
|
280 |
// different than the default
|
|
281 |
try {
|
|
282 |
|
|
283 |
separator = PropertyService.getProperty("document.accNumSeparator");
|
|
284 |
|
|
285 |
} catch ( PropertyNotFoundException pnfe ) {
|
|
286 |
|
|
287 |
// use the default separator, but log the issue
|
|
288 |
logMetacat.debug("There was a problem finding the document " +
|
|
289 |
"separator property. The error message was: " + pnfe.getMessage());
|
|
290 |
}
|
|
291 |
|
|
292 |
// get the latest accession number if it is in Metacat
|
|
293 |
this.rev =
|
|
294 |
DBUtil.getLatestRevisionInDocumentTable(this.OBJECT_FORMAT_DOCID);
|
|
295 |
|
|
296 |
if ( this.rev != -1 ) {
|
|
297 |
this.accNumber = this.OBJECT_FORMAT_DOCID +
|
|
298 |
this.separator +
|
|
299 |
this.rev;
|
|
300 |
DocumentImpl objectFormatsDocument = new DocumentImpl(accNumber, false);
|
|
301 |
ByteArrayInputStream bais =
|
|
302 |
new ByteArrayInputStream(objectFormatsDocument.toString().getBytes());
|
|
303 |
|
|
304 |
// deserialize the object format list
|
|
305 |
try {
|
|
306 |
|
|
307 |
objectFormatList =
|
|
308 |
TypeMarshaller.unmarshalTypeFromStream(ObjectFormatList.class, bais);
|
|
309 |
|
|
310 |
} catch (IOException e) {
|
|
311 |
throw new ServiceFailure("4841", "Unexpected exception from the service - " +
|
|
312 |
e.getClass() + ": " + e.getMessage());
|
|
313 |
|
|
314 |
} catch (InstantiationException e) {
|
|
315 |
throw new ServiceFailure("4841", "Unexpected exception from the service - " +
|
|
316 |
e.getClass() + ": " + e.getMessage());
|
|
317 |
|
|
318 |
} catch (IllegalAccessException e) {
|
|
319 |
throw new ServiceFailure("4841", "Unexpected exception from the service - " +
|
|
320 |
e.getClass() + ": " + e.getMessage());
|
|
321 |
|
|
322 |
} catch (JiBXException e) {
|
|
323 |
throw new ServiceFailure("4841", "Unexpected exception from the service - " +
|
|
324 |
e.getClass() + ": " + e.getMessage());
|
|
325 |
|
|
326 |
}
|
|
327 |
|
|
328 |
} else {
|
|
329 |
throw new NotFound("4843", "The object formats collection could not " +
|
|
330 |
"be found at this node.");
|
|
331 |
}
|
|
332 |
|
|
333 |
} catch ( SQLException sqle ) {
|
|
334 |
throw new ServiceFailure("4841", "Unexpected exception from the service - " +
|
|
335 |
sqle.getClass() + ": " + sqle.getMessage());
|
|
336 |
|
|
337 |
} catch (McdbException mcdbe) {
|
|
338 |
throw new ServiceFailure("4841", "Unexpected exception from the service - " +
|
|
339 |
mcdbe.getClass() + ": " + mcdbe.getMessage());
|
|
340 |
|
|
341 |
}
|
|
342 |
|
|
343 |
// index the object format list based on the format identifier string
|
|
344 |
int listSize = objectFormatList.sizeObjectFormats();
|
|
345 |
|
|
346 |
for (int i = 0; i < listSize; i++ ) {
|
|
347 |
|
|
348 |
ObjectFormat objectFormat =
|
|
349 |
objectFormatList.getObjectFormat(i);
|
|
350 |
String identifier = objectFormat.getFmtid().getValue();
|
|
351 |
getObjectFormatMap().put(identifier, objectFormat);
|
|
352 |
|
|
353 |
}
|
|
354 |
|
|
355 |
return objectFormatList;
|
|
356 |
|
|
357 |
}
|
|
358 |
|
183 |
359 |
}
|
184 |
360 |
|
Updated CNCoreImpl to implement listFormats() and getFormat(), and changed calls to ObjectFormatCache in IdentifierManager, MetacatHandler to call getInstance(). Removed the ObjectFormatService registration from MetaCatServlet since it is replaced by CNCoreImpl.