Show.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.AMQException;
024 import org.apache.qpid.framing.AMQShortString;
025 import org.apache.qpid.framing.BasicContentHeaderProperties;
026 import org.apache.qpid.framing.FieldTable;
027 import org.apache.qpid.framing.abstraction.MessagePublishInfo;
028 import org.apache.qpid.server.queue.AMQMessage;
029 import org.apache.qpid.server.queue.AMQQueue;
030 import org.apache.qpid.server.queue.QueueEntry;
031 import org.apache.qpid.tools.messagestore.MessageStoreTool;
032 import org.apache.qpid.tools.utils.Console;
033 
034 import java.util.LinkedList;
035 import java.util.List;
036 
037 public class Show extends AbstractCommand
038 {
039     protected boolean _amqHeaders = false;
040     protected boolean _routing = false;
041     protected boolean _msgHeaders = false;
042 
043     public Show(MessageStoreTool tool)
044     {
045         super(tool);
046     }
047 
048     public String help()
049     {
050         return "Shows the messages headers.";
051     }
052 
053     public String usage()
054     {
055         return getCommand() " [show=[all],[msgheaders],[amqheaders],[routing]] [id=<msgid e.g. 1,2,4-10>]";
056     }
057 
058     public String getCommand()
059     {
060         return "show";
061     }
062 
063     public void execute(String... args)
064     {
065         assert args.length > 0;
066         assert args[0].equals(getCommand());
067 
068         if (args.length < 2)
069         {
070             parseArgs("all");
071         }
072         else
073         {
074             parseArgs(args);
075         }
076 
077         performShow();
078     }
079 
080     protected void parseArgs(String... args)
081     {
082         List<Long> msgids = null;
083 
084         if (args.length >= 2)
085         {
086             for (String arg : args)
087             {
088                 if (arg.startsWith("show="))
089                 {
090                     _msgHeaders = arg.contains("msgheaders"|| arg.contains("all");
091                     _amqHeaders = arg.contains("amqheaders"|| arg.contains("all");
092                     _routing = arg.contains("routing"|| arg.contains("all");
093                 }
094 
095                 if (arg.startsWith("id="))
096                 {
097                     _tool.getState().setMessages(msgids);
098                 }
099             }//for args
100         }// if args > 2
101     }
102 
103     protected void performShow()
104     {
105         if (_tool.getState().getVhost() == null)
106         {
107             _console.println("No Virtualhost selected. 'DuSelect' a Virtualhost first.");
108             return;
109         }
110 
111         AMQQueue _queue = _tool.getState().getQueue();
112 
113         List<Long> msgids = _tool.getState().getMessages();
114 
115         if (_queue != null)
116         {
117             List<QueueEntry> messages = _queue.getMessagesOnTheQueue();
118             if (messages == null || messages.size() == 0)
119             {
120                 _console.println("No messages on queue");
121                 return;
122             }
123 
124             List<List> data = createMessageData(msgids, messages, _amqHeaders, _routing, _msgHeaders);
125             if (data != null)
126             {
127                 _console.printMap(null, data);
128             }
129             else
130             {
131                 String message = "No data to display.";
132                 if (msgids != null)
133                 {
134                     message += " Is message selection correct? " + _tool.getState().printMessages();
135                 }
136                 _console.println(message);
137             }
138 
139         }
140         else
141         {
142             _console.println("No Queue specified to show.");
143         }
144     }
145 
146     /**
147      * Create the list data for display from the messages.
148      *
149      @param msgids The list of message ids to display
150      @param messages A list of messages to format and display.
151      @param showHeaders should the header info be shown
152      @param showRouting show the routing info be shown
153      @param showMessageHeaders show the msg headers be shown
154      @return the formated data lists for printing
155      */
156     protected List<List> createMessageData(List<Long> msgids, List<QueueEntry> messages, boolean showHeaders, boolean showRouting,
157                                            boolean showMessageHeaders)
158     {
159 
160         // Currenly exposed message properties
161 //        //Printing the content Body
162 //        msg.getContentBodyIterator();
163 //        //Print the Headers
164 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getAppId();
165 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getAppIdAsString();
166 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getClusterId();
167 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getContentType();
168 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getCorrelationId();
169 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getDeliveryMode();
170 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getEncoding();
171 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getExpiration();
172 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getHeaders();
173 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getMessageId();
174 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getPriority();
175 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getPropertyFlags();
176 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getReplyTo();
177 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getTimestamp();
178 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getType();
179 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getUserId();
180 //
181 //        //Print out all the property names
182 //        ((BasicContentHeaderProperties)msg.getContentHeaderBody().properties).getHeaders().getPropertyNames();
183 //
184 //        msg.getMessageId();
185 //        msg.getSize();
186 //        msg.getArrivalTime();
187 
188 //        msg.getDeliveredSubscription();
189 //        msg.getDeliveredToConsumer();
190 //        msg.getMessageHandle();
191 //        msg.getMessageId();
192 //        msg.getMessagePublishInfo();
193 //        msg.getPublisher();
194 
195 //        msg.getStoreContext();
196 //        msg.isAllContentReceived();
197 //        msg.isPersistent();
198 //        msg.isRedelivered();
199 //        msg.isRejectedBy();
200 //        msg.isTaken();
201 
202         //Header setup
203 
204         List<List> data = new LinkedList<List>();
205 
206         List<String> id = new LinkedList<String>();
207         data.add(id);
208         id.add(Columns.ID.name());
209         id.add(Console.ROW_DIVIDER);
210 
211         List<String> exchange = new LinkedList<String>();
212         List<String> routingkey = new LinkedList<String>();
213         List<String> immediate = new LinkedList<String>();
214         List<String> mandatory = new LinkedList<String>();
215         if (showRouting)
216         {
217             data.add(exchange);
218             exchange.add(Columns.Exchange.name());
219             exchange.add(Console.ROW_DIVIDER);
220 
221             data.add(routingkey);
222             routingkey.add(Columns.RoutingKey.name());
223             routingkey.add(Console.ROW_DIVIDER);
224 
225             data.add(immediate);
226             immediate.add(Columns.isImmediate.name());
227             immediate.add(Console.ROW_DIVIDER);
228 
229             data.add(mandatory);
230             mandatory.add(Columns.isMandatory.name());
231             mandatory.add(Console.ROW_DIVIDER);
232         }
233 
234         List<String> size = new LinkedList<String>();
235         List<String> appid = new LinkedList<String>();
236         List<String> clusterid = new LinkedList<String>();
237         List<String> contenttype = new LinkedList<String>();
238         List<String> correlationid = new LinkedList<String>();
239         List<String> deliverymode = new LinkedList<String>();
240         List<String> encoding = new LinkedList<String>();
241         List<String> arrival = new LinkedList<String>();
242         List<String> expiration = new LinkedList<String>();
243         List<String> priority = new LinkedList<String>();
244         List<String> propertyflag = new LinkedList<String>();
245         List<String> replyto = new LinkedList<String>();
246         List<String> timestamp = new LinkedList<String>();
247         List<String> type = new LinkedList<String>();
248         List<String> userid = new LinkedList<String>();
249         List<String> ispersitent = new LinkedList<String>();
250         List<String> isredelivered = new LinkedList<String>();
251         List<String> isdelivered = new LinkedList<String>();
252 
253         data.add(size);
254         size.add(Columns.Size.name());
255         size.add(Console.ROW_DIVIDER);
256 
257         if (showHeaders)
258         {
259             data.add(ispersitent);
260             ispersitent.add(Columns.isPersistent.name());
261             ispersitent.add(Console.ROW_DIVIDER);
262 
263             data.add(isredelivered);
264             isredelivered.add(Columns.isRedelivered.name());
265             isredelivered.add(Console.ROW_DIVIDER);
266 
267             data.add(isdelivered);
268             isdelivered.add(Columns.isDelivered.name());
269             isdelivered.add(Console.ROW_DIVIDER);
270 
271             data.add(appid);
272             appid.add(Columns.App_ID.name());
273             appid.add(Console.ROW_DIVIDER);
274 
275             data.add(clusterid);
276             clusterid.add(Columns.Cluster_ID.name());
277             clusterid.add(Console.ROW_DIVIDER);
278 
279             data.add(contenttype);
280             contenttype.add(Columns.Content_Type.name());
281             contenttype.add(Console.ROW_DIVIDER);
282 
283             data.add(correlationid);
284             correlationid.add(Columns.Correlation_ID.name());
285             correlationid.add(Console.ROW_DIVIDER);
286 
287             data.add(deliverymode);
288             deliverymode.add(Columns.Delivery_Mode.name());
289             deliverymode.add(Console.ROW_DIVIDER);
290 
291             data.add(encoding);
292             encoding.add(Columns.Encoding.name());
293             encoding.add(Console.ROW_DIVIDER);
294 
295             data.add(arrival);
296             expiration.add(Columns.Arrival.name());
297             expiration.add(Console.ROW_DIVIDER);
298 
299             data.add(expiration);
300             expiration.add(Columns.Expiration.name());
301             expiration.add(Console.ROW_DIVIDER);
302 
303             data.add(priority);
304             priority.add(Columns.Priority.name());
305             priority.add(Console.ROW_DIVIDER);
306 
307             data.add(propertyflag);
308             propertyflag.add(Columns.Property_Flag.name());
309             propertyflag.add(Console.ROW_DIVIDER);
310 
311             data.add(replyto);
312             replyto.add(Columns.ReplyTo.name());
313             replyto.add(Console.ROW_DIVIDER);
314 
315             data.add(timestamp);
316             timestamp.add(Columns.Timestamp.name());
317             timestamp.add(Console.ROW_DIVIDER);
318 
319             data.add(type);
320             type.add(Columns.Type.name());
321             type.add(Console.ROW_DIVIDER);
322 
323             data.add(userid);
324             userid.add(Columns.UserID.name());
325             userid.add(Console.ROW_DIVIDER);
326         }
327 
328         List<String> msgHeaders = new LinkedList<String>();
329         if (showMessageHeaders)
330         {
331             data.add(msgHeaders);
332             msgHeaders.add(Columns.MsgHeaders.name());
333             msgHeaders.add(Console.ROW_DIVIDER);
334         }
335 
336         //Add create the table of data
337         for (QueueEntry entry : messages)
338         {
339             AMQMessage msg = entry.getMessage();
340             if (!includeMsg(msg, msgids))
341             {
342                 continue;
343             }
344 
345             id.add(msg.getMessageId().toString());
346 
347             size.add("" + msg.getSize());
348 
349             arrival.add("" + msg.getArrivalTime());
350 
351             ispersitent.add(msg.isPersistent() "true" "false");
352 
353             isredelivered.add(entry.isRedelivered() "true" "false");
354 
355             isdelivered.add(msg.getDeliveredToConsumer() "true" "false");
356 
357             BasicContentHeaderProperties headers = null;
358 
359             headers = ((BasicContentHeaderPropertiesmsg.getContentHeaderBody().properties);
360 
361             if (headers != null)
362             {
363                 String appidS = headers.getAppIdAsString();
364                 appid.add(appidS == null "null" : appidS);
365 
366                 String clusterS = headers.getClusterIdAsString();
367                 clusterid.add(clusterS == null "null" : clusterS);
368 
369                 String contentS = headers.getContentTypeAsString();
370                 contenttype.add(contentS == null "null" : contentS);
371 
372                 String correlationS = headers.getCorrelationIdAsString();
373                 correlationid.add(correlationS == null "null" : correlationS);
374 
375                 deliverymode.add("" + headers.getDeliveryMode());
376 
377                 AMQShortString encodeSS = headers.getEncoding();
378                 encoding.add(encodeSS == null "null" : encodeSS.toString());
379 
380                 expiration.add("" + headers.getExpiration());
381 
382                 FieldTable headerFT = headers.getHeaders();
383                 msgHeaders.add(headerFT == null "none" "" + headerFT.toString());
384 
385                 priority.add("" + headers.getPriority());
386                 propertyflag.add("" + headers.getPropertyFlags());
387 
388                 AMQShortString replytoSS = headers.getReplyTo();
389                 replyto.add(replytoSS == null "null" : replytoSS.toString());
390 
391                 timestamp.add("" + headers.getTimestamp());
392 
393                 AMQShortString typeSS = headers.getType();
394                 type.add(typeSS == null "null" : typeSS.toString());
395 
396                 AMQShortString useridSS = headers.getUserId();
397                 userid.add(useridSS == null "null" : useridSS.toString());
398 
399                 MessagePublishInfo info = msg.getMessagePublishInfo();
400 
401                 if (info != null)
402                 {
403                     AMQShortString exchangeSS = info.getExchange();
404                     exchange.add(exchangeSS == null "null" : exchangeSS.toString());
405 
406                     AMQShortString routingkeySS = info.getRoutingKey();
407                     routingkey.add(routingkeySS == null "null" : routingkeySS.toString());
408 
409                     immediate.add(info.isImmediate() "true" "false");
410                     mandatory.add(info.isMandatory() "true" "false");
411                 }
412 
413 //        msg.getPublisher(); -- only used in clustering
414 //        msg.getStoreContext();
415 //        msg.isAllContentReceived();
416 
417             }// if headers!=null
418 
419 // need to access internal map and do lookups.
420 //        msg.isTaken();
421 //        msg.getDeliveredSubscription();
422 //        msg.isRejectedBy();
423 
424         }
425 
426         // if id only had the header and the divider in it then we have no data to display
427         if (id.size() == 2)
428         {
429             return null;
430         }
431         return data;
432     }
433 
434     protected boolean includeMsg(AMQMessage msg, List<Long> msgids)
435     {
436         if (msgids == null)
437         {
438             return true;
439         }
440 
441         Long msgid = msg.getMessageId();
442 
443         boolean found = false;
444 
445         if (msgids != null)
446         {
447             //check msgid is in msgids
448             for (Long l : msgids)
449             {
450                 if (l.equals(msgid))
451                 {
452                     found = true;
453                     break;
454                 }
455             }
456         }
457         return found;
458     }
459 
460     public enum Columns
461     {
462         ID,
463         Size,
464         Exchange,
465         RoutingKey,
466         isImmediate,
467         isMandatory,
468         isPersistent,
469         isRedelivered,
470         isDelivered,
471         App_ID,
472         Cluster_ID,
473         Content_Type,
474         Correlation_ID,
475         Delivery_Mode,
476         Encoding,
477         Arrival,
478         Expiration,
479         Priority,
480         Property_Flag,
481         ReplyTo,
482         Timestamp,
483         Type,
484         UserID,
485         MsgHeaders
486     }
487 }
488