diff --git a/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/model/ERCloudFilesAttachment.java b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/model/ERCloudFilesAttachment.java index ea4eaee09a6..ce210cf86e9 100644 --- a/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/model/ERCloudFilesAttachment.java +++ b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/model/ERCloudFilesAttachment.java @@ -9,6 +9,7 @@ import com.rackspacecloud.client.cloudfiles.FilesClient; import com.webobjects.eocontrol.EOEditingContext; +import er.attachment.upload.ERRemoteAttachment; import er.extensions.eof.ERXGenericRecord; import er.extensions.foundation.ERXProperties; @@ -17,7 +18,7 @@ * * @author probert */ -public class ERCloudFilesAttachment extends _ERCloudFilesAttachment { +public class ERCloudFilesAttachment extends _ERCloudFilesAttachment implements ERRemoteAttachment { @SuppressWarnings("unused") private static Logger log = Logger.getLogger(ERCloudFilesAttachment.class); public static final String STORAGE_TYPE = "cf"; diff --git a/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/model/ERS3Attachment.java b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/model/ERS3Attachment.java index 5f09d1f84a8..3208c5786fd 100644 --- a/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/model/ERS3Attachment.java +++ b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/model/ERS3Attachment.java @@ -8,6 +8,7 @@ import com.amazon.s3.QueryStringAuthGenerator; import com.webobjects.eocontrol.EOEditingContext; +import er.attachment.upload.ERRemoteAttachment; import er.extensions.eof.ERXGenericRecord; import er.extensions.foundation.ERXProperties; @@ -24,7 +25,7 @@ * * @author mschrag */ -public class ERS3Attachment extends _ERS3Attachment { +public class ERS3Attachment extends _ERS3Attachment implements ERRemoteAttachment { /** * Do I need to update serialVersionUID? * See section 5.6 Type Changes Affecting Serialization on page 51 of the diff --git a/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/processors/ERCloudFilesAttachmentProcessor.java b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/processors/ERCloudFilesAttachmentProcessor.java index 30c1d33a3b7..97f4c728881 100644 --- a/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/processors/ERCloudFilesAttachmentProcessor.java +++ b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/processors/ERCloudFilesAttachmentProcessor.java @@ -22,9 +22,7 @@ import er.attachment.ERAttachmentRequestHandler; import er.attachment.model.ERCloudFilesAttachment; -import er.extensions.concurrency.ERXAsyncQueue; -import er.extensions.eof.ERXEC; -import er.extensions.foundation.ERXExceptionUtilities; +import er.attachment.upload.ERAttachmentUploadQueue; import er.extensions.foundation.ERXProperties; /** @@ -49,7 +47,7 @@ public class ERCloudFilesAttachmentProcessor extends private ERCloudFilesUploadQueue _queue; public ERCloudFilesAttachmentProcessor() { - _queue = new ERCloudFilesUploadQueue(); + _queue = new ERCloudFilesUploadQueue("ERCloudFilesAsyncQueue", this); _queue.start(); } @@ -232,115 +230,33 @@ public void performUpload(File uploadedFile, String originalFileName, } - public class ERCloudFilesQueueEntry { - private File _uploadedFile; - private ERCloudFilesAttachment _attachment; - - public ERCloudFilesQueueEntry(File uploadedFile, ERCloudFilesAttachment attachment) { - _uploadedFile = uploadedFile; - _attachment = attachment; - } - - public File uploadedFile() { - return _uploadedFile; + public class ERCloudFilesUploadQueue extends ERAttachmentUploadQueue { + public ERCloudFilesUploadQueue(String name, ERAttachmentProcessor processor) { + super(name, processor); } - public ERCloudFilesAttachment attachment() { - return _attachment; - } - } - - public class ERCloudFilesUploadQueue extends ERXAsyncQueue { - private EOEditingContext _editingContext; - - public ERCloudFilesUploadQueue() { - super("ERS3AsyncQueue"); - _editingContext = ERXEC.newEditingContext(); - } + @Override + protected void performUpload(ERCloudFilesAttachment attachment, File uploadedFile) throws Exception { + String bucket; + String key; + String mimeType; + String originalFileName = null; - public void enqueue(ERCloudFilesAttachment attachment) { _editingContext.lock(); + try { - ERCloudFilesAttachment localAttachment = attachment - .localInstanceIn(_editingContext); - ERCloudFilesQueueEntry entry = new ERCloudFilesQueueEntry( - attachment._pendingUploadFile(), localAttachment); - enqueue(entry); + bucket = attachment.container(); + key = attachment.key(); + mimeType = attachment.mimeType(); + + if (proxyAsAttachment(attachment)) { + originalFileName = attachment.originalFileName(); + } } finally { _editingContext.unlock(); } - } - @Override - public void process(ERCloudFilesQueueEntry object) { - ERCloudFilesAttachment attachment = object.attachment(); - - File uploadedFile = object.uploadedFile(); - if (uploadedFile != null && uploadedFile.exists()) { - String bucket; - String key; - String mimeType; - String configurationName; - String originalFileName = null; - - _editingContext.lock(); - try { - bucket = attachment.container(); - key = attachment.key(); - mimeType = attachment.mimeType(); - configurationName = attachment.configurationName(); - if (proxyAsAttachment(attachment)) { - originalFileName = attachment.originalFileName(); - } - } finally { - _editingContext.unlock(); - } - - try { - performUpload(uploadedFile, originalFileName, - bucket, key, mimeType, attachment); - - _editingContext.lock(); - try { - attachment.setAvailable(Boolean.TRUE); - _editingContext.saveChanges(); - } finally { - _editingContext.unlock(); - } - if (delegate() != null) { - delegate() - .attachmentAvailable( - ERCloudFilesAttachmentProcessor.this, - attachment); - } - } catch (Throwable t) { - if (delegate() != null) { - delegate() - .attachmentNotAvailable( - ERCloudFilesAttachmentProcessor.this, - attachment, - ERXExceptionUtilities.toParagraph(t)); - } - ERAttachmentProcessor.log.error("Failed to upload '" - + uploadedFile + "' to CloudFiles.", t); - } finally { - if (attachment._isPendingDelete()) { - uploadedFile.delete(); - } - } - } else { - if (delegate() != null) { - delegate() - .attachmentNotAvailable( - ERCloudFilesAttachmentProcessor.this, - attachment, - "Missing attachment file '" + uploadedFile - + "'."); - } - ERAttachmentProcessor.log.error("Missing attachment file '" - + uploadedFile + "'."); - } + ((ERCloudFilesAttachmentProcessor)_processor).performUpload(uploadedFile, originalFileName, bucket, key, mimeType, attachment); } } - } diff --git a/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/processors/ERS3AttachmentProcessor.java b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/processors/ERS3AttachmentProcessor.java index f2183d761fd..cef3ed05e37 100644 --- a/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/processors/ERS3AttachmentProcessor.java +++ b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/processors/ERS3AttachmentProcessor.java @@ -24,9 +24,7 @@ import er.attachment.ERAttachmentRequestHandler; import er.attachment.model.ERS3Attachment; -import er.extensions.concurrency.ERXAsyncQueue; -import er.extensions.eof.ERXEC; -import er.extensions.foundation.ERXExceptionUtilities; +import er.attachment.upload.ERAttachmentUploadQueue; import er.extensions.foundation.ERXProperties; /** @@ -51,7 +49,7 @@ public class ERS3AttachmentProcessor extends private ERS3UploadQueue _queue; public ERS3AttachmentProcessor() { - _queue = new ERS3UploadQueue(); + _queue = new ERS3UploadQueue("ERS3AsyncQueue", this); _queue.start(); } @@ -233,115 +231,31 @@ protected boolean failed(Response response) throws IOException { return (responseCode < 200 || responseCode >= 300); } - public class ERS3QueueEntry { - private File _uploadedFile; - private ERS3Attachment _attachment; - - public ERS3QueueEntry(File uploadedFile, ERS3Attachment attachment) { - _uploadedFile = uploadedFile; - _attachment = attachment; - } - - public File uploadedFile() { - return _uploadedFile; + public class ERS3UploadQueue extends ERAttachmentUploadQueue { + public ERS3UploadQueue(String name, ERAttachmentProcessor processor) { + super(name, processor); } - public ERS3Attachment attachment() { - return _attachment; - } - } - - public class ERS3UploadQueue extends ERXAsyncQueue { - private EOEditingContext _editingContext; - - public ERS3UploadQueue() { - super("ERS3AsyncQueue"); - _editingContext = ERXEC.newEditingContext(); - } + @Override + protected void performUpload(ERS3Attachment attachment, File uploadedFile) throws Exception { + String bucket; + String key; + String mimeType; + String originalFileName = null; - public void enqueue(ERS3Attachment attachment) { _editingContext.lock(); try { - ERS3Attachment localAttachment = attachment - .localInstanceIn(_editingContext); - ERS3QueueEntry entry = new ERS3QueueEntry( - attachment._pendingUploadFile(), localAttachment); - enqueue(entry); + bucket = attachment.bucket(); + key = attachment.key(); + mimeType = attachment.mimeType(); + if (proxyAsAttachment(attachment)) { + originalFileName = attachment.originalFileName(); + } } finally { _editingContext.unlock(); } - } - - @Override - public void process(ERS3QueueEntry object) { - ERS3Attachment attachment = object.attachment(); - - File uploadedFile = object.uploadedFile(); - if (uploadedFile != null && uploadedFile.exists()) { - String bucket; - String key; - String mimeType; - String configurationName; - String originalFileName = null; - - _editingContext.lock(); - try { - bucket = attachment.bucket(); - key = attachment.key(); - mimeType = attachment.mimeType(); - configurationName = attachment.configurationName(); - if (proxyAsAttachment(attachment)) { - originalFileName = attachment.originalFileName(); - } - } finally { - _editingContext.unlock(); - } - try { - performUpload(uploadedFile, originalFileName, - bucket, key, mimeType, attachment); - - _editingContext.lock(); - try { - attachment.setAvailable(Boolean.TRUE); - _editingContext.saveChanges(); - } finally { - _editingContext.unlock(); - } - if (delegate() != null) { - delegate() - .attachmentAvailable( - ERS3AttachmentProcessor.this, - attachment); - } - } catch (Throwable t) { - if (delegate() != null) { - delegate() - .attachmentNotAvailable( - ERS3AttachmentProcessor.this, - attachment, - ERXExceptionUtilities.toParagraph(t)); - } - ERAttachmentProcessor.log.error("Failed to upload '" - + uploadedFile + "' to S3.", t); - } finally { - if (attachment._isPendingDelete()) { - uploadedFile.delete(); - } - } - } else { - if (delegate() != null) { - delegate() - .attachmentNotAvailable( - ERS3AttachmentProcessor.this, - attachment, - "Missing attachment file '" + uploadedFile - + "'."); - } - ERAttachmentProcessor.log.error("Missing attachment file '" - + uploadedFile + "'."); - } + ((ERS3AttachmentProcessor) _processor).performUpload(uploadedFile, originalFileName, bucket, key, mimeType, attachment); } } - } diff --git a/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/upload/ERAttachmentQueueEntry.java b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/upload/ERAttachmentQueueEntry.java new file mode 100644 index 00000000000..0014bc8fa01 --- /dev/null +++ b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/upload/ERAttachmentQueueEntry.java @@ -0,0 +1,35 @@ +package er.attachment.upload; + +import java.io.File; + +import er.attachment.model.ERAttachment; + +/** + * The ERAttachmentQueueEntry is a wrapper that keeps a reference + * to the ERAttachment and the file being enqueued for uploading. + * + * @author Henrique Prange + * + * @param + * the type of the attachment that can queued for uploading. + * + * @see ERRemoteAttachment + * @see ERAttachmentUploadQueue + */ +public class ERAttachmentQueueEntry { + private File _uploadedFile; + private T _attachment; + + public ERAttachmentQueueEntry(File uploadedFile, T attachment) { + _uploadedFile = uploadedFile; + _attachment = attachment; + } + + public File uploadedFile() { + return _uploadedFile; + } + + public T attachment() { + return _attachment; + } +} diff --git a/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/upload/ERAttachmentUploadQueue.java b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/upload/ERAttachmentUploadQueue.java new file mode 100644 index 00000000000..62c90d9943e --- /dev/null +++ b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/upload/ERAttachmentUploadQueue.java @@ -0,0 +1,107 @@ +package er.attachment.upload; + +import java.io.File; + +import com.webobjects.eocontrol.EOEditingContext; + +import er.attachment.model.ERAttachment; +import er.attachment.processors.ERAttachmentProcessor; +import er.extensions.concurrency.ERXAsyncQueue; +import er.extensions.eof.ERXEC; +import er.extensions.eof.ERXEOControlUtilities; +import er.extensions.foundation.ERXExceptionUtilities; + +/** + * The ERAttachmentUploadQueue is a queue to upload attachments. + *

+ * Only ERRemoteAttachments can be upload using this queue. + * + * @author Henrique Prange + * + * @param + * the type of the attachment that can queued for uploading. + * + * @see ERRemoteAttachment + */ +public abstract class ERAttachmentUploadQueue extends ERXAsyncQueue> { + protected final EOEditingContext _editingContext; + protected final ERAttachmentProcessor _processor; + + public ERAttachmentUploadQueue(String name, ERAttachmentProcessor processor) { + super(name); + + _processor = processor; + _editingContext = ERXEC.newEditingContext(); + } + + /** + * Adds an attachment to the end of the queue. + * + * @param attachment + * the attachment to upload + */ + public void enqueue(T attachment) { + _editingContext.lock(); + + try { + T localAttachment = ERXEOControlUtilities.localInstanceOfObject(_editingContext, attachment); + ERAttachmentQueueEntry entry = new ERAttachmentQueueEntry(attachment._pendingUploadFile(), localAttachment); + + enqueue(entry); + } finally { + _editingContext.unlock(); + } + } + + @Override + public void process(ERAttachmentQueueEntry entry) { + T attachment = entry.attachment(); + File uploadedFile = entry.uploadedFile(); + + if (uploadedFile != null && uploadedFile.exists()) { + try { + performUpload(attachment, uploadedFile); + + _editingContext.lock(); + + try { + attachment.setAvailable(Boolean.TRUE); + + _editingContext.saveChanges(); + } finally { + _editingContext.unlock(); + } + + if (_processor.delegate() != null) { + _processor.delegate().attachmentAvailable(_processor, attachment); + } + } catch (Throwable t) { + if (_processor.delegate() != null) { + _processor.delegate().attachmentNotAvailable(_processor, attachment, ERXExceptionUtilities.toParagraph(t)); + } + + ERAttachmentProcessor.log.error("Failed to upload '" + uploadedFile + "' to the remote server.", t); + } finally { + if (attachment._isPendingDelete()) { + uploadedFile.delete(); + } + } + } else { + if (_processor.delegate() != null) { + _processor.delegate().attachmentNotAvailable(_processor, attachment, "Missing attachment file '" + uploadedFile + "'."); + } + + ERAttachmentProcessor.log.error("Missing attachment file '" + uploadedFile + "'."); + } + } + + /** + * Perform the upload of the attachment to the remote server. + * + * @param attachment + * the attachment to upload + * @param uploadedFile + * the file to upload + */ + protected abstract void performUpload(T attachment, File uploadedFile) throws Exception; +} diff --git a/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/upload/ERRemoteAttachment.java b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/upload/ERRemoteAttachment.java new file mode 100644 index 00000000000..d513c5d9a7b --- /dev/null +++ b/Frameworks/BusinessLogic/ERAttachment/Sources/er/attachment/upload/ERRemoteAttachment.java @@ -0,0 +1,18 @@ +package er.attachment.upload; + +import java.io.File; + +/** + * ERRemoteAttachment represents an attachment that should be + * stored in a remote server (service). This kind of attachment can be uploaded + * using an ERAttachmentUploadQueue + * + * @author Henrique Prange + * + * @see ERAttachmentUploadQueue + */ +public interface ERRemoteAttachment { + public File _pendingUploadFile(); + + public boolean _isPendingDelete(); +}