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: * @(#)TestVerifier.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.Iterator;
032: import java.util.Set;
033:
034: import javax.management.MBeanServerConnection;
035: import javax.management.remote.JMXConnector;
036: import javax.management.remote.JMXConnectorFactory;
037: import javax.management.remote.JMXServiceURL;
038: import javax.management.ObjectName;
039:
040: import javax.management.openmbean.ArrayType;
041: import javax.management.openmbean.CompositeData;
042: import javax.management.openmbean.CompositeDataSupport;
043: import javax.management.openmbean.TabularData;
044: import javax.management.openmbean.CompositeType;
045: import javax.management.openmbean.OpenType;
046: import javax.management.openmbean.OpenDataException;
047: import javax.management.openmbean.SimpleType;
048:
049: /**
050: * This class is used to test Application Verifier.
051: */
052: public class TestStats {
053: /** MBean Server Connection */
054: private MBeanServerConnection mbns;
055:
056: /** Result Prefix */
057: private static final String RESULT_PREFIX = "##### Result of ";
058:
059: /** system properties used by this class */
060: private static final String USER = "jmx.user";
061: private static final String PASSWORD = "jmx.password";
062: private static final String PROVIDER = "jmx.provider";
063:
064: private static final String TARGET = "target";
065:
066: /**
067: * Initialize connection to MBean Server
068: */
069: public void initMBeanServerConnection() throws Exception {
070: java.util.Map<String, String[]> env = new java.util.HashMap();
071: String user = System.getProperty(USER);
072: String pass = System.getProperty(PASSWORD);
073: String[] credentials = new String[] { user, pass };
074: env.put("jmx.remote.credentials", credentials);
075:
076: String jmxProvider = System.getProperty(PROVIDER);
077:
078: JMXConnector connector = JMXConnectorFactory.connect(
079: new JMXServiceURL(jmxProvider), env);
080:
081: mbns = connector.getMBeanServerConnection();
082: }
083:
084: /**
085: * get stats
086: */
087: public TabularData getFrameworkStats(String target)
088: throws Exception {
089: String info = "com.sun.jbi:ServiceName=JbiReferenceAdminUiService,ComponentType=System";
090: return (TabularData) mbns.invoke(new ObjectName(info),
091: "getFrameworkStats", new Object[] { target },
092: new String[] { "java.lang.String" });
093: }
094:
095: /**
096: * get stats
097: */
098: public TabularData getNMRStats(String target) throws Exception {
099: String info = "com.sun.jbi:ServiceName=JbiReferenceAdminUiService,ComponentType=System";
100: return (TabularData) mbns.invoke(new ObjectName(info),
101: "getNMRStats", new Object[] { target },
102: new String[] { "java.lang.String" });
103: }
104:
105: /**
106: * get stats
107: */
108: public TabularData getComponentStats(String target,
109: String componentName) throws Exception {
110: String info = "com.sun.jbi:ServiceName=JbiReferenceAdminUiService,ComponentType=System";
111: return (TabularData) mbns.invoke(new ObjectName(info),
112: "getComponentStats", new Object[] { componentName,
113: target }, new String[] { "java.lang.String",
114: "java.lang.String" });
115: }
116:
117: /**
118: * get stats
119: */
120: public TabularData getEndpointStats(String target,
121: String endpointName) throws Exception {
122: String info = "com.sun.jbi:ServiceName=JbiReferenceAdminUiService,ComponentType=System";
123: return (TabularData) mbns.invoke(new ObjectName(info),
124: "getEndpointStats",
125: new Object[] { endpointName, target }, new String[] {
126: "java.lang.String", "java.lang.String" });
127: }
128:
129: /**
130: * get stats
131: */
132: public TabularData getSAStats(String target, String saName)
133: throws Exception {
134: String info = "com.sun.jbi:ServiceName=JbiReferenceAdminUiService,ComponentType=System";
135: return (TabularData) mbns.invoke(new ObjectName(info),
136: "getServiceAssemblyStats", new Object[] { saName,
137: target }, new String[] { "java.lang.String",
138: "java.lang.String" });
139: }
140:
141: /**
142: * parse and print the report
143: */
144: public void printCompositeData(TabularData statsReport) {
145: Set names = statsReport.keySet();
146: for (Object name : names) {
147: Object[] key = ((java.util.List) name).toArray();
148: CompositeData compData = statsReport.get(key);
149: System.out.println();
150: printCompositeData(compData, 1);
151: }
152: }
153:
154: protected void printCompositeData(CompositeData compData,
155: int recursiveLevel) {
156:
157: String tabT = "";
158: for (int i = 0; i < recursiveLevel; ++i) {
159: tabT = tabT + "\t";
160: }
161: recursiveLevel++;
162:
163: CompositeType compType = compData.getCompositeType();
164: Iterator itr = compType.keySet().iterator();
165: while (itr.hasNext()) {
166: String keyName = (String) itr.next();
167: OpenType openType = compType.getType(keyName);
168: Object itemValue = compData.get(keyName);
169: String className = null;
170: if (itemValue != null) {
171: className = itemValue.getClass().getName();
172: }
173:
174: if (openType.isArray()) {
175: System.out.println(tabT + keyName + ":");
176: if ((openType.getTypeName() != null)
177: && (openType.getTypeName().compareTo(
178: "[Ljava.lang.String;") == 0)) {
179: String[] strArray = (String[]) compData
180: .get(keyName);
181: for (int i = 0; i < strArray.length; ++i) {
182: System.out.println(tabT + "\t" + strArray[i]);
183: }
184: } else if ((openType.getTypeName() != null)
185: && (openType
186: .getTypeName()
187: .compareTo(
188: "[Ljavax.management.openmbean.CompositeData;") == 0)) {
189: CompositeData[] compDataArray = (CompositeData[]) compData
190: .get(keyName);
191: for (int i = 0; i < compDataArray.length; ++i) {
192: printCompositeData(compDataArray[i],
193: recursiveLevel);
194: }
195: }
196:
197: } else {
198:
199: if (className != null
200: && (className
201: .equals("javax.management.openmbean.CompositeDataSupport") || className
202: .equals("javax.management.openmbean.CompositeData"))) {
203: printCompositeData((CompositeData) compData
204: .get(keyName), recursiveLevel);
205: } else if (className != null
206: && (className
207: .equals("javax.management.openmbean.TabularDataSupport") || className
208: .equals("javax.management.openmbean.TabularData"))) {
209: printCompositeData((TabularData) compData
210: .get(keyName));
211: } else {
212: System.out.println(tabT + keyName + "="
213: + compData.get(keyName));
214: }
215: }
216: }
217:
218: }
219:
220: /**
221: * get stats
222: */
223: public TabularData getConsumingEndpointsForComponent(String target,
224: String componentName) throws Exception {
225: String info = "com.sun.jbi:ServiceName=JbiReferenceAdminUiService,ComponentType=System";
226: return (TabularData) mbns.invoke(new ObjectName(info),
227: "getConsumingEndpointsForComponent", new Object[] {
228: componentName, target }, new String[] {
229: "java.lang.String", "java.lang.String" });
230: }
231:
232: /**
233: * get stats
234: */
235: public TabularData getProvidingEndpointsForComponent(String target,
236: String componentName) throws Exception {
237: String info = "com.sun.jbi:ServiceName=JbiReferenceAdminUiService,ComponentType=System";
238: return (TabularData) mbns.invoke(new ObjectName(info),
239: "getProvidingEndpointsForComponent", new Object[] {
240: componentName, target }, new String[] {
241: "java.lang.String", "java.lang.String" });
242: }
243:
244: /**
245: * The main method
246: */
247: public static void main(String[] params) throws Exception {
248: if (params.length == 0) {
249: throw new Exception(
250: "Usage: TestVerifier Framework|NMR|SA|Component|Endpoint");
251: }
252: try {
253:
254: String target = System.getProperty(TARGET);
255:
256: TestStats testStats = new TestStats();
257: testStats.initMBeanServerConnection();
258:
259: if (params[0].equals("Framework")) {
260: TabularData frameworkStats = testStats
261: .getFrameworkStats(target);
262: testStats.printCompositeData(frameworkStats);
263: } else if (params[0].equals("NMR")) {
264: TabularData nmrStats = testStats.getNMRStats(target);
265: testStats.printCompositeData(nmrStats);
266: } else if (params[0].equals("Component")) {
267: TabularData componentStats = testStats
268: .getComponentStats(target, params[1]);
269: testStats.printCompositeData(componentStats);
270: }
271:
272: else if (params[0].equals("Endpoint")) {
273: TabularData epStats = testStats.getEndpointStats(
274: target, params[1]);
275: testStats.printCompositeData(epStats);
276: } else if (params[0].equals("SA")) {
277: TabularData saStats = testStats.getSAStats(target,
278: params[1]);
279: testStats.printCompositeData(saStats);
280: } else if (params[0].equals("listconsumingendpoints")) {
281: TabularData endpointsList = testStats
282: .getConsumingEndpointsForComponent(target,
283: params[1]);
284: testStats.printCompositeData(endpointsList);
285: } else if (params[0].equals("listprovidingendpoints")) {
286: TabularData endpointsList = testStats
287: .getProvidingEndpointsForComponent(target,
288: params[1]);
289: testStats.printCompositeData(endpointsList);
290: }
291: } catch (Exception ex) {
292: ex.printStackTrace();
293: }
294:
295: }
296:
297: }
|