activemq-cpp-3.8.2
ActiveMQMessageTemplate.h
Go to the documentation of this file.
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 
18 #ifndef _ACTIVEMQ_COMMANDS_ACTIVEMQMESSAGETEMPLATE_H_
19 #define _ACTIVEMQ_COMMANDS_ACTIVEMQMESSAGETEMPLATE_H_
20 
21 #include <cms/DeliveryMode.h>
22 #include <activemq/util/Config.h>
29 
31 
36 
37 namespace activemq {
38 namespace commands {
39 
40  template<typename T>
41  class AMQCPP_API ActiveMQMessageTemplate: public T, public Message {
42  private:
43 
44  std::auto_ptr<wireformat::openwire::utils::MessagePropertyInterceptor> propertiesInterceptor;
45 
46  public:
47 
48  ActiveMQMessageTemplate() : commands::Message(), propertiesInterceptor() {
49  this->propertiesInterceptor.reset(new wireformat::openwire::utils::MessagePropertyInterceptor(this, &this->getMessageProperties()));
50  }
51 
52  virtual ~ActiveMQMessageTemplate() throw () {
53  }
54 
55  public:
56 
57  virtual void acknowledge() const {
58  try {
59  this->getAckHandler()->acknowledgeMessage(this);
60  }
62  }
63 
64  virtual void onSend() {
65  this->setReadOnlyBody(true);
66  this->setReadOnlyProperties(true);
67  }
68 
69  virtual bool equals(const DataStructure* value) const {
70  try {
71 
72  if (this == value) {
73  return true;
74  }
75 
76  const ActiveMQMessageTemplate<T>* object =
77  dynamic_cast<const ActiveMQMessageTemplate<T>*> (value);
78 
79  if (object == NULL) {
80  return false;
81  }
82 
83  decaf::lang::Pointer<MessageId> thisMsgId = this->getMessageId();
84  decaf::lang::Pointer<MessageId> otherMsgId = object->getMessageId();
85 
86  return thisMsgId != NULL && otherMsgId != NULL && otherMsgId->equals(thisMsgId.get());
87  }
89  }
90 
91  virtual void clearBody() {
92  try {
93  this->setContent(std::vector<unsigned char>());
94  this->setReadOnlyBody(false);
95  }
97  }
98 
99  virtual void clearProperties() {
100  try {
101  this->getMessageProperties().clear();
102  this->setReadOnlyProperties(false);
103  }
105  }
106 
107  virtual std::vector<std::string> getPropertyNames() const {
108  try {
109  return getMessageProperties().keySet().toArray();
110  }
112  }
113 
114  virtual bool propertyExists(const std::string& name) const {
115  try {
116  return getMessageProperties().containsKey(name);
117  }
119  }
120 
121  virtual cms::Message::ValueType getPropertyValueType(const std::string& name) const {
122  try {
123  util::PrimitiveValueNode::PrimitiveType type = this->getMessageProperties().getValueType(name);
124 
125  // Just map the values that are actually allowed in Message Properties, the others
126  // all qualify as unknown.
127  switch(type) {
151  default:
152  break;
153  }
154 
156  }
158  }
159 
160  virtual bool getBooleanProperty(const std::string& name) const {
161  try {
162  return this->propertiesInterceptor->getBooleanProperty(name);
165  }
167  }
168 
169  virtual unsigned char getByteProperty(const std::string& name) const {
170  try {
171  return this->propertiesInterceptor->getByteProperty(name);
174  }
176  }
177 
178  virtual double getDoubleProperty(const std::string& name) const {
179 
180  try {
181  return this->propertiesInterceptor->getDoubleProperty(name);
184  }
186  }
187 
188  virtual float getFloatProperty(const std::string& name) const {
189 
190  try {
191  return this->propertiesInterceptor->getFloatProperty(name);
194  }
196  }
197 
198  virtual int getIntProperty(const std::string& name) const {
199 
200  try {
201  return this->propertiesInterceptor->getIntProperty(name);
204  }
206  }
207 
208  virtual long long getLongProperty(const std::string& name) const {
209 
210  try {
211  return this->propertiesInterceptor->getLongProperty(name);
214  }
216  }
217 
218  virtual short getShortProperty(const std::string& name) const {
219 
220  try {
221  return this->propertiesInterceptor->getShortProperty(name);
224  }
226  }
227 
228  virtual std::string getStringProperty(const std::string& name) const {
229 
230  try {
231  return this->propertiesInterceptor->getStringProperty(name);
234  }
236  }
237 
238  virtual void setBooleanProperty(const std::string& name, bool value) {
239 
240  if (name == "") {
241  throw cms::CMSException("Message Property names must not be empty", NULL);
242  }
243 
244  failIfReadOnlyProperties();
245  try {
246  this->propertiesInterceptor->setBooleanProperty(name, value);
247  }
249  }
250 
251  virtual void setByteProperty(const std::string& name, unsigned char value) {
252 
253  if (name == "") {
254  throw cms::CMSException("Message Property names must not be empty", NULL);
255  }
256 
257  failIfReadOnlyProperties();
258  try {
259  this->propertiesInterceptor->setByteProperty(name, value);
260  }
262  }
263 
264  virtual void setDoubleProperty(const std::string& name, double value) {
265 
266  if (name == "") {
267  throw cms::CMSException("Message Property names must not be empty", NULL);
268  }
269 
270  failIfReadOnlyProperties();
271  try {
272  this->propertiesInterceptor->setDoubleProperty(name, value);
273  }
275  }
276 
277  virtual void setFloatProperty(const std::string& name, float value) {
278 
279  if (name == "") {
280  throw cms::CMSException("Message Property names must not be empty", NULL);
281  }
282 
283  failIfReadOnlyProperties();
284  try {
285  this->propertiesInterceptor->setFloatProperty(name, value);
286  }
288  }
289 
290  virtual void setIntProperty(const std::string& name, int value) {
291 
292  if (name == "") {
293  throw cms::CMSException("Message Property names must not be empty", NULL);
294  }
295 
296  failIfReadOnlyProperties();
297  try {
298  this->propertiesInterceptor->setIntProperty(name, value);
299  }
301  }
302 
303  virtual void setLongProperty(const std::string& name, long long value) {
304 
305  if (name == "") {
306  throw cms::CMSException("Message Property names must not be empty", NULL);
307  }
308 
309  failIfReadOnlyProperties();
310  try {
311  this->propertiesInterceptor->setLongProperty(name, value);
312  }
314  }
315 
316  virtual void setShortProperty(const std::string& name, short value) {
317 
318  if (name == "") {
319  throw cms::CMSException("Message Property names must not be empty", NULL);
320  }
321 
322  failIfReadOnlyProperties();
323  try {
324  this->propertiesInterceptor->setShortProperty(name, value);
325  }
327  }
328 
329  virtual void setStringProperty(const std::string& name, const std::string& value) {
330 
331  if (name == "") {
332  throw cms::CMSException("Message Property names must not be empty", NULL);
333  }
334 
335  failIfReadOnlyProperties();
336  try {
337  this->propertiesInterceptor->setStringProperty(name, value);
338  }
340  }
341 
342  virtual std::string getCMSCorrelationID() const {
343  return this->getCorrelationId();
344  }
345 
346  virtual void setCMSCorrelationID(const std::string& correlationId) {
347  this->setCorrelationId(correlationId);
348  }
349 
350  virtual int getCMSDeliveryMode() const {
351  return !this->isPersistent();
352  }
353 
354  virtual void setCMSDeliveryMode(int mode) {
355  this->setPersistent(mode == (int) cms::DeliveryMode::PERSISTENT);
356  }
357 
358  virtual const cms::Destination* getCMSDestination() const {
359  return dynamic_cast<const cms::Destination*> (this->getDestination().get());
360  }
361 
362  virtual void setCMSDestination(const cms::Destination* destination) {
363 
364  try {
365  if (destination != NULL) {
366  this->setDestination(decaf::lang::Pointer<ActiveMQDestination>(
367  dynamic_cast<ActiveMQDestination*> (destination->clone())));
368  } else {
369  this->getDestination().reset(NULL);
370  }
371  }
373  }
374 
375  virtual long long getCMSExpiration() const {
376  return this->getExpiration();
377  }
378 
379  virtual void setCMSExpiration(long long expireTime) {
380  this->setExpiration(expireTime);
381  }
382 
383  virtual std::string getCMSMessageID() const {
384  try {
386  }
388  }
389 
390  virtual void setCMSMessageID(const std::string& value) {
391  try {
392  Pointer<MessageId> id(new MessageId(value));
393  this->setMessageId(id);
395  // we must be some foreign JMS provider or strange user-supplied
396  // String so lets set the IDs to be 1
398  id->setTextView(value);
399  this->setMessageId(messageId);
400  }
401  }
402 
403  virtual int getCMSPriority() const {
404  return this->getPriority();
405  }
406 
407  virtual void setCMSPriority(int priority) {
408  this->setPriority((unsigned char) priority);
409  }
410 
411  virtual bool getCMSRedelivered() const {
412  return this->getRedeliveryCounter() != 0;
413  }
414 
415  virtual void setCMSRedelivered(bool redelivered AMQCPP_UNUSED ) {
416  }
417 
418  virtual const cms::Destination* getCMSReplyTo() const {
419  return dynamic_cast<const cms::Destination*> (this->getReplyTo().get());
420  }
421 
422  virtual void setCMSReplyTo(const cms::Destination* destination) {
423 
424  try {
425  if (destination != NULL) {
427  dynamic_cast<ActiveMQDestination*> (destination->clone())));
428  } else {
429  this->setReplyTo(decaf::lang::Pointer<ActiveMQDestination>());
430  }
431  }
433  }
434 
435  virtual long long getCMSTimestamp() const {
436  return this->getTimestamp();
437  }
438 
439  virtual void setCMSTimestamp(long long timeStamp) {
440  this->setTimestamp(timeStamp);
441  }
442 
443  virtual std::string getCMSType() const {
444  return this->getType();
445  }
446 
447  virtual void setCMSType(const std::string& type) {
448  this->setType(type);
449  }
450 
451  protected:
452 
453  void failIfWriteOnlyBody() const {
454  if (!this->isReadOnlyBody()) {
455  throw cms::MessageNotReadableException("message is in write-only mode and cannot be read from", NULL);
456  }
457  }
458 
459  void failIfReadOnlyBody() const {
460  if (this->isReadOnlyBody()) {
461  throw cms::MessageNotWriteableException("Message Body is Read-Only.", NULL);
462  }
463  }
464 
466  if (this->isReadOnlyProperties()) {
467  throw cms::MessageNotWriteableException("Message Properties are Read-Only.", NULL);
468  }
469  }
470 
471  };
472 
473 }
474 }
475 
476 #endif /* _ACTIVEMQ_COMMANDS_ACTIVEMQMESSAGETEMPLATE_H_ */
static cms::MessageFormatException createMessageFormatException(const decaf::lang::Exception &cause)
Definition: Message.h:113
ValueType
Defines the Type Identifiers used to identify the type contained within a specific Message property o...
Definition: Message.h:112
virtual void clearBody()
Definition: ActiveMQMessageTemplate.h:91
Definition: Message.h:119
Definition: Message.h:120
virtual cms::Destination * clone() const =0
Creates a new instance of this destination type that is a copy of this one, and returns it...
virtual std::string getStringProperty(const std::string &name) const
Definition: ActiveMQMessageTemplate.h:228
virtual unsigned char getByteProperty(const std::string &name) const
Definition: ActiveMQMessageTemplate.h:169
virtual void setCMSMessageID(const std::string &value)
Definition: ActiveMQMessageTemplate.h:390
A Destination object encapsulates a provider-specific address.
Definition: Destination.h:39
virtual void setCMSCorrelationID(const std::string &correlationId)
Definition: ActiveMQMessageTemplate.h:346
#define AMQCPP_API
Definition: Config.h:30
This exception must be thrown when a CMS client attempts to write to a read-only message.
Definition: MessageNotWriteableException.h:31
Definition: Message.h:123
virtual std::string getCMSCorrelationID() const
Definition: ActiveMQMessageTemplate.h:342
virtual int getCMSPriority() const
Definition: ActiveMQMessageTemplate.h:403
Definition: PrimitiveValueNode.h:58
Definition: PrimitiveValueNode.h:54
virtual void acknowledge() const
Definition: ActiveMQMessageTemplate.h:57
#define NULL
Definition: Config.h:33
Used the base ActiveMQMessage class to intercept calls to get and set properties in order to capture ...
Definition: MessagePropertyInterceptor.h:47
Definition: PrimitiveValueNode.h:51
Definition: PrimitiveValueNode.h:52
Definition: PrimitiveValueNode.h:53
virtual void setCMSDestination(const cms::Destination *destination)
Definition: ActiveMQMessageTemplate.h:362
virtual void clearProperties()
Definition: ActiveMQMessageTemplate.h:99
virtual bool getCMSRedelivered() const
Definition: ActiveMQMessageTemplate.h:411
virtual const cms::Destination * getCMSDestination() const
Definition: ActiveMQMessageTemplate.h:358
virtual long long getLongProperty(const std::string &name) const
Definition: ActiveMQMessageTemplate.h:208
virtual short getShortProperty(const std::string &name) const
Definition: ActiveMQMessageTemplate.h:218
virtual bool propertyExists(const std::string &name) const
Definition: ActiveMQMessageTemplate.h:114
virtual void setBooleanProperty(const std::string &name, bool value)
Definition: ActiveMQMessageTemplate.h:238
virtual void setDoubleProperty(const std::string &name, double value)
Definition: ActiveMQMessageTemplate.h:264
virtual float getFloatProperty(const std::string &name) const
Definition: ActiveMQMessageTemplate.h:188
virtual void setLongProperty(const std::string &name, long long value)
Definition: ActiveMQMessageTemplate.h:303
virtual long long getCMSTimestamp() const
Definition: ActiveMQMessageTemplate.h:435
void failIfReadOnlyBody() const
Definition: ActiveMQMessageTemplate.h:459
virtual void onSend()
Allows derived Message classes to perform tasks before a message is sent.
Definition: ActiveMQMessageTemplate.h:64
virtual void setStringProperty(const std::string &name, const std::string &value)
Definition: ActiveMQMessageTemplate.h:329
virtual std::string getCMSType() const
Definition: ActiveMQMessageTemplate.h:443
Definition: PrimitiveValueNode.h:46
virtual const cms::Destination * getCMSReplyTo() const
Definition: ActiveMQMessageTemplate.h:418
Definition: DataStructure.h:27
Definition: UnsupportedOperationException.h:32
virtual int getIntProperty(const std::string &name) const
Definition: ActiveMQMessageTemplate.h:198
virtual bool equals(const DataStructure *value) const
Compares the DataStructure passed in to this one, and returns if they are equivalent.
Definition: ActiveMQMessageTemplate.h:69
Definition: Message.h:122
virtual long long getCMSExpiration() const
Definition: ActiveMQMessageTemplate.h:375
virtual void setCMSDeliveryMode(int mode)
Definition: ActiveMQMessageTemplate.h:354
virtual void setFloatProperty(const std::string &name, float value)
Definition: ActiveMQMessageTemplate.h:277
virtual void setIntProperty(const std::string &name, int value)
Definition: ActiveMQMessageTemplate.h:290
ActiveMQMessageTemplate()
Definition: ActiveMQMessageTemplate.h:48
virtual void setCMSTimestamp(long long timeStamp)
Definition: ActiveMQMessageTemplate.h:439
virtual int getCMSDeliveryMode() const
Definition: ActiveMQMessageTemplate.h:350
PrimitiveType
Enumeration for the various primitive types.
Definition: PrimitiveValueNode.h:44
static std::string toString(const commands::MessageId *id)
Converts the object to a String.
CMS API Exception that is the base for all exceptions thrown from CMS classes.
Definition: CMSException.h:50
Definition: Message.h:116
Definition: Message.h:115
Definition: PrimitiveValueNode.h:55
Definition: Message.h:59
Definition: Message.h:118
virtual void setCMSPriority(int priority)
Definition: ActiveMQMessageTemplate.h:407
virtual void setCMSType(const std::string &type)
Definition: ActiveMQMessageTemplate.h:447
Definition: MessageId.h:49
Definition: DeliveryMode.h:55
virtual std::vector< std::string > getPropertyNames() const
Definition: ActiveMQMessageTemplate.h:107
#define AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
Macro for catching an exception of one type and then re-throwing as a Basic CMSException, good for cases where the method isn&#39;t specific about what CMS Exceptions are thrown, bad if you need to throw an exception of MessageNotReadableException for instance.
Definition: CMSExceptionSupport.h:73
PointerType get() const
Gets the real pointer that is contained within this Pointer.
Definition: Pointer.h:188
virtual std::string getCMSMessageID() const
Definition: ActiveMQMessageTemplate.h:383
Definition: Message.h:124
virtual void setByteProperty(const std::string &name, unsigned char value)
Definition: ActiveMQMessageTemplate.h:251
Definition: NumberFormatException.h:33
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: CachedConsumer.h:24
virtual void setCMSRedelivered(bool redelivered AMQCPP_UNUSED)
Definition: ActiveMQMessageTemplate.h:415
Definition: PrimitiveValueNode.h:47
virtual void setShortProperty(const std::string &name, short value)
Definition: ActiveMQMessageTemplate.h:316
Definition: PrimitiveValueNode.h:50
Definition: Message.h:121
virtual void setCMSReplyTo(const cms::Destination *destination)
Definition: ActiveMQMessageTemplate.h:422
This exception must be thrown when a CMS client attempts to read a write-only message.
Definition: MessageNotReadableException.h:31
virtual ~ActiveMQMessageTemplate()
Definition: ActiveMQMessageTemplate.h:52
Definition: Message.h:117
Decaf&#39;s implementation of a Smart Pointer that is a template on a Type and is Thread Safe if the defa...
Definition: Pointer.h:53
Definition: PrimitiveValueNode.h:48
void failIfWriteOnlyBody() const
Definition: ActiveMQMessageTemplate.h:453
virtual void setCMSExpiration(long long expireTime)
Definition: ActiveMQMessageTemplate.h:379
void failIfReadOnlyProperties() const
Definition: ActiveMQMessageTemplate.h:465
virtual bool getBooleanProperty(const std::string &name) const
Definition: ActiveMQMessageTemplate.h:160
Definition: ActiveMQMessageTemplate.h:41
virtual double getDoubleProperty(const std::string &name) const
Definition: ActiveMQMessageTemplate.h:178
Definition: PrimitiveValueNode.h:49
Definition: Message.h:114
Definition: PrimitiveValueNode.h:45
virtual cms::Message::ValueType getPropertyValueType(const std::string &name) const
Definition: ActiveMQMessageTemplate.h:121