001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sun.manager.jbi.util;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.List;
046: import java.util.Set;
047:
048: import javax.management.Attribute;
049: import javax.management.MBeanAttributeInfo;
050: import javax.management.MBeanInfo;
051: import javax.management.MBeanServerConnection;
052: import javax.management.ObjectName;
053:
054: public class MBeanUtil {
055:
056: public ObjectName[] getAllObjectNames(
057: MBeanServerConnection connection) throws Exception {
058: Set<ObjectName> set = (Set<ObjectName>) connection.queryNames(
059: null, null);
060: return (ObjectName[]) set.toArray(new ObjectName[0]);
061: }
062:
063: public String[] getAttributeNames(MBeanServerConnection connection,
064: ObjectName objectName) throws Exception {
065: Collection<String> list = new ArrayList<String>();
066: MBeanInfo info = connection.getMBeanInfo(objectName);
067: MBeanAttributeInfo[] attributes = info.getAttributes();
068: for (int i = 0; i < attributes.length; i++) {
069: MBeanAttributeInfo attributeInfo = (MBeanAttributeInfo) attributes[i];
070: String attributeName = attributeInfo.getName();
071: list.add(attributeName);
072: }
073: return list.toArray(new String[0]);
074: }
075:
076: public Object getAttributeValue(MBeanServerConnection connection,
077: ObjectName objectName, String attributeName)
078: throws Exception {
079: return connection.getAttribute(objectName, attributeName);
080: }
081:
082: public Object[] getAttributeValues(
083: MBeanServerConnection connection, ObjectName name,
084: String[] attributeNames) throws Exception {
085: Collection<Object> list = new ArrayList<Object>();
086: Attribute[] attributes = getAttributes(connection, name,
087: attributeNames);
088: for (int i = 0; i < attributes.length; i++) {
089: Attribute attribute = attributes[i];
090: list.add(attribute.getValue());
091: }
092: return list.toArray(new Object[0]);
093: }
094:
095: public Attribute[] getAttributes(MBeanServerConnection connection,
096: ObjectName objectName, String[] attributeNames)
097: throws Exception {
098: List<Object> list = (List<Object>) connection.getAttributes(
099: objectName, attributeNames);
100: return (Attribute[]) list.toArray(new Attribute[0]);
101: }
102:
103: public void print(MBeanServerConnection connection)
104: throws Exception {
105: print(connection, null, null, null);
106: }
107:
108: public void print(MBeanServerConnection connection,
109: String objectNameFilter, String attributeNameFilter,
110: String attributeValueFilter) throws Exception {
111: ObjectName[] objectNames = getAllObjectNames(connection);
112: for (int i = 0; i < objectNames.length; i++) {
113: ObjectName objectName = objectNames[i];
114: if (objectNameFilter == null
115: || String.valueOf(objectName).indexOf(
116: objectNameFilter) != -1) {
117: String[] attributeNames = getAttributeNames(connection,
118: objectName);
119: for (int j = 0; j < attributeNames.length; j++) {
120: String attributeName = attributeNames[j];
121: if (attributeNameFilter == null
122: || attributeName
123: .indexOf(attributeNameFilter) != -1) {
124: Object attributeValue;
125: try {
126: attributeValue = getAttributeValue(
127: connection, objectName,
128: attributeName);
129: } catch (Exception e) {
130: attributeValue = e.getMessage();
131: }
132: if (attributeValueFilter == null
133: || String.valueOf(attributeValue)
134: .indexOf(attributeValueFilter) != -1) {
135: System.out.println("objectName: "
136: + objectName + " attributeName: "
137: + attributeName
138: + " attributeValue: "
139: + attributeValue); // NOI18N
140: }
141: }
142: }
143: }
144: }
145: }
146: }
|