KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > command > AbstractAmqCommand


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.console.command;
19
20 import org.apache.activemq.ActiveMQConnectionFactory;
21 import org.apache.activemq.console.formatter.GlobalWriter;
22
23 import javax.jms.ConnectionFactory JavaDoc;
24 import javax.jms.Connection JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URISyntaxException JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 public abstract class AbstractAmqCommand extends AbstractCommand {
33     private URI JavaDoc brokerUrl;
34     private ConnectionFactory factory;
35     private final List JavaDoc connections = new ArrayList JavaDoc();
36
37     /**
38      * Establishes a connection to the remote broker specified by the broker url.
39      * @return - connection to the broker
40      * @throws JMSException
41      */

42     protected Connection JavaDoc createConnection() throws JMSException JavaDoc {
43         if (getBrokerUrl() == null) {
44             GlobalWriter.printException(new IllegalStateException JavaDoc("You must specify a broker URL to connect to using the --amqurl option."));
45             return null;
46         }
47
48         if (factory == null) {
49             factory = new ActiveMQConnectionFactory(getBrokerUrl());
50         }
51
52         Connection JavaDoc conn = factory.createConnection();
53         connections.add(conn);
54
55         return conn;
56     }
57
58     /**
59      * Establishes a connection to the remote broker specified by the broker url.
60      * @param username - username for the connection
61      * @param password - password for the connection
62      * @return - connection to the broker
63      * @throws JMSException
64      */

65     protected Connection JavaDoc createConnection(String JavaDoc username, String JavaDoc password) throws JMSException JavaDoc {
66         if (getBrokerUrl() == null) {
67             GlobalWriter.printException(new IllegalStateException JavaDoc("You must specify a broker URL to connect to using the --amqurl option."));
68             return null;
69         }
70
71         if (factory == null) {
72             factory = new ActiveMQConnectionFactory(getBrokerUrl());
73         }
74
75         Connection JavaDoc conn = factory.createConnection(username, password);
76         connections.add(conn);
77         conn.start();
78
79         return conn;
80     }
81
82     /**
83      * Close all created connections.
84      */

85     protected void closeAllConnections() {
86         for (Iterator JavaDoc i=connections.iterator(); i.hasNext();) {
87             try {
88                 ((Connection JavaDoc)i.next()).close();
89             } catch (Exception JavaDoc e) { }
90         }
91
92         connections.clear();
93     }
94
95     /**
96      * Handle the --amqurl option.
97      * @param token - current option
98      * @param tokens - succeeding list of arguments
99      * @throws Exception
100      */

101     protected void handleOption(String JavaDoc token, List JavaDoc tokens) throws Exception JavaDoc {
102         // Try to handle the options first
103
if (token.equals("--amqurl")) {
104             // If no broker url specified, or next token is a new option
105
if (tokens.isEmpty() || ((String JavaDoc)tokens.get(0)).startsWith("-")) {
106                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Broker URL not specified."));
107                 tokens.clear();
108                 return;
109             }
110
111             // If broker url already specified
112
if (getBrokerUrl() != null) {
113                 GlobalWriter.printException(new IllegalArgumentException JavaDoc("Multiple broker URL cannot be specified."));
114                 tokens.clear();
115                 return;
116             }
117
118             String JavaDoc strBrokerUrl = (String JavaDoc)tokens.remove(0);
119
120             try {
121                 setBrokerUrl(new URI JavaDoc(strBrokerUrl));
122             } catch (URISyntaxException JavaDoc e) {
123                 GlobalWriter.printException(e);
124                 tokens.clear();
125                 return;
126             }
127         } else {
128             // Let the super class handle the option
129
super.handleOption(token, tokens);
130         }
131     }
132
133     /**
134      * Set the broker url.
135      * @param brokerUrl - new broker url
136      */

137     protected void setBrokerUrl(URI JavaDoc brokerUrl) {
138         this.brokerUrl = brokerUrl;
139     }
140
141     /**
142      * Set the broker url.
143      * @param address - address of the new broker url
144      * @throws URISyntaxException
145      */

146     protected void setBrokerUrl(String JavaDoc address) throws URISyntaxException JavaDoc {
147         this.brokerUrl = new URI JavaDoc(address);
148     }
149
150     /**
151      * Get the current broker url.
152      * @return current broker url
153      */

154     protected URI JavaDoc getBrokerUrl() {
155         return brokerUrl;
156     }
157 }
158
Popular Tags