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