001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.console.formatter; 018 019import java.io.OutputStream; 020import java.io.PrintStream; 021import java.util.Collection; 022import java.util.Iterator; 023import java.util.Map; 024import java.util.Arrays; 025 026import javax.jms.Message; 027import javax.management.Attribute; 028import javax.management.AttributeList; 029import javax.management.ObjectInstance; 030import javax.management.ObjectName; 031 032public class CommandShellOutputFormatter implements OutputFormatter { 033 private OutputStream outputStream; 034 private PrintStream out; 035 036 public CommandShellOutputFormatter(OutputStream out) { 037 038 this.outputStream = out; 039 if (out instanceof PrintStream) { 040 this.out = (PrintStream)out; 041 } else { 042 this.out = new PrintStream(out); 043 } 044 } 045 046 /** 047 * Retrieve the output stream being used by the formatter 048 */ 049 public OutputStream getOutputStream() { 050 return outputStream; 051 } 052 053 /** 054 * Print an ObjectInstance format of an mbean 055 * 056 * @param mbean - mbean to print 057 */ 058 public void printMBean(ObjectInstance mbean) { 059 printMBean(mbean.getObjectName()); 060 } 061 062 /** 063 * Print an ObjectName format of an mbean 064 * 065 * @param mbean - mbean to print 066 */ 067 public void printMBean(ObjectName mbean) { 068 printMBean(mbean.getKeyPropertyList()); 069 } 070 071 /** 072 * Print an AttributeList format of an mbean 073 * 074 * @param mbean - mbean to print 075 */ 076 public void printMBean(AttributeList mbean) { 077 for (Iterator i = mbean.iterator(); i.hasNext();) { 078 Attribute attrib = (Attribute)i.next(); 079 if (attrib.getValue() instanceof ObjectName) { 080 printMBean((ObjectName)attrib.getValue()); 081 } else if (attrib.getValue() instanceof ObjectInstance) { 082 printMBean((ObjectInstance)attrib.getValue()); 083 } else { 084 out.println(attrib.getName() + " = " + attrib.getValue().toString()); 085 out.println(); 086 } 087 } 088 } 089 090 /** 091 * Print a Map format of an mbean 092 * 093 * @param mbean - mbean to print 094 */ 095 public void printMBean(Map mbean) { 096 for (Iterator i = mbean.keySet().iterator(); i.hasNext();) { 097 String key = (String)i.next(); 098 String val = mbean.get(key).toString(); 099 out.println(key + " = " + val); 100 } 101 out.println(); 102 } 103 104 /** 105 * Print a collection of mbean 106 * 107 * @param mbean - collection of mbeans 108 */ 109 public void printMBean(Collection mbean) { 110 for (Iterator i = mbean.iterator(); i.hasNext();) { 111 Object obj = i.next(); 112 if (obj instanceof ObjectInstance) { 113 printMBean((ObjectInstance)obj); 114 } else if (obj instanceof ObjectName) { 115 printMBean((ObjectName)obj); 116 } else if (obj instanceof Map) { 117 printMBean((Map)obj); 118 } else if (obj instanceof AttributeList) { 119 printMBean((AttributeList)obj); 120 } else if (obj instanceof Collection) { 121 printMessage((Collection)obj); 122 } else { 123 printException(new UnsupportedOperationException("Unknown mbean type: " + obj.getClass().getName())); 124 } 125 } 126 } 127 128 /** 129 * Print a Map format of a JMS message 130 * 131 * @param msg 132 */ 133 public void printMessage(Map msg) { 134 for (Iterator i = msg.keySet().iterator(); i.hasNext();) { 135 String key = (String)i.next(); 136 String val = msg.get(key).toString(); 137 out.println(key + " = " + val); 138 } 139 out.println(); 140 } 141 142 /** 143 * Print a Message format of a JMS message 144 * 145 * @param msg - JMS message to print 146 */ 147 public void printMessage(Message msg) { 148 // TODO 149 } 150 151 /** 152 * Print a collection of JMS messages 153 * 154 * @param msg - collection of JMS messages 155 */ 156 public void printMessage(Collection msg) { 157 for (Iterator i = msg.iterator(); i.hasNext();) { 158 Object obj = i.next(); 159 if (obj instanceof Message) { 160 printMessage((Message)obj); 161 } else if (obj instanceof Map) { 162 printMessage((Map)obj); 163 } else if (obj instanceof Collection) { 164 printMessage((Collection)obj); 165 } else { 166 printException(new UnsupportedOperationException("Unknown message type: " + obj.getClass().getName())); 167 } 168 } 169 } 170 171 /** 172 * Print help messages 173 * 174 * @param helpMsgs - help messages to print 175 */ 176 public void printHelp(String[] helpMsgs) { 177 for (int i = 0; i < helpMsgs.length; i++) { 178 out.println(helpMsgs[i]); 179 } 180 out.println(); 181 } 182 183 /** 184 * Print an information message 185 * 186 * @param info - information message to print 187 */ 188 public void printInfo(String info) { 189 out.println("INFO: " + info); 190 } 191 192 /** 193 * Print an exception message 194 * 195 * @param e - exception to print 196 */ 197 public void printException(Exception e) { 198 out.println("ERROR: " + e); 199 e.printStackTrace(out); 200 } 201 202 /** 203 * Print a version information 204 * 205 * @param version - version info to print 206 */ 207 public void printVersion(String version) { 208 out.println(""); 209 out.println("ActiveMQ " + version); 210 out.println("For help or more information please see: http://activemq.apache.org"); 211 out.println(""); 212 } 213 214 /** 215 * Print a generic key value mapping 216 * 217 * @param map to print 218 */ 219 public void print(Map map) { 220 for (Iterator i = map.keySet().iterator(); i.hasNext();) { 221 String key = (String)i.next(); 222 String val = map.get(key).toString(); 223 out.println(key + " = " + val); 224 } 225 out.println(); 226 } 227 228 /** 229 * Print a generic array of strings 230 * 231 * @param strings - string array to print 232 */ 233 public void print(String[] strings) { 234 for (int i = 0; i < strings.length; i++) { 235 out.println(strings[i]); 236 } 237 out.println(); 238 } 239 240 /** 241 * Print a collection of objects 242 * 243 * @param collection - collection to print 244 */ 245 public void print(Collection collection) { 246 for (Iterator i = collection.iterator(); i.hasNext();) { 247 out.println(i.next().toString()); 248 } 249 out.println(); 250 } 251 252 /** 253 * Print a java string 254 * 255 * @param string - string to print 256 */ 257 public void print(String string) { 258 out.println(string); 259 } 260 261}