summaryrefslogtreecommitdiff
path: root/qpid/tools/src/java/qpid-qmf2-tools/src/main/java/org/apache/qpid/qmf2/tools/QpidCtrl.java
blob: fa06b06519619e893254f8acd494900ab5d6c8ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 *
 * 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.qmf2.tools;

// JMS Imports
import javax.jms.Connection;

// Misc Imports
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// QMF2 Imports
import org.apache.qpid.qmf2.common.ObjectId;
import org.apache.qpid.qmf2.common.QmfData;
import org.apache.qpid.qmf2.common.QmfException;
import org.apache.qpid.qmf2.console.Agent;
import org.apache.qpid.qmf2.console.Console;
import org.apache.qpid.qmf2.console.MethodResult;
import org.apache.qpid.qmf2.console.QmfConsoleData;

import org.apache.qpid.qmf2.util.ConnectionHelper;
import org.apache.qpid.qmf2.util.GetOpt;

// Reuse this class as it provides a handy mechanism to parse an args String into a Map
import org.apache.qpid.messaging.util.AddressParser;

/**
 * A tool to allow QMF2 methods to be invoked from the command line.
 * <pre>
 * Usage: QpidCtrl [options] command [args]
 * The args need to be in a Stringified Map format (similar to an Address String)
 * e.g. to set broker log level: QpidCtrl setLogLevel "{level:\"debug+:Broker\"}"
 * The listValues command lists property names and values of the specified object.
 * The listObjects command lists all objects of the specified package and class.
 * 
 * Options:
 *   -h, --help            show this help message and exit
 *   -v                    enable logging
 *   -a &lt;address&gt;, --broker-address=&lt;address&gt;
 *                         broker-addr is in the form:  [username/password@]
 *                         hostname | ip-address [:&lt;port&gt;]   ex:  localhost,
 *                         10.1.1.7:10000, broker-host:10000,
 *                         guest/guest@localhost
 *   -c &lt;class&gt;, --class=&lt;class&gt;
 *                         class of object on which command is being invoked
 *                         (default broker)
 *   -p &lt;package&gt;, --package=&lt;package&gt;
 *                         package of object on which command is being invoked
 *                         (default org.apache.qpid.broker)
 *   -i &lt;id&gt;, --id=&lt;id&gt;    identifier of object on which command is being invoked
 *                         (default amqp-broker)
 *   --agent=&lt;agent name&gt;
 *                         The name of the Agent to which commands will be sent
 *                         This will try to match &lt;agent name&gt; against the Agent name
 *                         the Agent product name and will also check if the Agent name
 *                         contains the &lt;agent name&gt; String
 *                         (default qpidd)
 *   --sasl-mechanism=&lt;mech&gt;
 *                         SASL mechanism for authentication (e.g. EXTERNAL,
 *                         ANONYMOUS, PLAIN, CRAM-MD5, DIGEST-MD5, GSSAPI). SASL
 *                         automatically picks the most secure available
 *                         mechanism - use this option to override.
 * </pre>
 * Examples (Note the quotes and escaped quotes are significant!):
 * <p>
 * Get the current broker log level:
 * <pre>QpidCtrl getLogLevel</pre>
 *
 * Set the current broker log level to notice+:
 * <pre>QpidCtrl setLogLevel "{level:\"notice+\"}"</pre>
 * 
 * Set the current broker log level to debug+ for all Management Objects:
 * <pre>QpidCtrl setLogLevel "{level:\"debug+\"}"</pre>
 *
 * Set the current broker log level to debug+ for just the Broker Management Object:
 * <pre>QpidCtrl setLogLevel "{level:\"debug+:Broker\"}"</pre>
 *
 * List the properties of the qmf.default.direct exchange:
 * <pre>QpidCtrl -c exchange -i qmf.default.direct listValues</pre>
 *
 * Create a queue called test with a flow-to-disk limit policy:
 * <pre>QpidCtrl create "{type:queue,name:test,properties:{'qpid.policy_type':ring}}"</pre>
 *
 * Delete a queue called test:
 * <pre>QpidCtrl delete "{type:queue,name:test}"</pre>
 *
 * Create a binding called bind1 between the amq.match exchange and the test queue matching the headers name=fadams
 * and gender=male:
 * <pre>QpidCtrl create "{type:binding,name:'amq.match/test/bind1',properties:{x-match:all,name:fadams,gender:male}}"</pre>
 *
 * Delete the binding called bind1 between the amq.match exchange and the test queue:
 * <pre>QpidCtrl delete "{type:binding,name:'amq.match/test/bind1'}"</pre>
 *
 * Get the broker to echo a message:
 * <pre>QpidCtrl echo "{sequence:1234,body:'Peaches En Regalia'}"</pre>
 *
 * Invoke the event method on the gizmo Agent (launch gizmo Agent via AgentTest):
 * <pre>QpidCtrl -p com.profitron.gizmo -c control -i OPERATIONAL --agent=gizmo event</pre>
 *
 * Invoke the create_child method on the gizmo Agent (launch gizmo Agent via AgentTest):
 * <pre>QpidCtrl -p com.profitron.gizmo -c control -i OPERATIONAL --agent=gizmo create_child "{name:monkeyBoy}"</pre>
 *
 * Invoke the stop method on the gizmo Agent (launch gizmo Agent via AgentTest):
 * <pre>QpidCtrl -p com.profitron.gizmo -c control -i OPERATIONAL --agent=gizmo stop "{message:'Will I dream?'}"</pre>
 *
 * @author Fraser Adams
 */
public final class QpidCtrl
{
    private static final String _usage =
    "Usage: QpidCtrl [options] command [args]\n" +
    "The args need to be in a Stringified Map format (similar to an Address String)\n" +
    "e.g. to set broker log level: QpidCtrl setLogLevel \"{level:\\\"debug+:Broker\\\"}\"\n" +
    "The listValues command lists property names and values of the specified object.\n" +
    "The listObjects command lists all objects of the specified package and class.\n";

    private static final String _options =
    "Options:\n" +
    "  -h, --help            show this help message and exit\n" +
    "  -v                    enable logging\n" +
    "  -a <address>, --broker-address=<address>\n" +
    "                        broker-addr is in the form:  [username/password@]\n" +
    "                        hostname | ip-address [:<port>]   ex:  localhost,\n" +
    "                        10.1.1.7:10000, broker-host:10000,\n" +
    "                        guest/guest@localhost\n" +
    "  -c <class>, --class=<class>\n" +
    "                        class of object on which command is being invoked\n" +
    "                        (default broker)\n" +
    "  -p <package>, --package=<package>\n" +
    "                        package of object on which command is being invoked\n" +
    "                        (default org.apache.qpid.broker)\n" +
    "  -i <id>, --id=<id>    identifier of object on which command is being invoked\n" +
    "                        (default amqp-broker)\n" +
    "  --agent=<agent name>\n" +
    "                        The name of the Agent to which commands will be sent\n" +
    "                        This will try to match <agent name> against the Agent name,\n" +
    "                        the Agent product name and will also check if the Agent name\n" +
    "                        contains the <agent name> String\n" +
    "                        (default qpidd)\n" +
    "  --sasl-mechanism=<mech>\n" +
    "                        SASL mechanism for authentication (e.g. EXTERNAL,\n" +
    "                        ANONYMOUS, PLAIN, CRAM-MD5, DIGEST-MD5, GSSAPI). SASL\n" +
    "                        automatically picks the most secure available\n" +
    "                        mechanism - use this option to override.\n";

    private Console _console;

    /**
     * Basic constructor. Creates JMS Session, Initialises Destinations, Producers &amp; Consumers and starts connection.
     * @param url the Connection URL.
     * @param connectionOptions the connection options String to pass to ConnectionHelper.
     * @param pkg the package name of the object we're invoking the method on.
     * @param cls the class name of the object we're invoking the method on.
     * @param id the ObjectId name of the object we're invoking the method on.
     * @param agentName the name of the Agent to invoke the QMF method on.
     * @param command the QMF method we're invoking.
     * @param args the Stringified Map form of the method arguments.
     */
    public QpidCtrl(final String url, final String connectionOptions, final String pkg, final String cls,
                    final String id, final String agentName, final String command, final String args)
    {
        try
        {
            Connection connection = ConnectionHelper.createConnection(url, connectionOptions);        
            _console = new Console();
            _console.addConnection(connection);

            // Find the specified Agent
            Agent agent = _console.findAgent(agentName);
            if (agent == null)
            {
                System.out.println("Agent " + agentName + " not found");
                System.exit(1);
            }

            List<Agent> agentList = Arrays.asList(new Agent[] {agent});
            List<QmfConsoleData> objects = _console.getObjects(pkg, cls, agentList);

            // Parse the args String
            QmfData inArgs = (args == null) ? new QmfData() : new QmfData(new AddressParser(args).map());

            // Find the required QmfConsoleData object and invoke the specified command
            MethodResult results = null;
            for (QmfConsoleData object : objects)
            {
                String objectName = object.getObjectId().getObjectName();
                if (command.equals("listObjects"))
                {
                    System.out.println(objectName);
                }
                else
                {
                    if (objectName.contains(id))
                    { // Use contains as ObjectNames may comprise other identifiers tha make using equals impractical
                        if (command.equals("listValues"))
                        {
                            object.listValues();
                            System.exit(1);
                        }
                        else
                        {
                            results = object.invokeMethod(command, inArgs);
                        }
                        break;
                    }
                }
            }

            if (results == null)
            {
                if (objects.size() == 0)
                {
                    System.out.println("getObjects(" + pkg + ", " + cls + ", " + agentName + ") returned no objects.");
                }
                else
                {
                    System.out.println("Id " + id + " not found in " + pkg + ":" + cls);
                }
            }
            else
            {
                if (results.succeeded())
                {
                    results.listValues();
                }
                else
                {
                    System.err.println ("QmfException " + results.getQmfException().getMessage() +
                                        " returned from " + command + " method");
                }
            }
        }
        catch (QmfException qmfe)
        {
            System.err.println ("QmfException " + qmfe.getMessage() + " caught in QpidCtrl constructor");
        }
    }

    /**
     * Runs QpidCtrl.
     * @param args the command line arguments.
     */
    public static void main(final String[] args)
    {
        String logLevel = System.getProperty("amqj.logging.level");
        logLevel = (logLevel == null) ? "FATAL" : logLevel; // Set default log level to FATAL rather than DEBUG.
        System.setProperty("amqj.logging.level", logLevel);

        // As of Qpid 0.16 the Session Dispatcher Thread is non-Daemon so the JVM gets prevented from exiting.
        // Setting the following property to true makes it a Daemon Thread.
        System.setProperty("qpid.jms.daemon.dispatcher", "true");

        String[] longOpts = {"help", "broker-address=", "class=", "package=", "id=", "agent=", "sasl-mechanism="};
        try
        {
            String host = "localhost";
            String connectionOptions = "{reconnect: true}";
            String cls = "broker";
            String pkg = "org.apache.qpid.broker";
            String id = "amqp-broker";
            String agentName = "qpidd";
            String command = null;
            String arg = null;

            GetOpt getopt = new GetOpt(args, "ha:c:p:i:v", longOpts);
            List<String[]> optList = getopt.getOptList();
            String[] cargs = {};
            cargs = getopt.getEncArgs().toArray(cargs);

            for (String[] opt : optList)
            {
                if (opt[0].equals("-h") || opt[0].equals("--help"))
                {
                    System.out.println(_usage);
                    System.out.println(_options);
                    System.exit(1);
                }
                else if (opt[0].equals("-a") || opt[0].equals("--broker-address"))
                {
                    host = opt[1];
                }
                else if (opt[0].equals("-c") || opt[0].equals("--class"))
                {
                    cls = opt[1];
                }
                else if (opt[0].equals("-p") || opt[0].equals("--package"))
                {
                    pkg = opt[1];
                }
                else if (opt[0].equals("-i") || opt[0].equals("--id"))
                {
                    id = opt[1];
                }
                else if (opt[0].equals("--agent"))
                {
                    agentName = opt[1];
                }
                else if (opt[0].equals("-v"))
                {
                    System.setProperty("amqj.logging.level", "DEBUG");
                }
                else if (opt[0].equals("--sasl-mechanism"))
                {
                    connectionOptions = "{reconnect: true, sasl_mechs: " + opt[1] + "}";
                }
            }

            if (cargs.length < 1 || cargs.length > 2)
            {
                System.out.println(Arrays.asList(cargs));
                System.out.println(_usage);
                System.exit(1);
            }
    
            command = cargs[0];

            if (cargs.length == 2)
            {
                arg = cargs[1];
                if (!arg.startsWith("{") || !arg.endsWith("}"))
                {
                    System.out.println("Incorrect format for args.");
                    System.out.println("This needs to be in a Stringified Map format similar to an Address String");
                    System.exit(1);
                }
            }

            QpidCtrl qpidCtrl = new QpidCtrl(host, connectionOptions, pkg, cls, id, agentName, command, arg);
        }
        catch (IllegalArgumentException e)
        {
            System.out.println(_usage);
            System.out.println(e.getMessage());
            System.exit(1);
        }
    }
}