KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > standard > InfoUtil


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jmx.compliance.standard;
23
24 import javax.management.InstanceAlreadyExistsException JavaDoc;
25 import javax.management.InstanceNotFoundException JavaDoc;
26 import javax.management.IntrospectionException JavaDoc;
27 import javax.management.MBeanAttributeInfo JavaDoc;
28 import javax.management.MBeanConstructorInfo JavaDoc;
29 import javax.management.MBeanInfo JavaDoc;
30 import javax.management.MBeanOperationInfo JavaDoc;
31 import javax.management.MBeanParameterInfo JavaDoc;
32 import javax.management.MBeanRegistrationException JavaDoc;
33 import javax.management.MBeanServer JavaDoc;
34 import javax.management.MBeanServerFactory JavaDoc;
35 import javax.management.MalformedObjectNameException JavaDoc;
36 import javax.management.NotCompliantMBeanException JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.ReflectionException JavaDoc;
39
40 import junit.framework.Assert;
41
42 public class InfoUtil
43 {
44    public static MBeanInfo JavaDoc getMBeanInfo(Object JavaDoc mbean, String JavaDoc name)
45    {
46       MBeanInfo JavaDoc info = null;
47
48       try
49       {
50          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
51
52          ObjectName JavaDoc objectName = new ObjectName JavaDoc(name);
53          server.registerMBean(mbean, objectName);
54          info = server.getMBeanInfo(objectName);
55       }
56       catch (MalformedObjectNameException JavaDoc e)
57       {
58          Assert.fail("got spurious MalformedObjectNameException");
59       }
60       catch (InstanceAlreadyExistsException JavaDoc e)
61       {
62          Assert.fail("got spurious InstanceAlreadyExistsException");
63       }
64       catch (MBeanRegistrationException JavaDoc e)
65       {
66          Assert.fail("got spurious MBeanRegistrationException");
67       }
68       catch (NotCompliantMBeanException JavaDoc e)
69       {
70          Assert.fail("got spurious NotCompliantMBeanException");
71       }
72       catch (InstanceNotFoundException JavaDoc e)
73       {
74          Assert.fail("got spurious InstanceNotFoundException");
75       }
76       catch (IntrospectionException JavaDoc e)
77       {
78          Assert.fail("got spurious IntrospectionException");
79       }
80       catch (ReflectionException JavaDoc e)
81       {
82          Assert.fail("got spurious ReflectionException");
83       }
84
85       return info;
86    }
87
88    public static MBeanAttributeInfo JavaDoc findAttribute(MBeanAttributeInfo JavaDoc[] attributes, String JavaDoc name)
89    {
90       for (int i = 0; i < attributes.length; i++)
91       {
92          if (attributes[i].getName().equals(name))
93          {
94             return attributes[i];
95          }
96       }
97       return null;
98    }
99
100    public static void dumpConstructors(MBeanConstructorInfo JavaDoc[] constructors)
101    {
102       System.out.println("");
103       System.out.println("Constructors:");
104       for (int i = 0; i < constructors.length; i++)
105       {
106          StringBuffer JavaDoc dump = new StringBuffer JavaDoc();
107          MBeanConstructorInfo JavaDoc constructor = constructors[i];
108          dump.append("name=").append(constructor.getName());
109          dump.append(",signature=").append(makeSignatureString(constructor.getSignature()));
110
111          System.out.println(dump);
112       }
113    }
114
115    public static void dumpAttributes(MBeanAttributeInfo JavaDoc[] attributes)
116    {
117       System.out.println("");
118       System.out.println("Attributes:");
119       for (int i = 0; i < attributes.length; i++)
120       {
121          StringBuffer JavaDoc dump = new StringBuffer JavaDoc();
122          MBeanAttributeInfo JavaDoc attribute = attributes[i];
123          dump.append("name=").append(attribute.getName());
124          dump.append(",type=").append(attribute.getType());
125          dump.append(",readable=").append(attribute.isReadable());
126          dump.append(",writable=").append(attribute.isWritable());
127          dump.append(",isIS=").append(attribute.isIs());
128          System.out.println(dump);
129       }
130    }
131
132    public static void dumpOperations(MBeanOperationInfo JavaDoc[] operations)
133    {
134       System.out.println("");
135       System.out.println("Operations:");
136       for (int i = 0; i < operations.length; i++)
137       {
138          StringBuffer JavaDoc dump = new StringBuffer JavaDoc();
139          MBeanOperationInfo JavaDoc operation = operations[i];
140          dump.append("name=").append(operation.getName());
141          dump.append(",impact=").append(decodeImpact(operation.getImpact()));
142          dump.append(",returnType=").append(operation.getReturnType());
143          dump.append(",signature=").append(makeSignatureString(operation.getSignature()));
144
145          System.out.println(dump);
146       }
147    }
148
149    public static String JavaDoc makeSignatureString(MBeanParameterInfo JavaDoc[] info)
150    {
151       String JavaDoc[] sig = new String JavaDoc[info.length];
152       for (int i = 0; i < info.length; i++)
153       {
154          sig[i] = info[i].getType();
155       }
156       return makeSignatureString(sig);
157    }
158
159    public static String JavaDoc makeSignatureString(String JavaDoc[] sig)
160    {
161       StringBuffer JavaDoc buf = new StringBuffer JavaDoc("(");
162       for (int i = 0; i < sig.length; i++)
163       {
164          buf.append(sig[i]);
165          if (i != sig.length - 1)
166          {
167             buf.append(",");
168          }
169       }
170       buf.append(")");
171       return buf.toString();
172    }
173
174    public static String JavaDoc decodeImpact(int impact)
175    {
176       switch (impact)
177       {
178          case MBeanOperationInfo.ACTION:
179             return "ACTION";
180          case MBeanOperationInfo.ACTION_INFO:
181             return "ACTION_INFO";
182          case MBeanOperationInfo.INFO:
183             return "INFO";
184          case MBeanOperationInfo.UNKNOWN:
185             return "UNKNOWN";
186       }
187       throw new IllegalArgumentException JavaDoc("unknown impact value:" + impact);
188    }
189 }
190
Popular Tags