X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.activemq%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fjms%2FJmsAttachmentUploader.java;h=bdc242c9475d573bb1074aa78714f2346d3cfd61;hb=1fdb1b4e7b1d2b0cabb6483238301b857a6392fa;hp=44dcd3e1ba4eb8c778422d509ea92cd167a53553;hpb=ad602245885ef5a89fa056d33848dc6538b56f34;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAttachmentUploader.java b/runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAttachmentUploader.java index 44dcd3e1b..bdc242c94 100644 --- a/runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAttachmentUploader.java +++ b/runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAttachmentUploader.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2010 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.argeo.slc.jms; import java.io.IOException; @@ -10,14 +26,20 @@ import javax.jms.Message; import javax.jms.Session; import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.argeo.slc.SlcException; import org.argeo.slc.core.attachment.Attachment; import org.argeo.slc.core.attachment.AttachmentUploader; import org.springframework.core.io.Resource; +import org.springframework.jms.JmsException; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; public class JmsAttachmentUploader implements AttachmentUploader { + private final static Log log = LogFactory + .getLog(JmsAttachmentUploader.class); + public final static String ATTACHMENT_ID = "slc_attachmentId"; public final static String ATTACHMENT_NAME = "slc_attachmentName"; public final static String ATTACHMENT_CONTENT_TYPE = "slc_attachmentContentType"; @@ -26,36 +48,44 @@ public class JmsAttachmentUploader implements AttachmentUploader { private Destination destination; public void upload(final Attachment attachment, final Resource resource) { - jmsTemplate.send(destination, new MessageCreator() { + try { + jmsTemplate.send(destination, new MessageCreator() { - public Message createMessage(Session session) throws JMSException { - BytesMessage message = session.createBytesMessage(); - message.setStringProperty(ATTACHMENT_ID, attachment.getUuid()); - message - .setStringProperty(ATTACHMENT_NAME, attachment - .getName()); - message.setStringProperty(ATTACHMENT_CONTENT_TYPE, attachment - .getContentType()); + public Message createMessage(Session session) + throws JMSException { + BytesMessage message = session.createBytesMessage(); + message.setStringProperty(ATTACHMENT_ID, attachment + .getUuid()); + message.setStringProperty(ATTACHMENT_NAME, attachment + .getName()); + message.setStringProperty(ATTACHMENT_CONTENT_TYPE, + attachment.getContentType()); - InputStream in = null; - try { - in = resource.getInputStream(); - byte[] buffer = new byte[1024 * 1024]; - int read = -1; - while ((read = in.read(buffer)) > 0) { - message.writeBytes(buffer, 0, read); + InputStream in = null; + try { + in = resource.getInputStream(); + byte[] buffer = new byte[1024 * 1024]; + int read = -1; + while ((read = in.read(buffer)) > 0) { + message.writeBytes(buffer, 0, read); + } + } catch (IOException e) { + throw new SlcException( + "Cannot write into byte message for attachment " + + attachment + " and resource " + + resource, e); + } finally { + IOUtils.closeQuietly(in); } - } catch (IOException e) { - throw new SlcException( - "Cannot write into byte message for attachment " - + attachment + " and resource " + resource, - e); - } finally { - IOUtils.closeQuietly(in); + return message; } - return message; - } - }); + }); + } catch (JmsException e) { + if (log.isTraceEnabled()) + log.debug("Cannot upload", e); + else if (log.isDebugEnabled()) + log.debug("Cannot upload: " + e.getMessage()); + } }