Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » console » formatter » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.console.formatter;
   18   
   19   import java.io.OutputStream;
   20   import java.io.PrintStream;
   21   import java.util.Collection;
   22   import java.util.Iterator;
   23   import java.util.Map;
   24   import java.util.Arrays;
   25   
   26   import javax.jms.Message;
   27   import javax.management.Attribute;
   28   import javax.management.AttributeList;
   29   import javax.management.ObjectInstance;
   30   import javax.management.ObjectName;
   31   
   32   public class CommandShellOutputFormatter implements OutputFormatter {
   33       private OutputStream outputStream;
   34       private PrintStream out;
   35   
   36       public CommandShellOutputFormatter(OutputStream out) {
   37   
   38           this.outputStream = out;
   39           if (out instanceof PrintStream) {
   40               this.out = (PrintStream)out;
   41           } else {
   42               this.out = new PrintStream(out);
   43           }
   44       }
   45   
   46       /**
   47        * Retrieve the output stream being used by the formatter
   48        * 
   49        * @return
   50        */
   51       public OutputStream getOutputStream() {
   52           return outputStream;
   53       }
   54   
   55       /**
   56        * Print an ObjectInstance format of an mbean
   57        * 
   58        * @param mbean - mbean to print
   59        */
   60       public void printMBean(ObjectInstance mbean) {
   61           printMBean(mbean.getObjectName());
   62       }
   63   
   64       /**
   65        * Print an ObjectName format of an mbean
   66        * 
   67        * @param mbean - mbean to print
   68        */
   69       public void printMBean(ObjectName mbean) {
   70           printMBean(mbean.getKeyPropertyList());
   71       }
   72   
   73       /**
   74        * Print an AttributeList format of an mbean
   75        * 
   76        * @param mbean - mbean to print
   77        */
   78       public void printMBean(AttributeList mbean) {
   79           for (Iterator i = mbean.iterator(); i.hasNext();) {
   80               Attribute attrib = (Attribute)i.next();
   81               if (attrib.getValue() instanceof ObjectName) {
   82                   printMBean((ObjectName)attrib.getValue());
   83               } else if (attrib.getValue() instanceof ObjectInstance) {
   84                   printMBean((ObjectInstance)attrib.getValue());
   85               } else {
   86                   out.println(attrib.getName() + " = " + attrib.getValue().toString());
   87                   out.println();
   88               }
   89           }
   90       }
   91   
   92       /**
   93        * Print a Map format of an mbean
   94        * 
   95        * @param mbean - mbean to print
   96        */
   97       public void printMBean(Map mbean) {
   98           for (Iterator i = mbean.keySet().iterator(); i.hasNext();) {
   99               String key = (String)i.next();
  100               String val = mbean.get(key).toString();
  101               out.println(key + " = " + val);
  102           }
  103           out.println();
  104       }
  105   
  106       /**
  107        * Print a collection of mbean
  108        * 
  109        * @param mbean - collection of mbeans
  110        */
  111       public void printMBean(Collection mbean) {
  112           for (Iterator i = mbean.iterator(); i.hasNext();) {
  113               Object obj = i.next();
  114               if (obj instanceof ObjectInstance) {
  115                   printMBean((ObjectInstance)obj);
  116               } else if (obj instanceof ObjectName) {
  117                   printMBean((ObjectName)obj);
  118               } else if (obj instanceof Map) {
  119                   printMBean((Map)obj);
  120               } else if (obj instanceof AttributeList) {
  121                   printMBean((AttributeList)obj);
  122               } else if (obj instanceof Collection) {
  123                   printMessage((Collection)obj);
  124               } else {
  125                   printException(new UnsupportedOperationException("Unknown mbean type: " + obj.getClass().getName()));
  126               }
  127           }
  128       }
  129   
  130       /**
  131        * Print a Map format of a JMS message
  132        * 
  133        * @param msg
  134        */
  135       public void printMessage(Map msg) {
  136           for (Iterator i = msg.keySet().iterator(); i.hasNext();) {
  137               String key = (String)i.next();
  138               String val = msg.get(key).toString();
  139               out.println(key + " = " + val);
  140           }
  141           out.println();
  142       }
  143   
  144       /**
  145        * Print a Message format of a JMS message
  146        * 
  147        * @param msg - JMS message to print
  148        */
  149       public void printMessage(Message msg) {
  150           // TODO
  151       }
  152   
  153       /**
  154        * Print a collection of JMS messages
  155        * 
  156        * @param msg - collection of JMS messages
  157        */
  158       public void printMessage(Collection msg) {
  159           for (Iterator i = msg.iterator(); i.hasNext();) {
  160               Object obj = i.next();
  161               if (obj instanceof Message) {
  162                   printMessage((Message)obj);
  163               } else if (obj instanceof Map) {
  164                   printMessage((Map)obj);
  165               } else if (obj instanceof Collection) {
  166                   printMessage((Collection)obj);
  167               } else {
  168                   printException(new UnsupportedOperationException("Unknown message type: " + obj.getClass().getName()));
  169               }
  170           }
  171       }
  172   
  173       /**
  174        * Print help messages
  175        * 
  176        * @param helpMsgs - help messages to print
  177        */
  178       public void printHelp(String[] helpMsgs) {
  179           for (int i = 0; i < helpMsgs.length; i++) {
  180               out.println(helpMsgs[i]);
  181           }
  182           out.println();
  183       }
  184   
  185       /**
  186        * Print an information message
  187        * 
  188        * @param info - information message to print
  189        */
  190       public void printInfo(String info) {
  191           out.println("INFO: " + info);
  192       }
  193   
  194       /**
  195        * Print an exception message
  196        * 
  197        * @param e - exception to print
  198        */
  199       public void printException(Exception e) {
  200           out.println("ERROR: " + e);
  201           e.printStackTrace(out);
  202       }
  203   
  204       /**
  205        * Print a version information
  206        * 
  207        * @param version - version info to print
  208        */
  209       public void printVersion(String version) {
  210           out.println("");
  211           out.println("ActiveMQ " + version);
  212           out.println("For help or more information please see: http://activemq.apache.org");
  213           out.println("");
  214       }
  215   
  216       /**
  217        * Print a generic key value mapping
  218        * 
  219        * @param map to print
  220        */
  221       public void print(Map map) {
  222           for (Iterator i = map.keySet().iterator(); i.hasNext();) {
  223               String key = (String)i.next();
  224               String val = map.get(key).toString();
  225               out.println(key + " = " + val);
  226           }
  227           out.println();
  228       }
  229   
  230       /**
  231        * Print a generic array of strings
  232        * 
  233        * @param strings - string array to print
  234        */
  235       public void print(String[] strings) {
  236           for (int i = 0; i < strings.length; i++) {
  237               out.println(strings[i]);
  238           }
  239           out.println();
  240       }
  241   
  242       /**
  243        * Print a collection of objects
  244        * 
  245        * @param collection - collection to print
  246        */
  247       public void print(Collection collection) {
  248           for (Iterator i = collection.iterator(); i.hasNext();) {
  249               out.println(i.next().toString());
  250           }
  251           out.println();
  252       }
  253   
  254       /**
  255        * Print a java string
  256        * 
  257        * @param string - string to print
  258        */
  259       public void print(String string) {
  260           out.println(string);
  261       }
  262   
  263   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » console » formatter » [javadoc | source]