001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.activemq.tool;
019    
020    import org.activemq.ActiveMQConnection;
021    import org.activemq.ActiveMQConnectionFactory;
022    import org.activemq.util.IndentPrinter;
023    
024    import javax.jms.Connection;
025    import javax.jms.Destination;
026    import javax.jms.JMSException;
027    import javax.jms.Session;
028    
029    /**
030     * Abstract base class useful for implementation inheritence
031     *
032     * @version $Revision$
033     */
034    public class ToolSupport {
035    
036    
037        protected Destination destination;
038        protected String subject = "TOOL.DEFAULT";
039        protected boolean topic = true;
040        protected String user = ActiveMQConnection.DEFAULT_USER;
041        protected String pwd = ActiveMQConnection.DEFAULT_PASSWORD;
042        protected String url = ActiveMQConnection.DEFAULT_URL;
043        protected boolean transacted = false;
044        protected boolean durable = false;
045        protected String clientID = getClass().getName();
046        protected int ackMode = Session.AUTO_ACKNOWLEDGE;
047        protected String consumerName = "James";
048    
049    
050        protected Session createSession(Connection connection) throws Exception {
051            if (durable) {
052                connection.setClientID(clientID);
053            }
054            Session session = connection.createSession(transacted, ackMode);
055            if (topic) {
056                destination = session.createTopic(subject);
057            }
058            else {
059                destination = session.createQueue(subject);
060            }
061            return session;
062        }
063    
064        protected Connection createConnection() throws JMSException, Exception {
065            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, pwd, url);
066            return connectionFactory.createConnection();
067        }
068    
069        protected void close(Connection connection, Session session) throws JMSException {
070            // lets dump the stats
071            dumpStats(connection);
072    
073            if (session != null) {
074                session.close();
075            }
076            if (connection != null) {
077                connection.close();
078            }
079        }
080    
081        protected void dumpStats(Connection connection) {
082            ActiveMQConnection c = (ActiveMQConnection) connection;
083            c.getConnectionStats().dump(new IndentPrinter());
084        }
085    }