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.karaf.commands;
018
019import org.apache.activemq.console.CommandContext;
020import org.apache.activemq.console.command.AbstractJmxCommand;
021import org.apache.activemq.console.command.Command;
022import org.apache.activemq.console.formatter.CommandShellOutputFormatter;
023import org.apache.felix.gogo.commands.Argument;
024import org.apache.karaf.shell.console.OsgiCommandSupport;
025
026import java.util.ArrayList;
027
028/**
029 * @version $Rev: 960482 $ $Date: 2010-07-05 10:28:33 +0200 (Mon, 05 Jul 2010) $
030 */
031public class ActiveMQCommandSupport extends OsgiCommandSupport {
032
033    private Command command;
034
035    @Argument(index=0, multiValued=true, required=true)
036    private ArrayList<String> arguments = new ArrayList<String>();
037
038    protected Object doExecute() throws Exception {
039        CommandContext context2 = new CommandContext();
040        context2.setFormatter(new CommandShellOutputFormatter(System.out));
041        Command currentCommand = command.getClass().newInstance();
042
043        try {
044            currentCommand.setCommandContext(context2);
045            // must be added first
046            if (command instanceof AbstractJmxCommand) {
047                arguments.add(0, "--jmxlocal");
048            }
049            currentCommand.execute(arguments);
050            return null;
051        } catch (Throwable e) {
052            Throwable cur = e;
053            while (cur.getCause() != null) {
054                cur = cur.getCause();
055            }
056            if (cur instanceof java.net.ConnectException) {
057                context2
058                    .print("\n"
059                        + "Could not connect to JMX server.  This command requires that the remote JMX server be enabled.\n"
060                        + "This is typically done by adding the following JVM arguments: \n"
061                        + "   -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.authenticate=false \n"
062                        + "   -Dcom.sun.management.jmxremote.ssl=false \n" + "\n"
063                        + "The connection error was: " + cur + "\n");
064            } else {
065                if (e instanceof Exception) {
066                    throw (Exception)e;
067                } else {
068                    throw new RuntimeException(e);
069                }
070
071            }
072        }
073        return null;
074
075    }
076
077    /**
078     * @return the description of the command.
079     */
080    public String description() {
081        return command.getOneLineDescription();
082    }
083
084    public Command getCommand() {
085        return command;
086    }
087
088    public void setCommand(Command command) {
089        this.command = command;
090    }
091
092    public static String[] toStringArray(Object args[]) {
093        String strings[] = new String[args.length];
094        for(int i = 0; i < args.length; i++) {
095            strings[i] = String.valueOf(args[i]);
096        }
097        return strings;
098    }
099}