activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nmitt...@apache.org
Subject svn commit: r609175 - in /activemq/activemq-cpp/trunk/src: main/ main/activemq/cmsutil/ test/ test/activemq/cmsutil/
Date Sat, 05 Jan 2008 16:58:57 GMT
Author: nmittler
Date: Sat Jan  5 08:58:56 2008
New Revision: 609175

URL: http://svn.apache.org/viewvc?rev=609175&view=rev
Log:
AMQCPP-152 - Adding classes for support of CmsTemplate

Added:
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsAccessor.h
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsDestinationAccessor.h
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DestinationResolver.h
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DynamicDestinationResolver.cpp
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DynamicDestinationResolver.h
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ResourceLifecycleManager.cpp
    activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ResourceLifecycleManager.h
    activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/
    activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummySession.h
    activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DynamicDestinationResolverTest.cpp
    activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DynamicDestinationResolverTest.h
Modified:
    activemq/activemq-cpp/trunk/src/main/Makefile.am
    activemq/activemq-cpp/trunk/src/test/Makefile.am

Modified: activemq/activemq-cpp/trunk/src/main/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/Makefile.am?rev=609175&r1=609174&r2=609175&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/main/Makefile.am Sat Jan  5 08:58:56 2008
@@ -22,6 +22,8 @@
     activemq/util/PrimitiveMap.cpp \
     activemq/util/LongSequenceGenerator.cpp \
     activemq/util/URISupport.cpp \
+    activemq/cmsutil/DynamicDestinationResolver.cpp \
+    activemq/cmsutil/ResourceLifecycleManager.cpp \
     activemq/core/ActiveMQConsumer.cpp \
     activemq/core/ActiveMQConnection.cpp \
     activemq/core/ActiveMQSession.cpp \
@@ -87,6 +89,11 @@
     activemq/core/ActiveMQSessionExecutor.h \
     activemq/core/DispatchData.h \
     activemq/core/Dispatcher.h \
+    activemq/cmsutil/CmsAccessor.h \
+    activemq/cmsutil/CmsDestinationAccessor.h \
+    activemq/cmsutil/DestinationResolver.h \
+    activemq/cmsutil/DynamicDestinationResolver.h \
+    activemq/cmsutil/ResourceLifecycleManager.h \
     activemq/support/LibraryInit.h \
     activemq/support/InitDirector.h \
     activemq/io/LoggingInputStream.h \

Added: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsAccessor.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsAccessor.h?rev=609175&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsAccessor.h (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsAccessor.h Sat Jan  5 08:58:56
2008
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef _ACTIVEMQ_CMSUTIL_CMSACCESSOR_H_
+#define _ACTIVEMQ_CMSUTIL_CMSACCESSOR_H_
+
+#include <cms/ConnectionFactory.h>
+
+namespace activemq {
+namespace cmsutil {
+
+    /**
+     * Base class for {@link activemq.cms.CmsTemplate} and other
+     * CMS-accessing gateway helpers, defining common properties such as the
+     * CMS {@link ConnectionFactory} to operate on. The subclass
+     * {@link activemq.cms.CmsDestinationAccessor}
+     * adds further, destination-related properties.
+     *
+     * <p>Not intended to be used directly.
+     * 
+     * @see activemq.cms.CmsDestinationAccessor
+     * @see activemq.cms.CmsTemplate
+     */
+    class CmsAccessor {
+    
+    private:
+    
+        cms::ConnectionFactory* connectionFactory;
+    
+        bool sessionTransacted;
+    
+        int sessionAcknowledgeMode;
+    
+    public:
+        
+        CmsAccessor() {
+            sessionTransacted = false;
+            sessionAcknowledgeMode = cms::Session::AUTO_ACKNOWLEDGE;
+        }
+        
+        virtual ~CmsAccessor() {            
+        }
+    
+        /**
+         * Set the ConnectionFactory to use for obtaining CMS Connections.
+         */
+        virtual void setConnectionFactory(ConnectionFactory* connectionFactory) {
+            this->connectionFactory = connectionFactory;
+        }
+    
+        /**
+         * Return the ConnectionFactory that this accessor uses for
+         * obtaining CMS Connections.
+         */
+        virtual const ConnectionFactory* getConnectionFactory() const {
+            return this->connectionFactory;
+        }
+    
+        /**
+         * Return the ConnectionFactory that this accessor uses for
+         * obtaining CMS Connections.
+         */
+        virtual ConnectionFactory* getConnectionFactory() {
+            return this->connectionFactory;
+        }
+    
+        /**
+         * Set the transaction mode that is used when creating a CMS Session.
+         * Default is "false".
+         * 
+         * @param sessionTransacted the transaction mode
+         */
+        virtual void setSessionTransacted(bool sessionTransacted) {
+            this->sessionTransacted = sessionTransacted;
+        }
+    
+        /**
+         * Return whether the CMS sessions used by this
+         * accessor are supposed to be transacted.
+         * @return <code>true</code> if the CMS Sessions used are transacted
+         * @see #setSessionTransacted(bool)
+         */
+        virtual bool isSessionTransacted() const {
+            return this->sessionTransacted;
+        }
+    
+        /**
+         * Set the CMS acknowledgement mode that is used when creating a CMS
+         * Session to send a message.
+         * <p>Default is <code>AUTO_ACKNOWLEDGE</code>.
+         * @param sessionAcknowledgeMode the acknowledgement mode
+         */
+        virtual void setSessionAcknowledgeMode(int sessionAcknowledgeMode) {
+            this->sessionAcknowledgeMode = sessionAcknowledgeMode;
+        }
+    
+        /**
+         * Return the acknowledgement mode for CMS sessions.
+         * @return the acknowledgement mode applied by this accessor
+         */
+        virtual int getSessionAcknowledgeMode() const {
+            return this->sessionAcknowledgeMode;
+        }
+    
+    protected:
+    
+        /**
+         * Create a CMS Connection via this template's ConnectionFactory.
+         * @return the new CMS Connection
+         * @throws cms::CMSException if thrown by CMS API methods
+         */
+        virtual cms::Connection* createConnection() throw (cms::CMSException) {
+            return getConnectionFactory()->createConnection();
+        }
+    
+        /**
+         * Create a CMS Session for the given Connection.
+         * @param con the CMS Connection to create a Session for
+         * @return the new CMS Session
+         * @throws cms::CMSException if thrown by CMS API methods
+         */
+        virtual cms::Session* createSession(cms::Connection* con) throw (cms::CMSException)
{
+            return con->createSession(isSessionTransacted(), getSessionAcknowledgeMode());
+        }
+    
+        /**
+         * Determine whether the given Session is in client acknowledge mode.
+         * @param session the CMS Session to check
+         * @return whether the given Session is in client acknowledge mode
+         * @throws cms::CMSException if thrown by CMS API methods
+         */
+        virtual bool isClientAcknowledge(cms::Session* session) throws JMSException {
+            return (session.getAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE);
+        }
+    
+    };
+
+}}
+
+#endif /* _ACTIVEMQ_CMSUTIL_CMSACCESSOR_H_ */

Added: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsDestinationAccessor.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsDestinationAccessor.h?rev=609175&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsDestinationAccessor.h (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsDestinationAccessor.h Sat Jan
 5 08:58:56 2008
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef _ACTIVEMQ_CMSUTIL_CMSDESTINATIONACCESSOR_H_
+#define _ACTIVEMQ_CMSUTIL_CMSDESTINATIONACCESSOR_H_
+
+#include <activemq/cmsutil/CmsAccessor.h>
+
+namespace activemq {
+namespace cmsutil {
+
+    /**
+     * Extends the <code>CmsAccessor</code> to add support for resolving destination
names.
+     * 
+     * <p>Not intended to be used directly.
+     * 
+     * @see CmsTemplate
+     * @see CmsAccessor
+     */
+    class CmsDestinationAccessor : public CmsAccessor {
+    
+    private:
+        
+        /**
+         * The default destination resolver.
+         */
+        DynamicDestinationResolver defaultDestinationResolver;
+        
+        /**
+         * The destination resolver to use.
+         */
+        DestinationResolver* destinationResolver;
+    
+        /**
+         * Determines whether to use topics or queues by default.
+         */
+        bool pubSubDomain;
+    
+    public:
+        
+        CmsDestinationAccessor() {
+            pubSubDomain = false;             
+            destinationResolver = &defaultDestinationResolver;
+        }
+        
+        virtual ~CmsDestinationAccessor() {            
+        }
+        
+        virtual bool isPubSubDomain() const {
+            return this->pubSubDomain;
+        }
+        
+        virtual void setPubSubDomain( bool pubSubDomain ) {
+            this->pubSubDomain = pubSubDomain;
+        }
+    
+        virtual DestinationResolver* getDestinationResolver() {
+            return destinationResolver;
+        }
+        
+        virtual const DestinationResolver* getDestinationResolver() const {
+            return destinationResolver;
+        }
+        
+        virtual void setDestinationResolver( DestinationResolver* destRes ) {
+            this->destinationResolver = destRes;
+        }
+        
+    protected:
+        
+        /**
+         * Resolves the destination via the <code>DestinationResolver</code>.
+         * @param session
+         *      the session
+         * @param destName
+         *      the name of the destination.
+         * @return the destination
+         * @throws cms::CMSException if resolution failed.
+         */
+        virtual cms::Destination* resolveDestinationName( 
+                cms::Session* session, 
+                const std::string& destName ) throws (cms::CMSException) {
+            
+            getDestinationResolver().resolveDestinationName(session, 
+                    destName, 
+                    isPubSubDomain());
+        }
+    };
+
+}}
+
+#endif /* _ACTIVEMQ_CMSUTIL_CMSDESTINATIONACCESSOR_H_ */

Added: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DestinationResolver.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DestinationResolver.h?rev=609175&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DestinationResolver.h (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DestinationResolver.h Sat Jan  5
08:58:56 2008
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef _ACTIVEMQ_CMSUTIL_DESTINATIONRESOLVER_H_
+#define _ACTIVEMQ_CMSUTIL_DESTINATIONRESOLVER_H_
+
+#include <cms/Session.h>
+
+namespace activemq {
+namespace cmsutil {
+
+    // Forward declarations.
+    class ResourceLifecycleManager;
+    
+    /**
+     * Resolves a CMS destination name to a <code>Destination</code>.
+     */
+    class DestinationResolver {
+    
+    public:
+    
+        virtual ~DestinationResolver() {}
+    
+        /**
+         * Sets the <code>ResourceLifecycleManager</code> to be used for
+         * any allocated resources.
+         * 
+         * @param mgr
+         *      the resource lifecycle manager.
+         */
+        virtual void setResourceLifecycleManager( ResourceLifecycleManager* mgr) = 0;
+        
+        /**
+         * Resolves the given name to a destination.  If 
+         * <code>pubSubDomain</code> is true, a topic will be returned, 
+         * otherwise a queue will be returned.
+         * 
+         * @param session 
+         *      the session for which to retrieve resolve the 
+         *      destination.
+         * @param destName 
+         *      the name to be resolved.
+         * @param pubSubDomain 
+         *      If true, the name will be resolved to a Topic,
+         *      otherwise a Queue.
+         * @return the resolved destination
+         * @throws cms::CMSException if resolution failed.
+         */
+        virtual cms::Destination* resolveDestinationName(
+                cms::Session* session,
+                const std::string& destName,
+                bool pubSubDomain ) throw (cms::CMSException) = 0;
+    };
+
+}}
+
+#endif /* _ACTIVEMQ_CMSUTIL_DESTINATIONRESOLVER_H_ */

Added: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DynamicDestinationResolver.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DynamicDestinationResolver.cpp?rev=609175&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DynamicDestinationResolver.cpp (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DynamicDestinationResolver.cpp Sat
Jan  5 08:58:56 2008
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "DynamicDestinationResolver.h"
+#include "ResourceLifecycleManager.h"
+
+using namespace activemq::cmsutil;
+using namespace decaf::util;
+using namespace std;
+
+////////////////////////////////////////////////////////////////////////////////
+cms::Topic* DynamicDestinationResolver::SessionResolver::getTopic(
+        const std::string& topicName ) throw (cms::CMSException) {
+
+    cms::Topic* topic = NULL;
+    try {
+        
+        // See if we already have a topic with this name.
+        topic = topicMap.getValue(topicName);
+        
+    } catch (decaf::lang::exceptions::NoSuchElementException& ex) {
+        
+        // Create a new topic.
+        topic = session->createTopic(topicName);
+        
+        // Add the topic to the lifecycle manager.
+        resourceLifecycleManager->addDestination(topic);
+        
+        // Add the topic to the map.
+        topicMap.setValue(topicName, topic);
+    }
+    return topic;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+cms::Queue* DynamicDestinationResolver::SessionResolver::getQueue(
+        const std::string& queueName) throw (cms::CMSException) {
+
+    cms::Queue* queue = NULL;
+    try {
+        
+        // See if we already have a queue with this name.
+        queue = queueMap.getValue(queueName);
+        
+    } catch (decaf::lang::exceptions::NoSuchElementException& ex) {
+        
+        // Create a new queue.
+        queue = session->createQueue(queueName);
+        
+        // Add the queue to the lifecycle manager.
+        resourceLifecycleManager->addDestination(queue);
+        
+        // Add the queue to the map.
+        queueMap.setValue(queueName, queue);
+    }
+    return queue;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+DynamicDestinationResolver::~DynamicDestinationResolver() {
+    
+    // Destroy the session resolvers.
+    vector<SessionResolver*> r = sessionResolverMap.getValues();
+    for( size_t ix=0; ix<r.size(); ++ix ) {
+        delete r[ix];
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+cms::Destination* DynamicDestinationResolver::resolveDestinationName(
+        cms::Session* session, const std::string& destName, bool pubSubDomain)
+        throw (cms::CMSException) {
+ 
+    // Get the resolver for this session.
+    SessionResolver* resolver = NULL;
+    try {
+        resolver = sessionResolverMap.getValue(session);
+    } catch (decaf::lang::exceptions::NoSuchElementException& ex) {
+        resolver = new SessionResolver(session, resourceLifecycleManager);
+        sessionResolverMap.setValue(session, resolver);
+    }
+    
+    // Return the appropriate destination.
+    if( pubSubDomain ) {
+        return resolver->getTopic(destName);
+    } else {
+        return resolver->getQueue(destName);
+    }
+}

Added: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DynamicDestinationResolver.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DynamicDestinationResolver.h?rev=609175&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DynamicDestinationResolver.h (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/DynamicDestinationResolver.h Sat
Jan  5 08:58:56 2008
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef _ACTIVEMQ_CMSUTIL_DYNAMICDESTINATIONRESOLVER_H_
+#define _ACTIVEMQ_CMSUTIL_DYNAMICDESTINATIONRESOLVER_H_
+
+#include <activemq/cmsutil/DestinationResolver.h>
+#include <cms/Session.h>
+#include <decaf/util/Map.h>
+
+namespace activemq {
+namespace cmsutil {
+
+    /**
+     * Resolves a CMS destination name to a <code>Destination</code>.
+     */
+    class DynamicDestinationResolver : public DestinationResolver {
+    
+    private:
+        
+        /**
+         * Manages maps of names to topics and queues for a single session.
+         */
+        class SessionResolver {
+        private:
+            ResourceLifecycleManager* resourceLifecycleManager;
+            cms::Session* session;
+            decaf::util::Map<std::string, cms::Topic*> topicMap;
+            decaf::util::Map<std::string, cms::Queue*> queueMap;
+            
+        public:
+            
+            SessionResolver(cms::Session* session,
+                ResourceLifecycleManager* resourceLifecycleManager ) {
+                
+                this->session = session;
+                this->resourceLifecycleManager = resourceLifecycleManager;
+            }
+            
+            virtual ~SessionResolver() {}
+            
+            cms::Topic* getTopic(const std::string& topicName ) 
+                throw (cms::CMSException);
+            
+            cms::Queue* getQueue(const std::string& queueName ) 
+                throw (cms::CMSException);
+        };
+        
+        /**
+         * Maps a given session to the resolver for that session.
+         */
+        decaf::util::Map< cms::Session*, SessionResolver*> sessionResolverMap;
+        
+        /**
+         * Manages lifecycle of any allocated topics/queues.
+         */
+        ResourceLifecycleManager* resourceLifecycleManager;
+        
+    public:
+    
+        virtual ~DynamicDestinationResolver();
+    
+        /**
+         * Sets the <code>ResourceLifecycleManager</code> to be used for
+         * any allocated resources.
+         * 
+         * @param mgr
+         *      the resource lifecycle manager.
+         */
+        virtual void setResourceLifecycleManager( ResourceLifecycleManager* mgr) {
+            this->resourceLifecycleManager = mgr;
+        }
+                
+        /**
+         * Resolves the given name to a destination.  If 
+         * <code>pubSubDomain</code> is true, a topic will be returned, 
+         * otherwise a queue will be returned.
+         * 
+         * @param session 
+         *      the session for which to retrieve resolve the 
+         *      destination.
+         * @param destName 
+         *      the name to be resolved.
+         * @param pubSubDomain 
+         *      If true, the name will be resolved to a Topic,
+         *      otherwise a Queue.
+         * @return the resolved destination
+         * @throws cms::CMSException if resolution failed.
+         */
+        virtual cms::Destination* resolveDestinationName(
+                cms::Session* session,
+                const std::string& destName,
+                bool pubSubDomain ) throw (cms::CMSException);
+    };
+
+}}
+
+#endif /* _ACTIVEMQ_CMSUTIL_DYNAMICDESTINATIONRESOLVER_H_ */

Added: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ResourceLifecycleManager.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ResourceLifecycleManager.cpp?rev=609175&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ResourceLifecycleManager.cpp (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ResourceLifecycleManager.cpp Sat
Jan  5 08:58:56 2008
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ResourceLifecycleManager.h"
+
+using namespace activemq::cmsutil;
+
+////////////////////////////////////////////////////////////////////////////////
+ResourceLifecycleManager::ResourceLifecycleManager() {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ResourceLifecycleManager::~ResourceLifecycleManager() {
+
+    try {
+        
+        // Destroy all the resources
+        destroy();
+        
+    } catch( cms::CMSException& e ) { /* Absorb*/}
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ResourceLifecycleManager::releaseAll() {
+
+    connections.clear();
+    sessions.clear();
+    destinations.clear();
+    producers.clear();
+    consumers.clear();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ResourceLifecycleManager::destroy() throw (cms::CMSException) {
+
+    // Close all the connections.
+    for (std::size_t ix=0; ix<connections.size(); ++ix) {
+        connections[ix]->close();
+    }
+
+    // Destroy the producers.
+    for (std::size_t ix=0; ix<producers.size(); ++ix) {
+        delete producers[ix];
+    }
+
+    // Destroy the consumers.
+    for (std::size_t ix=0; ix<consumers.size(); ++ix) {
+        delete consumers[ix];
+    }
+
+    // Destroy the destinations.
+    for (std::size_t ix=0; ix<destinations.size(); ++ix) {
+        delete destinations[ix];
+    }
+
+    // Destroy the sessions.
+    for (std::size_t ix=0; ix<sessions.size(); ++ix) {
+        delete sessions[ix];
+    }
+
+    // Destroy the connections,
+    for (std::size_t ix=0; ix<connections.size(); ++ix) {
+        delete connections[ix];
+    }
+
+    // Empty all the vectors.
+    releaseAll();
+}
+

Added: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ResourceLifecycleManager.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ResourceLifecycleManager.h?rev=609175&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ResourceLifecycleManager.h (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/ResourceLifecycleManager.h Sat Jan
 5 08:58:56 2008
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ACTIVEMQ_CMSUTIL_RESOURCELIFECYCLEMANAGER_H_
+#define ACTIVEMQ_CMSUTIL_RESOURCELIFECYCLEMANAGER_H_
+
+#include <cms/Connection.h>
+#include <cms/Session.h>
+#include <cms/Destination.h>
+#include <cms/MessageProducer.h>
+#include <cms/MessageConsumer.h>
+
+namespace activemq {
+namespace cmsutil {
+
+    /**
+     * Manages the lifecycle of a set of CMS resources.  A call to
+     * <code>destroy</code> will close and destroy all of the contained
+     * resources in the appropriate manner.
+     */
+    class ResourceLifecycleManager {
+    private:
+    
+        std::vector<cms::Connection*> connections;
+        std::vector<cms::Session*> sessions;
+        std::vector<cms::Destination*> destinations;
+        std::vector<cms::MessageProducer*> producers;
+        std::vector<cms::MessageConsumer*> consumers;
+    
+    public:
+    
+        ResourceLifecycleManager();
+        
+        /**
+         * Destructor - calls <code>destroy</code>
+         */
+        virtual ~ResourceLifecycleManager();
+    
+        /**
+         * Adds a connection so that its life will be managed by
+         * this object.
+         * 
+         * @param connection
+         *         the object to be managed
+         */
+        void addConnection(cms::Connection* connection) {
+            connections.push_back(connection);
+        }
+    
+        /**
+         * Adds a session so that its life will be managed by
+         * this object.
+         * 
+         * @param session
+         *         the object to be managed
+         */
+        void addSession(cms::Session* session) {
+            sessions.push_back(session);
+        }
+        
+        /**
+         * Adds a destination so that its life will be managed by
+         * this object.
+         * 
+         * @param dest
+         *         the object to be managed
+         */
+        void addDestination(cms::Destination* dest) {
+            destinations.push_back(dest);
+        }
+        
+        /**
+         * Adds a message producer so that its life will be managed by
+         * this object.
+         * 
+         * @param producer
+         *         the object to be managed
+         */
+        void addMessageProducer(cms::MessageProducer* producer) {
+            producers.push_back(producer);
+        }
+        
+        /**
+         * Adds a message consumer so that its life will be managed by
+         * this object.
+         * 
+         * @param consumer
+         *         the object to be managed
+         */
+        void addMessageConsumer(cms::MessageConsumer* consumer) {
+            consumers.push_back(consumer);
+        }
+    
+        /**
+         * Closes and destroys the contained CMS resources.
+         * @throws cms::CMSException thrown if an error occurs.
+         */
+        void destroy() throw (cms::CMSException);
+    
+        /**
+         * Releases all of the contained resources so that this 
+         * object will no longer control their lifetimes. 
+         */
+        void releaseAll();
+    };
+
+}}
+
+#endif /*ACTIVEMQ_CMSUTIL_RESOURCELIFECYCLEMANAGER_H_*/

Modified: activemq/activemq-cpp/trunk/src/test/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/Makefile.am?rev=609175&r1=609174&r2=609175&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/test/Makefile.am Sat Jan  5 08:58:56 2008
@@ -60,6 +60,7 @@
   activemq/core/ActiveMQConnectionFactoryTest.cpp \
   activemq/core/ActiveMQConnectionTest.cpp \
   activemq/core/ActiveMQSessionTest.cpp \
+  activemq/cmsutil/DynamicDestinationResolverTest.cpp \
   activemq/exceptions/ActiveMQExceptionTest.cpp \
   activemq/transport/IOTransportTest.cpp \
   activemq/transport/filters/ResponseCorrelatorTest.cpp \
@@ -79,7 +80,7 @@
 check_PROGRAMS = activemq-test
 
 ## Also run the tests as part of make check
-## TESTS = $(check_PROGRAMS)
+TESTS = $(check_PROGRAMS)
 
 ## 
 ## Compiler/Linker Options

Added: activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummySession.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummySession.h?rev=609175&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummySession.h (added)
+++ activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummySession.h Sat Jan  5 08:58:56
2008
@@ -0,0 +1,98 @@
+#ifndef ACTIVEMQ_CMSUTIL_DUMMYSESSION_H_
+#define ACTIVEMQ_CMSUTIL_DUMMYSESSION_H_
+
+#include <cms/Session.h>
+#include <activemq/connector/stomp/StompTopic.h>
+#include <activemq/connector/stomp/StompQueue.h>
+
+namespace activemq {
+namespace cmsutil {
+
+    class DummySession : public cms::Session {
+        
+    public:
+
+            virtual ~DummySession() {}
+
+            virtual void close() throw( cms::CMSException ){}
+            
+            virtual void commit() throw ( cms::CMSException ) {}
+            
+            virtual void rollback() throw ( cms::CMSException ) {}
+
+            virtual cms::MessageConsumer* createConsumer(
+                const cms::Destination* destination )
+                    throw ( cms::CMSException ) { return NULL; }
+
+            virtual cms::MessageConsumer* createConsumer( 
+                const cms::Destination* destination,
+                const std::string& selector )
+                    throw ( cms::CMSException ) {
+                return NULL;
+            }
+            
+            virtual cms::MessageConsumer* createConsumer( 
+                const cms::Destination* destination,
+                const std::string& selector,
+                bool noLocal )
+                    throw ( cms::CMSException ) { return NULL; }
+
+            virtual cms::MessageConsumer* createDurableConsumer(
+                const cms::Topic* destination,
+                const std::string& name,
+                const std::string& selector,
+                bool noLocal = false )
+                    throw ( cms::CMSException ) { return NULL; }
+
+            virtual cms::MessageProducer* createProducer( const cms::Destination* destination
)
+                throw ( cms::CMSException ) { return NULL; }
+
+            virtual cms::Queue* createQueue( const std::string& queueName )
+                throw ( cms::CMSException ) {
+                return new activemq::connector::stomp::StompQueue(queueName);
+            }
+            
+            virtual cms::Topic* createTopic( const std::string& topicName )
+                throw ( cms::CMSException ) {
+                return new activemq::connector::stomp::StompTopic(topicName);
+            }
+
+            virtual cms::TemporaryQueue* createTemporaryQueue()
+                throw ( cms::CMSException ) { return NULL; }
+
+            virtual cms::TemporaryTopic* createTemporaryTopic()
+                throw ( cms::CMSException ){ return NULL; }
+
+            virtual cms::Message* createMessage() 
+                throw ( cms::CMSException ){ return NULL; }
+
+            virtual cms::BytesMessage* createBytesMessage() 
+                throw ( cms::CMSException){ return NULL; }
+
+            virtual cms::BytesMessage* createBytesMessage(
+                const unsigned char* bytes,
+                std::size_t bytesSize ) 
+                    throw ( cms::CMSException){
+                return NULL;
+            }
+
+            virtual cms::TextMessage* createTextMessage() 
+                throw ( cms::CMSException ){ return NULL; }
+
+            virtual cms::TextMessage* createTextMessage( const std::string& text ) 
+                throw ( cms::CMSException ){ return NULL; }
+
+            virtual cms::MapMessage* createMapMessage() 
+                throw ( cms::CMSException ){ return NULL; }
+
+            virtual cms::Session::AcknowledgeMode getAcknowledgeMode() const { return cms::Session::AUTO_ACKNOWLEDGE;
}
+
+            virtual bool isTransacted() const{ return false; }
+ 
+            virtual void unsubscribe( const std::string& name ) 
+                throw ( cms::CMSException ){}
+    };
+    
+}}
+
+#endif /*ACTIVEMQ_CMSUTIL_DUMMYSESSION_H_*/

Added: activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DynamicDestinationResolverTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DynamicDestinationResolverTest.cpp?rev=609175&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DynamicDestinationResolverTest.cpp
(added)
+++ activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DynamicDestinationResolverTest.cpp
Sat Jan  5 08:58:56 2008
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "DynamicDestinationResolverTest.h"
+#include <activemq/cmsutil/DynamicDestinationResolver.h>
+#include <activemq/cmsutil/ResourceLifecycleManager.h>
+#include "DummySession.h"
+
+CPPUNIT_TEST_SUITE_REGISTRATION( activemq::cmsutil::DynamicDestinationResolverTest );
+
+using namespace activemq;
+using namespace activemq::cmsutil;
+
+////////////////////////////////////////////////////////////////////////////////
+void DynamicDestinationResolverTest::testTopics() {
+
+    ResourceLifecycleManager mgr;    
+    DynamicDestinationResolver resolver;
+    resolver.setResourceLifecycleManager(&mgr);
+    
+    DummySession session;
+    
+    // Test topic
+    cms::Destination* testTopic = dynamic_cast<cms::Topic*>(resolver.resolveDestinationName(&session,

+            (std::string)"test", true ));
+    
+    CPPUNIT_ASSERT(testTopic != NULL);
+    
+    // Hello topic
+    cms::Destination* helloTopic = resolver.resolveDestinationName(&session, 
+            (std::string)"hello", true );
+    
+    CPPUNIT_ASSERT(helloTopic != NULL);
+    CPPUNIT_ASSERT(helloTopic != testTopic);
+    
+    cms::Destination* testTopic2 = dynamic_cast<cms::Topic*>(resolver.resolveDestinationName(&session,

+                (std::string)"test", true ));
+    
+    CPPUNIT_ASSERT(testTopic2 != NULL);
+    CPPUNIT_ASSERT(testTopic == testTopic2);
+    
+    mgr.destroy();
+    
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DynamicDestinationResolverTest::testQueues() {
+
+    ResourceLifecycleManager mgr;    
+    DynamicDestinationResolver resolver;
+    resolver.setResourceLifecycleManager(&mgr);
+    
+    DummySession session;
+    
+    // Queue topic
+    cms::Destination* testQueue = dynamic_cast<cms::Queue*>(resolver.resolveDestinationName(&session,

+            (std::string)"test", false ));
+    
+    CPPUNIT_ASSERT(testQueue != NULL);
+    
+    // Hello queue
+    cms::Destination* helloQueue = resolver.resolveDestinationName(&session, 
+            (std::string)"hello", false );
+    
+    CPPUNIT_ASSERT(helloQueue != NULL);
+    CPPUNIT_ASSERT(helloQueue != testQueue);
+    
+    cms::Destination* testQueue2 = dynamic_cast<cms::Queue*>(resolver.resolveDestinationName(&session,

+                (std::string)"test", false ));
+    
+    CPPUNIT_ASSERT(testQueue2 != NULL);
+    CPPUNIT_ASSERT(testQueue == testQueue2);
+    
+    mgr.destroy();
+}

Added: activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DynamicDestinationResolverTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DynamicDestinationResolverTest.h?rev=609175&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DynamicDestinationResolverTest.h
(added)
+++ activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DynamicDestinationResolverTest.h
Sat Jan  5 08:58:56 2008
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _ACTIVEMQ_CMSUTIL_DYNAMICDESTINATIONRESOLVERTEST_H_
+#define _ACTIVEMQ_CMSUTIL_DYNAMICDESTINATIONRESOLVERTEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+
+namespace activemq{
+namespace cmsutil{
+
+    class DynamicDestinationResolverTest : public CppUnit::TestFixture
+    {
+        CPPUNIT_TEST_SUITE( DynamicDestinationResolverTest );
+        CPPUNIT_TEST( testTopics );
+        CPPUNIT_TEST( testQueues );
+        CPPUNIT_TEST_SUITE_END();
+
+    public:
+
+        DynamicDestinationResolverTest() {}
+        virtual ~DynamicDestinationResolverTest() {}
+
+        void testTopics();
+        void testQueues();
+    };
+
+}}
+
+#endif /*_ACTIVEMQ_CMSUTIL_DYNAMICDESTINATIONRESOLVERTEST_H_*/



Mime
View raw message