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