001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestRuntimeConfiguration.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package java4ant;
030:
031: import javax.management.modelmbean.*;
032: import javax.management.remote.*;
033: import javax.management.*;
034:
035: /**
036: * TestRuntimeConfiguration gets the MBeanInfo of a runtime configuration MBean.
037: * This gives details on the attributes supported by the MBean and it's meta-data.
038: */
039: public class TestRuntimeConfiguration {
040:
041: private MBeanServerConnection mbns;
042:
043: private static final String USER = "jmx.user";
044: private static final String PASSWORD = "jmx.password";
045: private static final String PROVIDER = "jmx.provider";
046: private static final String TARGET = "target";
047: private static final String CATEGORY = "category";
048:
049: public void initMBeanServerConnection() throws Exception {
050: java.util.Map<String, String[]> env = new java.util.HashMap();
051: String user = System.getProperty(USER);
052: String pass = System.getProperty(PASSWORD);
053: String[] credentials = new String[] { user, pass };
054: env.put("jmx.remote.credentials", credentials);
055:
056: String jmxProvider = System.getProperty(PROVIDER);
057:
058: JMXConnector connector = JMXConnectorFactory.connect(
059: new JMXServiceURL(jmxProvider), env);
060:
061: mbns = connector.getMBeanServerConnection();
062: }
063:
064: public ObjectName getRuntimeConfigMBeanName() throws Exception {
065: String target = System.getProperty(TARGET);
066: String category = System.getProperty(CATEGORY);
067: String rtConfig = "com.sun.jbi:" + "Target=" + target
068: + ",ServiceName=ConfigurationService" + ",ServiceType="
069: + category;
070:
071: return new ObjectName(rtConfig);
072: }
073:
074: public void getMBeanInfo() throws Exception {
075: MBeanInfo mbeanInfo = mbns
076: .getMBeanInfo(getRuntimeConfigMBeanName());
077:
078: if (mbeanInfo != null) {
079: System.out.println("Attribute Information for MBean "
080: + getRuntimeConfigMBeanName());
081:
082: MBeanAttributeInfo[] attribs = mbeanInfo.getAttributes();
083:
084: for (MBeanAttributeInfo attrib : attribs) {
085: System.out.println("\nName : " + attrib.getName());
086: System.out.println("Description : "
087: + attrib.getDescription());
088:
089: if (attrib instanceof ModelMBeanAttributeInfo) {
090: Descriptor descr = ((ModelMBeanAttributeInfo) attrib)
091: .getDescriptor();
092: String[] fields = descr.getFieldNames();
093: for (String field : fields) {
094: System.out.println(" " + field + " = "
095: + descr.getFieldValue(field));
096: }
097: }
098: }
099: }
100:
101: }
102:
103: public static void main(String[] params) throws Exception {
104: TestRuntimeConfiguration test = new TestRuntimeConfiguration();
105:
106: test.initMBeanServerConnection();
107: test.getMBeanInfo();
108: }
109:
110: }
|