001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.compliance.standard;
023:
024: import javax.management.InstanceAlreadyExistsException;
025: import javax.management.InstanceNotFoundException;
026: import javax.management.IntrospectionException;
027: import javax.management.MBeanAttributeInfo;
028: import javax.management.MBeanConstructorInfo;
029: import javax.management.MBeanInfo;
030: import javax.management.MBeanOperationInfo;
031: import javax.management.MBeanParameterInfo;
032: import javax.management.MBeanRegistrationException;
033: import javax.management.MBeanServer;
034: import javax.management.MBeanServerFactory;
035: import javax.management.MalformedObjectNameException;
036: import javax.management.NotCompliantMBeanException;
037: import javax.management.ObjectInstance;
038: import javax.management.ObjectName;
039: import javax.management.ReflectionException;
040:
041: import junit.framework.Assert;
042:
043: public class InfoUtil {
044: public static MBeanInfo getMBeanInfo(Object mbean, String name) {
045: MBeanInfo info = null;
046:
047: try {
048: MBeanServer server = MBeanServerFactory.newMBeanServer();
049:
050: ObjectName objectName = new ObjectName(name);
051: ObjectInstance instance = server.registerMBean(mbean,
052: objectName);
053: info = server.getMBeanInfo(objectName);
054: } catch (MalformedObjectNameException e) {
055: Assert.fail("got spurious MalformedObjectNameException");
056: } catch (InstanceAlreadyExistsException e) {
057: Assert.fail("got spurious InstanceAlreadyExistsException");
058: } catch (MBeanRegistrationException e) {
059: Assert.fail("got spurious MBeanRegistrationException");
060: } catch (NotCompliantMBeanException e) {
061: Assert.fail("got spurious NotCompliantMBeanException");
062: } catch (InstanceNotFoundException e) {
063: Assert.fail("got spurious InstanceNotFoundException");
064: } catch (IntrospectionException e) {
065: Assert.fail("got spurious IntrospectionException");
066: } catch (ReflectionException e) {
067: Assert.fail("got spurious ReflectionException");
068: }
069:
070: return info;
071: }
072:
073: public static MBeanAttributeInfo findAttribute(
074: MBeanAttributeInfo[] attributes, String name) {
075: for (int i = 0; i < attributes.length; i++) {
076: if (attributes[i].getName().equals(name)) {
077: return attributes[i];
078: }
079: }
080: return null;
081: }
082:
083: public static void dumpConstructors(
084: MBeanConstructorInfo[] constructors) {
085: System.out.println("");
086: System.out.println("Constructors:");
087: for (int i = 0; i < constructors.length; i++) {
088: StringBuffer dump = new StringBuffer();
089: MBeanConstructorInfo constructor = constructors[i];
090: dump.append("name=").append(constructor.getName());
091: dump.append(",signature=").append(
092: makeSignatureString(constructor.getSignature()));
093:
094: System.out.println(dump);
095: }
096: }
097:
098: public static void dumpAttributes(MBeanAttributeInfo[] attributes) {
099: System.out.println("");
100: System.out.println("Attributes:");
101: for (int i = 0; i < attributes.length; i++) {
102: StringBuffer dump = new StringBuffer();
103: MBeanAttributeInfo attribute = attributes[i];
104: dump.append("name=").append(attribute.getName());
105: dump.append(",type=").append(attribute.getType());
106: dump.append(",readable=").append(attribute.isReadable());
107: dump.append(",writable=").append(attribute.isWritable());
108: dump.append(",isIS=").append(attribute.isIs());
109: System.out.println(dump);
110: }
111: }
112:
113: public static void dumpOperations(MBeanOperationInfo[] operations) {
114: System.out.println("");
115: System.out.println("Operations:");
116: for (int i = 0; i < operations.length; i++) {
117: StringBuffer dump = new StringBuffer();
118: MBeanOperationInfo operation = operations[i];
119: dump.append("name=").append(operation.getName());
120: dump.append(",impact=").append(
121: decodeImpact(operation.getImpact()));
122: dump.append(",returnType=").append(
123: operation.getReturnType());
124: dump.append(",signature=").append(
125: makeSignatureString(operation.getSignature()));
126:
127: System.out.println(dump);
128: }
129: }
130:
131: public static String makeSignatureString(MBeanParameterInfo[] info) {
132: String[] sig = new String[info.length];
133: for (int i = 0; i < info.length; i++) {
134: sig[i] = info[i].getType();
135: }
136: return makeSignatureString(sig);
137: }
138:
139: public static String makeSignatureString(String[] sig) {
140: StringBuffer buf = new StringBuffer("(");
141: for (int i = 0; i < sig.length; i++) {
142: buf.append(sig[i]);
143: if (i != sig.length - 1) {
144: buf.append(",");
145: }
146: }
147: buf.append(")");
148: return buf.toString();
149: }
150:
151: public static String decodeImpact(int impact) {
152: switch (impact) {
153: case MBeanOperationInfo.ACTION:
154: return "ACTION";
155: case MBeanOperationInfo.ACTION_INFO:
156: return "ACTION_INFO";
157: case MBeanOperationInfo.INFO:
158: return "INFO";
159: case MBeanOperationInfo.UNKNOWN:
160: return "UNKNOWN";
161: }
162: throw new IllegalArgumentException("unknown impact value:"
163: + impact);
164: }
165: }
|