]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAttachmentUploader.java
Remove SlcExecution and process packages
[gpl/argeo-slc.git] / legacy / runtime / org.argeo.slc.support.activemq / src / main / java / org / argeo / slc / jms / JmsAttachmentUploader.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 package org.argeo.slc.jms;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20
21 import javax.jms.BytesMessage;
22 import javax.jms.Destination;
23 import javax.jms.JMSException;
24 import javax.jms.Message;
25 import javax.jms.Session;
26
27 import org.apache.commons.io.IOUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.argeo.slc.SlcException;
31 import org.argeo.slc.core.attachment.Attachment;
32 import org.argeo.slc.core.attachment.AttachmentUploader;
33 import org.springframework.core.io.Resource;
34 import org.springframework.jms.JmsException;
35 import org.springframework.jms.core.JmsTemplate;
36 import org.springframework.jms.core.MessageCreator;
37
38 public class JmsAttachmentUploader implements AttachmentUploader {
39 private final static Log log = LogFactory
40 .getLog(JmsAttachmentUploader.class);
41
42 public final static String ATTACHMENT_ID = "slc_attachmentId";
43 public final static String ATTACHMENT_NAME = "slc_attachmentName";
44 public final static String ATTACHMENT_CONTENT_TYPE = "slc_attachmentContentType";
45
46 private JmsTemplate jmsTemplate;
47 private Destination destination;
48
49 public void upload(final Attachment attachment, final Resource resource) {
50 try {
51 jmsTemplate.send(destination, new MessageCreator() {
52
53 public Message createMessage(Session session)
54 throws JMSException {
55 BytesMessage message = session.createBytesMessage();
56 message.setStringProperty(ATTACHMENT_ID, attachment
57 .getUuid());
58 message.setStringProperty(ATTACHMENT_NAME, attachment
59 .getName());
60 message.setStringProperty(ATTACHMENT_CONTENT_TYPE,
61 attachment.getContentType());
62
63 InputStream in = null;
64 try {
65 in = resource.getInputStream();
66 byte[] buffer = new byte[1024 * 1024];
67 int read = -1;
68 while ((read = in.read(buffer)) > 0) {
69 message.writeBytes(buffer, 0, read);
70 }
71 } catch (IOException e) {
72 throw new SlcException(
73 "Cannot write into byte message for attachment "
74 + attachment + " and resource "
75 + resource, e);
76 } finally {
77 IOUtils.closeQuietly(in);
78 }
79 return message;
80 }
81 });
82 } catch (JmsException e) {
83 if (log.isTraceEnabled())
84 log.debug("Cannot upload", e);
85 else if (log.isDebugEnabled())
86 log.debug("Cannot upload: " + e.getMessage());
87 }
88
89 }
90
91 public void setJmsTemplate(JmsTemplate jmsTemplate) {
92 this.jmsTemplate = jmsTemplate;
93 }
94
95 public void setDestination(Destination destination) {
96 this.destination = destination;
97 }
98
99 }