1 18 package org.apache.activemq.console.formatter; 19 20 import javax.management.ObjectInstance ; 21 import javax.management.ObjectName ; 22 import javax.management.AttributeList ; 23 import javax.management.Attribute ; 24 import javax.jms.Message ; 25 import java.util.Map ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.io.PrintStream ; 29 import java.io.OutputStream ; 30 31 public class CommandShellOutputFormatter implements OutputFormatter { 32 private OutputStream outputStream; 33 private PrintStream out; 34 35 public CommandShellOutputFormatter(OutputStream out) { 36 37 this.outputStream = out; 38 if (out instanceof PrintStream ) { 39 this.out = (PrintStream )out; 40 } else { 41 this.out = new PrintStream (out); 42 } 43 } 44 45 49 public OutputStream getOutputStream() { 50 return outputStream; 51 } 52 53 57 public void printMBean(ObjectInstance mbean) { 58 printMBean(mbean.getObjectName()); 59 } 60 61 65 public void printMBean(ObjectName mbean) { 66 printMBean(mbean.getKeyPropertyList()); 67 } 68 69 73 public void printMBean(AttributeList mbean) { 74 for (Iterator i=mbean.iterator(); i.hasNext();) { 75 Attribute attrib = (Attribute )i.next(); 76 if (attrib.getValue() instanceof ObjectName ) { 77 printMBean((ObjectName )attrib.getValue()); 78 } else if (attrib.getValue() instanceof ObjectInstance ) { 79 printMBean((ObjectInstance )attrib.getValue()); 80 } else { 81 out.println(attrib.getName() + " = " + attrib.getValue().toString()); 82 out.println(); 83 } 84 } 85 } 86 87 91 public void printMBean(Map mbean) { 92 for (Iterator i=mbean.keySet().iterator(); i.hasNext();) { 93 String key = (String )i.next(); 94 String val = mbean.get(key).toString(); 95 out.println(key + " = " + val); 96 } 97 out.println(); 98 } 99 100 104 public void printMBean(Collection mbean) { 105 for (Iterator i=mbean.iterator(); i.hasNext();) { 106 Object obj = i.next(); 107 if (obj instanceof ObjectInstance ) { 108 printMBean((ObjectInstance )obj); 109 } else if (obj instanceof ObjectName ) { 110 printMBean((ObjectName )obj); 111 } else if (obj instanceof Map ) { 112 printMBean((Map )obj); 113 } else if (obj instanceof AttributeList ) { 114 printMBean((AttributeList )obj); 115 } else if (obj instanceof Collection ) { 116 printMessage((Collection )obj); 117 } else { 118 printException(new UnsupportedOperationException ("Unknown mbean type: " + obj.getClass().getName())); 119 } 120 } 121 } 122 123 127 public void printMessage(Map msg) { 128 for (Iterator i=msg.keySet().iterator(); i.hasNext();) { 129 String key = (String )i.next(); 130 String val = msg.get(key).toString(); 131 out.println(key + " = " + val); 132 } 133 out.println(); 134 } 135 136 140 public void printMessage(Message msg) { 141 } 143 144 148 public void printMessage(Collection msg) { 149 for (Iterator i=msg.iterator(); i.hasNext();) { 150 Object obj = i.next(); 151 if (obj instanceof Message ) { 152 printMessage((Message )obj); 153 } else if (obj instanceof Map ) { 154 printMessage((Map )obj); 155 } else if (obj instanceof Collection ) { 156 printMessage((Collection )obj); 157 } else { 158 printException(new UnsupportedOperationException ("Unknown message type: " + obj.getClass().getName())); 159 } 160 } 161 } 162 163 167 public void printHelp(String [] helpMsgs) { 168 for (int i=0; i<helpMsgs.length; i++) { 169 out.println(helpMsgs[i]); 170 } 171 out.println(); 172 } 173 174 178 public void printInfo(String info) { 179 out.println("INFO: " + info); 180 } 181 182 186 public void printException(Exception e) { 187 out.println("ERROR: " + e); 188 e.printStackTrace(out); 189 } 190 191 195 public void printVersion(String version) { 196 out.println(""); 197 out.println("ActiveMQ " + version); 198 out.println("For help or more information please see: http://www.logicblaze.com"); 199 out.println(""); 200 } 201 202 206 public void print(Map map) { 207 for (Iterator i=map.keySet().iterator(); i.hasNext();) { 208 String key = (String )i.next(); 209 String val = map.get(key).toString(); 210 out.println(key + " = " + val); 211 } 212 out.println(); 213 } 214 215 219 public void print(String [] strings) { 220 for (int i=0; i<strings.length; i++) { 221 out.println(strings[i]); 222 } 223 out.println(); 224 } 225 226 230 public void print(Collection collection) { 231 for (Iterator i=collection.iterator(); i.hasNext();) { 232 out.println(i.next().toString()); 233 } 234 out.println(); 235 } 236 237 241 public void print(String string) { 242 out.println(string); 243 } 244 245 } 246 | Popular Tags |