summaryrefslogtreecommitdiff
path: root/qpid/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commanddelete.java
blob: cdbb8f1d4f94ea277dacefd3a9a55ce349d80c15 (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
/*
 *
 * 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 java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

import org.apache.qpid.commands.objects.QueueObject;
import org.apache.qpid.utils.JMXinfo;

public class Commanddelete extends CommandImpl
{
    private int number = 0;
    private QueueObject objname;
    private MBeanServerConnection mbsc;
    private String method1, method2;
    private ObjectName queue;
    public static final String COMMAND_NAME = "delete";

    public Commanddelete(JMXinfo info)
    {
        super(info);
        this.mbsc = info.getmbserverconnector();
        this.objname = new QueueObject(mbsc);
        this.method1 = "deleteMessageFromTop";
        this.method2 = "clearQueue";

    }

    public void deletemessages()
    {
        Set set = null;
        objname.setQueryString(this.getObject(), this.getName(), this.getVirtualhost());
        set = objname.returnObjects();

        if (objname.getSet().size() != 0)
        {
            Iterator it = set.iterator();
            this.queue = (ObjectName) it.next();
            try
            {
                if (this.number == 0)
                {
                    echo("");
                    System.out.print("Do you want to delete all the messages from the Queue [Y/N] :");
                    InputStreamReader isr = new InputStreamReader(System.in);
                    BufferedReader br = new BufferedReader(isr);
                    String s = br.readLine();
                    echo(s);
                    if (s.compareToIgnoreCase("y") == 0)
                        this.mbsc.invoke(queue, this.method2, null, null);
                    else
                        return;
                }
                else if (objname.getmessagecount(this.queue) < this.number)
                {
                    echo("Given number is Greater than the Queue Depth");
                    return;
                }
                else
                {
                    for (int i = 0; i < this.number; i++)
                    {
                        this.mbsc.invoke(queue, this.method1, null, null);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }

        }
        else
        {
            if (hasName())
            {

                echo("The Queue you have specified is not in the current broker");
                echo("");
            }
            else
            {
                printusage();
            }
        }
    }

    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("object") || checkoptionsetting("o"))
        {
            String object = optionchecker("object");
            if (object == null)
            {
                object = optionchecker("o");
            }
            if (object.compareToIgnoreCase("queue") == 0)
                setObject(object);
            else
            {
                unrecognizeoption();
                echo("This command is only applicable for delete command so please start with queue");
            }
            if (checkoptionsetting("name") || checkoptionsetting("n"))
            {
                String name = optionchecker("name");
                if (name == null)
                    name = optionchecker("n");

                setName(name);
            }
            if (checkoptionsetting("virtualhost") || checkoptionsetting("v"))
            {
                String vhost = optionchecker("virtualhost");
                if (vhost == null)
                    vhost = optionchecker("v");
                setVirtualhost(vhost);
            }
            if (checkoptionsetting("top") || checkoptionsetting("t"))
            {
                String number = optionchecker("top");
                if (number == null)
                    number = optionchecker("t");

                setnumber(removeSpaces(number));
            }
            this.deletemessages();
        }
        else if (checkoptionsetting("h") || checkoptionsetting("help"))
            printusage();
        else
            unrecognizeoption();
    }

    public void printusage()
    {
        echo("");
        echo("Usage:delete [OPTION] ... [OBJECT TYPE]...\n");
        echo("Delete the top most messages from the given queue object\n");
        echo("To specify the desired queue you have to give the virtualhost name and the queue name with following commands\n");
        echo("Where possible options include:\n");
        echo("        -v      --virtualhost Give the virtuallhost name of the desired queue");
        echo("        -n      --name        Give the queue name of the desired queue you want to do the delete operation");
        echo("        -t      --top         Give how many number of messages you want to delete from the top (Default = all the messages will be deleted");
        echo("        -h      --help        Display the help and back to the qpid-cli prompt\n");

    }

    private void setnumber(String number)
    {
        Integer i = new Integer(number);
        this.number = i.intValue();
    }

    public int getnumber()
    {
        return this.number;
    }

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