Project

General

Profile

« Previous | Next » 

Revision 6124

Merged in the D1_0_6_2_BRANCH changes that include the transition from ObjectFormat calls to ObjectFormatCache calls.

View differences:

ObjectFormatService.java
48 48

  
49 49
import org.dataone.client.CNode;
50 50
import org.dataone.client.D1Client;
51
import org.dataone.client.ObjectFormatCache;
51 52
import org.dataone.service.exceptions.InsufficientResources;
52 53
import org.dataone.service.exceptions.InvalidRequest;
53 54
import org.dataone.service.exceptions.NotFound;
......
55 56
import org.dataone.service.exceptions.ServiceFailure;
56 57
import org.dataone.service.types.Identifier;
57 58
import org.dataone.service.types.ObjectFormat;
59
import org.dataone.service.types.ObjectFormatList;
58 60
//import org.dataone.service.types.ObjectFormatList;
59 61
import org.dataone.service.types.util.ServiceTypeUtil;
60 62
import org.jibx.runtime.JiBXException;
......
87 89
  private String accNumber = null;
88 90

  
89 91
  /* The list of object formats */
90
  //private static ObjectFormatList objectFormatList = null;
92
  private static ObjectFormatList objectFormatList = null;
91 93
  
92 94
  /* the searchable map of object formats */
93 95
  private static HashMap<String, ObjectFormat> objectFormatMap = 
......
182 184
      throw new ServiceException(message);
183 185
    }
184 186
    
185
    // For DataONE Member Nodes, get the authoritative list from the 
186
    // Coordinating Node, otherwise, get the cached list
187
    if ( this.nodeType.equals("mn") ) {
188
      
189
      /*
190
       *try {
191
       *  
192
       *  // try to get the authoritative list remotely
193
       *  D1Client d1Client = new D1Client(coordinatingNodeBaseURL);
194
       *  this.cn = D1Client.getCN();
195
       *  this.objectFormatList = cn.listFormats();
196
       *
197
       *} catch (ServiceFailure e) {
198
       *  
199
       *  logMetacat.debug("There was a getting the object format list from " +
200
       *    "the coordinating node. The message was: " +
201
       *    e.getMessage());
202
       *  this.getCachedList();  
203
       *  
204
       *} catch (NotImplemented e) {
205
       *  
206
       *  logMetacat.debug("There was a getting the object format list from " +
207
       *    "the coordinating node. The message was: " + e.getMessage());
208
       *  this.getCachedList();  
209
       *  
210
       *} catch (InvalidRequest e) {
211
       *  
212
       *  logMetacat.debug("There was a getting the object format list from " +
213
       *    "the coordinating node. The message was: " + e.getMessage());
214
       *  this.getCachedList();  
215
       *  
216
       *} catch (NotFound e) {
217
       *  
218
       *  logMetacat.debug("There was a getting the object format list from " +
219
       *    "the coordinating node. The message was: " + e.getMessage());
220
       *  this.getCachedList();  
221
       *  
222
       *} catch (InsufficientResources e) {
223
       *  
224
       *  logMetacat.debug("There was a getting the object format list from " +
225
       *    "the coordinating node. The message was: " + e.getMessage());
226
       *  this.getCachedList();  
227
       *  
228
       *}
229
       */
230
      
231
      // this will be replaced with external lookup code above
232
      //this.getCachedList();
233
      
234
    } else if ( this.nodeType.equals("cn") ) {
235
      
236
      // get the list from the local metacat database
237
      //this.getCachedList();  
238

  
239
    } else {
240
      
241
      // the node type is not recognized
242
      throw new ServiceException("An error occurred in initializing the " +
243
        "ObjectFormatService. The DataONE node type needs to be either "  +
244
        "'cn' or 'mn', but instead was: " + this.nodeType);
245
    }
246
    
247
    // index the object format list based on the format identifier
248
    //int listSize = ObjectFormatService.objectFormatList.sizeObjectFormats();
249
    //
250
    //for (int i = 0; i < listSize; i++ ) {
251
    //  
252
    //  ObjectFormat objectFormat = 
253
    //    ObjectFormatService.objectFormatList.getObjectFormat(i);
254
    //  String identifier = objectFormat.getFmtid();
255
    //  ObjectFormatService.objectFormatMap.put(identifier, objectFormat);
256
    //  
257
    //}
258
        
259

  
260
    
187
    this.getCachedList();  
261 188
  }
262 189

  
263 190
  /**
......
287 214
   * 
288 215
   * @return objectFormatList - the list of object formats
289 216
   */
290
  //public ObjectFormatList listFormats() {
291
  //  
292
  //  return ObjectFormatService.objectFormatList;
293
  //  
294
  //}
217
  public ObjectFormatList listFormats() {
218
    
219
    return ObjectFormatService.objectFormatList;
220
    
221
  }
295 222
  
296 223
  /**
297 224
   * Get the object format based on the given identifier as a string.
......
299 226
   * @param format - the object format as a string
300 227
   * @return objectFormat - the ObjectFormat represented by the format string
301 228
   */
302
  //public static ObjectFormat getFormat(String format) {
303
  //  
304
  //  ObjectFormat objectFormat = null;
305
  //  objectFormat = ObjectFormatService.objectFormatMap.get(format);
306
  //  
307
  //  return objectFormat;
308
  //  
309
  //}
229
  public static ObjectFormat getFormat(String format) {
230
    
231
    ObjectFormat objectFormat = null;
232
    objectFormat = ObjectFormatService.objectFormatMap.get(format);
233
    
234
    return objectFormat;
235
    
236
  }
310 237
  /**
311 238
   * Get the object format list cached in Metacat, or on failure, via a
312 239
   * cached file on disk
313 240
   * 
314 241
   * @return objectFormatList - the cached object format list
315 242
   */
316
  //private void getCachedList()
317
  //  throws ServiceException {
318
  //        
319
  //  try {
320
  //    
321
  //    // reset the accession number separator in case it is 
322
  //    // different than the default
323
  //    try {
324
  //      
325
  //      this.separator = PropertyService.getProperty("document.accNumSeparator");
326
  //      
327
  //    } catch ( PropertyNotFoundException pnfe ) {
328
  //      
329
  //      // use the default separator, but log the issue
330
  //      logMetacat.debug("There was a problem finding the document " +
331
  //        "separator property. The error message was: " + pnfe.getMessage());
332
  //    }
333
  //    
334
  //    // get the latest accession number if it is in Metacat
335
  //    this.rev = 
336
  //      DBUtil.getLatestRevisionInDocumentTable(this.OBJECT_FORMAT_DOCID);
337
  //    
338
  //    if ( this.rev != -1 ) {
339
  //      this.accNumber = this.OBJECT_FORMAT_DOCID + 
340
  //                       this.separator + 
341
  //                       this.rev;
342
  //      DocumentImpl objectFormatsDocument = new DocumentImpl(accNumber, false);
343
  //      ByteArrayInputStream bais = 
344
  //        new ByteArrayInputStream(objectFormatsDocument.toString().getBytes());
345
  //      ObjectFormatService.objectFormatList = 
346
  //        this.deserializeObjectFormatList(bais);
347
  //
348
  //    } else {
349
  //      
350
  //      logMetacat.info("The latest revision number of "   + 
351
  //        this.OBJECT_FORMAT_DOCID                         +
352
  //        " could not be found. Reverting to the on-disk " +
353
  //        "object format list");
354
  //      try {
355
  //        
356
  //        ObjectFormatService.objectFormatList = this.getListFromDisk();
357
  //      
358
  //      } catch (ServiceException se) {
359
  //        
360
  //        throw new ServiceException(se.getMessage());
361
  //      
362
  //      }
363
  //
364
  //    }
365
  //    
366
  //  } catch ( SQLException sqle ) {
367
  //    
368
  //    // we either have a metacat error or the docid was not inserted yet
369
  //    logMetacat.info("The latest revision number of "   + 
370
  //      this.OBJECT_FORMAT_DOCID                         +
371
  //      " could not be found. Reverting to the on-disk " +
372
  //      "object format list");
373
  //    
374
  //    try {
375
  //      
376
  //      ObjectFormatService.objectFormatList = this.getListFromDisk();
377
  //    
378
  //    } catch (ServiceException se) {
379
  //      
380
  //      throw se; 
381
  //      
382
  //    }
383
  //    
384
  //  } catch (McdbException mcdbe) {
385
  //    
386
  //    // we have a metacat doc not found error
387
  //    logMetacat.info( this.OBJECT_FORMAT_DOCID          +
388
  //      " could not be found. Reverting to the on-disk " +
389
  //      "object format list");
390
  //    
391
  //    try {
392
  //      
393
  //      ObjectFormatService.objectFormatList = this.getListFromDisk();
394
  //    
395
  //    } catch (ServiceException se) {
396
  //      
397
  //      throw se; 
398
  //      
399
  //    }
400
  //    
401
  //
402
  //  }
403
  //
404
  //  return;
405
  //  
406
  //}
243
  private void getCachedList()
244
    throws ServiceException {
245
          
246
    try {
247
      
248
      // reset the accession number separator in case it is 
249
      // different than the default
250
      try {
251
        
252
        this.separator = PropertyService.getProperty("document.accNumSeparator");
253
        
254
      } catch ( PropertyNotFoundException pnfe ) {
255
        
256
        // use the default separator, but log the issue
257
        logMetacat.debug("There was a problem finding the document " +
258
          "separator property. The error message was: " + pnfe.getMessage());
259
      }
260
      
261
      // get the latest accession number if it is in Metacat
262
      this.rev = 
263
        DBUtil.getLatestRevisionInDocumentTable(this.OBJECT_FORMAT_DOCID);
264
      
265
      if ( this.rev != -1 ) {
266
        this.accNumber = this.OBJECT_FORMAT_DOCID + 
267
                         this.separator + 
268
                         this.rev;
269
        DocumentImpl objectFormatsDocument = new DocumentImpl(accNumber, false);
270
        ByteArrayInputStream bais = 
271
          new ByteArrayInputStream(objectFormatsDocument.toString().getBytes());
272
        ObjectFormatService.objectFormatList = 
273
          this.deserializeObjectFormatList(bais);
274
  
275
      } else {
276
        
277
        logMetacat.info("The latest revision number of "   + 
278
          this.OBJECT_FORMAT_DOCID                         +
279
          " could not be found. Reverting to the on-disk " +
280
          "object format list");
281
          
282
        ObjectFormatService.objectFormatList = ObjectFormatCache.listFormats();
283
      }
284
      
285
    } catch ( SQLException sqle ) {
286
      
287
      // we either have a metacat error or the docid was not inserted yet
288
      logMetacat.info("The latest revision number of "   + 
289
        this.OBJECT_FORMAT_DOCID                         +
290
        " could not be found. Reverting to the on-disk " +
291
        "object format list");
292
        
293
      ObjectFormatService.objectFormatList = ObjectFormatCache.listFormats();
294
      
295
    } catch (McdbException mcdbe) {
296
      
297
      // we have a metacat doc not found error
298
      logMetacat.info( this.OBJECT_FORMAT_DOCID          +
299
        " could not be found. Reverting to the on-disk " +
300
        "object format list");
301
      
302
      ObjectFormatService.objectFormatList = ObjectFormatCache.listFormats();
303
  
304
    }
305
  
306
    return;
307
    
308
  }
407 309

  
408 310
  /**
409
   * Get the object format list cached on disk as stated in the metacat
410
   * properties file.
411
   * 
412
   * @return objectFormatList - the on-disk object format list
413
   * @throws ServiceException
414
   */
415
  //private ObjectFormatList getListFromDisk() 
416
  //  throws ServiceException {
417
  //  
418
  //  // the filesystem location of the object format list xml file
419
  //  String objectFormatFilePath = null;
420
  //  
421
  //  // get the object format list from disk and parse it
422
  //  try {
423
  //    objectFormatFilePath = SystemUtil.getContextDir() + "/WEB-INF/" +
424
  //      PropertyService.getProperty("dataone.objectFormatListName");
425
  //          
426
  //    FileInputStream fileInputStream = new FileInputStream(objectFormatFilePath);         
427
  //
428
  //    ObjectFormatService.objectFormatList = 
429
  //      this.deserializeObjectFormatList(fileInputStream);
430
  //
431
  //  } catch (PropertyNotFoundException pnfe) {
432
  //    
433
  //    logMetacat.error("The object format list file could not be parsed. " +
434
  //      "The error message was: " + pnfe.getMessage());
435
  //    throw new ServiceException(pnfe.getMessage());
436
  //    
437
  //  } catch ( FileNotFoundException fnfe ) {
438
  //    
439
  //    logMetacat.debug("There was an error reading the file " +
440
  //      objectFormatFilePath + ". The error was: "            +
441
  //      fnfe.getMessage());
442
  //    throw new ServiceException(fnfe.getMessage());
443
  //        
444
  //  }
445
  //  
446
  //  return ObjectFormatService.objectFormatList;
447
  //}
448

  
449
  /**
450 311
   * Deserialize the object format list file to an object format list object
451 312
   * 
452 313
   * @param fileInputStream - the file input stream of the XML object format list file
453 314
   * @return objectFormatList - the ObjectFormatList object from the XML stream
454 315
   * @throws ServiceException
455 316
   */
456
  //private ObjectFormatList deserializeObjectFormatList(InputStream inputStream) 
457
  //  throws ServiceException {
458
  //  
459
  //  ObjectFormatList objectFormatList;
460
  //  try {
461
  //    objectFormatList = (ObjectFormatList)
462
  //    ServiceTypeUtil.deserializeServiceType(ObjectFormatList.class, inputStream);
463
  //  
464
  //  } catch ( JiBXException jbxe ) {
465
  //    
466
  //    logMetacat.debug("There was an error deserializing the input stream. " +
467
  //      "The error was: " + jbxe.getMessage());
468
  //    jbxe.printStackTrace();
469
  //    throw new ServiceException(jbxe.getMessage());
470
  //  }
471
  //  
472
  //  return objectFormatList;
473
  //}
317
  private ObjectFormatList deserializeObjectFormatList(InputStream inputStream) 
318
    throws ServiceException {
319
    
320
    ObjectFormatList objectFormatList;
321
    try {
322
      objectFormatList = (ObjectFormatList)
323
      ServiceTypeUtil.deserializeServiceType(ObjectFormatList.class, inputStream);
324
    
325
    } catch ( JiBXException jbxe ) {
326
      
327
      logMetacat.debug("There was an error deserializing the input stream. " +
328
        "The error was: " + jbxe.getMessage());
329
      jbxe.printStackTrace();
330
      throw new ServiceException(jbxe.getMessage());
331
    }
332
    
333
    return objectFormatList;
334
  }
474 335
  
475 336
}

Also available in: Unified diff