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.console.command;
018    
019    import java.util.HashSet;
020    import java.util.List;
021    import java.util.Set;
022    
023    import org.apache.activemq.console.util.JmxMBeansUtil;
024    
025    public class ListCommand extends AbstractJmxCommand {
026    
027        protected String[] helpFile = new String[] {
028            "Task Usage: Main list [list-options]",
029            "Description:  Lists all available broker in the specified JMX context.",
030            "",
031            "List Options:",
032            "    --jmxurl <url>             Set the JMX URL to connect to.",
033            "    --pid <pid>                Set the pid to connect to (only on Sun JVM).",            
034            "    --jmxuser <user>           Set the JMX user used for authenticating.",
035            "    --jmxpassword <password>   Set the JMX password used for authenticating.",
036            "    --jmxlocal                 Use the local JMX server instead of a remote one.",
037            "    --version                  Display the version information.",
038            "    -h,-?,--help               Display the stop broker help information.",
039            ""
040        };
041    
042        @Override
043        public String getName() {
044            return "list";
045        }
046    
047        @Override
048        public String getOneLineDescription() {
049            return "Lists all available brokers in the specified JMX context";
050        }
051    
052        /**
053         * List all running brokers registered in the specified JMX context
054         * @param tokens - command arguments
055         * @throws Exception
056         */
057        protected void runTask(List tokens) throws Exception {
058            try {
059                Set<String> propsView = new HashSet<String>();
060                propsView.add("brokerName");
061                context.printMBean(JmxMBeansUtil.filterMBeansView(JmxMBeansUtil.getAllBrokers(createJmxConnection()), propsView));
062            } catch (Exception e) {
063                context.printException(new RuntimeException("Failed to execute list task. Reason: " + e));
064                throw new Exception(e);
065            }
066        }
067        
068        /**
069         * Print the help messages for the browse command
070         */
071        protected void printHelp() {
072            context.printHelp(helpFile);
073        }
074    
075    }