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; 018 019import java.io.OutputStream; 020import java.util.Collection; 021import java.util.Map; 022 023import javax.jms.Message; 024import javax.management.AttributeList; 025import javax.management.ObjectInstance; 026import javax.management.ObjectName; 027 028import org.apache.activemq.console.formatter.OutputFormatter; 029 030public final class CommandContext { 031 private OutputFormatter formatter; 032 033 /** 034 * Retrieve the output stream being used by the global formatter 035 * 036 * @return 037 */ 038 public OutputStream getOutputStream() { 039 if (formatter == null) { 040 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 041 } 042 return formatter.getOutputStream(); 043 } 044 045 /** 046 * Print an ObjectInstance format of an mbean 047 * 048 * @param mbean - mbean to print 049 */ 050 public void printMBean(ObjectInstance mbean) { 051 if (formatter == null) { 052 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 053 } 054 formatter.printMBean(mbean); 055 } 056 057 /** 058 * Print an ObjectName format of an mbean 059 * 060 * @param mbean - mbean to print 061 */ 062 public void printMBean(ObjectName mbean) { 063 if (formatter == null) { 064 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 065 } 066 formatter.printMBean(mbean); 067 } 068 069 /** 070 * Print an AttributeList format of an mbean 071 * 072 * @param mbean - mbean to print 073 */ 074 public void printMBean(AttributeList mbean) { 075 if (formatter == null) { 076 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 077 } 078 formatter.printMBean(mbean); 079 } 080 081 /** 082 * Print a Map format of an mbean 083 * 084 * @param mbean 085 */ 086 public void printMBean(Map mbean) { 087 if (formatter == null) { 088 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 089 } 090 formatter.printMBean(mbean); 091 } 092 093 /** 094 * Print a Collection format of mbeans 095 * 096 * @param mbean - collection of mbeans 097 */ 098 public void printMBean(Collection mbean) { 099 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}