KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > jmx > ShowMBeanTask


1 package org.apache.tools.ant.taskdefs.optional.jmx;
2
3 /*
4  * ============================================================================
5  * The Apache Software License, Version 1.1
6  * ============================================================================
7  *
8  * Copyright (C) 2000-2002 The Apache Software Foundation. All
9  * rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modifica-
12  * tion, are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any, must
22  * include the following acknowledgment: "This product includes software
23  * developed by the Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself, if
25  * and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Ant" and "Apache Software Foundation" must not be used to
28  * endorse or promote products derived from this software without prior
29  * written permission. For written permission, please contact
30  * apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache", nor may
33  * "Apache" appear in their name, without prior written permission of the
34  * Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
37  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
38  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
39  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
40  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
41  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
42  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * This software consists of voluntary contributions made by many individuals
48  * on behalf of the Apache Software Foundation. For more information on the
49  * Apache Software Foundation, please see <http://www.apache.org/>.
50  *
51  */

52
53
54 import javax.management.MBeanParameterInfo JavaDoc;
55 import org.apache.tools.ant.BuildException;
56
57
58
59 /**
60  * This is an Ant task that writes the definition (structure) of a JMX mbean and all of its
61  * current attributes values to the Ant log file.
62  * </br></br>
63  *
64  * Refer to the user documentation for more information and examples on how to use
65  * this task.
66  *
67  * @author <a HREF="mailto:bdueck@yahoo.com">Brian Dueck</a>
68  * @version $Id: ShowMBeanTask.java,v 1.5 2003/05/28 22:28:26 bdueck Exp $
69  *
70  */

71 public class ShowMBeanTask extends AbstractMBeanTask {
72             
73
74     /**
75      * Size of the Name column for attributes.
76      */

77     private static final int ATTR_NAME_COL_LENGTH = 35;
78     
79     /**
80      * Size of the Type column for attributes.
81      */

82     private static final int ATTR_TYPE_COL_LENGTH = 40;
83
84     /**
85      * Size of the Permission column for attributes.
86      */

87     private static final int ATTR_PERM_COL_LENGTH = 6;
88         
89     /**
90      * Size of the Value column for attributes.
91      */

92     private static final int ATTR_VALUE_COL_LENGTH = 20;
93     
94     /**
95      * Process all nested <copyMBeans> elements
96      *
97      * @param mbserver The MBeanServer that hosts the mbean.
98      * @throws BuildException When an error occurs.
99      */

100     protected void execute(javax.management.MBeanServer JavaDoc mbserver)
101         throws BuildException {
102             
103         try {
104             javax.management.ObjectName JavaDoc mbeanName = getObjectName();
105
106             if (mbserver.isRegistered(mbeanName)) {
107                 
108                 try {
109
110                     javax.management.MBeanInfo JavaDoc mbeanInfo = mbserver.getMBeanInfo(mbeanName);
111
112                     if (mbeanInfo != null) {
113                         log("Description of MBean [" + mbeanName+ "]");
114                         
115                         showAttributes(mbserver,mbeanInfo);
116                         showOperations(mbserver,mbeanInfo);
117                         showConstructors(mbserver,mbeanInfo);
118
119                     } else {
120                         log("Warning! MBeanInfo was null for mbean at " + toString(),org.apache.tools.ant.Project.MSG_WARN);
121                     }
122
123                 } catch (Exception JavaDoc x) {
124                     String JavaDoc message = "Cannot show mbean " + mbeanName;
125                     throw new BuildException(message);
126                 }
127
128             } else {
129                 throw new BuildException("Cannot find MBean. " + mbeanName.getCanonicalName());
130             }
131             
132         } catch (javax.management.MalformedObjectNameException JavaDoc x) {
133             x.printStackTrace();
134             throw new BuildException(x);
135         }
136     }
137
138     private static void repeat(StringBuffer JavaDoc buffer, char toRepeat, int repeatTimes) {
139         for (int counter = 0; counter < repeatTimes; counter++) {
140             buffer.append(toRepeat);
141         }
142     }
143     
144     private static void pad(StringBuffer JavaDoc buffer, String JavaDoc toPad, int length) {
145         int diff = length - toPad.length();
146         if (diff <= 0) {
147             buffer.append(toPad.substring(0,length));
148         } else {
149             buffer.append(toPad);
150             repeat(buffer,' ',diff);
151         }
152     }
153     
154     protected void showAttributes(javax.management.MBeanServer JavaDoc mbserver,
155         javax.management.MBeanInfo JavaDoc mbeanInfo)
156         throws javax.management.MalformedObjectNameException JavaDoc,
157                 javax.management.MBeanException JavaDoc,
158                 javax.management.AttributeNotFoundException JavaDoc,
159                 javax.management.InstanceNotFoundException JavaDoc,
160                 javax.management.ReflectionException JavaDoc
161     {
162               
163         log("\nList of attributes for MBean [" + getObjectName() + "]");
164         StringBuffer JavaDoc header = new StringBuffer JavaDoc();
165         pad(header,"Name",ATTR_NAME_COL_LENGTH+1);
166         pad(header,"Type",ATTR_TYPE_COL_LENGTH+1);
167         pad(header,"Access",ATTR_PERM_COL_LENGTH+1);
168         header.append("Value");
169         log(header.toString());
170         
171         header = new StringBuffer JavaDoc();
172         repeat(header,'-',ATTR_NAME_COL_LENGTH);
173         header.append(" ");
174         repeat(header,'-',ATTR_TYPE_COL_LENGTH);
175         header.append(" ");
176         repeat(header,'-',ATTR_PERM_COL_LENGTH);
177         header.append(" ");
178         repeat(header,'-',ATTR_VALUE_COL_LENGTH);
179         log(header.toString());
180                 
181         javax.management.MBeanAttributeInfo JavaDoc[] info = mbeanInfo.getAttributes();
182         if (info != null) {
183             // iterate through the array of
184
// MBeanAttributeInfo's and print basic information
185
// about each attribute to the log
186
//
187
for (int counter = 0; counter < info.length; counter++) {
188                 // don't attempt to get the value
189
// for non-readable attributes
190
//
191
String JavaDoc attribValueAsString = "";
192                 if (info[counter].isReadable()) {
193                     Object JavaDoc attribValue = mbserver.getAttribute(getObjectName(),info[counter].getName());
194                     attribValueAsString = org.apache.tools.ant.taskdefs.optional.jmx.converter.ValueFactory.toString(attribValue);
195                 }
196                 StringBuffer JavaDoc message = new StringBuffer JavaDoc();
197                 
198                 pad(message,info[counter].getName(),ATTR_NAME_COL_LENGTH);
199                 message.append(" ");
200                 pad(message,info[counter].getType(),ATTR_TYPE_COL_LENGTH);
201                 message.append(" ");
202                 if (info[counter].isReadable() && info[counter].isWritable()) {
203                     pad(message,"RW",ATTR_PERM_COL_LENGTH);
204                 } else if (info[counter].isReadable()) {
205                     pad(message,"R-",ATTR_PERM_COL_LENGTH);
206                 } else if (info[counter].isWritable()) {
207                     pad(message,"-W",ATTR_PERM_COL_LENGTH);
208                 } else {
209                     pad(message,"--",ATTR_PERM_COL_LENGTH);
210                 }
211                 message.append(" ");
212                 message.append(attribValueAsString != null ? attribValueAsString : "null");
213                 log(message.toString());
214             }
215
216         }
217     }
218
219     protected void showOperations(javax.management.MBeanServer JavaDoc mbserver,
220         javax.management.MBeanInfo JavaDoc mbeanInfo)
221         throws javax.management.MalformedObjectNameException JavaDoc {
222             
223         log("\nList of operations for MBean [" + getObjectName() + "]");
224         javax.management.MBeanOperationInfo JavaDoc[] info = mbeanInfo.getOperations();
225         if (info != null) {
226             // iterate through the array of
227
// MBeanOperationInfo's and print basic information
228
// about each operation to the log
229
//
230
for (int counter = 0; counter < info.length; counter++) {
231                 StringBuffer JavaDoc opDesc = new StringBuffer JavaDoc();
232                                 
233                 opDesc.append(info[counter].getReturnType() + " " + info[counter].getName() + "(");
234                 MBeanParameterInfo JavaDoc[] paramInfo = info[counter].getSignature();
235                 for (int paramCounter = 0; paramCounter < paramInfo.length; paramCounter++) {
236                     opDesc.append(paramInfo[paramCounter].getType());
237                     if ((paramInfo[paramCounter].getName() == null) || (paramInfo[paramCounter].getName().length() == 0)
238                         || (paramInfo[paramCounter].getType() == paramInfo[paramCounter].getName())) {
239                         opDesc.append(" arg" + paramCounter);
240                     } else {
241                         opDesc.append(" " + paramInfo[paramCounter].getName());
242                     }
243                     if ((paramCounter+1) < paramInfo.length) {
244                         opDesc.append(", ");
245                     }
246                 }
247                 opDesc.append(");");
248                 log("\t"+opDesc.toString());
249             }
250         }
251     }
252     
253     protected void showConstructors(javax.management.MBeanServer JavaDoc mbserver,
254         javax.management.MBeanInfo JavaDoc mbeanInfo)
255         throws javax.management.MalformedObjectNameException JavaDoc {
256             
257         log("\nList of constructors for MBean [" + getObjectName() + "]");
258         javax.management.MBeanConstructorInfo JavaDoc[] info = mbeanInfo.getConstructors();
259         if (info != null) {
260             // iterate through the array of
261
// MBeanConstructorInfo's and print basic information
262
// about each operation to the log
263
//
264
for (int counter = 0; counter < info.length; counter++) {
265                 StringBuffer JavaDoc opDesc = new StringBuffer JavaDoc();
266                                 
267                 opDesc.append(info[counter].getName() + "(");
268                 MBeanParameterInfo JavaDoc[] paramInfo = info[counter].getSignature();
269                 for (int paramCounter = 0; paramCounter < paramInfo.length; paramCounter++) {
270                     opDesc.append(paramInfo[paramCounter].getType());
271                     if ((paramInfo[paramCounter].getName() == null) || (paramInfo[paramCounter].getName().length() == 0)
272                         || (paramInfo[paramCounter].getType() == paramInfo[paramCounter].getName())) {
273                         opDesc.append(" arg" + paramCounter);
274                     } else {
275                         opDesc.append(" " + paramInfo[paramCounter].getName());
276                     }
277                     if ((paramCounter+1) < paramInfo.length) {
278                         opDesc.append(", ");
279                     }
280                 }
281                 opDesc.append(");");
282                 log("\t"+opDesc.toString());
283             }
284         }
285     }
286     
287     
288 }
289
290 /*
291  * $Log: ShowMBeanTask.java,v $
292  * Revision 1.5 2003/05/28 22:28:26 bdueck
293  * *** empty log message ***
294  *
295  * Revision 1.4 2003/05/26 10:41:37 bdueck
296  * Renamed "Perm" column to "Access".
297  *
298  * Revision 1.3 2003/04/21 15:29:41 bdueck
299  * Various changes in preparation for version 1.2.
300  *
301  *
302  */
Popular Tags