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 formatter's output stream 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 @SuppressWarnings("rawtypes") 087 public void printMBean(Map mbean) { 088 if (formatter == null) { 089 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 090 } 091 formatter.printMBean(mbean); 092 } 093 094 /** 095 * Print a Collection format of mbeans 096 * 097 * @param mbean - collection of mbeans 098 */ 099 @SuppressWarnings("rawtypes") 100 public void printMBean(Collection mbean) { 101 if (formatter == null) { 102 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 103 } 104 formatter.printMBean(mbean); 105 } 106 107 /** 108 * Print a Map format of a JMS message 109 * 110 * @param msg 111 */ 112 @SuppressWarnings("rawtypes") 113 public void printMessage(Map msg) { 114 if (formatter == null) { 115 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 116 } 117 formatter.printMessage(msg); 118 } 119 120 /** 121 * Print a Message format of a JMS message 122 * 123 * @param msg - JMS message to print 124 */ 125 public void printMessage(Message msg) { 126 if (formatter == null) { 127 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 128 } 129 formatter.printMessage(msg); 130 } 131 132 /** 133 * Print a collection of JMS messages 134 * 135 * @param msg - collection of JMS messages 136 */ 137 @SuppressWarnings("rawtypes") 138 public void printMessage(Collection msg) { 139 if (formatter == null) { 140 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 141 } 142 formatter.printMessage(msg); 143 } 144 145 /** 146 * Print help messages 147 * 148 * @param helpMsgs - help messages to print 149 */ 150 public void printHelp(String[] helpMsgs) { 151 if (formatter == null) { 152 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 153 } 154 formatter.printHelp(helpMsgs); 155 } 156 157 /** 158 * Print an information message 159 * 160 * @param info - information message to print 161 */ 162 public void printInfo(String info) { 163 if (formatter == null) { 164 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 165 } 166 formatter.printInfo(info); 167 } 168 169 /** 170 * Print an exception message 171 * 172 * @param e - exception to print 173 */ 174 public void printException(Exception e) { 175 if (formatter == null) { 176 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 177 } 178 formatter.printException(e); 179 } 180 181 /** 182 * Print a version information 183 * 184 * @param version - version info to print 185 */ 186 public void printVersion(String version) { 187 if (formatter == null) { 188 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 189 } 190 formatter.printVersion(version); 191 } 192 193 /** 194 * Print a generic key value mapping 195 * 196 * @param map to print 197 */ 198 @SuppressWarnings("rawtypes") 199 public void print(Map map) { 200 if (formatter == null) { 201 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 202 } 203 formatter.print(map); 204 } 205 206 /** 207 * Print a generic array of strings 208 * 209 * @param strings - string array to print 210 */ 211 public void print(String[] strings) { 212 if (formatter == null) { 213 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 214 } 215 formatter.print(strings); 216 } 217 218 /** 219 * Print a collection of objects 220 * 221 * @param collection - collection to print 222 */ 223 @SuppressWarnings("rawtypes") 224 public void print(Collection collection) { 225 if (formatter == null) { 226 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 227 } 228 formatter.print(collection); 229 } 230 231 /** 232 * Print a java string 233 * 234 * @param string - string to print 235 */ 236 public void print(String string) { 237 if (formatter == null) { 238 throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter)."); 239 } 240 formatter.print(string); 241 } 242 243 public OutputFormatter getFormatter() { 244 return formatter; 245 } 246 247 public void setFormatter(OutputFormatter formatter) { 248 this.formatter = formatter; 249 } 250}