activemq-cpp-3.3.0

src/main/activemq/commands/ActiveMQMessageTemplate.h

Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
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(
00050                 new wireformat::openwire::utils::MessagePropertyInterceptor(
00051                     this, &this->getMessageProperties() ) );
00052         }
00053 
00054         virtual ~ActiveMQMessageTemplate() throw() {}
00055 
00056     public:  // cms::Message related methods
00057 
00058         virtual void acknowledge() const {
00059             try{
00060                 this->getAckHandler()->acknowledgeMessage( this );
00061             }
00062             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00063         }
00064 
00065         virtual void onSend() {
00066             this->setReadOnlyBody(true);
00067             this->setReadOnlyProperties(true);
00068         }
00069 
00070         virtual bool equals( const DataStructure* value ) const {
00071             try{
00072 
00073                 if( this == value ) {
00074                     return true;
00075                 }
00076 
00077                 const ActiveMQMessageTemplate<T>* object =
00078                     dynamic_cast< const ActiveMQMessageTemplate<T>* >( value );
00079 
00080                 if( object == NULL ) {
00081                     return false;
00082                 }
00083 
00084                 decaf::lang::Pointer<MessageId> thisMsgId = this->getMessageId();
00085                 decaf::lang::Pointer<MessageId> otherMsgId = object->getMessageId();
00086 
00087                 return thisMsgId != NULL && otherMsgId != NULL && otherMsgId->equals( thisMsgId.get() );
00088             }
00089             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00090         }
00091 
00092         virtual void clearBody() {
00093             try{
00094                 this->setContent( std::vector<unsigned char>() );
00095                 this->setReadOnlyBody( false );
00096             }
00097             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00098         }
00099 
00100         virtual void clearProperties() {
00101             try{
00102                 this->getMessageProperties().clear();
00103                 this->setReadOnlyProperties( false );
00104             }
00105             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00106         }
00107 
00108         virtual std::vector<std::string> getPropertyNames() const {
00109             try{
00110                 return getMessageProperties().keySet();
00111             }
00112             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00113         }
00114 
00115         virtual bool propertyExists( const std::string& name ) const {
00116             try{
00117                 return getMessageProperties().containsKey( name );
00118             }
00119             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00120         }
00121 
00122         virtual bool getBooleanProperty( const std::string& name ) const {
00123 
00124             try{
00125                 return this->propertiesInterceptor->getBooleanProperty( name );
00126             } catch( decaf::lang::exceptions::UnsupportedOperationException& ex ) {
00127                 throw activemq::util::CMSExceptionSupport::createMessageFormatException( ex );
00128             }
00129             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00130         }
00131 
00132         virtual unsigned char getByteProperty( const std::string& name ) const {
00133 
00134             try{
00135                 return this->propertiesInterceptor->getByteProperty( name );
00136             } catch( decaf::lang::exceptions::UnsupportedOperationException& ex ) {
00137                 throw activemq::util::CMSExceptionSupport::createMessageFormatException( ex );
00138             }
00139             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00140         }
00141 
00142         virtual double getDoubleProperty( const std::string& name ) const {
00143 
00144             try{
00145                 return this->propertiesInterceptor->getDoubleProperty( name );
00146             } catch( decaf::lang::exceptions::UnsupportedOperationException& ex ) {
00147                 throw activemq::util::CMSExceptionSupport::createMessageFormatException( ex );
00148             }
00149             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00150         }
00151 
00152         virtual float getFloatProperty( const std::string& name ) const {
00153 
00154             try{
00155                 return this->propertiesInterceptor->getFloatProperty( name );
00156             } catch( decaf::lang::exceptions::UnsupportedOperationException& ex ) {
00157                 throw activemq::util::CMSExceptionSupport::createMessageFormatException( ex );
00158             }
00159             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00160         }
00161 
00162         virtual int getIntProperty( const std::string& name ) const {
00163 
00164             try{
00165                 return this->propertiesInterceptor->getIntProperty( name );
00166             } catch( decaf::lang::exceptions::UnsupportedOperationException& ex ) {
00167                 throw activemq::util::CMSExceptionSupport::createMessageFormatException( ex );
00168             }
00169             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00170         }
00171 
00172         virtual long long getLongProperty( const std::string& name ) const {
00173 
00174             try{
00175                 return this->propertiesInterceptor->getLongProperty( name );
00176             } catch( decaf::lang::exceptions::UnsupportedOperationException& ex ) {
00177                 throw activemq::util::CMSExceptionSupport::createMessageFormatException( ex );
00178             }
00179             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00180         }
00181 
00182         virtual short getShortProperty( const std::string& name ) const {
00183 
00184             try{
00185                 return this->propertiesInterceptor->getShortProperty( name );
00186             } catch( decaf::lang::exceptions::UnsupportedOperationException& ex ) {
00187                 throw activemq::util::CMSExceptionSupport::createMessageFormatException( ex );
00188             }
00189             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00190         }
00191 
00192         virtual std::string getStringProperty( const std::string& name ) const {
00193 
00194             try{
00195                 return this->propertiesInterceptor->getStringProperty( name );
00196             } catch( decaf::lang::exceptions::UnsupportedOperationException& ex ) {
00197                 throw activemq::util::CMSExceptionSupport::createMessageFormatException( ex );
00198             }
00199             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00200         }
00201 
00202         virtual void setBooleanProperty( const std::string& name, bool value ) {
00203 
00204             if( name == "" ) {
00205                 throw cms::CMSException( "Message Property names must not be empty", NULL );
00206             }
00207 
00208             failIfReadOnlyProperties();
00209             try{
00210                 this->propertiesInterceptor->setBooleanProperty( name, value );
00211             }
00212             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00213         }
00214 
00215         virtual void setByteProperty( const std::string& name, unsigned char value ) {
00216 
00217             if( name == "" ) {
00218                 throw cms::CMSException( "Message Property names must not be empty", NULL );
00219             }
00220 
00221             failIfReadOnlyProperties();
00222             try{
00223                 this->propertiesInterceptor->setByteProperty( name, value );
00224             }
00225             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00226         }
00227 
00228         virtual void setDoubleProperty( const std::string& name, double value ) {
00229 
00230             if( name == "" ) {
00231                 throw cms::CMSException( "Message Property names must not be empty", NULL );
00232             }
00233 
00234             failIfReadOnlyProperties();
00235             try{
00236                 this->propertiesInterceptor->setDoubleProperty( name, value );
00237             }
00238             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00239         }
00240 
00241         virtual void setFloatProperty( const std::string& name, float value ) {
00242 
00243             if( name == "" ) {
00244                 throw cms::CMSException( "Message Property names must not be empty", NULL );
00245             }
00246 
00247             failIfReadOnlyProperties();
00248             try{
00249                 this->propertiesInterceptor->setFloatProperty( name, value );
00250             }
00251             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00252         }
00253 
00254         virtual void setIntProperty( const std::string& name, int value ) {
00255 
00256             if( name == "" ) {
00257                 throw cms::CMSException( "Message Property names must not be empty", NULL );
00258             }
00259 
00260             failIfReadOnlyProperties();
00261             try{
00262                 this->propertiesInterceptor->setIntProperty( name, value );
00263             }
00264             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00265         }
00266 
00267         virtual void setLongProperty( const std::string& name, long long value ) {
00268 
00269             if( name == "" ) {
00270                 throw cms::CMSException( "Message Property names must not be empty", NULL );
00271             }
00272 
00273             failIfReadOnlyProperties();
00274             try{
00275                 this->propertiesInterceptor->setLongProperty( name, value );
00276             }
00277             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00278         }
00279 
00280         virtual void setShortProperty( const std::string& name, short value ) {
00281 
00282             if( name == "" ) {
00283                 throw cms::CMSException( "Message Property names must not be empty", NULL );
00284             }
00285 
00286             failIfReadOnlyProperties();
00287             try{
00288                 this->propertiesInterceptor->setShortProperty( name, value );
00289             }
00290             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00291         }
00292 
00293         virtual void setStringProperty( const std::string& name, const std::string& value ) {
00294 
00295             if( name == "" ) {
00296                 throw cms::CMSException( "Message Property names must not be empty", NULL );
00297             }
00298 
00299             failIfReadOnlyProperties();
00300             try{
00301                 this->propertiesInterceptor->setStringProperty( name, value );
00302             }
00303             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00304         }
00305 
00306         virtual std::string getCMSCorrelationID() const {
00307             return this->getCorrelationId();
00308         }
00309 
00310         virtual void setCMSCorrelationID( const std::string& correlationId ) {
00311             this->setCorrelationId( correlationId );
00312         }
00313 
00314         virtual int getCMSDeliveryMode() const {
00315             return !this->isPersistent();
00316         }
00317 
00318         virtual void setCMSDeliveryMode( int mode ) {
00319             this->setPersistent( mode == (int)cms::DeliveryMode::PERSISTENT );
00320         }
00321 
00322         virtual const cms::Destination* getCMSDestination() const {
00323             return dynamic_cast<const cms::Destination*>( this->getDestination().get() );
00324         }
00325 
00326         virtual void setCMSDestination( const cms::Destination* destination ) {
00327 
00328             try{
00329                 if( destination != NULL ) {
00330                     this->setDestination(
00331                         decaf::lang::Pointer<ActiveMQDestination>(
00332                             dynamic_cast<ActiveMQDestination*>( destination->clone() ) ) );
00333                 } else {
00334                     this->getDestination().reset( NULL );
00335                 }
00336             }
00337             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00338         }
00339 
00340         virtual long long getCMSExpiration() const {
00341             return this->getExpiration();
00342         }
00343 
00344         virtual void setCMSExpiration( long long expireTime ) {
00345             this->setExpiration( expireTime );
00346         }
00347 
00348         virtual std::string getCMSMessageID() const {
00349             try{
00350                 return wireformat::openwire::marshal::BaseDataStreamMarshaller::toString( this->getMessageId().get() );
00351             }
00352             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00353         }
00354 
00355         virtual void setCMSMessageID( const std::string& id AMQCPP_UNUSED ) {}
00356 
00357         virtual int getCMSPriority() const {
00358             return this->getPriority();
00359         }
00360 
00361         virtual void setCMSPriority( int priority ) {
00362             this->setPriority( (unsigned char)priority );
00363         }
00364 
00365         virtual bool getCMSRedelivered() const {
00366             return this->getRedeliveryCounter() != 0;
00367         }
00368 
00369         virtual void setCMSRedelivered( bool redelivered AMQCPP_UNUSED ) {}
00370 
00371         virtual const cms::Destination* getCMSReplyTo() const {
00372             return dynamic_cast< const cms::Destination* >( this->getReplyTo().get() );
00373         }
00374 
00375         virtual void setCMSReplyTo( const cms::Destination* destination ) {
00376 
00377             try{
00378                 if( destination != NULL ) {
00379                     this->setReplyTo(
00380                         decaf::lang::Pointer<ActiveMQDestination>(
00381                             dynamic_cast<ActiveMQDestination*>( destination->clone() ) ) );
00382                 } else {
00383                     this->setReplyTo( decaf::lang::Pointer<ActiveMQDestination>() );
00384                 }
00385             }
00386             AMQ_CATCH_ALL_THROW_CMSEXCEPTION()
00387         }
00388 
00389         virtual long long getCMSTimestamp() const {
00390             return this->getTimestamp();
00391         }
00392 
00393         virtual void setCMSTimestamp( long long timeStamp ) {
00394             this->setTimestamp( timeStamp );
00395         }
00396 
00397         virtual std::string getCMSType() const {
00398             return this->getType();
00399         }
00400 
00401         virtual void setCMSType( const std::string& type ) {
00402             this->setType( type );
00403         }
00404 
00405     protected:
00406 
00407         void failIfWriteOnlyBody() const {
00408             if( !this->isReadOnlyBody() ){
00409                 throw cms::MessageNotReadableException(
00410                     "message is in write-only mode and cannot be read from", NULL );
00411             }
00412         }
00413 
00414         void failIfReadOnlyBody() const {
00415             if( this->isReadOnlyBody() ) {
00416                 throw cms::MessageNotWriteableException(
00417                     "Message Body is Read-Only.", NULL );
00418             }
00419         }
00420 
00421         void failIfReadOnlyProperties() const {
00422             if( this->isReadOnlyProperties() ) {
00423                 throw cms::MessageNotWriteableException(
00424                     "Message Properties are Read-Only.", NULL );
00425             }
00426         }
00427 
00428     };
00429 
00430 }}
00431 
00432 #endif /* _ACTIVEMQ_COMMANDS_ACTIVEMQMESSAGETEMPLATE_H_ */