KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > AbstractJmsClient


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.tool;
19
20 import org.apache.activemq.tool.properties.JmsClientProperties;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.jms.ConnectionFactory JavaDoc;
25 import javax.jms.Connection JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.jms.JMSException JavaDoc;
28 import javax.jms.Destination JavaDoc;
29 import java.util.Enumeration JavaDoc;
30
31 public abstract class AbstractJmsClient {
32     private static final Log log = LogFactory.getLog(AbstractJmsClient.class);
33
34     protected ConnectionFactory JavaDoc factory;
35     protected Connection JavaDoc jmsConnection;
36     protected Session JavaDoc jmsSession;
37
38     protected int destCount = 1, destIndex = 0;
39     protected String JavaDoc clientName = "";
40
41     public AbstractJmsClient(ConnectionFactory JavaDoc factory) {
42         this.factory = factory;
43     }
44
45     abstract public JmsClientProperties getClient();
46     abstract public void setClient(JmsClientProperties client);
47
48     public ConnectionFactory JavaDoc getFactory() {
49         return factory;
50     }
51
52     public void setFactory(ConnectionFactory JavaDoc factory) {
53         this.factory = factory;
54     }
55
56     public int getDestCount() {
57         return destCount;
58     }
59
60     public void setDestCount(int destCount) {
61         this.destCount = destCount;
62     }
63
64     public int getDestIndex() {
65         return destIndex;
66     }
67
68     public void setDestIndex(int destIndex) {
69         this.destIndex = destIndex;
70     }
71
72     public String JavaDoc getClientName() {
73         return clientName;
74     }
75
76     public void setClientName(String JavaDoc clientName) {
77         this.clientName = clientName;
78     }
79
80     public Connection JavaDoc getConnection() throws JMSException JavaDoc {
81         if (jmsConnection == null) {
82             jmsConnection = factory.createConnection();
83             jmsConnection.setClientID(getClientName());
84             log.info("Creating JMS Connection: Provider=" + getClient().getJmsProvider() + ", JMS Spec=" + getClient().getJmsVersion());
85         }
86         return jmsConnection;
87     }
88
89     public Session JavaDoc getSession() throws JMSException JavaDoc {
90         if (jmsSession == null) {
91             int ackMode;
92             if (getClient().getSessAckMode().equalsIgnoreCase(JmsClientProperties.SESSION_AUTO_ACKNOWLEDGE)) {
93                 ackMode = Session.AUTO_ACKNOWLEDGE;
94             } else if (getClient().getSessAckMode().equalsIgnoreCase(JmsClientProperties.SESSION_CLIENT_ACKNOWLEDGE)) {
95                 ackMode = Session.CLIENT_ACKNOWLEDGE;
96             } else if (getClient().getSessAckMode().equalsIgnoreCase(JmsClientProperties.SESSION_DUPS_OK_ACKNOWLEDGE)) {
97                 ackMode = Session.DUPS_OK_ACKNOWLEDGE;
98             } else if (getClient().getSessAckMode().equalsIgnoreCase(JmsClientProperties.SESSION_TRANSACTED)) {
99                 ackMode = Session.SESSION_TRANSACTED;
100             } else {
101                 ackMode = Session.AUTO_ACKNOWLEDGE;
102             }
103             jmsSession = getConnection().createSession(getClient().isSessTransacted(), ackMode);
104         }
105         return jmsSession;
106     }
107
108     public Destination JavaDoc[] createDestination(int destIndex, int destCount) throws JMSException JavaDoc {
109
110         if (getClient().isDestComposite()) {
111             return new Destination JavaDoc[] {createCompositeDestination(getClient().getDestName(), destIndex, destCount)};
112         } else {
113             Destination JavaDoc[] dest = new Destination JavaDoc[destCount];
114             for (int i=0; i<destCount; i++) {
115                 dest[i] = createDestination(getClient().getDestName() + "." + (destIndex + i));
116             }
117
118             return dest;
119         }
120     }
121
122     public Destination JavaDoc createCompositeDestination(int destIndex, int destCount) throws JMSException JavaDoc {
123         return createCompositeDestination(getClient().getDestName(), destIndex, destCount);
124     }
125
126     protected Destination JavaDoc createCompositeDestination(String JavaDoc name, int destIndex, int destCount) throws JMSException JavaDoc {
127         String JavaDoc compDestName;
128         String JavaDoc simpleName;
129
130         if (name.startsWith("queue://")) {
131             simpleName = name.substring("queue://".length());
132         } else if (name.startsWith("topic://")) {
133             simpleName = name.substring("topic://".length());
134         } else {
135             simpleName = name;
136         }
137
138         int i;
139         compDestName = name + "." + destIndex + ","; // First destination
140
for (i=1; i<destCount-1; i++) {
141             compDestName += (simpleName + "." + (destIndex + i) +",");
142         }
143         compDestName += (simpleName + "." + (destIndex + i)); // Last destination (minus the comma)
144

145         return createDestination(compDestName);
146     }
147
148     protected Destination JavaDoc createDestination(String JavaDoc name) throws JMSException JavaDoc {
149         if (name.startsWith("queue://")) {
150             return getSession().createQueue(name.substring("queue://".length()));
151         } else if (name.startsWith("topic://")) {
152             return getSession().createTopic(name.substring("topic://".length()));
153         } else {
154             return getSession().createTopic(name);
155         }
156     }
157
158 }
159
Popular Tags