activemq-cpp-3.6.0
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& id AMQCPP_UNUSED ) {
391  }
392 
393  virtual int getCMSPriority() const {
394  return this->getPriority();
395  }
396 
397  virtual void setCMSPriority(int priority) {
398  this->setPriority((unsigned char) priority);
399  }
400 
401  virtual bool getCMSRedelivered() const {
402  return this->getRedeliveryCounter() != 0;
403  }
404 
405  virtual void setCMSRedelivered(bool redelivered AMQCPP_UNUSED ) {
406  }
407 
408  virtual const cms::Destination* getCMSReplyTo() const {
409  return dynamic_cast<const cms::Destination*> (this->getReplyTo().get());
410  }
411 
412  virtual void setCMSReplyTo(const cms::Destination* destination) {
413 
414  try {
415  if (destination != NULL) {
417  dynamic_cast<ActiveMQDestination*> (destination->clone())));
418  } else {
419  this->setReplyTo(decaf::lang::Pointer<ActiveMQDestination>());
420  }
421  }
423  }
424 
425  virtual long long getCMSTimestamp() const {
426  return this->getTimestamp();
427  }
428 
429  virtual void setCMSTimestamp(long long timeStamp) {
430  this->setTimestamp(timeStamp);
431  }
432 
433  virtual std::string getCMSType() const {
434  return this->getType();
435  }
436 
437  virtual void setCMSType(const std::string& type) {
438  this->setType(type);
439  }
440 
441  protected:
442 
443  void failIfWriteOnlyBody() const {
444  if (!this->isReadOnlyBody()) {
445  throw cms::MessageNotReadableException("message is in write-only mode and cannot be read from", NULL);
446  }
447  }
448 
449  void failIfReadOnlyBody() const {
450  if (this->isReadOnlyBody()) {
451  throw cms::MessageNotWriteableException("Message Body is Read-Only.", NULL);
452  }
453  }
454 
456  if (this->isReadOnlyProperties()) {
457  throw cms::MessageNotWriteableException("Message Properties are Read-Only.", NULL);
458  }
459  }
460 
461  };
462 
463 }
464 }
465 
466 #endif /* _ACTIVEMQ_COMMANDS_ACTIVEMQMESSAGETEMPLATE_H_ */