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