List.java
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.queue.AMQQueue;
026 import org.apache.qpid.server.registry.ApplicationRegistry;
027 import org.apache.qpid.server.virtualhost.VirtualHost;
028 import org.apache.qpid.tools.messagestore.MessageStoreTool;
029 import org.apache.qpid.tools.utils.Console;
030 
031 import java.util.Collection;
032 import java.util.LinkedList;
033 
034 public class List extends AbstractCommand
035 {
036 
037     public List(MessageStoreTool tool)
038     {
039         super(tool);
040     }
041 
042     public void setOutput(Console out)
043     {
044         _console = out;
045     }
046 
047     public String help()
048     {
049         return "list available items.";
050     }
051 
052     public String usage()
053     {
054         return "list queues [<exchange>] | exchanges | bindings [<exchange>] | all";
055     }
056 
057     public String getCommand()
058     {
059         return "list";
060     }
061 
062     public void execute(String... args)
063     {
064         assert args.length > 0;
065         assert args[0].equals(getCommand());
066 
067         if (args.length > 1)
068         {
069             if ((args[1].equals("exchanges"))
070                 || (args[1].equals("queues"))
071                 || (args[1].equals("bindings"))
072                 || (args[1].equals("all")))
073             {
074                 if (args.length == 2)
075                 {
076                     doList(args[1]);
077                 }
078                 else if (args.length == 3)
079                 {
080                     doList(args[1], args[2]);
081                 }
082             }
083             else
084             {
085                 commandError("Unknown options. ", args);
086             }
087         }
088         else if (args.length < 2)
089         {
090             doList("all");
091         }
092         else
093         {
094             doList(args[1]);
095         }
096     }
097 
098     private void doList(String... listItem)
099     {
100         if (_tool.getState().getVhost() == null)
101         {
102             _console.println("No Virtualhost open. Open a Virtualhost first.");
103             listVirtualHosts();
104             return;
105         }
106 
107         VirtualHost vhost = _tool.getState().getVhost();
108 
109         java.util.List<String> data = null;
110 
111         if (listItem[0].equals("queues"))
112         {
113             if (listItem.length > 1)
114             {
115                 data = listQueues(vhost, new AMQShortString(listItem[1]));
116             }
117             else
118             {
119                 Exchange exchange = _tool.getState().getExchange();
120                 data = listQueues(vhost, exchange);
121             }
122         }
123 
124         if (listItem[0].equals("exchanges"))
125         {
126             data = listExchanges(vhost);
127         }
128 
129         if (listItem[0].equals("bindings"))
130         {
131 
132             if (listItem.length > 1)
133             {
134                 data = listBindings(vhost, new AMQShortString(listItem[1]));
135             }
136             else
137             {
138                 Exchange exchange = _tool.getState().getExchange();
139 
140                 data = listBindings(vhost, exchange);
141             }
142         }
143 
144         if (data != null)
145         {
146             if (data.size() == 1)
147             {
148                 _console.println("No '" + listItem[0"' to display,");
149             }
150             else
151             {
152                 _console.displayList(true, data.toArray(new String[0]));
153             }
154         }
155 
156 
157         if (listItem[0].equals("all"))
158         {
159 
160             boolean displayed = false;
161             Exchange exchange = _tool.getState().getExchange();
162 
163             //Do the display here for each one so that they are pretty printed
164             data = listQueues(vhost, exchange);
165             if (data != null)
166             {
167                 displayed = true;
168                 _console.displayList(true, data.toArray(new String[0]));
169             }
170 
171             if (exchange == null)
172             {
173                 data = listExchanges(vhost);
174                 if (data != null)
175                 {
176                     displayed = true;
177                     _console.displayList(true, data.toArray(new String[0]));
178                 }
179             }
180 
181             data = listBindings(vhost, exchange);
182             if (data != null)
183             {
184                 displayed = true;
185                 _console.displayList(true, data.toArray(new String[0]));
186             }
187 
188             if (!displayed)
189             {
190                 _console.println("Nothing to list");
191             }
192         }
193     }
194 
195     private void listVirtualHosts()
196     {
197         Collection<VirtualHost> vhosts = ApplicationRegistry.getInstance()
198                 .getVirtualHostRegistry().getVirtualHosts();
199 
200         String[] data = new String[vhosts.size() 1];
201 
202         data[0"Available VirtualHosts";
203 
204         int index = 1;
205         for (VirtualHost vhost : vhosts)
206         {
207             data[index= vhost.getName();
208             index++;
209         }
210 
211         _console.displayList(true, data);
212     }
213 
214     private java.util.List<String> listBindings(VirtualHost vhost, AMQShortString exchangeName)
215     {
216         return listBindings(vhost, vhost.getExchangeRegistry().getExchange(exchangeName));
217     }
218 
219     private java.util.List<String> listBindings(VirtualHost vhost, Exchange exchange)
220     {
221         Collection<AMQShortString> queues = vhost.getQueueRegistry().getQueueNames();
222 
223         if (queues == null || queues.size() == 0)
224         {
225             return null;
226         }
227 
228         java.util.List<String> data = new LinkedList<String>();
229 
230         data.add("Current Bindings");
231 
232         for (AMQShortString queue : queues)
233         {
234             if (exchange != null)
235             {
236                 if (exchange.isBound(queue))
237                 {
238                     data.add(queue.toString());
239                 }
240             }
241             else
242             {
243                 data.add(queue.toString());
244             }
245         }
246 
247         return data;
248     }
249 
250     private java.util.List<String> listExchanges(VirtualHost vhost)
251     {
252         Collection<AMQShortString> queues = vhost.getExchangeRegistry().getExchangeNames();
253 
254         if (queues == null || queues.size() == 0)
255         {
256             return null;
257         }
258 
259         java.util.List<String> data = new LinkedList<String>();
260 
261         data.add("Available Exchanges");
262 
263         for (AMQShortString queue : queues)
264         {
265             data.add(queue.toString());
266         }
267 
268         return data;
269     }
270 
271     private java.util.List<String> listQueues(VirtualHost vhost, AMQShortString exchangeName)
272     {
273         return listQueues(vhost, vhost.getExchangeRegistry().getExchange(exchangeName));
274     }
275 
276     private java.util.List<String> listQueues(VirtualHost vhost, Exchange exchange)
277     {
278         Collection<AMQQueue> queues = vhost.getQueueRegistry().getQueues();
279 
280         if (queues == null || queues.size() == 0)
281         {
282             return null;
283         }
284 
285         java.util.List<String> data = new LinkedList<String>();
286 
287         data.add("Available Queues");
288 
289         for (AMQQueue queue : queues)
290         {
291             if (exchange != null)
292             {
293                 if (exchange.isBound(queue))
294                 {
295                     data.add(queue.getName().toString());
296                 }
297             }
298             else
299             {
300                 data.add(queue.getName().toString());
301             }
302         }
303 
304         if (exchange != null)
305         {
306             if (queues.size() == 1)
307             {
308                 return null;
309             }
310         }
311 
312         return data;
313     }
314 }