1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activemq.tool;
19
20 import org.codehaus.activemq.ActiveMQConnection;
21 import org.codehaus.activemq.ActiveMQConnectionFactory;
22 import org.codehaus.activemq.util.IndentPrinter;
23
24 import javax.jms.Connection;
25 import javax.jms.Destination;
26 import javax.jms.JMSException;
27 import javax.jms.Session;
28
29 /***
30 * Abstract base class useful for implementation inheritence
31 *
32 * @version $Revision: 1.9 $
33 */
34 public class ToolSupport {
35
36
37 protected Connection connection;
38 protected Session session;
39 protected Destination destination;
40 protected String subject = "TOOL.DEFAULT";
41 protected boolean topic = true;
42 protected String user = ActiveMQConnection.DEFAULT_USER;
43 protected String pwd = ActiveMQConnection.DEFAULT_PASSWORD;
44 protected String url = ActiveMQConnection.DEFAULT_URL;
45 protected boolean transacted = false;
46 protected boolean durable = false;
47 protected String clientID = getClass().getName();
48 protected int ackMode = Session.AUTO_ACKNOWLEDGE;
49 protected String consumerName = "James";
50
51
52 protected void createSession() throws JMSException {
53 connection = createConnection();
54 if (durable) {
55 connection.setClientID(clientID);
56 }
57 session = connection.createSession(transacted, ackMode);
58 if (topic) {
59 destination = session.createTopic(subject);
60 }
61 else {
62 destination = session.createQueue(subject);
63 }
64 }
65
66 protected Connection createConnection() throws JMSException {
67 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, pwd, url);
68 return connectionFactory.createConnection();
69 }
70
71 protected void close() throws JMSException {
72
73 dumpStats();
74
75 if (session != null) {
76 session.close();
77 }
78 if (connection != null) {
79 connection.close();
80 }
81 }
82
83 protected void dumpStats() {
84 ActiveMQConnection c = (ActiveMQConnection) connection;
85 c.getConnectionStats().dump(new IndentPrinter());
86 }
87 }