KickJava   Java API By Example, From Geeks To Geeks.

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