KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > command > ActiveMQBlobMessage


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.activemq.command;
18
19 import org.apache.activemq.BlobMessage;
20 import org.apache.activemq.blob.BlobUploader;
21 import org.apache.activemq.util.JMSExceptionSupport;
22
23 import javax.jms.JMSException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28
29 /**
30  * An implementation of {@link BlobMessage} for out of band BLOB transfer
31  *
32  * @version $Revision: $
33  * @openwire:marshaller code="29"
34  */

35 public class ActiveMQBlobMessage extends ActiveMQMessage implements BlobMessage {
36     public static final byte DATA_STRUCTURE_TYPE = CommandTypes.ACTIVEMQ_BLOB_MESSAGE;
37
38     public static final String JavaDoc BINARY_MIME_TYPE = "application/octet-stream";
39
40     private String JavaDoc remoteBlobUrl;
41     private String JavaDoc mimeType;
42     private String JavaDoc name;
43     private boolean deletedByBroker;
44
45     private transient BlobUploader blobUploader;
46     private transient URL JavaDoc url;
47
48
49     public Message copy() {
50         ActiveMQBlobMessage copy = new ActiveMQBlobMessage();
51         copy(copy);
52         return copy;
53     }
54
55     private void copy(ActiveMQBlobMessage copy) {
56         super.copy(copy);
57         copy.setRemoteBlobUrl(getRemoteBlobUrl());
58         copy.setMimeType(getMimeType());
59         copy.setDeletedByBroker(isDeletedByBroker());
60     }
61
62     public byte getDataStructureType() {
63         return DATA_STRUCTURE_TYPE;
64     }
65
66     /**
67      * @openwire:property version=3 cache=false
68      */

69     public String JavaDoc getRemoteBlobUrl() {
70         return remoteBlobUrl;
71     }
72
73     public void setRemoteBlobUrl(String JavaDoc remoteBlobUrl) {
74         this.remoteBlobUrl = remoteBlobUrl;
75         url = null;
76     }
77
78     /**
79      * The MIME type of the BLOB which can be used to apply different content types to messages.
80      *
81      * @openwire:property version=3 cache=true
82      */

83     public String JavaDoc getMimeType() {
84         if (mimeType == null) {
85             return BINARY_MIME_TYPE;
86         }
87         return mimeType;
88     }
89
90     public void setMimeType(String JavaDoc mimeType) {
91         this.mimeType = mimeType;
92     }
93
94     public String JavaDoc getName() {
95         return name;
96     }
97
98     /**
99      * The name of the attachment which can be useful information if transmitting files over ActiveMQ
100      *
101      * @openwire:property version=3 cache=false
102      */

103     public void setName(String JavaDoc name) {
104         this.name = name;
105     }
106
107     /**
108      * @openwire:property version=3 cache=false
109      */

110     public boolean isDeletedByBroker() {
111         return deletedByBroker;
112     }
113
114     public void setDeletedByBroker(boolean deletedByBroker) {
115         this.deletedByBroker = deletedByBroker;
116     }
117
118     public String JavaDoc getJMSXMimeType() {
119         return getMimeType();
120     }
121
122     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, JMSException JavaDoc {
123         URL JavaDoc value = getURL();
124         if (value == null) {
125             return null;
126         }
127         return value.openStream();
128     }
129
130     public URL JavaDoc getURL() throws JMSException JavaDoc {
131         if (url == null && remoteBlobUrl != null) {
132             try {
133                 url = new URL JavaDoc(remoteBlobUrl);
134             }
135             catch (MalformedURLException JavaDoc e) {
136                 throw JMSExceptionSupport.create(e);
137             }
138         }
139         return url;
140     }
141
142     public void setURL(URL JavaDoc url) {
143         this.url = url;
144         remoteBlobUrl = url != null ? url.toExternalForm() : null;
145     }
146
147
148     public BlobUploader getBlobUploader() {
149         return blobUploader;
150     }
151
152     public void setBlobUploader(BlobUploader blobUploader) {
153         this.blobUploader = blobUploader;
154     }
155
156     public void onSend() throws JMSException JavaDoc {
157         super.onSend();
158
159         // lets ensure we upload the BLOB first out of band before we send the message
160
if (blobUploader != null) {
161             try {
162                 URL JavaDoc value = blobUploader.upload(this);
163                 setURL(value);
164             }
165             catch (IOException JavaDoc e) {
166                 throw JMSExceptionSupport.create(e);
167             }
168         }
169     }
170 }
171
Popular Tags