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 org.jboss.test.jbossmx.compliance.standard;
023:
024: import org.jboss.test.jbossmx.compliance.standard.support.Torture;
025:
026: import javax.management.MBeanInfo;
027: import javax.management.MBeanServer;
028: import javax.management.MBeanServerFactory;
029: import javax.management.ObjectName;
030: import javax.management.ObjectInstance;
031: import javax.management.MalformedObjectNameException;
032: import javax.management.InstanceAlreadyExistsException;
033: import javax.management.MBeanRegistrationException;
034: import javax.management.NotCompliantMBeanException;
035: import javax.management.InstanceNotFoundException;
036: import javax.management.IntrospectionException;
037: import javax.management.ReflectionException;
038: import javax.management.MBeanAttributeInfo;
039: import javax.management.MBeanConstructorInfo;
040: import javax.management.MBeanOperationInfo;
041: import javax.management.MBeanParameterInfo;
042:
043: import junit.framework.Assert;
044:
045: public class InfoUtil {
046: static org.apache.log4j.Category log = org.apache.log4j.Category
047: .getInstance(InfoUtil.class);
048:
049: public static MBeanInfo getMBeanInfo(Object mbean, String name) {
050: MBeanInfo info = null;
051:
052: try {
053: MBeanServer server = MBeanServerFactory.newMBeanServer();
054:
055: ObjectName objectName = new ObjectName(name);
056: ObjectInstance instance = server.registerMBean(mbean,
057: objectName);
058: info = server.getMBeanInfo(objectName);
059: } catch (MalformedObjectNameException e) {
060: Assert.fail("got spurious MalformedObjectNameException");
061: } catch (InstanceAlreadyExistsException e) {
062: Assert.fail("got spurious InstanceAlreadyExistsException");
063: } catch (MBeanRegistrationException e) {
064: Assert.fail("got spurious MBeanRegistrationException");
065: } catch (NotCompliantMBeanException e) {
066: Assert.fail("got spurious NotCompliantMBeanException");
067: } catch (InstanceNotFoundException e) {
068: Assert.fail("got spurious InstanceNotFoundException");
069: } catch (IntrospectionException e) {
070: Assert.fail("got spurious IntrospectionException");
071: } catch (ReflectionException e) {
072: Assert.fail("got spurious ReflectionException");
073: }
074:
075: return info;
076: }
077:
078: public static MBeanAttributeInfo findAttribute(
079: MBeanAttributeInfo[] attributes, String name) {
080: for (int i = 0; i < attributes.length; i++) {
081: if (attributes[i].getName().equals(name)) {
082: return attributes[i];
083: }
084: }
085: return null;
086: }
087:
088: public static void dumpConstructors(
089: MBeanConstructorInfo[] constructors) {
090: log.debug("");
091: log.debug("Constructors:");
092: for (int i = 0; i < constructors.length; i++) {
093: StringBuffer dump = new StringBuffer();
094: MBeanConstructorInfo constructor = constructors[i];
095: dump.append("name=").append(constructor.getName());
096: dump.append(",signature=").append(
097: makeSignatureString(constructor.getSignature()));
098:
099: log.debug(dump);
100: }
101: }
102:
103: public static void dumpAttributes(MBeanAttributeInfo[] attributes) {
104: log.debug("");
105: log.debug("Attributes:");
106: for (int i = 0; i < attributes.length; i++) {
107: StringBuffer dump = new StringBuffer();
108: MBeanAttributeInfo attribute = attributes[i];
109: dump.append("name=").append(attribute.getName());
110: dump.append(",type=").append(attribute.getType());
111: dump.append(",readable=").append(attribute.isReadable());
112: dump.append(",writable=").append(attribute.isWritable());
113: dump.append(",isIS=").append(attribute.isIs());
114: log.debug(dump);
115: }
116: }
117:
118: public static void dumpOperations(MBeanOperationInfo[] operations) {
119: log.debug("");
120: log.debug("Operations:");
121: for (int i = 0; i < operations.length; i++) {
122: StringBuffer dump = new StringBuffer();
123: MBeanOperationInfo operation = operations[i];
124: dump.append("name=").append(operation.getName());
125: dump.append(",impact=").append(
126: decodeImpact(operation.getImpact()));
127: dump.append(",returnType=").append(
128: operation.getReturnType());
129: dump.append(",signature=").append(
130: makeSignatureString(operation.getSignature()));
131:
132: log.debug(dump);
133: }
134: }
135:
136: public static String makeSignatureString(MBeanParameterInfo[] info) {
137: String[] sig = new String[info.length];
138: for (int i = 0; i < info.length; i++) {
139: sig[i] = info[i].getType();
140: }
141: return makeSignatureString(sig);
142: }
143:
144: public static String makeSignatureString(String[] sig) {
145: StringBuffer buf = new StringBuffer("(");
146: for (int i = 0; i < sig.length; i++) {
147: buf.append(sig[i]);
148: if (i != sig.length - 1) {
149: buf.append(",");
150: }
151: }
152: buf.append(")");
153: return buf.toString();
154: }
155:
156: public static String decodeImpact(int impact) {
157: switch (impact) {
158: case MBeanOperationInfo.ACTION:
159: return "ACTION";
160: case MBeanOperationInfo.ACTION_INFO:
161: return "ACTION_INFO";
162: case MBeanOperationInfo.INFO:
163: return "INFO";
164: case MBeanOperationInfo.UNKNOWN:
165: return "UNKNOWN";
166: }
167: throw new IllegalArgumentException("unknown impact value:"
168: + impact);
169: }
170: }
|