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