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: * @(#)TestComponentConfiguration.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.Properties;
033: import java.util.Set;
034: import javax.management.openmbean.*;
035: import javax.management.remote.*;
036: import javax.management.*;
037:
038: /**
039: */
040: public class TestComponentConfigMetaData {
041:
042: private MBeanServerConnection mbns;
043: private static final String RESULT_PREFIX = "##### Result of ";
044:
045: private static final String USER = "jmx.user";
046: private static final String PASSWORD = "jmx.password";
047: private static final String PROVIDER = "jmx.provider";
048: private static final String COMPONENT_NAME = "component.name";
049: private static final String TARGET = "target";
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 getJbiAdminCommandsUI() throws Exception {
067: String admin = "com.sun.jbi:"
068: + "ServiceName=JbiReferenceAdminUiService,ComponentType=System";
069: return new ObjectName(admin);
070: }
071:
072: public void retrieveConfigurationDisplayData(
073: boolean expectingException) throws Exception {
074:
075: String target = System.getProperty(TARGET);
076: Object[] params = new Object[2];
077: params[0] = System.getProperty(COMPONENT_NAME);
078: params[1] = target;
079:
080: String[] signature = new String[2];
081: signature[0] = "java.lang.String";
082: signature[1] = "java.lang.String";
083:
084: String data = null;
085:
086: try {
087: data = (String) mbns.invoke(getJbiAdminCommandsUI(),
088: "retrieveConfigurationDisplayData", params,
089: signature);
090:
091: System.out.println(RESULT_PREFIX
092: + " retrieveConfigurationDisplayData( target="
093: + target + " ):");
094: if (expectingException) {
095: System.out
096: .println("*** Was expecting exception, but didn't get one! ***");
097: } else {
098: System.out.print(" " + data);
099: }
100:
101: } catch (Exception e) {
102: if (expectingException) {
103: System.out.println(RESULT_PREFIX
104: + " retrieveConfigurationDisplayData( target="
105: + target + " ): caught expected exception.");
106: } else {
107: System.out.println(RESULT_PREFIX
108: + " retrieveConfigurationDisplayData( target="
109: + target + " ): caught unexpected Exception: ");
110: }
111: printExceptionMessage(e, "retrieveConfigurationDisplayData");
112: }
113: }
114:
115: public void retrieveConfigurationDisplaySchema(
116: boolean expectingException) throws Exception {
117:
118: String target = System.getProperty(TARGET);
119: Object[] params = new Object[2];
120: params[0] = System.getProperty(COMPONENT_NAME);
121: params[1] = target;
122:
123: String[] signature = new String[2];
124: signature[0] = "java.lang.String";
125: signature[1] = "java.lang.String";
126:
127: String schema = null;
128:
129: try {
130: schema = (String) mbns.invoke(getJbiAdminCommandsUI(),
131: "retrieveConfigurationDisplaySchema", params,
132: signature);
133:
134: System.out.println(RESULT_PREFIX
135: + " retrieveConfigurationDisplaySchema( target="
136: + target + " ):");
137: if (expectingException) {
138: System.out
139: .println("*** Was expecting exception, but didn't get one! ***");
140: } else {
141: System.out.print(" " + schema);
142: }
143: } catch (Exception e) {
144: if (expectingException) {
145: System.out
146: .println(RESULT_PREFIX
147: + " retrieveConfigurationDisplaySchema( target="
148: + target
149: + " ): caught expected exception.");
150: } else {
151: System.out
152: .println(RESULT_PREFIX
153: + " retrieveConfigurationDisplaySchema( target="
154: + target
155: + " ): caught unexpected exception");
156: }
157: printExceptionMessage(e,
158: "retrieveConfigurationDisplaySchema");
159: }
160: }
161:
162: public void retrieveAppConfigType(boolean expectingException)
163: throws Exception {
164:
165: String target = System.getProperty(TARGET);
166: Object[] params = new Object[2];
167: params[0] = System.getProperty(COMPONENT_NAME);
168: params[1] = target;
169:
170: String[] signature = new String[2];
171: signature[0] = "java.lang.String";
172: signature[1] = "java.lang.String";
173:
174: try {
175: CompositeType ct = (CompositeType) mbns.invoke(
176: getJbiAdminCommandsUI(),
177: "queryApplicationConfigurationType", params,
178: signature);
179:
180: System.out.println(RESULT_PREFIX
181: + " queryApplicationConfigurationType( target="
182: + target + " ):" + ct);
183: if (expectingException) {
184: System.out
185: .println("*** Was expecting exception, but didn't get one! ***");
186: }
187: } catch (Exception e) {
188: if (expectingException) {
189: System.out.println(RESULT_PREFIX
190: + " queryApplicationConfigurationType( target="
191: + target + " ): caught expected exception.");
192: } else {
193: System.out.println(RESULT_PREFIX
194: + " queryApplicationConfigurationType( target="
195: + target + " ): caught unexpected exception");
196: }
197: printExceptionMessage(e,
198: "queryApplicationConfigurationType");
199: }
200: }
201:
202: private void printExceptionMessage(Exception ex, String opName) {
203: Throwable cause = ex;
204:
205: while (cause instanceof javax.management.JMException
206: && cause != null) {
207: cause = cause.getCause();
208: }
209:
210: System.out.println("Exception " + cause.getClass()
211: + " thrown by " + opName + " with Message \n"
212: + cause.getMessage().replace("en_US", "en"));
213: }
214:
215: public static void main(String[] params) throws Exception {
216: // test setup
217: TestComponentConfigMetaData test = new TestComponentConfigMetaData();
218: test.initMBeanServerConnection();
219:
220: // test
221: test.retrieveConfigurationDisplayData(true);
222: test.retrieveConfigurationDisplaySchema(true);
223: test.retrieveAppConfigType(true);
224:
225: }
226: }
|