KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > formatter > CommandShellOutputFormatter


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.console.formatter;
19
20 import javax.management.ObjectInstance JavaDoc;
21 import javax.management.ObjectName JavaDoc;
22 import javax.management.AttributeList JavaDoc;
23 import javax.management.Attribute JavaDoc;
24 import javax.jms.Message JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.io.PrintStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30
31 public class CommandShellOutputFormatter implements OutputFormatter {
32     private OutputStream JavaDoc outputStream;
33     private PrintStream JavaDoc out;
34
35     public CommandShellOutputFormatter(OutputStream JavaDoc out) {
36
37         this.outputStream = out;
38         if (out instanceof PrintStream JavaDoc) {
39             this.out = (PrintStream JavaDoc)out;
40         } else {
41             this.out = new PrintStream JavaDoc(out);
42         }
43     }
44
45     /**
46      * Retrieve the output stream being used by the formatter
47      * @return
48      */

49     public OutputStream JavaDoc getOutputStream() {
50         return outputStream;
51     }
52
53     /**
54      * Print an ObjectInstance format of an mbean
55      * @param mbean - mbean to print
56      */

57     public void printMBean(ObjectInstance JavaDoc mbean) {
58         printMBean(mbean.getObjectName());
59     }
60
61     /**
62      * Print an ObjectName format of an mbean
63      * @param mbean - mbean to print
64      */

65     public void printMBean(ObjectName JavaDoc mbean) {
66         printMBean(mbean.getKeyPropertyList());
67     }
68
69     /**
70      * Print an AttributeList format of an mbean
71      * @param mbean - mbean to print
72      */

73     public void printMBean(AttributeList JavaDoc mbean) {
74         for (Iterator JavaDoc i=mbean.iterator(); i.hasNext();) {
75             Attribute JavaDoc attrib = (Attribute JavaDoc)i.next();
76             if (attrib.getValue() instanceof ObjectName JavaDoc) {
77                 printMBean((ObjectName JavaDoc)attrib.getValue());
78             } else if (attrib.getValue() instanceof ObjectInstance JavaDoc) {
79                 printMBean((ObjectInstance JavaDoc)attrib.getValue());
80             } else {
81                 out.println(attrib.getName() + " = " + attrib.getValue().toString());
82                 out.println();
83             }
84         }
85     }
86
87     /**
88      * Print a Map format of an mbean
89      * @param mbean - mbean to print
90      */

91     public void printMBean(Map JavaDoc mbean) {
92         for (Iterator JavaDoc i=mbean.keySet().iterator(); i.hasNext();) {
93             String JavaDoc key = (String JavaDoc)i.next();
94             String JavaDoc val = mbean.get(key).toString();
95             out.println(key + " = " + val);
96         }
97         out.println();
98     }
99
100     /**
101      * Print a collection of mbean
102      * @param mbean - collection of mbeans
103      */

104     public void printMBean(Collection JavaDoc mbean) {
105         for (Iterator JavaDoc i=mbean.iterator(); i.hasNext();) {
106             Object JavaDoc obj = i.next();
107             if (obj instanceof ObjectInstance JavaDoc) {
108                 printMBean((ObjectInstance JavaDoc)obj);
109             } else if (obj instanceof ObjectName JavaDoc) {
110                 printMBean((ObjectName JavaDoc)obj);
111             } else if (obj instanceof Map JavaDoc) {
112                 printMBean((Map JavaDoc)obj);
113             } else if (obj instanceof AttributeList JavaDoc) {
114                 printMBean((AttributeList JavaDoc)obj);
115             } else if (obj instanceof Collection JavaDoc) {
116                 printMessage((Collection JavaDoc)obj);
117             } else {
118                 printException(new UnsupportedOperationException JavaDoc("Unknown mbean type: " + obj.getClass().getName()));
119             }
120         }
121     }
122
123     /**
124      * Print a Map format of a JMS message
125      * @param msg
126      */

127     public void printMessage(Map JavaDoc msg) {
128         for (Iterator JavaDoc i=msg.keySet().iterator(); i.hasNext();) {
129             String JavaDoc key = (String JavaDoc)i.next();
130             String JavaDoc val = msg.get(key).toString();
131             out.println(key + " = " + val);
132         }
133         out.println();
134     }
135
136     /**
137      * Print a Message format of a JMS message
138      * @param msg - JMS message to print
139      */

140     public void printMessage(Message JavaDoc msg) {
141         // TODO
142
}
143
144     /**
145      * Print a collection of JMS messages
146      * @param msg - collection of JMS messages
147      */

148     public void printMessage(Collection JavaDoc msg) {
149         for (Iterator JavaDoc i=msg.iterator(); i.hasNext();) {
150             Object JavaDoc obj = i.next();
151             if (obj instanceof Message JavaDoc) {
152                 printMessage((Message JavaDoc)obj);
153             } else if (obj instanceof Map JavaDoc) {
154                 printMessage((Map JavaDoc)obj);
155             } else if (obj instanceof Collection JavaDoc) {
156                 printMessage((Collection JavaDoc)obj);
157             } else {
158                 printException(new UnsupportedOperationException JavaDoc("Unknown message type: " + obj.getClass().getName()));
159             }
160         }
161     }
162
163     /**
164      * Print help messages
165      * @param helpMsgs - help messages to print
166      */

167     public void printHelp(String JavaDoc[] helpMsgs) {
168         for (int i=0; i<helpMsgs.length; i++) {
169             out.println(helpMsgs[i]);
170         }
171         out.println();
172     }
173
174     /**
175      * Print an information message
176      * @param info - information message to print
177      */

178     public void printInfo(String JavaDoc info) {
179         out.println("INFO: " + info);
180     }
181
182     /**
183      * Print an exception message
184      * @param e - exception to print
185      */

186     public void printException(Exception JavaDoc e) {
187         out.println("ERROR: " + e);
188         e.printStackTrace(out);
189     }
190
191     /**
192      * Print a version information
193      * @param version - version info to print
194      */

195     public void printVersion(String JavaDoc version) {
196         out.println("");
197         out.println("ActiveMQ " + version);
198         out.println("For help or more information please see: http://www.logicblaze.com");
199         out.println("");
200     }
201
202     /**
203      * Print a generic key value mapping
204      * @param map to print
205      */

206     public void print(Map JavaDoc map) {
207         for (Iterator JavaDoc i=map.keySet().iterator(); i.hasNext();) {
208             String JavaDoc key = (String JavaDoc)i.next();
209             String JavaDoc val = map.get(key).toString();
210             out.println(key + " = " + val);
211         }
212         out.println();
213     }
214
215     /**
216      * Print a generic array of strings
217      * @param strings - string array to print
218      */

219     public void print(String JavaDoc[] strings) {
220         for (int i=0; i<strings.length; i++) {
221             out.println(strings[i]);
222         }
223         out.println();
224     }
225
226     /**
227      * Print a collection of objects
228      * @param collection - collection to print
229      */

230     public void print(Collection JavaDoc collection) {
231         for (Iterator JavaDoc i=collection.iterator(); i.hasNext();) {
232             out.println(i.next().toString());
233         }
234         out.println();
235     }
236
237     /**
238      * Print a java string
239      * @param string - string to print
240      */

241     public void print(String JavaDoc string) {
242         out.println(string);
243     }
244
245 }
246
Popular Tags