00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _ACTIVEMQ_COMMANDS_ACTIVEMQMESSAGETEMPLATE_H_
00019 #define _ACTIVEMQ_COMMANDS_ACTIVEMQMESSAGETEMPLATE_H_
00020
00021 #include <cms/DeliveryMode.h>
00022 #include <activemq/util/Config.h>
00023 #include <activemq/commands/Message.h>
00024 #include <activemq/core/ActiveMQAckHandler.h>
00025 #include <activemq/core/ActiveMQConnection.h>
00026 #include <activemq/wireformat/openwire/utils/MessagePropertyInterceptor.h>
00027 #include <activemq/wireformat/openwire/marshal/BaseDataStreamMarshaller.h>
00028 #include <activemq/util/CMSExceptionSupport.h>
00029
00030 #include <decaf/lang/exceptions/UnsupportedOperationException.h>
00031
00032 #include <cms/IllegalStateException.h>
00033 #include <cms/MessageFormatException.h>
00034 #include <cms/MessageNotReadableException.h>
00035 #include <cms/MessageNotWriteableException.h>
00036
00037 namespace activemq {
00038 namespace commands {
00039
00040 template<typename T>
00041 class AMQCPP_API ActiveMQMessageTemplate: public T, public Message {
00042 private:
00043
00044 std::auto_ptr<wireformat::openwire::utils::MessagePropertyInterceptor> propertiesInterceptor;
00045
00046 public:
00047
00048 ActiveMQMessageTemplate() : commands::Message(), propertiesInterceptor() {
00049 this->propertiesInterceptor.reset(new wireformat::openwire::utils::MessagePropertyInterceptor(this, &this->getMessageProperties()));
00050 }
00051
00052 virtual ~ActiveMQMessageTemplate() throw () {
00053 }
00054
00055 public:
00056
00057 virtual void acknowledge() const {
00058 try {
00059 this->getAckHandler()->acknowledgeMessage(this);
00060 }
00061 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00062 }
00063
00064 virtual void onSend() {
00065 this->setReadOnlyBody(true);
00066 this->setReadOnlyProperties(true);
00067 }
00068
00069 virtual bool equals(const DataStructure* value) const {
00070 try {
00071
00072 if (this == value) {
00073 return true;
00074 }
00075
00076 const ActiveMQMessageTemplate<T>* object =
00077 dynamic_cast<const ActiveMQMessageTemplate<T>*> (value);
00078
00079 if (object == NULL) {
00080 return false;
00081 }
00082
00083 decaf::lang::Pointer<MessageId> thisMsgId = this->getMessageId();
00084 decaf::lang::Pointer<MessageId> otherMsgId = object->getMessageId();
00085
00086 return thisMsgId != NULL && otherMsgId != NULL && otherMsgId->equals(thisMsgId.get());
00087 }
00088 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00089 }
00090
00091 virtual void clearBody() {
00092 try {
00093 this->setContent(std::vector<unsigned char>());
00094 this->setReadOnlyBody(false);
00095 }
00096 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00097 }
00098
00099 virtual void clearProperties() {
00100 try {
00101 this->getMessageProperties().clear();
00102 this->setReadOnlyProperties(false);
00103 }
00104 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00105 }
00106
00107 virtual std::vector<std::string> getPropertyNames() const {
00108 try {
00109 return getMessageProperties().keySet().toArray();
00110 }
00111 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00112 }
00113
00114 virtual bool propertyExists(const std::string& name) const {
00115 try {
00116 return getMessageProperties().containsKey(name);
00117 }
00118 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00119 }
00120
00121 virtual cms::Message::ValueType getPropertyValueType(const std::string& name) const {
00122 try {
00123 util::PrimitiveValueNode::PrimitiveType type = this->getMessageProperties().getValueType(name);
00124
00125
00126
00127 switch(type) {
00128 case util::PrimitiveValueNode::NULL_TYPE:
00129 return cms::Message::NULL_TYPE;
00130 case util::PrimitiveValueNode::BOOLEAN_TYPE:
00131 return cms::Message::BOOLEAN_TYPE;
00132 case util::PrimitiveValueNode::BYTE_TYPE:
00133 return cms::Message::BYTE_TYPE;
00134 case util::PrimitiveValueNode::CHAR_TYPE:
00135 return cms::Message::CHAR_TYPE;
00136 case util::PrimitiveValueNode::SHORT_TYPE:
00137 return cms::Message::SHORT_TYPE;
00138 case util::PrimitiveValueNode::INTEGER_TYPE:
00139 return cms::Message::INTEGER_TYPE;
00140 case util::PrimitiveValueNode::LONG_TYPE:
00141 return cms::Message::LONG_TYPE;
00142 case util::PrimitiveValueNode::DOUBLE_TYPE:
00143 return cms::Message::DOUBLE_TYPE;
00144 case util::PrimitiveValueNode::FLOAT_TYPE:
00145 return cms::Message::FLOAT_TYPE;
00146 case util::PrimitiveValueNode::STRING_TYPE:
00147 case util::PrimitiveValueNode::BIG_STRING_TYPE:
00148 return cms::Message::STRING_TYPE;
00149 case util::PrimitiveValueNode::BYTE_ARRAY_TYPE:
00150 return cms::Message::BYTE_ARRAY_TYPE;
00151 default:
00152 break;
00153 }
00154
00155 return cms::Message::UNKNOWN_TYPE;
00156 }
00157 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00158 }
00159
00160 virtual bool getBooleanProperty(const std::string& name) const {
00161 try {
00162 return this->propertiesInterceptor->getBooleanProperty(name);
00163 } catch (decaf::lang::exceptions::UnsupportedOperationException& ex) {
00164 throw activemq::util::CMSExceptionSupport::createMessageFormatException(ex);
00165 }
00166 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00167 }
00168
00169 virtual unsigned char getByteProperty(const std::string& name) const {
00170 try {
00171 return this->propertiesInterceptor->getByteProperty(name);
00172 } catch (decaf::lang::exceptions::UnsupportedOperationException& ex) {
00173 throw activemq::util::CMSExceptionSupport::createMessageFormatException(ex);
00174 }
00175 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00176 }
00177
00178 virtual double getDoubleProperty(const std::string& name) const {
00179
00180 try {
00181 return this->propertiesInterceptor->getDoubleProperty(name);
00182 } catch (decaf::lang::exceptions::UnsupportedOperationException& ex) {
00183 throw activemq::util::CMSExceptionSupport::createMessageFormatException(ex);
00184 }
00185 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00186 }
00187
00188 virtual float getFloatProperty(const std::string& name) const {
00189
00190 try {
00191 return this->propertiesInterceptor->getFloatProperty(name);
00192 } catch (decaf::lang::exceptions::UnsupportedOperationException& ex) {
00193 throw activemq::util::CMSExceptionSupport::createMessageFormatException(ex);
00194 }
00195 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00196 }
00197
00198 virtual int getIntProperty(const std::string& name) const {
00199
00200 try {
00201 return this->propertiesInterceptor->getIntProperty(name);
00202 } catch (decaf::lang::exceptions::UnsupportedOperationException& ex) {
00203 throw activemq::util::CMSExceptionSupport::createMessageFormatException(ex);
00204 }
00205 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00206 }
00207
00208 virtual long long getLongProperty(const std::string& name) const {
00209
00210 try {
00211 return this->propertiesInterceptor->getLongProperty(name);
00212 } catch (decaf::lang::exceptions::UnsupportedOperationException& ex) {
00213 throw activemq::util::CMSExceptionSupport::createMessageFormatException(ex);
00214 }
00215 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00216 }
00217
00218 virtual short getShortProperty(const std::string& name) const {
00219
00220 try {
00221 return this->propertiesInterceptor->getShortProperty(name);
00222 } catch (decaf::lang::exceptions::UnsupportedOperationException& ex) {
00223 throw activemq::util::CMSExceptionSupport::createMessageFormatException(ex);
00224 }
00225 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00226 }
00227
00228 virtual std::string getStringProperty(const std::string& name) const {
00229
00230 try {
00231 return this->propertiesInterceptor->getStringProperty(name);
00232 } catch (decaf::lang::exceptions::UnsupportedOperationException& ex) {
00233 throw activemq::util::CMSExceptionSupport::createMessageFormatException(ex);
00234 }
00235 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00236 }
00237
00238 virtual void setBooleanProperty(const std::string& name, bool value) {
00239
00240 if (name == "") {
00241 throw cms::CMSException("Message Property names must not be empty", NULL);
00242 }
00243
00244 failIfReadOnlyProperties();
00245 try {
00246 this->propertiesInterceptor->setBooleanProperty(name, value);
00247 }
00248 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00249 }
00250
00251 virtual void setByteProperty(const std::string& name, unsigned char value) {
00252
00253 if (name == "") {
00254 throw cms::CMSException("Message Property names must not be empty", NULL);
00255 }
00256
00257 failIfReadOnlyProperties();
00258 try {
00259 this->propertiesInterceptor->setByteProperty(name, value);
00260 }
00261 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00262 }
00263
00264 virtual void setDoubleProperty(const std::string& name, double value) {
00265
00266 if (name == "") {
00267 throw cms::CMSException("Message Property names must not be empty", NULL);
00268 }
00269
00270 failIfReadOnlyProperties();
00271 try {
00272 this->propertiesInterceptor->setDoubleProperty(name, value);
00273 }
00274 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00275 }
00276
00277 virtual void setFloatProperty(const std::string& name, float value) {
00278
00279 if (name == "") {
00280 throw cms::CMSException("Message Property names must not be empty", NULL);
00281 }
00282
00283 failIfReadOnlyProperties();
00284 try {
00285 this->propertiesInterceptor->setFloatProperty(name, value);
00286 }
00287 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00288 }
00289
00290 virtual void setIntProperty(const std::string& name, int value) {
00291
00292 if (name == "") {
00293 throw cms::CMSException("Message Property names must not be empty", NULL);
00294 }
00295
00296 failIfReadOnlyProperties();
00297 try {
00298 this->propertiesInterceptor->setIntProperty(name, value);
00299 }
00300 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00301 }
00302
00303 virtual void setLongProperty(const std::string& name, long long value) {
00304
00305 if (name == "") {
00306 throw cms::CMSException("Message Property names must not be empty", NULL);
00307 }
00308
00309 failIfReadOnlyProperties();
00310 try {
00311 this->propertiesInterceptor->setLongProperty(name, value);
00312 }
00313 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00314 }
00315
00316 virtual void setShortProperty(const std::string& name, short value) {
00317
00318 if (name == "") {
00319 throw cms::CMSException("Message Property names must not be empty", NULL);
00320 }
00321
00322 failIfReadOnlyProperties();
00323 try {
00324 this->propertiesInterceptor->setShortProperty(name, value);
00325 }
00326 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00327 }
00328
00329 virtual void setStringProperty(const std::string& name, const std::string& value) {
00330
00331 if (name == "") {
00332 throw cms::CMSException("Message Property names must not be empty", NULL);
00333 }
00334
00335 failIfReadOnlyProperties();
00336 try {
00337 this->propertiesInterceptor->setStringProperty(name, value);
00338 }
00339 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00340 }
00341
00342 virtual std::string getCMSCorrelationID() const {
00343 return this->getCorrelationId();
00344 }
00345
00346 virtual void setCMSCorrelationID(const std::string& correlationId) {
00347 this->setCorrelationId(correlationId);
00348 }
00349
00350 virtual int getCMSDeliveryMode() const {
00351 return !this->isPersistent();
00352 }
00353
00354 virtual void setCMSDeliveryMode(int mode) {
00355 this->setPersistent(mode == (int) cms::DeliveryMode::PERSISTENT);
00356 }
00357
00358 virtual const cms::Destination* getCMSDestination() const {
00359 return dynamic_cast<const cms::Destination*> (this->getDestination().get());
00360 }
00361
00362 virtual void setCMSDestination(const cms::Destination* destination) {
00363
00364 try {
00365 if (destination != NULL) {
00366 this->setDestination(decaf::lang::Pointer<ActiveMQDestination>(
00367 dynamic_cast<ActiveMQDestination*> (destination->clone())));
00368 } else {
00369 this->getDestination().reset(NULL);
00370 }
00371 }
00372 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00373 }
00374
00375 virtual long long getCMSExpiration() const {
00376 return this->getExpiration();
00377 }
00378
00379 virtual void setCMSExpiration(long long expireTime) {
00380 this->setExpiration(expireTime);
00381 }
00382
00383 virtual std::string getCMSMessageID() const {
00384 try {
00385 return wireformat::openwire::marshal::BaseDataStreamMarshaller::toString(this->getMessageId().get());
00386 }
00387 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00388 }
00389
00390 virtual void setCMSMessageID(const std::string& value) {
00391 try {
00392 Pointer<MessageId> id(new MessageId(value));
00393 this->setMessageId(id);
00394 } catch (decaf::lang::exceptions::NumberFormatException& e) {
00395
00396
00397 Pointer<MessageId> id(new MessageId);
00398 id->setTextView(value);
00399 this->setMessageId(messageId);
00400 }
00401 }
00402
00403 virtual int getCMSPriority() const {
00404 return this->getPriority();
00405 }
00406
00407 virtual void setCMSPriority(int priority) {
00408 this->setPriority((unsigned char) priority);
00409 }
00410
00411 virtual bool getCMSRedelivered() const {
00412 return this->getRedeliveryCounter() != 0;
00413 }
00414
00415 virtual void setCMSRedelivered(bool redelivered AMQCPP_UNUSED ) {
00416 }
00417
00418 virtual const cms::Destination* getCMSReplyTo() const {
00419 return dynamic_cast<const cms::Destination*> (this->getReplyTo().get());
00420 }
00421
00422 virtual void setCMSReplyTo(const cms::Destination* destination) {
00423
00424 try {
00425 if (destination != NULL) {
00426 this->setReplyTo(decaf::lang::Pointer<ActiveMQDestination>(
00427 dynamic_cast<ActiveMQDestination*> (destination->clone())));
00428 } else {
00429 this->setReplyTo(decaf::lang::Pointer<ActiveMQDestination>());
00430 }
00431 }
00432 AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00433 }
00434
00435 virtual long long getCMSTimestamp() const {
00436 return this->getTimestamp();
00437 }
00438
00439 virtual void setCMSTimestamp(long long timeStamp) {
00440 this->setTimestamp(timeStamp);
00441 }
00442
00443 virtual std::string getCMSType() const {
00444 return this->getType();
00445 }
00446
00447 virtual void setCMSType(const std::string& type) {
00448 this->setType(type);
00449 }
00450
00451 protected:
00452
00453 void failIfWriteOnlyBody() const {
00454 if (!this->isReadOnlyBody()) {
00455 throw cms::MessageNotReadableException("message is in write-only mode and cannot be read from", NULL);
00456 }
00457 }
00458
00459 void failIfReadOnlyBody() const {
00460 if (this->isReadOnlyBody()) {
00461 throw cms::MessageNotWriteableException("Message Body is Read-Only.", NULL);
00462 }
00463 }
00464
00465 void failIfReadOnlyProperties() const {
00466 if (this->isReadOnlyProperties()) {
00467 throw cms::MessageNotWriteableException("Message Properties are Read-Only.", NULL);
00468 }
00469 }
00470
00471 };
00472
00473 }
00474 }
00475
00476 #endif