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; 18 19 import java.io.OutputStream; 20 import java.util.Collection; 21 import java.util.Map; 22 23 import javax.jms.Message; 24 import javax.management.AttributeList; 25 import javax.management.ObjectInstance; 26 import javax.management.ObjectName; 27 28 import org.apache.activemq.console.formatter.OutputFormatter; 29 30 public final class CommandContext { 31 private OutputFormatter formatter; 32 33 /** 34 * Retrieve the output stream being used by the global formatter 35 * 36 * @return 37 */ 38 public OutputStream getOutputStream() { 39 if (formatter == null) { 40 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 41 } 42 return formatter.getOutputStream(); 43 } 44 45 /** 46 * Print an ObjectInstance format of an mbean 47 * 48 * @param mbean - mbean to print 49 */ 50 public void printMBean(ObjectInstance mbean) { 51 if (formatter == null) { 52 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 53 } 54 formatter.printMBean(mbean); 55 } 56 57 /** 58 * Print an ObjectName format of an mbean 59 * 60 * @param mbean - mbean to print 61 */ 62 public void printMBean(ObjectName mbean) { 63 if (formatter == null) { 64 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 65 } 66 formatter.printMBean(mbean); 67 } 68 69 /** 70 * Print an AttributeList format of an mbean 71 * 72 * @param mbean - mbean to print 73 */ 74 public void printMBean(AttributeList mbean) { 75 if (formatter == null) { 76 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 77 } 78 formatter.printMBean(mbean); 79 } 80 81 /** 82 * Print a Map format of an mbean 83 * 84 * @param mbean 85 */ 86 public void printMBean(Map mbean) { 87 if (formatter == null) { 88 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 89 } 90 formatter.printMBean(mbean); 91 } 92 93 /** 94 * Print a Collection format of mbeans 95 * 96 * @param mbean - collection of mbeans 97 */ 98 public void printMBean(Collection mbean) { 99 if (formatter == null) { 100 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 101 } 102 formatter.printMBean(mbean); 103 } 104 105 /** 106 * Print a Map format of a JMS message 107 * 108 * @param msg 109 */ 110 public void printMessage(Map msg) { 111 if (formatter == null) { 112 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 113 } 114 formatter.printMessage(msg); 115 } 116 117 /** 118 * Print a Message format of a JMS message 119 * 120 * @param msg - JMS message to print 121 */ 122 public void printMessage(Message msg) { 123 if (formatter == null) { 124 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 125 } 126 formatter.printMessage(msg); 127 } 128 129 /** 130 * Print a collection of JMS messages 131 * 132 * @param msg - collection of JMS messages 133 */ 134 public void printMessage(Collection msg) { 135 if (formatter == null) { 136 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 137 } 138 formatter.printMessage(msg); 139 } 140 141 /** 142 * Print help messages 143 * 144 * @param helpMsgs - help messages to print 145 */ 146 public void printHelp(String[] helpMsgs) { 147 if (formatter == null) { 148 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 149 } 150 formatter.printHelp(helpMsgs); 151 } 152 153 /** 154 * Print an information message 155 * 156 * @param info - information message to print 157 */ 158 public void printInfo(String info) { 159 if (formatter == null) { 160 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 161 } 162 formatter.printInfo(info); 163 } 164 165 /** 166 * Print an exception message 167 * 168 * @param e - exception to print 169 */ 170 public void printException(Exception e) { 171 if (formatter == null) { 172 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 173 } 174 formatter.printException(e); 175 } 176 177 /** 178 * Print a version information 179 * 180 * @param version - version info to print 181 */ 182 public void printVersion(String version) { 183 if (formatter == null) { 184 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 185 } 186 formatter.printVersion(version); 187 } 188 189 /** 190 * Print a generic key value mapping 191 * 192 * @param map to print 193 */ 194 public void print(Map map) { 195 if (formatter == null) { 196 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 197 } 198 formatter.print(map); 199 } 200 201 /** 202 * Print a generic array of strings 203 * 204 * @param strings - string array to print 205 */ 206 public void print(String[] strings) { 207 if (formatter == null) { 208 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 209 } 210 formatter.print(strings); 211 } 212 213 /** 214 * Print a collection of objects 215 * 216 * @param collection - collection to print 217 */ 218 public void print(Collection collection) { 219 if (formatter == null) { 220 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 221 } 222 formatter.print(collection); 223 } 224 225 /** 226 * Print a java string 227 * 228 * @param string - string to print 229 */ 230 public void print(String string) { 231 if (formatter == null) { 232 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 233 } 234 formatter.print(string); 235 } 236 237 public OutputFormatter getFormatter() { 238 return formatter; 239 } 240 241 public void setFormatter(OutputFormatter formatter) { 242 this.formatter = formatter; 243 } 244 }