001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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    package org.apache.activemq.tool;
018    
019    import javax.jms.Connection;
020    import javax.jms.ConnectionFactory;
021    import javax.jms.Destination;
022    import javax.jms.JMSException;
023    import javax.jms.Session;
024    
025    import org.apache.activemq.tool.properties.JmsClientProperties;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    
029    public abstract class AbstractJmsClient {
030    
031        private static final Log LOG = LogFactory.getLog(AbstractJmsClient.class);
032    
033        protected ConnectionFactory factory;
034        protected Connection jmsConnection;
035        protected Session jmsSession;
036    
037        protected int destCount = 1;
038        protected int destIndex;
039        protected String clientName = "";
040    
041        public AbstractJmsClient(ConnectionFactory factory) {
042            this.factory = factory;
043        }
044    
045        public abstract JmsClientProperties getClient();
046    
047        public abstract void setClient(JmsClientProperties client);
048    
049        public ConnectionFactory getFactory() {
050            return factory;
051        }
052    
053        public void setFactory(ConnectionFactory factory) {
054            this.factory = factory;
055        }
056    
057        public int getDestCount() {
058            return destCount;
059        }
060    
061        public void setDestCount(int destCount) {
062            this.destCount = destCount;
063        }
064    
065        public int getDestIndex() {
066            return destIndex;
067        }
068    
069        public void setDestIndex(int destIndex) {
070            this.destIndex = destIndex;
071        }
072    
073        public String getClientName() {
074            return clientName;
075        }
076    
077        public void setClientName(String clientName) {
078            this.clientName = clientName;
079        }
080    
081        public Connection getConnection() throws JMSException {
082            if (jmsConnection == null) {
083                jmsConnection = factory.createConnection();
084                jmsConnection.setClientID(getClientName());
085                LOG.info("Creating JMS Connection: Provider=" + getClient().getJmsProvider() + ", JMS Spec=" + getClient().getJmsVersion());
086            }
087            return jmsConnection;
088        }
089    
090        public Session getSession() throws JMSException {
091            if (jmsSession == null) {
092                int ackMode;
093                if (getClient().getSessAckMode().equalsIgnoreCase(JmsClientProperties.SESSION_AUTO_ACKNOWLEDGE)) {
094                    ackMode = Session.AUTO_ACKNOWLEDGE;
095                } else if (getClient().getSessAckMode().equalsIgnoreCase(JmsClientProperties.SESSION_CLIENT_ACKNOWLEDGE)) {
096                    ackMode = Session.CLIENT_ACKNOWLEDGE;
097                } else if (getClient().getSessAckMode().equalsIgnoreCase(JmsClientProperties.SESSION_DUPS_OK_ACKNOWLEDGE)) {
098                    ackMode = Session.DUPS_OK_ACKNOWLEDGE;
099                } else if (getClient().getSessAckMode().equalsIgnoreCase(JmsClientProperties.SESSION_TRANSACTED)) {
100                    ackMode = Session.SESSION_TRANSACTED;
101                } else {
102                    ackMode = Session.AUTO_ACKNOWLEDGE;
103                }
104                jmsSession = getConnection().createSession(getClient().isSessTransacted(), ackMode);
105            }
106            return jmsSession;
107        }
108    
109        public Destination[] createDestination(int destIndex, int destCount) throws JMSException {
110    
111            if (getClient().isDestComposite()) {
112                return new Destination[] {
113                    createCompositeDestination(getClient().getDestName(), destIndex, destCount)
114                };
115            } else {
116                Destination[] dest = new Destination[destCount];
117                for (int i = 0; i < destCount; i++) {
118                    dest[i] = createDestination(getClient().getDestName() + "." + (destIndex + i));
119                }
120    
121                return dest;
122            }
123        }
124    
125        public Destination createCompositeDestination(int destIndex, int destCount) throws JMSException {
126            return createCompositeDestination(getClient().getDestName(), destIndex, destCount);
127        }
128    
129        protected Destination createCompositeDestination(String name, int destIndex, int destCount) throws JMSException {
130            String compDestName;
131            String simpleName;
132    
133            if (name.startsWith("queue://")) {
134                simpleName = name.substring("queue://".length());
135            } else if (name.startsWith("topic://")) {
136                simpleName = name.substring("topic://".length());
137            } else {
138                simpleName = name;
139            }
140    
141            int i;
142            compDestName = name + "." + destIndex + ","; // First destination
143            for (i = 1; i < destCount - 1; i++) {
144                compDestName += simpleName + "." + (destIndex + i) + ",";
145            }
146            // Last destination (minus the comma)
147            compDestName += simpleName + "." + (destIndex + i);
148    
149            return createDestination(compDestName);
150        }
151    
152        protected Destination createDestination(String name) throws JMSException {
153            if (name.startsWith("queue://")) {
154                return getSession().createQueue(name.substring("queue://".length()));
155            } else if (name.startsWith("topic://")) {
156                return getSession().createTopic(name.substring("topic://".length()));
157            } else {
158                return getSession().createTopic(name);
159            }
160        }
161    
162    }