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 */
017package org.apache.activemq.console.command;
018
019import java.net.URI;
020import java.net.URISyntaxException;
021import java.util.ArrayList;
022import java.util.Iterator;
023import java.util.List;
024
025import javax.jms.Connection;
026import javax.jms.ConnectionFactory;
027import javax.jms.JMSException;
028
029import org.apache.activemq.ActiveMQConnectionFactory;
030
031public abstract class AbstractAmqCommand extends AbstractCommand {
032    private URI brokerUrl;
033    private ConnectionFactory factory;
034    private final List<Connection> connections = new ArrayList<Connection>();
035
036    /**
037     * Establishes a connection to the remote broker specified by the broker
038     * url.
039     * 
040     * @return - connection to the broker
041     * @throws JMSException
042     */
043    protected Connection createConnection() throws JMSException {
044        if (getBrokerUrl() == null) {
045            context
046                .printException(new IllegalStateException("You must specify a broker "
047                                                          + "URL to connect to using the --amqurl option."));
048            return null;
049        }
050
051        if (factory == null) {
052            factory = new ActiveMQConnectionFactory(getBrokerUrl());
053        }
054
055        Connection conn = factory.createConnection();
056        connections.add(conn);
057
058        return conn;
059    }
060
061    /**
062     * Establishes a connection to the remote broker specified by the broker
063     * url.
064     * 
065     * @param username - username for the connection
066     * @param password - password for the connection
067     * @return - connection to the broker
068     * @throws JMSException
069     */
070    protected Connection createConnection(String username, String password) throws JMSException {
071        if (getBrokerUrl() == null) {
072            context
073                .printException(new IllegalStateException(
074                                                          "You must specify a broker URL to connect to using the --amqurl option."));
075            return null;
076        }
077
078        if (factory == null) {
079            factory = new ActiveMQConnectionFactory(getBrokerUrl());
080        }
081
082        Connection conn = factory.createConnection(username, password);
083        connections.add(conn);
084        conn.start();
085
086        return conn;
087    }
088
089    /**
090     * Close all created connections.
091     */
092    protected void closeAllConnections() {
093        for (Iterator<Connection> i = connections.iterator(); i.hasNext();) {
094            try {
095                i.next().close();
096            } catch (Exception e) {
097            }
098        }
099
100        connections.clear();
101    }
102
103    /**
104     * Handle the --amqurl option.
105     * 
106     * @param token - current option
107     * @param tokens - succeeding list of arguments
108     * @throws Exception
109     */
110    protected void handleOption(String token, List tokens) throws Exception {
111        // Try to handle the options first
112        if (token.equals("--amqurl")) {
113            // If no broker url specified, or next token is a new option
114            if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
115                context.printException(new IllegalArgumentException("Broker URL not specified."));
116                tokens.clear();
117                return;
118            }
119
120            // If broker url already specified
121            if (getBrokerUrl() != null) {
122                context
123                    .printException(new IllegalArgumentException("Multiple broker URL cannot be specified."));
124                tokens.clear();
125                return;
126            }
127
128            String strBrokerUrl = (String)tokens.remove(0);
129
130            try {
131                setBrokerUrl(new URI(strBrokerUrl));
132            } catch (URISyntaxException e) {
133                context.printException(e);
134                tokens.clear();
135                return;
136            }
137        } else {
138            // Let the super class handle the option
139            super.handleOption(token, tokens);
140        }
141    }
142
143    /**
144     * Set the broker url.
145     * 
146     * @param brokerUrl - new broker url
147     */
148    protected void setBrokerUrl(URI brokerUrl) {
149        this.brokerUrl = brokerUrl;
150    }
151
152    /**
153     * Set the broker url.
154     * 
155     * @param address - address of the new broker url
156     * @throws URISyntaxException
157     */
158    protected void setBrokerUrl(String address) throws URISyntaxException {
159        this.brokerUrl = new URI(address);
160    }
161
162    /**
163     * Get the current broker url.
164     * 
165     * @return current broker url
166     */
167    protected URI getBrokerUrl() {
168        return brokerUrl;
169    }
170}