]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAttachmentUploader.java
Start working on serialized JMS
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.activemq / src / main / java / org / argeo / slc / jms / JmsAttachmentUploader.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.jms;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21
22 import javax.jms.BytesMessage;
23 import javax.jms.Destination;
24 import javax.jms.JMSException;
25 import javax.jms.Message;
26 import javax.jms.Session;
27
28 import org.apache.commons.io.IOUtils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.argeo.slc.SlcException;
32 import org.argeo.slc.core.attachment.Attachment;
33 import org.argeo.slc.core.attachment.AttachmentUploader;
34 import org.springframework.core.io.Resource;
35 import org.springframework.jms.JmsException;
36 import org.springframework.jms.core.JmsTemplate;
37 import org.springframework.jms.core.MessageCreator;
38
39 public class JmsAttachmentUploader implements AttachmentUploader {
40 private final static Log log = LogFactory
41 .getLog(JmsAttachmentUploader.class);
42
43 public final static String ATTACHMENT_ID = "slc_attachmentId";
44 public final static String ATTACHMENT_NAME = "slc_attachmentName";
45 public final static String ATTACHMENT_CONTENT_TYPE = "slc_attachmentContentType";
46
47 private JmsTemplate jmsTemplate;
48 private Destination destination;
49
50 public void upload(final Attachment attachment, final Resource resource) {
51 try {
52 jmsTemplate.send(destination, new MessageCreator() {
53
54 public Message createMessage(Session session)
55 throws JMSException {
56 BytesMessage message = session.createBytesMessage();
57 message.setStringProperty(ATTACHMENT_ID, attachment
58 .getUuid());
59 message.setStringProperty(ATTACHMENT_NAME, attachment
60 .getName());
61 message.setStringProperty(ATTACHMENT_CONTENT_TYPE,
62 attachment.getContentType());
63
64 InputStream in = null;
65 try {
66 in = resource.getInputStream();
67 byte[] buffer = new byte[1024 * 1024];
68 int read = -1;
69 while ((read = in.read(buffer)) > 0) {
70 message.writeBytes(buffer, 0, read);
71 }
72 } catch (IOException e) {
73 throw new SlcException(
74 "Cannot write into byte message for attachment "
75 + attachment + " and resource "
76 + resource, e);
77 } finally {
78 IOUtils.closeQuietly(in);
79 }
80 return message;
81 }
82 });
83 } catch (JmsException e) {
84 if (log.isTraceEnabled())
85 log.debug("Cannot upload", e);
86 else if (log.isDebugEnabled())
87 log.debug("Cannot upload: " + e.getMessage());
88 }
89
90 }
91
92 public void setJmsTemplate(JmsTemplate jmsTemplate) {
93 this.jmsTemplate = jmsTemplate;
94 }
95
96 public void setDestination(Destination destination) {
97 this.destination = destination;
98 }
99
100 }