001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 *
020 */
021 package org.apache.qpid.tools.messagestore.commands;
022
023 import org.apache.qpid.framing.AMQShortString;
024 import org.apache.qpid.server.exchange.Exchange;
025 import org.apache.qpid.server.registry.ApplicationRegistry;
026 import org.apache.qpid.server.virtualhost.VirtualHost;
027 import org.apache.qpid.server.queue.AMQQueue;
028 import org.apache.qpid.tools.messagestore.MessageStoreTool;
029
030 import java.util.LinkedList;
031 import java.util.StringTokenizer;
032
033 public class Select extends AbstractCommand
034 {
035
036 public Select(MessageStoreTool tool)
037 {
038 super(tool);
039 }
040
041 public String help()
042 {
043 return "Perform a selection.";
044 }
045
046 public String usage()
047 {
048 return "select virtualhost <name> |exchange <name> |queue <name> | msg id=<msgids eg. 1,2,4-10>";
049 }
050
051 public String getCommand()
052 {
053 return "select";
054 }
055
056 public void execute(String... args)
057 {
058 assert args.length > 2;
059 assert args[0].equals("select");
060
061 if (args.length < 3)
062 {
063 if (args[1].equals("show"))
064 {
065 doSelect(args[1], null);
066 }
067 else
068 {
069 _console.print("select : unknown command:");
070 _console.println(help());
071 }
072 }
073 else
074 {
075 if (args[1].equals("virtualhost")
076 || args[1].equals("vhost")
077 || args[1].equals("exchange")
078 || args[1].equals("queue")
079 || args[1].equals("msg")
080 )
081 {
082 doSelect(args[1], args[2]);
083 }
084 else
085 {
086 _console.println(help());
087 }
088 }
089 }
090
091 private void doSelect(String type, String item)
092 {
093 if (type.equals("virtualhost"))
094 {
095
096 VirtualHost vhost = ApplicationRegistry.getInstance()
097 .getVirtualHostRegistry().getVirtualHost(item);
098
099 if (vhost == null)
100 {
101 _console.println("Virtualhost '" + item + "' not found.");
102 }
103 else
104 {
105 _tool.getState().setVhost(vhost);
106 }
107 }
108
109 if (type.equals("exchange"))
110 {
111
112 VirtualHost vhost = _tool.getState().getVhost();
113
114 if (vhost == null)
115 {
116 _console.println("No Virtualhost open. Open a Virtualhost first.");
117 return;
118 }
119
120
121 Exchange exchange = vhost.getExchangeRegistry().getExchange(new AMQShortString(item));
122
123 if (exchange == null)
124 {
125 _console.println("Exchange '" + item + "' not found.");
126 }
127 else
128 {
129 _tool.getState().setExchange(exchange);
130 }
131
132 if (_tool.getState().getQueue() != null)
133 {
134 if (!exchange.isBound(_tool.getState().getQueue()))
135 {
136 _tool.getState().setQueue(null);
137 }
138 }
139 }
140
141 if (type.equals("queue"))
142 {
143 VirtualHost vhost = _tool.getState().getVhost();
144
145 if (vhost == null)
146 {
147 _console.println("No Virtualhost open. Open a Virtualhost first.");
148 return;
149 }
150
151 AMQQueue queue = vhost.getQueueRegistry().getQueue(new AMQShortString(item));
152
153 if (queue == null)
154 {
155 _console.println("Queue '" + item + "' not found.");
156 }
157 else
158 {
159 _tool.getState().setQueue(queue);
160
161 if (_tool.getState().getExchange() == null)
162 {
163 for (AMQShortString exchangeName : vhost.getExchangeRegistry().getExchangeNames())
164 {
165 Exchange exchange = vhost.getExchangeRegistry().getExchange(exchangeName);
166 if (exchange.isBound(queue))
167 {
168 _tool.getState().setExchange(exchange);
169 break;
170 }
171 }
172 }
173
174 //remove the message selection
175 _tool.getState().setMessages((java.util.List<Long>) null);
176 }
177 }
178
179 if (type.equals("msg"))
180 {
181 if (item.startsWith("id="))
182 {
183 StringTokenizer tok = new StringTokenizer(item.substring(item.indexOf("=") + 1), ",");
184
185 java.util.List<Long> msgids = null;
186
187 if (tok.hasMoreTokens())
188 {
189 msgids = new LinkedList<Long>();
190 }
191
192 while (tok.hasMoreTokens())
193 {
194 String next = tok.nextToken();
195 if (next.contains("-"))
196 {
197 Long start = Long.parseLong(next.substring(0, next.indexOf("-")));
198 Long end = Long.parseLong(next.substring(next.indexOf("-") + 1));
199
200 if (end >= start)
201 {
202 for (long l = start; l <= end; l++)
203 {
204 msgids.add(l);
205 }
206 }
207 }
208 else
209 {
210 msgids.add(Long.parseLong(next));
211 }
212 }
213
214 _tool.getState().setMessages(msgids);
215 }
216
217 }
218
219 if (type.equals("show"))
220 {
221 _console.println(_tool.getState().toString());
222 if (_tool.getState().getMessages() != null)
223 {
224 _console.print("Msgs:");
225 for (Long l : _tool.getState().getMessages())
226 {
227 _console.print(" " + l);
228 }
229 _console.println("");
230 }
231 }
232 }
233 }
|