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: * @(#)TestComponentExtension.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 java.util.Map;
032: import java.util.Set;
033: import javax.management.remote.*;
034: import javax.management.*;
035:
036: /**
037: */
038: public class TestComponentExtension {
039:
040: private MBeanServerConnection mbns;
041: private static final String RESULT_PREFIX = "##### Result of ";
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 COMPONENT_NAME = "component.name";
048: private static final String CUSTOM_CONTROL_NAME = "custom.control.name";
049: private static final String LOGGER_NAME = "logger.name";
050:
051: public void initMBeanServerConnection() throws Exception {
052: java.util.Map<String, String[]> env = new java.util.HashMap();
053: String user = System.getProperty(USER);
054: String pass = System.getProperty(PASSWORD);
055: String[] credentials = new String[] { user, pass };
056: env.put("jmx.remote.credentials", credentials);
057:
058: String jmxProvider = System.getProperty(PROVIDER);
059:
060: JMXConnector connector = JMXConnectorFactory.connect(
061: new JMXServiceURL(jmxProvider), env);
062:
063: mbns = connector.getMBeanServerConnection();
064: }
065:
066: public ObjectName getComponentExtensionName() throws Exception {
067: String info = "com.sun.jbi:Target="
068: + System.getProperty(TARGET, "domain")
069: + ",ComponentName="
070: + System.getProperty(COMPONENT_NAME)
071: + ",ServiceType=Extension";
072: return new ObjectName(info);
073: }
074:
075: public void getCustomMBeanNames(String customName) throws Exception {
076: Map<String, ObjectName[]> objNameMap = (Map) mbns.invoke(
077: getComponentExtensionName(), "getCustomMBeanNames",
078: new Object[] { customName },
079: new String[] { "java.lang.String" });
080:
081: System.out.println(RESULT_PREFIX + " getCustomMBeanNames =");
082: Set<String> instances = objNameMap.keySet();
083: for (String instance : instances) {
084: System.out.println(" Custom MBeans on instance " + instance
085: + " : ");
086: ObjectName[] objNames = objNameMap.get(instance);
087: for (ObjectName mbnName : objNames) {
088: System.out.println(mbnName.toString());
089: }
090: }
091: }
092:
093: public void getComponentConfigurationFacadeMBeanName()
094: throws Exception {
095: ObjectName cfgMBean = (ObjectName) mbns.invoke(
096: getComponentExtensionName(),
097: "getComponentConfigurationFacadeMBeanName",
098: new Object[] { System.getProperty(TARGET) },
099: new String[] { "java.lang.String" });
100:
101: System.out.println(RESULT_PREFIX
102: + " getComponentConfigurationFacadeMBeanName = "
103: + cfgMBean);
104: }
105:
106: public void getLoggerMBeanNames(String logName) throws Exception {
107: Map<String, ObjectName[]> objNameMap = (Map) mbns.invoke(
108: getComponentExtensionName(), "getLoggerMBeanNames",
109: new Object[] { logName },
110: new String[] { "java.lang.String" });
111:
112: System.out.println(RESULT_PREFIX + " getLoggerMBeanNames =");
113: Set<String> instances = objNameMap.keySet();
114: for (String instance : instances) {
115: System.out.println(" Logger MBeans on instance " + instance
116: + " : ");
117: ObjectName[] objNames = objNameMap.get(instance);
118: for (ObjectName mbnName : objNames) {
119: System.out.println(mbnName.toString());
120: }
121: }
122: }
123:
124: public static void main(String[] params) throws Exception {
125: TestComponentExtension test = new TestComponentExtension();
126:
127: test.initMBeanServerConnection();
128:
129: String logger = System.getProperty(LOGGER_NAME, "$");
130: if (logger.startsWith("$")) {
131: test.getCustomMBeanNames(System
132: .getProperty(CUSTOM_CONTROL_NAME));
133: } else {
134: test.getLoggerMBeanNames(logger);
135: }
136: test.getComponentConfigurationFacadeMBeanName();
137: }
138: }
|