Advertisement
Guest User

Untitled

a guest
Aug 20th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1.     @Override
  2.     public BlobResource persist(BlobResource entity) throws Exception {
  3.         BlobResource result = null;
  4.         if (entity != null){
  5.             logger.info("Bytes to write: " + entity.getBytes().length);
  6.             logger.info("Mime type:  " + entity.getMimeType());
  7.             if (entity.getBytes() != null){
  8.                 try {
  9.                     FileService fileService = FileServiceFactory.getFileService();
  10.                     AppEngineFile file = fileService.createNewBlobFile(entity.getMimeType());
  11.                     String path = file.getFullPath();
  12.                     file = new AppEngineFile(path);
  13.                     boolean lock = true;
  14.                     FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
  15.                     // This time we write to the channel directly
  16.                     writeChannel.write(ByteBuffer.wrap(entity.getBytes()));
  17.                     writeChannel.closeFinally();
  18.                     BlobKey blobKey = fileService.getBlobKey(file);
  19.                     if (blobKey != null){ // Blob is assumed to be written already
  20.                         entity.setBlobKey(blobKey.getKeyString());
  21.                         Key<BlobResource> key = put(entity);
  22.                         try {
  23.                             result = get(key);
  24.                         } catch (Exception e) {
  25.                             throw new BlobResourceException("Error persisting entity");
  26.                         }
  27.                     }
  28.                     logger.info("Blob Key: " + blobKey.getKeyString());
  29.                 } catch (Exception e) {
  30.                     e.printStackTrace();
  31.                     throw new BlobResourceException();
  32.                 }
  33.             } else {
  34.                 throw new NullValueException();
  35.             }
  36.         } else {
  37.             throw new BlobResourceException();
  38.         }
  39.         return result;
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement