001 /*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 *
020 */
021 package org.apache.qpid.client.message;
022
023 import java.io.IOException;
024 import java.util.Enumeration;
025 import java.util.UUID;
026
027 import javax.jms.Destination;
028 import javax.jms.JMSException;
029 import javax.jms.MessageNotReadableException;
030 import javax.jms.MessageNotWriteableException;
031
032 import org.apache.mina.common.ByteBuffer;
033 import org.apache.qpid.AMQException;
034 import org.apache.qpid.client.AMQSession;
035 import org.apache.qpid.framing.AMQShortString;
036 import org.apache.qpid.framing.BasicContentHeaderProperties;
037
038 public abstract class AbstractJMSMessage implements org.apache.qpid.jms.Message
039 {
040
041
042
043 protected ByteBuffer _data;
044 protected boolean _readableMessage = false;
045 protected boolean _changedData = true;
046
047 /** If the acknowledge mode is CLIENT_ACKNOWLEDGE the session is required */
048
049
050
051
052 private AMQMessageDelegate _delegate;
053 private boolean _redelivered;
054
055 protected AbstractJMSMessage(AMQMessageDelegateFactory delegateFactory, ByteBuffer data)
056 {
057 _delegate = delegateFactory.createDelegate();
058 _data = data;
059 if (_data != null)
060 {
061 _data.acquire();
062 }
063
064
065 _readableMessage = (data != null);
066 _changedData = (data == null);
067
068 }
069
070 protected AbstractJMSMessage(AMQMessageDelegate delegate, ByteBuffer data) throws AMQException
071 {
072
073 _delegate = delegate;
074
075 _data = data;
076 if (_data != null)
077 {
078 _data.acquire();
079 }
080
081 _readableMessage = data != null;
082
083 }
084
085 public String getJMSMessageID() throws JMSException
086 {
087 return _delegate.getJMSMessageID();
088 }
089
090 public void setJMSMessageID(String messageId) throws JMSException
091 {
092 _delegate.setJMSMessageID(messageId);
093 }
094
095 public void setJMSMessageID(UUID messageId) throws JMSException
096 {
097 _delegate.setJMSMessageID(messageId);
098 }
099
100
101 public long getJMSTimestamp() throws JMSException
102 {
103 return _delegate.getJMSTimestamp();
104 }
105
106 public void setJMSTimestamp(long timestamp) throws JMSException
107 {
108 _delegate.setJMSTimestamp(timestamp);
109 }
110
111 public byte[] getJMSCorrelationIDAsBytes() throws JMSException
112 {
113 return _delegate.getJMSCorrelationIDAsBytes();
114 }
115
116 public void setJMSCorrelationIDAsBytes(byte[] bytes) throws JMSException
117 {
118 _delegate.setJMSCorrelationIDAsBytes(bytes);
119 }
120
121 public void setJMSCorrelationID(String correlationId) throws JMSException
122 {
123 _delegate.setJMSCorrelationID(correlationId);
124 }
125
126 public String getJMSCorrelationID() throws JMSException
127 {
128 return _delegate.getJMSCorrelationID();
129 }
130
131 public Destination getJMSReplyTo() throws JMSException
132 {
133 return _delegate.getJMSReplyTo();
134 }
135
136 public void setJMSReplyTo(Destination destination) throws JMSException
137 {
138 _delegate.setJMSReplyTo(destination);
139 }
140
141 public Destination getJMSDestination() throws JMSException
142 {
143 return _delegate.getJMSDestination();
144 }
145
146 public void setJMSDestination(Destination destination)
147 {
148 _delegate.setJMSDestination(destination);
149 }
150
151 public int getJMSDeliveryMode() throws JMSException
152 {
153 return _delegate.getJMSDeliveryMode();
154 }
155
156 public void setJMSDeliveryMode(int i) throws JMSException
157 {
158 _delegate.setJMSDeliveryMode(i);
159 }
160
161
162 public boolean getJMSRedelivered() throws JMSException
163 {
164 return _redelivered;
165 }
166
167 public void setJMSRedelivered(boolean b) throws JMSException
168 {
169 _redelivered = b;
170 }
171
172
173 public String getJMSType() throws JMSException
174 {
175 return _delegate.getJMSType();
176 }
177
178 public void setJMSType(String string) throws JMSException
179 {
180 _delegate.setJMSType(string);
181 }
182
183 public long getJMSExpiration() throws JMSException
184 {
185 return _delegate.getJMSExpiration();
186 }
187
188 public void setJMSExpiration(long l) throws JMSException
189 {
190 _delegate.setJMSExpiration(l);
191 }
192
193 public int getJMSPriority() throws JMSException
194 {
195 return _delegate.getJMSPriority();
196 }
197
198 public void setJMSPriority(int i) throws JMSException
199 {
200 _delegate.setJMSPriority(i);
201 }
202
203
204 public boolean propertyExists(String propertyName) throws JMSException
205 {
206 return _delegate.propertyExists(propertyName);
207 }
208
209 public boolean getBooleanProperty(final String s)
210 throws JMSException
211 {
212 return _delegate.getBooleanProperty(s);
213 }
214
215 public byte getByteProperty(final String s)
216 throws JMSException
217 {
218 return _delegate.getByteProperty(s);
219 }
220
221 public short getShortProperty(final String s)
222 throws JMSException
223 {
224 return _delegate.getShortProperty(s);
225 }
226
227 public int getIntProperty(final String s)
228 throws JMSException
229 {
230 return _delegate.getIntProperty(s);
231 }
232
233 public long getLongProperty(final String s)
234 throws JMSException
235 {
236 return _delegate.getLongProperty(s);
237 }
238
239 public float getFloatProperty(final String s)
240 throws JMSException
241 {
242 return _delegate.getFloatProperty(s);
243 }
244
245 public double getDoubleProperty(final String s)
246 throws JMSException
247 {
248 return _delegate.getDoubleProperty(s);
249 }
250
251 public String getStringProperty(final String s)
252 throws JMSException
253 {
254 return _delegate.getStringProperty(s);
255 }
256
257 public Object getObjectProperty(final String s)
258 throws JMSException
259 {
260 return _delegate.getObjectProperty(s);
261 }
262
263 public Enumeration getPropertyNames()
264 throws JMSException
265 {
266 return _delegate.getPropertyNames();
267 }
268
269 public void setBooleanProperty(final String s, final boolean b)
270 throws JMSException
271 {
272 _delegate.setBooleanProperty(s, b);
273 }
274
275 public void setByteProperty(final String s, final byte b)
276 throws JMSException
277 {
278 _delegate.setByteProperty(s, b);
279 }
280
281 public void setShortProperty(final String s, final short i)
282 throws JMSException
283 {
284 _delegate.setShortProperty(s, i);
285 }
286
287 public void setIntProperty(final String s, final int i)
288 throws JMSException
289 {
290 _delegate.setIntProperty(s, i);
291 }
292
293 public void setLongProperty(final String s, final long l)
294 throws JMSException
295 {
296 _delegate.setLongProperty(s, l);
297 }
298
299 public void setFloatProperty(final String s, final float v)
300 throws JMSException
301 {
302 _delegate.setFloatProperty(s, v);
303 }
304
305 public void setDoubleProperty(final String s, final double v)
306 throws JMSException
307 {
308 _delegate.setDoubleProperty(s, v);
309 }
310
311 public void setStringProperty(final String s, final String s1)
312 throws JMSException
313 {
314 _delegate.setStringProperty(s, s1);
315 }
316
317 public void setObjectProperty(final String s, final Object o)
318 throws JMSException
319 {
320 _delegate.setObjectProperty(s, o);
321 }
322
323
324
325 public void clearProperties() throws JMSException
326 {
327 _delegate.clearProperties();
328 }
329
330 public void clearBody() throws JMSException
331 {
332 clearBodyImpl();
333 _readableMessage = false;
334
335 }
336
337
338 public void acknowledgeThis() throws JMSException
339 {
340 _delegate.acknowledgeThis();
341 }
342
343 public void acknowledge() throws JMSException
344 {
345 _delegate.acknowledge();
346 }
347
348 /**
349 * This forces concrete classes to implement clearBody()
350 *
351 * @throws JMSException
352 */
353 public abstract void clearBodyImpl() throws JMSException;
354
355 /**
356 * Get a String representation of the body of the message. Used in the toString() method which outputs this before
357 * message properties.
358 */
359 public abstract String toBodyString() throws JMSException;
360
361 protected abstract String getMimeType();
362
363
364
365 public String toString()
366 {
367 try
368 {
369 StringBuffer buf = new StringBuffer("Body:\n");
370 buf.append(toBodyString());
371 buf.append("\nJMS Correlation ID: ").append(getJMSCorrelationID());
372 buf.append("\nJMS timestamp: ").append(getJMSTimestamp());
373 buf.append("\nJMS expiration: ").append(getJMSExpiration());
374 buf.append("\nJMS priority: ").append(getJMSPriority());
375 buf.append("\nJMS delivery mode: ").append(getJMSDeliveryMode());
376 //buf.append("\nJMS reply to: ").append(String.valueOf(getJMSReplyTo()));
377 buf.append("\nJMS Redelivered: ").append(_redelivered);
378 buf.append("\nJMS Destination: ").append(getJMSDestination());
379 buf.append("\nJMS Type: ").append(getJMSType());
380 buf.append("\nJMS MessageID: ").append(getJMSMessageID());
381 buf.append("\nAMQ message number: ").append(getDeliveryTag());
382
383 buf.append("\nProperties:");
384 final Enumeration propertyNames = getPropertyNames();
385 if (!propertyNames.hasMoreElements())
386 {
387 buf.append("<NONE>");
388 }
389 else
390 {
391 buf.append('\n');
392 while(propertyNames.hasMoreElements())
393 {
394 String propertyName = (String) propertyNames.nextElement();
395 buf.append(propertyName).append(":\t").append(getObjectProperty(propertyName));
396 }
397
398 }
399
400 return buf.toString();
401 }
402 catch (JMSException e)
403 {
404 return e.toString();
405 }
406 }
407
408
409 public AMQMessageDelegate getDelegate()
410 {
411 return _delegate;
412 }
413
414 public ByteBuffer getData()
415 {
416 // make sure we rewind the data just in case any method has moved the
417 // position beyond the start
418 if (_data != null)
419 {
420 reset();
421 }
422
423 return _data;
424 }
425
426 protected void checkReadable() throws MessageNotReadableException
427 {
428 if (!_readableMessage)
429 {
430 throw new MessageNotReadableException("You need to call reset() to make the message readable");
431 }
432 }
433
434 protected void checkWritable() throws MessageNotWriteableException
435 {
436 if (_readableMessage)
437 {
438 throw new MessageNotWriteableException("You need to call clearBody() to make the message writable");
439 }
440 }
441
442 public void reset()
443 {
444 if (!_changedData)
445 {
446 _data.rewind();
447 }
448 else
449 {
450 _data.flip();
451 _changedData = false;
452 }
453 }
454
455 public int getContentLength()
456 {
457 if(_data != null)
458 {
459 return _data.remaining();
460 }
461 else
462 {
463 return 0;
464 }
465 }
466
467 public void receivedFromServer()
468 {
469 _changedData = false;
470 }
471
472 /**
473 * The session is set when CLIENT_ACKNOWLEDGE mode is used so that the CHANNEL ACK can be sent when the user calls
474 * acknowledge()
475 *
476 * @param s the AMQ session that delivered this message
477 */
478 public void setAMQSession(AMQSession s)
479 {
480 _delegate.setAMQSession(s);
481 }
482
483 public AMQSession getAMQSession()
484 {
485 return _delegate.getAMQSession();
486 }
487
488 /**
489 * Get the AMQ message number assigned to this message
490 *
491 * @return the message number
492 */
493 public long getDeliveryTag()
494 {
495 return _delegate.getDeliveryTag();
496 }
497
498 /** Invoked prior to sending the message. Allows the message to be modified if necessary before sending. */
499 public void prepareForSending() throws JMSException
500 {
501 }
502
503
504 public void setContentType(String contentType)
505 {
506 _delegate.setContentType(contentType);
507 }
508
509 public String getContentType()
510 {
511 return _delegate.getContentType();
512 }
513
514 public void setEncoding(String encoding)
515 {
516 _delegate.setEncoding(encoding);
517 }
518
519 public String getEncoding()
520 {
521 return _delegate.getEncoding();
522 }
523
524 public String getReplyToString()
525 {
526 return _delegate.getReplyToString();
527 }
528
529 protected void removeProperty(final String propertyName) throws JMSException
530 {
531 _delegate.removeProperty(propertyName);
532 }
533
534 }
|