summaryrefslogtreecommitdiff
path: root/qpid/java/management/tools/qpid-cli/src/org/apache/qpid/commands/Commandmove.java
blob: 6d1803409bd8402b30926268a9f08a0be9b40f91 (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
/*
 *
 * 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.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 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)
    {
        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);
        set = queue1.returnObjects();
        if (queue1.getSet().size() != 0)
        { // find the queue
            Iterator it = set.iterator();
            this.queue = (ObjectName) it.next();
        }
        else
        {
            if (isname1() || isname2())
            { // if the specified queue is not there in the broker

                echo("The Queue you have specified is not in the current broker");
                echo("");
            }
            else
            {
                printusage();
            }
        }
        try
        {
            Object[] params1 = { getfmid(), gettmid(), this.name2 };
            String[] signature1 = { new String("long"), new String("long"), new String("java.lang.String") };
            this.mbsc.invoke(this.queue, this.method1, params1, signature1);

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            echo("Given messageId's might be wrong please run the view command and check messageId's you have given\n");
            echo("From MessageId should be greater than 0 and should less than To messageId");
        }

    }

    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 queue command so please start with queue");
            }
            if (checkoptionsetting("n2") && checkoptionsetting("n1"))
            {
                setname1(optionchecker("n1"));
                setname2(optionchecker("n2"));
            }
            else
            {
                echo("You have to specify both n1 and n2 option value to move a message"); /*
                                                                                            * when
                                                                                            * user
                                                                                            * forget
                                                                                            * to
                                                                                            * specify
                                                                                            * target
                                                                                            * or
                                                                                            * starting
                                                                                            * queue
                                                                                            * name
                                                                                            */
                return;
            }

            if (checkoptionsetting("v1"))
            {

                setvhost1(optionchecker("v1"));
            }
            if (checkoptionsetting("tmid") && checkoptionsetting("fmid"))
            {
                String tmid = optionchecker("tmid");
                String fmid = optionchecker("fmid");

                settomessageIdandfrommessageId(removeSpaces(tmid), removeSpaces(fmid));
            }
            else
            {
                echo("You have to set from MessageId and to MessageId in order to move messages between queues");
                echo("To view MessageId's use <view> command with -n and -v options");
                return;
            }
            this.movemessages();

        }
        else if (checkoptionsetting("h") || checkoptionsetting("help"))
            printusage();
        else
            unrecognizeoption();
    }

    public void printusage()
    {
        echo("");
        echo("Usage:move [OPTION] ... [OBJECT TYPE]...\n");
        echo("Move the top most messages from the given queue object to the given destination object\n");
        echo("To specify the desired queues you have to give the virtualhost name and the queue name with following commands\n");
        echo("Where possible options include:\n");
        echo("        -v1                   Give the virtuallhost name from which queue you want to move messages");
        echo("        -n1                   Give the queue name which you want to move messages from");
        echo("        -n2                   Give the queue name of the destination queue");
        echo("        -tmid                 Give From MessageId you want to move from the Queue");
        echo("        -fmid                 Give To MessageId you want to move from the Queue");
        echo("        -h      --help        Display the help and back to the qpid-cli prompt\n");

    }

    private void setname1(String name)
    {
        this.name1 = name;
    }

    private void setname2(String name)
    {
        this.name2 = name;
    }

    private boolean isname1()
    {
        if (this.name1 == null)
            return false;

        else
            return true;
    }

    private boolean isname2()
    {
        if (this.name2 == null)
            return false;

        else
            return true;
    }

    private void setvhost1(String vhost)
    {
        this.vhost1 = vhost;
    }

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

    private void settomessageIdandfrommessageId(String tmid, String fmid)
    {
        Integer i = new Integer(tmid);
        Integer j = new Integer(fmid);
        this.tmid = i.intValue();
        this.fmid = j.intValue();
    }

    public int gettmid()
    {
        return this.tmid;
    }

    public int getfmid()
    {
        return this.fmid;
    }

    public String getname1()
    {
        return this.name1;
    }

    public String getname2()
    {
        return this.name2;
    }

}