diff options
| author | Aidan Skinner <aidan@apache.org> | 2009-01-07 11:50:43 +0000 |
|---|---|---|
| committer | Aidan Skinner <aidan@apache.org> | 2009-01-07 11:50:43 +0000 |
| commit | 37ffad2ee4dbed6e95785e17eaf9757a89f4dd21 (patch) | |
| tree | 2bd640b19bae5ccdf120264b9eed7f90f8c1433b /java | |
| parent | b483bb1fc5ad4aa8e6c2449bbd6ec0e7d5f3d92d (diff) | |
| download | qpid-python-37ffad2ee4dbed6e95785e17eaf9757a89f4dd21.tar.gz | |
QPID-1522: Move command line constants to individual command files. Centralise list of commands in CommandLineInterpreter. Make CommandExecutionEngine look up command from registered list rather than use a big if().
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@732310 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java')
24 files changed, 845 insertions, 121 deletions
diff --git a/java/management/common/src/main/java/org/apache/qpid/management/common/JMXConnnectionFactory.java b/java/management/common/src/main/java/org/apache/qpid/management/common/JMXConnnectionFactory.java new file mode 100644 index 0000000000..c9955329d0 --- /dev/null +++ b/java/management/common/src/main/java/org/apache/qpid/management/common/JMXConnnectionFactory.java @@ -0,0 +1,249 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.management.common; + +import java.io.IOException; +import java.security.Security; +import java.util.HashMap; +import java.util.Map; + +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXServiceURL; +import javax.security.auth.callback.CallbackHandler; +import javax.security.sasl.SaslClientFactory; + +import org.apache.qpid.management.common.sasl.CRAMMD5HashedSaslClientFactory; +import org.apache.qpid.management.common.sasl.Constants; +import org.apache.qpid.management.common.sasl.JCAProvider; +import org.apache.qpid.management.common.sasl.SaslProvider; +import org.apache.qpid.management.common.sasl.UserPasswordCallbackHandler; +import org.apache.qpid.management.common.sasl.UsernameHashedPasswordCallbackHandler; + +public class JMXConnnectionFactory { + + public static JMXConnector getJMXConnection(long timeout, String host, int port, String username, String password) throws Exception + { + //auto-negotiate an RMI or JMXMP (SASL/CRAM-MD5 or SASL/PLAIN) JMX connection to broker + try + { + return createJMXconnector("RMI", timeout, host, port, username, password); + } + catch (IOException rmiIOE) + { + // check if the ioe was raised because we tried connecting to a non RMI-JRMP based JMX server + boolean jrmpServer = !rmiIOE.getMessage().contains("non-JRMP server at remote endpoint"); + + if (jrmpServer) + { + throw rmiIOE; + } + else + { + try + { + //It wasnt an RMI ConnectorServer at the broker end. Try to establish a JMXMP SASL/CRAM-MD5 connection instead. + return createJMXconnector("JMXMP_CRAM-MD5", timeout, host, port, username, password); + } + catch (IOException cramIOE) + { + // check if the IOE was raised because we tried connecting to a SASL/PLAIN server using SASL/CRAM-MD5 + boolean plainProfileServer = cramIOE.getMessage().contains("The server supported profiles [SASL/PLAIN]" + + " do not match the client required profiles [SASL/CRAM-MD5]"); + + if (!plainProfileServer) + { + IOException nioe = new IOException(); + nioe.initCause(cramIOE); + throw nioe; + } + else + { + try + { + // Try to establish a JMXMP SASL/PLAIN connection instead. + return createJMXconnector("JMXMP_PLAIN", timeout, host, port, username, password); + } + catch (IOException plainIOE) + { + /* Out of options now. Check that the IOE was raised because we tried connecting to a server + * which didnt support SASL/PLAIN. If so, signal an unknown profile type. If not, raise the exception. */ + boolean unknownProfile = cramIOE.getMessage().contains("do not match the client required profiles [SASL/PLAIN]"); + + if (unknownProfile) + { + throw new Exception("Unknown JMXMP authentication mechanism, unable to connect."); + } + else + { + IOException nioe = new IOException(); + nioe.initCause(plainIOE); + throw nioe; + } + } + } + } + } + } + } + + private static JMXConnector createJMXconnector(String connectionType, long timeout, String host, int port, String userName, String password) throws IOException, Exception + { + Map<String, Object> env = new HashMap<String, Object>(); + String securityMechanism = null; + JMXServiceURL jmxUrl = null; + + if (connectionType == "RMI") + { + securityMechanism = Constants.MECH_PLAIN; + + jmxUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi"); + env = null; + } + else if (connectionType.contains("JMXMP")) + { + // Check that the JMXMPConnector is available to provide SASL support + final String jmxmpcClass = "javax.management.remote.jmxmp.JMXMPConnector"; + + try + { + Class.forName(jmxmpcClass); + } + catch (ClassNotFoundException cnfe) + { + throw new Exception("JMXMPConnector class not found, unable to connect to specified server.\n\n" + + "Please add the jmxremote_optional.jar to the jmxremote.sasl plugin folder, or the classpath."); + } + + jmxUrl = new JMXServiceURL("jmxmp", host, port); + env = new HashMap<String, Object>(); + + /* set the package in which to find the JMXMP ClientProvider.class file loaded by the + * jmxremote.sasl plugin (from the jmxremote_optional.jar) */ + env.put("jmx.remote.protocol.provider.pkgs", "com.sun.jmx.remote.protocol"); + + if (connectionType == "JMXMP_CRAM-MD5") + { + securityMechanism = Constants.MECH_CRAMMD5; + + Map<String, Class<? extends SaslClientFactory>> map = new HashMap<String, Class<? extends SaslClientFactory>>(); + map.put("CRAM-MD5-HASHED", CRAMMD5HashedSaslClientFactory.class); + Security.addProvider(new JCAProvider(map)); + + CallbackHandler handler = new UsernameHashedPasswordCallbackHandler( + userName, password); + env.put("jmx.remote.profiles", Constants.SASL_CRAMMD5); + env.put("jmx.remote.sasl.callback.handler", handler); + } + else if (connectionType == "JMXMP_PLAIN") + { + securityMechanism = Constants.MECH_PLAIN; + + Security.addProvider(new SaslProvider()); + CallbackHandler handler = new UserPasswordCallbackHandler(userName, password); + env.put("jmx.remote.profiles", Constants.SASL_PLAIN); + env.put("jmx.remote.sasl.callback.handler", handler); + } + else + { + throw new Exception("Unknown authentication mechanism"); + } + } + else + { + throw new Exception("Unknown connection type"); + } + + ConnectWaiter connector = new ConnectWaiter(jmxUrl, env); + Thread connectorThread = new Thread(connector); + connectorThread.start(); + connectorThread.join(timeout); + + if (connector.getConnectionException() != null) + { + throw connector.getConnectionException(); + } + return connector.getJmxc(); + } + + public static class ConnectWaiter implements Runnable + { + private boolean _connected; + private Exception _connectionException; + private JMXConnector _jmxc; + private JMXServiceURL _jmxUrl; + private Map<String, ?> _env; + + public ConnectWaiter(JMXServiceURL url, Map<String, ?> env) + { + super(); + _jmxUrl = url; + _env = env; + } + + public void run() + { + try + { + setConnected(false); + setConnectionException(null); + setJmxc(JMXConnectorFactory.connect(_jmxUrl, _env)); + + setConnected(true); + } + catch (Exception ex) + { + setConnectionException(ex); + } + } + + public void setConnected(boolean _connected) + { + this._connected = _connected; + } + + public boolean getConnected() + { + return _connected; + } + + public void setConnectionException(Exception _connectionException) + { + this._connectionException = _connectionException; + } + + public Exception getConnectionException() + { + return _connectionException; + } + + public void setJmxc(JMXConnector _jmxc) + { + this._jmxc = _jmxc; + } + + public JMXConnector getJmxc() + { + return _jmxc; + } + } +} diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/Command.java b/java/management/tools/qpid-cli/src/org/apache/qpid/Command.java index 27f47dfd2f..fcb2a0031d 100644 --- a/java/management/tools/qpid-cli/src/org/apache/qpid/Command.java +++ b/java/management/tools/qpid-cli/src/org/apache/qpid/Command.java @@ -39,6 +39,8 @@ package org.apache.qpid; public interface Command { + public static String COMMAND_NAME = null; + public void execute(); public void printusage(); diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/CommandConstants.java b/java/management/tools/qpid-cli/src/org/apache/qpid/CommandConstants.java deleted file mode 100644 index 9285501693..0000000000 --- a/java/management/tools/qpid-cli/src/org/apache/qpid/CommandConstants.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -/* - * - * Copyright (c) 2006 The Apache Software Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -package org.apache.qpid; - -public interface CommandConstants { - - String LIST_COMMAND = "list"; - String INFO_COMMAND = "info"; - String HELP_COMMAND = "help"; - String DELETE_COMMAND = "delete"; - String MOVE_COMMAND = "move"; - String VIEW_COMMAND = "view"; - String VIEWCONTENT_COMMAND = "viewcontent"; - -} diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/CommandExecutionEngine.java b/java/management/tools/qpid-cli/src/org/apache/qpid/CommandExecutionEngine.java index 50bc294780..7db43032d5 100644 --- a/java/management/tools/qpid-cli/src/org/apache/qpid/CommandExecutionEngine.java +++ b/java/management/tools/qpid-cli/src/org/apache/qpid/CommandExecutionEngine.java @@ -37,11 +37,14 @@ */ package org.apache.qpid; +import java.util.HashMap; +import java.util.Map; + import org.apache.qpid.utils.JMXinfo; -import org.apache.qpid.commands.*; public class CommandExecutionEngine { + private static Map<String, Class<? extends Command>> _commands = new HashMap<String, Class<? extends Command>>(); private Command currentcommand = null; private String commandname = null; private JMXinfo info = null; @@ -51,29 +54,24 @@ public class CommandExecutionEngine { this.commandname = info.getCommandLineOptionParser().getcommandname(); } - public boolean CommandSelector() { - - if (CommandConstants.INFO_COMMAND.equalsIgnoreCase(this.commandname)) - currentcommand = new Commandinfo(info, this.commandname); - else if (CommandConstants.LIST_COMMAND.equalsIgnoreCase(this.commandname)) - currentcommand = new Commandlist(info, this.commandname); - else if (CommandConstants.HELP_COMMAND.equalsIgnoreCase(this.commandname)) - currentcommand = new Commandhelp(info, this.commandname); - else if (CommandConstants.DELETE_COMMAND.equalsIgnoreCase(this.commandname)) - currentcommand = new Commanddelete(info, this.commandname); - else if (CommandConstants.MOVE_COMMAND.equalsIgnoreCase(this.commandname)) - currentcommand = new Commandmove(info, this.commandname); - else if (CommandConstants.VIEW_COMMAND.equalsIgnoreCase(this.commandname)) - currentcommand = new Commandview(info, this.commandname); - else if (CommandConstants.VIEWCONTENT_COMMAND.equalsIgnoreCase(this.commandname)) - currentcommand = new Commandviewcontent(info, this.commandname); - else { + public boolean CommandSelector() throws Exception { + Class<? extends Command> commandClass = _commands.get(this.commandname); + if (commandClass != null) + { + Class<?> parameterTypes = JMXinfo.class; + currentcommand = (Command) commandClass.getConstructor(parameterTypes).newInstance(info); + } + else + { usage(); return false; } return true; - - + } + + public static void addCommand(String name, Class<? extends Command> newCommand) + { + _commands.put(name, newCommand); } public void runcommand() { diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/CommandLineInterpreter.java b/java/management/tools/qpid-cli/src/org/apache/qpid/CommandLineInterpreter.java index cede874186..b7a4187028 100644 --- a/java/management/tools/qpid-cli/src/org/apache/qpid/CommandLineInterpreter.java +++ b/java/management/tools/qpid-cli/src/org/apache/qpid/CommandLineInterpreter.java @@ -38,29 +38,29 @@ package org.apache.qpid; -import org.apache.qpid.utils.CommandLineOptionParser; -import org.apache.qpid.utils.JMXConfiguration; -import org.apache.qpid.utils.JMXinfo; +import java.io.PrintWriter; +import java.util.StringTokenizer; import javax.management.MBeanServerConnection; import javax.management.remote.JMXConnector; -import javax.naming.ServiceUnavailableException; -import java.io.PrintWriter; -import java.io.FileWriter; -import java.io.IOException; -import java.io.FileInputStream; -import java.util.LinkedList; -import java.util.List; -import java.util.StringTokenizer; -import java.util.Properties; -import java.rmi.ConnectException; -import jline.ConsoleReader; import jline.ArgumentCompletor; +import jline.ConsoleReader; import jline.SimpleCompletor; +import org.apache.qpid.commands.Commanddelete; +import org.apache.qpid.commands.Commandhelp; +import org.apache.qpid.commands.Commandinfo; +import org.apache.qpid.commands.Commandlist; +import org.apache.qpid.commands.Commandmove; +import org.apache.qpid.commands.Commandview; +import org.apache.qpid.commands.Commandviewcontent; +import org.apache.qpid.utils.CommandLineOptionParser; +import org.apache.qpid.utils.JMXConfiguration; +import org.apache.qpid.utils.JMXinfo; public class CommandLineInterpreter { + public static void main(String[] args) { Connector conn = null; try { @@ -86,6 +86,8 @@ public class CommandLineInterpreter { } } + registerCommands(); + /* Connecting with the broker */ try { if (commandlineoptionparser == null) @@ -145,6 +147,17 @@ public class CommandLineInterpreter { } } + private static void registerCommands() + { + CommandExecutionEngine.addCommand(Commanddelete.COMMAND_NAME, Commanddelete.class); + CommandExecutionEngine.addCommand(Commandhelp.COMMAND_NAME, Commandhelp.class); + CommandExecutionEngine.addCommand(Commandinfo.COMMAND_NAME, Commandinfo.class); + CommandExecutionEngine.addCommand(Commandlist.COMMAND_NAME, Commandlist.class); + CommandExecutionEngine.addCommand(Commandmove.COMMAND_NAME, Commandmove.class); + CommandExecutionEngine.addCommand(Commandview.COMMAND_NAME, Commandview.class); + CommandExecutionEngine.addCommand(Commandviewcontent.COMMAND_NAME, Commandviewcontent.class); + } + private static void Usage() { System.out.println("Connecting to localhost Qpid java broker..."); } @@ -161,7 +174,7 @@ public class CommandLineInterpreter { System.out.println("Please check the host name and the port given"); } - public static String[] oneshotmode(String[] args,CommandLineOptionParser commandlineoptionparser,JMXConnector jmxc,MBeanServerConnection mbsc) + public static String[] oneshotmode(String[] args,CommandLineOptionParser commandlineoptionparser,JMXConnector jmxc,MBeanServerConnection mbsc) throws Exception { int check = 0; String [] temp; diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/CommandImpl.java b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/CommandImpl.java index 28eb7d8ec6..ce5062f7e7 100644 --- a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/CommandImpl.java +++ b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/CommandImpl.java @@ -40,6 +40,7 @@ package org.apache.qpid.commands; import org.apache.qpid.Command; +import org.apache.qpid.CommandExecutionEngine; import org.apache.qpid.utils.JMXinfo; import org.apache.qpid.utils.CommandLineOption; @@ -48,7 +49,6 @@ import java.util.Map; public abstract class CommandImpl implements Command { protected JMXinfo info = null; - private String commandname = null; private String name; private String virtualhost = null; @@ -56,16 +56,16 @@ public abstract class CommandImpl implements Command { private String outputformat = null; private String seperator = ","; - - public CommandImpl(JMXinfo info, String name) { + + public CommandImpl(JMXinfo info) { this.info = info; - this.commandname = name; } - public CommandImpl() { + public CommandImpl() + { } - + protected void setName(String name) { this.name = name; } @@ -148,11 +148,12 @@ public abstract class CommandImpl implements Command { public void unrecognizeoption() { echo("list: Unrecognized option"); - echo("Try `" + this.commandname + " --help` for more information"); + echo("Try `" + COMMAND_NAME + " --help` for more information"); } public abstract void execute(); public abstract void printusage(); + } diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commanddelete.java b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commanddelete.java index 5e41bc7c0e..d8a8724b4d 100644 --- a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commanddelete.java +++ b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commanddelete.java @@ -65,9 +65,10 @@ public class Commanddelete extends CommandImpl { private MBeanServerConnection mbsc; private String method1, method2; private ObjectName queue; + public static final String COMMAND_NAME = "delete"; - public Commanddelete(JMXinfo info, String name) { - super(info, name); + public Commanddelete(JMXinfo info) { + super(info); this.mbsc = info.getmbserverconnector(); this.objname = new QueueObject(mbsc); this.method1 = "deleteMessageFromTop"; @@ -189,5 +190,4 @@ public class Commanddelete extends CommandImpl { while (st.hasMoreElements()) t += st.nextElement(); return t; } - } diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandget.java b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandget.java new file mode 100644 index 0000000000..50b0da69eb --- /dev/null +++ b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandget.java @@ -0,0 +1,232 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +/* + * + * Copyright (c) 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.qpid.commands; + +import org.apache.qpid.commands.objects.ObjectNames; +import org.apache.qpid.commands.objects.QueueObject; +import org.apache.qpid.utils.JMXinfo; + +import javax.management.MBeanServerConnection; +import javax.management.ObjectName; +import java.util.Set; + +public class Commandget extends CommandImpl +{ + + private String _attributeName; + private String _value; + public static String COMMAND_NAME = "get"; + + public Commandget(JMXinfo info) + { + super(info); + } + + private void getAttribute(String option_value) + { + if (option_value == null) + { + printusage(); + return; + } + MBeanServerConnection mbsc = info.getmbserverconnector(); + Set set = null; + ObjectNames objname = null; + + try + { + if (option_value.compareToIgnoreCase("queue") == 0 || option_value.compareToIgnoreCase("queues") == 0) + { + objname = new QueueObject(mbsc); + } + else + { + printusage(); + echo("Wrong objectName"); + return; + } + + if (_attributeName == null) + { + echo("attribute name not specified. See --help for details"); + return; + } + + objname.setQueryString(this.getObject(), this.getName(), this.getVirtualhost()); + objname.returnObjects(); + + if (objname.getSet().size() != 1) + { + echo("You quering return more than one queue to set was this intended?\n" + objname.getQueryString()); + } + else if (objname.getSet().size() == 1) + { + ObjectName object = (ObjectName) objname.getSet().iterator().next(); + + Object value= objname.getAttribute(object, _attributeName); + + echo(value.toString()); + } + else + { + if (hasName()) + { + + echo("You might quering wrong " + this.getObject() + " name with --name or -n option "); + echo(""); + echo(this.getObject() + "Type Objects might not in the broker currently"); + echo(""); + } + else + { + printusage(); + } + } + + } + catch (Exception ex) + { + ex.printStackTrace(); + } + + } + + public void execute() + { + /* In here you it's easy to handle any number of otpions which are going to add with the list command which works + with main option object or o + */ + if (checkoptionsetting("output")) + { + setOutputFormat(optionchecker("output")); + if (checkoptionsetting("separator")) + { + setSeperator(optionchecker("separator")); + } + } + if (checkoptionsetting("object") || checkoptionsetting("o")) + { + String object = optionchecker("object"); + if (object == null) + { + object = optionchecker("o"); + } + setObject(object); + + if (checkoptionsetting("name") || checkoptionsetting("n")) + { + String name = optionchecker("name"); + if (name == null) + { + name = optionchecker("n"); + } + + setName(name); + } + + if (checkoptionsetting("attribute") || checkoptionsetting("a")) + { + String name = optionchecker("attribute"); + if (name == null) + { + name = optionchecker("a"); + } + + setAttributeName(name); + } + if (checkoptionsetting("virtualhost") || checkoptionsetting("v")) + { + String vhost = optionchecker("virtualhost"); + if (vhost == null) + { + vhost = optionchecker("v"); + } + setVirtualhost(vhost); + } + getAttribute(this.getObject()); + } + else if (checkoptionsetting("h") || checkoptionsetting("help")) + { + printusage(); + } + else + { + unrecognizeoption(); + } + } + + private void setAttributeName(String name) + { + this._attributeName = name; + } + + public void printusage() + { + echo(""); + echo("Usage:set [OPTION] ... [OBJECT TYPE]...\n"); + echo("List the information about the given object\n"); + echo("Where possible options include:\n"); + echo(" -o --object type of objects which you want to list\n"); + echo(" ex: < list -o queue > : lists all the queues created in the java broker\n"); + echo(" For now list command is supporting following object typse \n"); + echo(" Queue Connection VirtualHost UserMangement Exchange"); + echo(" Or You can specify object type by giving it at the beginning"); + echo(" rather giving it as a argument"); + echo(" Ex:< queue list > this command is equal to list -o queue \n"); + echo(" -v --virtualhost After specifying the object type you can filter output with this option"); + echo(" list objects with the given virtualhost which will help to find "); + echo(" identical queue objects with -n option"); + echo(" ex: queue list -v develop ment"); + echo(" -n --name After specifying what type of objects you want to monitor you can filter"); + echo(" the output using -n option by specifying the name of the object you want "); + echo(" to monitor exactly"); + echo(" ex: <list -o queue -n ping> : list all the queue objects having queue name"); + echo(" of ping"); + echo(" ex: <queue list -n ping -v development> list all the queue objects with name "); + echo(" of ping and virtualhost of developement \n"); + echo(" -a --attribute "); + echo(" -h --help Display the help and back to the qpid-cli prompt\n"); + + } +} + + + + + diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandhelp.java b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandhelp.java index cb053a063d..1a95735305 100644 --- a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandhelp.java +++ b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandhelp.java @@ -48,10 +48,13 @@ import org.apache.qpid.utils.JMXinfo; * To change this template use File | Settings | File Templates. */ public class Commandhelp extends CommandImpl { - public Commandhelp(JMXinfo info, String name) { - super(info, name); - } + public static final String COMMAND_NAME = "help"; + + public Commandhelp(JMXinfo info) + { + } + public void execute() { printusage(); } @@ -70,4 +73,5 @@ public class Commandhelp extends CommandImpl { echo("[quit] This command is disconnect the connection with the Qpid Java broker and go back to normal propmt"); echo(""); } + } diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandinfo.java b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandinfo.java index ba192d6549..137304ba05 100644 --- a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandinfo.java +++ b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandinfo.java @@ -49,10 +49,11 @@ import java.util.Set; public class Commandinfo extends CommandImpl { - public Commandinfo(JMXinfo info, String name) { - - super(info, name); - + public static final String COMMAND_NAME = "info"; + + public Commandinfo(JMXinfo info) + { + super(info); } private void listobjects(String option_value) { diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandlist.java b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandlist.java index cca6709c01..fb67b1cf2a 100644 --- a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandlist.java +++ b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandlist.java @@ -55,9 +55,11 @@ import java.util.Map; public class Commandlist extends CommandImpl { - public Commandlist(JMXinfo info, String name) { - super(info, name); + public static final String COMMAND_NAME = "list"; + public Commandlist(JMXinfo info) + { + super(info); } private void listobjects(String option_value) { diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandmove.java b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandmove.java index 8dc5bc6087..cc98b7cac4 100644 --- a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandmove.java +++ b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandmove.java @@ -63,21 +63,26 @@ import java.io.BufferedReader; * To change this template use File | Settings | File Templates. */ public class Commandmove extends CommandImpl { + + + public static final String COMMAND_NAME = "move"; + private String name1 = null, name2 = null, vhost1 = null, vhost2 = null, method1 = null, method2 = null; //target and starting queue specifications happen with these options private QueueObject queue1; private MBeanServerConnection mbsc; private ObjectName queue; private int fmid = 0, tmid = 0; - public Commandmove(JMXinfo info, String name) { - super(info, name); + public Commandmove(JMXinfo info) { + super(info); + this.mbsc = info.getmbserverconnector(); this.queue1 = new QueueObject(mbsc); this.method1 = "moveMessages"; this.method2 = "getMessagesOnTheQueue"; } - + public void movemessages() { Set set = null; queue1.setQueryString(this.getObject(), this.name1, this.vhost1); diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandset.java b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandset.java new file mode 100644 index 0000000000..eecaf2adbe --- /dev/null +++ b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandset.java @@ -0,0 +1,261 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.commands; + +import org.apache.qpid.commands.objects.ObjectNames; +import org.apache.qpid.commands.objects.QueueObject; +import org.apache.qpid.utils.JMXinfo; + +import javax.management.Attribute; +import javax.management.MBeanServerConnection; +import javax.management.ObjectName; +import java.util.Set; + +public class Commandset extends CommandImpl +{ + private String _attributeName; + private String _value; + public static String COMMAND_NAME = "set"; + + public Commandset(JMXinfo info) + { + super(info); + } + + private void setAttribute(String option_value) + { + /*print usage if use is not give the correct option letter or no options */ + if (option_value == null) + { + printusage(); + return; + } + MBeanServerConnection mbsc = info.getmbserverconnector(); + Set set = null; + ObjectNames objname = null; + + try + { + if (option_value.compareToIgnoreCase("queue") == 0 || option_value.compareToIgnoreCase("queues") == 0) + { + objname = new QueueObject(mbsc); + } + else + { + printusage(); + echo("Wrong objectName"); + return; + } + + if (_attributeName == null) + { + echo("attribute name not specified. See --help for details"); + return; + } + + if (_value == null) + { + echo("new value not specified. See --help for details"); + return; + } + + objname.setQueryString(this.getObject(), this.getName(), this.getVirtualhost()); + objname.returnObjects(); + + if (objname.getSet().size() != 1) + { + echo("You quering return more than one queue to set was this intended?\n" + objname.getQueryString()); + } + else if (objname.getSet().size() == 1) + { + ObjectName object = (ObjectName) objname.getSet().iterator().next(); + + Object value = objname.getAttribute(object, _attributeName); + + Object attributeValue = _value; + + try + { + if (value instanceof Integer) + { + attributeValue = Integer.valueOf(_value); + } + else if (value instanceof Long) + { + attributeValue = Long.valueOf(_value); + } + } + catch (NumberFormatException nfe) + { + echo("Value(" + _attributeName + ") should be of type " + value.getClass().getName()); + return; + } + + Attribute attribute = new Attribute(_attributeName, attributeValue); + + objname.setAttribute(object, attribute); + } + else + { + if (hasName()) + { + + echo("You might quering wrong " + this.getObject() + " name with --name or -n option "); + echo(""); + echo(this.getObject() + "Type Objects might not in the broker currently"); + echo(""); + } + else + { + printusage(); + } + } + + } + catch (Exception ex) + { + ex.printStackTrace(); + } + + } + + public void execute() + { + /* In here you it's easy to handle any number of otpions which are going to add with the list command which works + with main option object or o + */ + if (checkoptionsetting("output")) + { + setOutputFormat(optionchecker("output")); + if (checkoptionsetting("separator")) + { + setSeperator(optionchecker("separator")); + } + } + if (checkoptionsetting("object") || checkoptionsetting("o")) + { + String object = optionchecker("object"); + if (object == null) + { + object = optionchecker("o"); + } + setObject(object); + + if (checkoptionsetting("name") || checkoptionsetting("n")) + { + String name = optionchecker("name"); + if (name == null) + { + name = optionchecker("n"); + } + + setName(name); + } + + if (checkoptionsetting("attribute") || checkoptionsetting("a")) + { + String name = optionchecker("attribute"); + if (name == null) + { + name = optionchecker("a"); + } + + setAttributeName(name); + } + + if (checkoptionsetting("set") || checkoptionsetting("s")) + { + String value = optionchecker("set"); + if (value == null) + { + value = optionchecker("s"); + } + + setAttributeValue(value); + } + + if (checkoptionsetting("virtualhost") || checkoptionsetting("v")) + { + String vhost = optionchecker("virtualhost"); + if (vhost == null) + { + vhost = optionchecker("v"); + } + setVirtualhost(vhost); + } + setAttribute(this.getObject()); + } + else if (checkoptionsetting("h") || checkoptionsetting("help")) + { + printusage(); + } + else + { + unrecognizeoption(); + } + } + + private void setAttributeValue(String value) + { + _value = value; + } + + private void setAttributeName(String name) + { + this._attributeName = name; + } + + public void printusage() + { + echo(""); + echo("Usage:set [OPTION] ... [OBJECT TYPE]...\n"); + echo("List the information about the given object\n"); + echo("Where possible options include:\n"); + echo(" -o --object type of objects which you want to list\n"); + echo(" ex: < list -o queue > : lists all the queues created in the java broker\n"); + echo(" For now list command is supporting following object typse \n"); + echo(" Queue Connection VirtualHost UserMangement Exchange"); + echo(" Or You can specify object type by giving it at the beginning"); + echo(" rather giving it as a argument"); + echo(" Ex:< queue list > this command is equal to list -o queue \n"); + echo(" -v --virtualhost After specifying the object type you can filter output with this option"); + echo(" list objects with the given virtualhost which will help to find "); + echo(" identical queue objects with -n option"); + echo(" ex: queue list -v develop ment"); + echo(" -n --name After specifying what type of objects you want to monitor you can filter"); + echo(" the output using -n option by specifying the name of the object you want "); + echo(" to monitor exactly"); + echo(" ex: <list -o queue -n ping> : list all the queue objects having queue name"); + echo(" of ping"); + echo(" ex: <queue list -n ping -v development> list all the queue objects with name "); + echo(" of ping and virtualhost of developement \n"); + echo(" -a --attribute "); + echo(" -h --help Display the help and back to the qpid-cli prompt\n"); + + } + +} + + + + + diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandview.java b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandview.java index 5c8f692ab9..94f85fb40d 100644 --- a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandview.java +++ b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandview.java @@ -60,14 +60,17 @@ import java.awt.font.OpenType; * To change this template use File | Settings | File Templates. */ public class Commandview extends CommandImpl { + + public static final String COMMAND_NAME = "view"; + private int number = 0; private QueueObject objname; private MBeanServerConnection mbsc; private String method1; private ObjectName queue; - public Commandview(JMXinfo info, String name) { - super(info, name); + public Commandview(JMXinfo info) { + super(info); this.mbsc = info.getmbserverconnector(); this.objname = new QueueObject(mbsc); this.method1 = "viewMessages"; diff --git a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandviewcontent.java b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandviewcontent.java index 8a71726c33..2067a5b35a 100644 --- a/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandviewcontent.java +++ b/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandviewcontent.java @@ -56,6 +56,9 @@ import java.nio.charset.Charset; * To change this template use File | Settings | File Templates. */ public class Commandviewcontent extends CommandImpl { + + public static final String COMMAND_NAME = "viewcontent"; + private String object; private String name; private String vhost; @@ -65,14 +68,14 @@ public class Commandviewcontent extends CommandImpl { private String method1; private ObjectName queue; - public Commandviewcontent(JMXinfo info, String name) { - super(info, name); + public Commandviewcontent(JMXinfo info) { + super(info); this.mbsc = info.getmbserverconnector(); this.objname = new QueueObject(mbsc); this.method1 = "viewMessageContent"; } - + public void viewcontent() { Set set = null; Object temp[] = {null}; diff --git a/java/management/tools/qpid-cli/test/org/apache/qpid/TestCommandExecutionEngine.java b/java/management/tools/qpid-cli/test/org/apache/qpid/TestCommandExecutionEngine.java index 923f34678a..278c56a417 100644 --- a/java/management/tools/qpid-cli/test/org/apache/qpid/TestCommandExecutionEngine.java +++ b/java/management/tools/qpid-cli/test/org/apache/qpid/TestCommandExecutionEngine.java @@ -69,7 +69,7 @@ public class TestCommandExecutionEngine { } @Test - public void TestCommandSelector() + public void TestCommandSelector() throws Exception { line = "list -o queue"; command = line.split(" "); diff --git a/java/management/tools/qpid-cli/test/org/apache/qpid/TestCommandLineInterpreter.java b/java/management/tools/qpid-cli/test/org/apache/qpid/TestCommandLineInterpreter.java index 36a2d29dfd..7d61e1c73c 100644 --- a/java/management/tools/qpid-cli/test/org/apache/qpid/TestCommandLineInterpreter.java +++ b/java/management/tools/qpid-cli/test/org/apache/qpid/TestCommandLineInterpreter.java @@ -80,7 +80,7 @@ public class TestCommandLineInterpreter { } @Test - public void TestSetQueryString() + public void TestSetQueryString() throws Exception { CommandLineInterpreter.oneshotmode(args,parser,jmxc,mbsc); Assert.assertEquals(args[0],"info"); diff --git a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommand.java b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommand.java index 833ad0478e..1504893355 100644 --- a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommand.java +++ b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommand.java @@ -75,7 +75,7 @@ public class TestCommand{ parser = new CommandLineOptionParser(list); test = ConnectorFactory.getConnector(ConnectionConstants.BROKER_HOSTNAME,ConnectionConstants.BROKER_PORT); info = new JMXinfo(test.getConnector(),parser,test.getMBeanServerConnection()); - cmd = new Commandinfo(info,"list"); + cmd = new Commandinfo(info); } @Test diff --git a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommanddelete.java b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommanddelete.java index 971c088fe1..224874910a 100644 --- a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommanddelete.java +++ b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommanddelete.java @@ -70,7 +70,7 @@ public class TestCommanddelete { MBeanServerConnection mbsc = conn.getMBeanServerConnection(); CommandLineOptionParser parser = new CommandLineOptionParser(command.split(" ")); info = new JMXinfo(jmxc,parser,mbsc); - delete = new Commanddelete(info,"delete"); + delete = new Commanddelete(info); diff --git a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandinfo.java b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandinfo.java index 5f0e8959a5..2d332fb13d 100644 --- a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandinfo.java +++ b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandinfo.java @@ -70,7 +70,7 @@ public class TestCommandinfo { MBeanServerConnection mbsc = conn.getMBeanServerConnection(); CommandLineOptionParser parser = new CommandLineOptionParser(command.split(" ")); info = new JMXinfo(jmxc,parser,mbsc); - infocommand = new Commandinfo(info,"list"); + infocommand = new Commandinfo(info); } @Test diff --git a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandlist.java b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandlist.java index 0af6f236f6..fd1063d24f 100644 --- a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandlist.java +++ b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandlist.java @@ -74,7 +74,7 @@ public class TestCommandlist { MBeanServerConnection mbsc = conn.getMBeanServerConnection(); CommandLineOptionParser parser = new CommandLineOptionParser(command.split(" ")); info = new JMXinfo(jmxc,parser,mbsc); - list = new Commandlist(info,"list"); + list = new Commandlist(info); } @Test diff --git a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandmove.java b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandmove.java index 3a5d55c8dd..a91a5b3c78 100644 --- a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandmove.java +++ b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandmove.java @@ -70,7 +70,7 @@ public class TestCommandmove { MBeanServerConnection mbsc = conn.getMBeanServerConnection(); CommandLineOptionParser parser = new CommandLineOptionParser(command.split(" ")); info = new JMXinfo(jmxc,parser,mbsc); - move = new Commandmove(info,"move"); + move = new Commandmove(info); diff --git a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandview.java b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandview.java index 6867cf0d01..f6c86734ec 100644 --- a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandview.java +++ b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandview.java @@ -70,7 +70,7 @@ public class TestCommandview { MBeanServerConnection mbsc = conn.getMBeanServerConnection(); CommandLineOptionParser parser = new CommandLineOptionParser(command.split(" ")); info = new JMXinfo(jmxc,parser,mbsc); - view = new Commandview(info,"view"); + view = new Commandview(info); } @Test public void TestSetQueryString() diff --git a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandviewcontent.java b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandviewcontent.java index 5ea6273561..aa8e6fffdc 100644 --- a/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandviewcontent.java +++ b/java/management/tools/qpid-cli/test/org/apache/qpid/commands/TestCommandviewcontent.java @@ -70,7 +70,7 @@ public class TestCommandviewcontent { MBeanServerConnection mbsc = conn.getMBeanServerConnection(); CommandLineOptionParser parser = new CommandLineOptionParser(command.split(" ")); info = new JMXinfo(jmxc,parser,mbsc); - viewcontent = new Commandviewcontent(info,"viewcontent"); + viewcontent = new Commandviewcontent(info); |
