summaryrefslogtreecommitdiff
path: root/java/management/tools/qpid-cli/src/org/apache/qpid/CommandLineInterpreter.java
blob: d8e76172bca130760ebef5affb8788afef5c9f28 (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
/*
 *
 * 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;

import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;

import jline.ArgumentCompletor;
import jline.ConsoleReader;
import jline.SimpleCompletor;

import org.apache.qpid.commands.Commanddelete;
import org.apache.qpid.commands.Commandget;
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.Commandset;
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
{
    private static final String OBJECT_VIRTUALHOST = "virtualhost";
    private static final String OBJECT_USERMANAGEMENT = "usermanagement";
    private static final String OBJECT_CONNECTION = "connection";
    private static final String OBJECT_EXCHANGE = "exchange";
    private static final String OBJECT_QUEUE = "queue";
    private static final String COMMAND_QUIT = "quit";
    private static final String COMMAND_EXIT = "exit";

    public static void main(String[] args)
    {
        Connector conn = null;
        try
        {
            // Create an RMI connector client and
            // connect it to the RMI connector server

            /*
             * checking the commandline options and parse them in to config
             * method
             */
            JMXConnector jmxc = null;
            MBeanServerConnection mbsc = null;
            ConsoleReader reader = new ConsoleReader();
            reader.setBellEnabled(false);
            CommandLineOptionParser commandlineoptionparser = null;

            if ((args == null) || (args.length) == 0)
            {
                Usage();
            }
            /*
             * here special constructor is calling, when parsing options,in here
             * first option value is starting from minus sign so this is handle
             * by a special constructor
             */
            else
            {
                if (args[0].startsWith("-"))
                {
                    commandlineoptionparser = new CommandLineOptionParser(args, args[0]); // if
                                                                                          // user
                                                                                          // specify
                                                                                          // any
                                                                                          // argument
                                                                                          // with
                                                                                          // the
                                                                                          // qpid-cli
                                                                                          // script
                }
            }

            registerCommands();

            /* Connecting with the broker */
            try
            {
                if (commandlineoptionparser == null)
                    commandlineoptionparser = new CommandLineOptionParser(args);

                JMXConfiguration config = new JMXConfiguration(commandlineoptionparser.getAlloptions());
                conn = ConnectorFactory.getConnector(config.gethostname(), config.getport(), config.getUsername(),
                        config.getPassword());
                jmxc = conn.getConnector();
                mbsc = conn.getMBeanServerConnection();
                if (config.checkoptionsetting("r", commandlineoptionparser.getAlloptions()))
                {
                    JMXinfo info = new JMXinfo(jmxc, commandlineoptionparser, mbsc);
                    ReportGenerator reportgen = new ReportGenerator(config.optionchecker("r", commandlineoptionparser
                            .getAlloptions()), info);
                    reportgen.loadproperties();
                    reportgen.run();
                }
                /*
                 * This implementation is for the people who are using the
                 * interactive mode for one shot this run the user given command
                 * and exit
                 */
                for (int i = 0; i < args.length; i++)
                {
                    if (CommandExecutionEngine.getCommands().keySet().contains(args[i]))
                    {
                        oneshotmode(args, commandlineoptionparser, jmxc, mbsc);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                connectionrefuse(ex);
                return;
            }
            /* In this point connection has been established */
            String line;
            String[] command;

            /* prividing GNU readline features using Jline library */
            PrintWriter out = new PrintWriter(System.out);
            SimpleCompletor completer = new SimpleCompletor(new String[] { 
                    COMMAND_EXIT, COMMAND_QUIT, OBJECT_QUEUE, OBJECT_EXCHANGE, OBJECT_CONNECTION,
                    OBJECT_USERMANAGEMENT, OBJECT_VIRTUALHOST});
            for (String commandName : CommandExecutionEngine.getCommands().keySet())
            {
                completer.addCandidateString(commandName);
            }
            reader.addCompletor(new ArgumentCompletor(completer));
            while ((line = reader.readLine("qpid-admin-$ ")) != null)
            {
                out.flush();
                if (removeSpaces(line).equalsIgnoreCase(COMMAND_QUIT) || removeSpaces(line).equalsIgnoreCase(COMMAND_EXIT))
                    break;
                else if (line.length() == 0)
                    continue;
                else
                {
                    command = line.split("\\s+");
                    commandlineoptionparser = new CommandLineOptionParser(command);
                    JMXinfo info = new JMXinfo(jmxc, commandlineoptionparser, mbsc);
                    CommandExecutionEngine engine = new CommandExecutionEngine(info);
                    if (engine.CommandSelector())
                        engine.runcommand();
                }
            }

            conn.getConnector().close();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    private static void registerCommands()
    {
        CommandExecutionEngine.addCommand(Commanddelete.COMMAND_NAME, Commanddelete.class);
        CommandExecutionEngine.addCommand(Commandget.COMMAND_NAME, Commandget.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(Commandset.COMMAND_NAME, Commandset.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...");
    }

    private static String removeSpaces(String s)
    {
        StringTokenizer st = new StringTokenizer(s, " ", false);
        String t = "";
        while (st.hasMoreElements())
            t += st.nextElement();
        return t;
    }

    private static void connectionrefuse(Exception e)
    {
        String message = e.getLocalizedMessage();
        if (e instanceof SecurityException)
        {
            message = " authentication failed, please check username and password";
        }
        else
        {
            Throwable cause = e.getCause();
            while (cause != null)
            {
                message = cause.getMessage();
                cause = cause.getCause();
            }
        }

        System.out.println("Cannot connect with the broker: " + message);
    }

    public static String[] oneshotmode(String[] args, CommandLineOptionParser commandlineoptionparser,
            JMXConnector jmxc, MBeanServerConnection mbsc) throws Exception
    {
        int check = 0;
        String[] temp;
        for (int i = 0; i < args.length; i++)
        {

            if (args[i].compareTo("-h") == 0)
                check++;
            else if (args[i].compareTo("-p") == 0)
                check++;
        }
        for (int i = 0; i < (args.length - 2 * check); i++)
        { // mulitply by 2 because have to remove the option letter with the
          // option value//
            args[i] = args[i + check * 2];
        }

        commandlineoptionparser = new CommandLineOptionParser(args); // change
                                                                     // the args
                                                                     // string
                                                                     // array
                                                                     // which
                                                                     // works as
                                                                     // interactive
                                                                     // mode//
        JMXinfo info = new JMXinfo(jmxc, commandlineoptionparser, mbsc);
        CommandExecutionEngine engine = new CommandExecutionEngine(info);
        if (engine.CommandSelector())
            engine.runcommand();
        return args;

    }
}