325N/A/*
325N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/A
325N/A
325N/Apackage com.sun.org.glassfish.gmbal;
325N/A
325N/Aimport java.io.IOException;
325N/Aimport java.util.HashMap;
325N/Aimport java.util.Map;
325N/Aimport javax.management.Attribute;
325N/Aimport javax.management.AttributeList;
325N/Aimport javax.management.AttributeNotFoundException;
325N/Aimport javax.management.Descriptor;
325N/Aimport javax.management.InstanceNotFoundException;
325N/Aimport javax.management.IntrospectionException;
325N/Aimport javax.management.InvalidAttributeValueException;
325N/Aimport javax.management.JMException;
325N/Aimport javax.management.MBeanException;
325N/Aimport javax.management.MBeanInfo;
325N/Aimport javax.management.MBeanServerConnection;
325N/Aimport javax.management.MalformedObjectNameException;
325N/Aimport javax.management.ObjectName;
325N/Aimport javax.management.ReflectionException;
325N/Aimport javax.management.RuntimeOperationsException;
325N/Aimport javax.management.modelmbean.ModelMBeanInfo;
325N/A
325N/A
325N/A/** This class implements a generic AMXMBeanInterface MBean which is connected to a possibly
325N/A * remote MBeanServerConnection (note that MBeanServer isA MBeanServerConnection,
325N/A * so we can actually create an AMXClientImpl simply by using the MBeanServer
325N/A * from the mom: this is useful for testing).
325N/A * <P>
325N/A * Note that this version of the AMXMBeanInterface API provides a generic get/set API that
325N/A * is identical to DynamicMBean, except that it only throws unchecked exceptions.
325N/A * This is far more convenient in practice than the JMX-standard checked exceptions.
325N/A *
325N/A * @author ken
325N/A */
325N/Apublic class AMXClient implements AMXMBeanInterface {
325N/A private static ObjectName makeObjectName( String str ) {
325N/A try {
325N/A return new ObjectName(str);
325N/A } catch (MalformedObjectNameException ex) {
325N/A return null ;
325N/A }
325N/A }
325N/A
325N/A /** Special object name used to represent a NULL objectName result.
325N/A */
325N/A public static final ObjectName NULL_OBJECTNAME = makeObjectName(
325N/A "null:type=Null,name=Null" ) ;
325N/A
325N/A private MBeanServerConnection server ;
325N/A private ObjectName oname ;
325N/A
325N/A @Override
325N/A public boolean equals( Object obj ) {
325N/A if (this == obj) {
325N/A return true ;
325N/A }
325N/A
325N/A if (!(obj instanceof AMXClient)) {
325N/A return false ;
325N/A }
325N/A
325N/A AMXClient other = (AMXClient)obj ;
325N/A
325N/A return oname.equals( other.oname ) ;
325N/A }
325N/A
325N/A @Override
325N/A public int hashCode() {
325N/A int hash = 5;
325N/A hash = 47 * hash + (this.oname != null ? this.oname.hashCode() : 0);
325N/A return hash;
325N/A }
325N/A
325N/A @Override
325N/A public String toString() {
325N/A return "AMXClient[" + oname + "]" ;
325N/A }
325N/A
325N/A private <T> T fetchAttribute( String name, Class<T> type ) {
325N/A try {
325N/A Object obj = server.getAttribute( oname, name ) ;
325N/A if (NULL_OBJECTNAME.equals( obj )) {
325N/A return null ;
325N/A } else {
325N/A return type.cast( obj ) ;
325N/A }
325N/A } catch (JMException exc) {
325N/A throw new GmbalException( "Exception in fetchAttribute", exc ) ;
325N/A } catch (IOException exc) {
325N/A throw new GmbalException( "Exception in fetchAttribute", exc ) ;
325N/A }
325N/A }
325N/A
325N/A public AMXClient( MBeanServerConnection server,
325N/A ObjectName oname ) {
325N/A this.server = server ;
325N/A this.oname = oname ;
325N/A }
325N/A
325N/A private AMXClient makeAMX( ObjectName on ) {
325N/A if (on == null) {
325N/A return null ;
325N/A }
325N/A return new AMXClient( this.server, on ) ;
325N/A }
325N/A
325N/A public String getName() {
325N/A return fetchAttribute( "Name", String.class ) ;
325N/A }
325N/A
325N/A public Map<String,?> getMeta() {
325N/A try {
325N/A ModelMBeanInfo mbi = (ModelMBeanInfo) server.getMBeanInfo( oname );
325N/A Descriptor desc = mbi.getMBeanDescriptor() ;
325N/A Map<String,Object> result = new HashMap<String,Object>() ;
325N/A for (String str : desc.getFieldNames()) {
325N/A result.put( str, desc.getFieldValue( str )) ;
325N/A }
325N/A return result ;
325N/A } catch (MBeanException ex) {
325N/A throw new GmbalException( "Exception in getMeta", ex ) ;
325N/A } catch (RuntimeOperationsException ex) {
325N/A throw new GmbalException( "Exception in getMeta", ex ) ;
325N/A } catch (InstanceNotFoundException ex) {
325N/A throw new GmbalException( "Exception in getMeta", ex ) ;
325N/A } catch (IntrospectionException ex) {
325N/A throw new GmbalException( "Exception in getMeta", ex ) ;
325N/A } catch (ReflectionException ex) {
325N/A throw new GmbalException( "Exception in getMeta", ex ) ;
325N/A } catch (IOException ex) {
325N/A throw new GmbalException( "Exception in getMeta", ex ) ;
325N/A }
325N/A }
325N/A
325N/A public AMXClient getParent() {
325N/A ObjectName res = fetchAttribute( "Parent", ObjectName.class ) ;
325N/A return makeAMX( res ) ;
325N/A }
325N/A
325N/A public AMXClient[] getChildren() {
325N/A ObjectName[] onames = fetchAttribute( "Children",
325N/A ObjectName[].class ) ;
325N/A return makeAMXArray( onames ) ;
325N/A }
325N/A
325N/A private AMXClient[] makeAMXArray( ObjectName[] onames ) {
325N/A AMXClient[] result = new AMXClient[onames.length] ;
325N/A int ctr=0 ;
325N/A for (ObjectName on : onames ) {
325N/A result[ctr++] = makeAMX( on ) ;
325N/A }
325N/A
325N/A return result ;
325N/A }
325N/A
325N/A public Object getAttribute(String attribute) {
325N/A try {
325N/A return server.getAttribute(oname, attribute);
325N/A } catch (MBeanException ex) {
325N/A throw new GmbalException( "Exception in getAttribute", ex ) ;
325N/A } catch (AttributeNotFoundException ex) {
325N/A throw new GmbalException( "Exception in getAttribute", ex ) ;
325N/A } catch (ReflectionException ex) {
325N/A throw new GmbalException( "Exception in getAttribute", ex ) ;
325N/A } catch (InstanceNotFoundException ex) {
325N/A throw new GmbalException( "Exception in getAttribute", ex ) ;
325N/A } catch (IOException ex) {
325N/A throw new GmbalException( "Exception in getAttribute", ex ) ;
325N/A }
325N/A }
325N/A
325N/A public void setAttribute(String name, Object value ) {
325N/A Attribute attr = new Attribute( name, value ) ;
325N/A setAttribute( attr ) ;
325N/A }
325N/A
325N/A public void setAttribute(Attribute attribute) {
325N/A try {
325N/A server.setAttribute(oname, attribute);
325N/A } catch (InstanceNotFoundException ex) {
325N/A throw new GmbalException( "Exception in setAttribute", ex ) ;
325N/A } catch (AttributeNotFoundException ex) {
325N/A throw new GmbalException( "Exception in setAttribute", ex ) ;
325N/A } catch (InvalidAttributeValueException ex) {
325N/A throw new GmbalException( "Exception in setAttribute", ex ) ;
325N/A } catch (MBeanException ex) {
325N/A throw new GmbalException( "Exception in setAttribute", ex ) ;
325N/A } catch (ReflectionException ex) {
325N/A throw new GmbalException( "Exception in setAttribute", ex ) ;
325N/A } catch (IOException ex) {
325N/A throw new GmbalException( "Exception in setAttribute", ex ) ;
325N/A }
325N/A }
325N/A
325N/A public AttributeList getAttributes(String[] attributes) {
325N/A try {
325N/A return server.getAttributes(oname, attributes);
325N/A } catch (InstanceNotFoundException ex) {
325N/A throw new GmbalException( "Exception in getAttributes", ex ) ;
325N/A } catch (ReflectionException ex) {
325N/A throw new GmbalException( "Exception in getAttributes", ex ) ;
325N/A } catch (IOException ex) {
325N/A throw new GmbalException( "Exception in getAttributes", ex ) ;
325N/A }
325N/A }
325N/A
325N/A public AttributeList setAttributes(AttributeList attributes) {
325N/A try {
325N/A return server.setAttributes(oname, attributes);
325N/A } catch (InstanceNotFoundException ex) {
325N/A throw new GmbalException( "Exception in setAttributes", ex ) ;
325N/A } catch (ReflectionException ex) {
325N/A throw new GmbalException( "Exception in setAttributes", ex ) ;
325N/A } catch (IOException ex) {
325N/A throw new GmbalException( "Exception in setAttributes", ex ) ;
325N/A }
325N/A }
325N/A
325N/A public Object invoke(String actionName, Object[] params, String[] signature)
325N/A throws MBeanException, ReflectionException {
325N/A try {
325N/A return server.invoke(oname, actionName, params, signature);
325N/A } catch (InstanceNotFoundException ex) {
325N/A throw new GmbalException( "Exception in invoke", ex ) ;
325N/A } catch (IOException ex) {
325N/A throw new GmbalException( "Exception in invoke", ex ) ;
325N/A }
325N/A }
325N/A
325N/A public MBeanInfo getMBeanInfo() {
325N/A try {
325N/A return server.getMBeanInfo(oname);
325N/A } catch (InstanceNotFoundException ex) {
325N/A throw new GmbalException( "Exception in invoke", ex ) ;
325N/A } catch (IntrospectionException ex) {
325N/A throw new GmbalException( "Exception in invoke", ex ) ;
325N/A } catch (ReflectionException ex) {
325N/A throw new GmbalException( "Exception in invoke", ex ) ;
325N/A } catch (IOException ex) {
325N/A throw new GmbalException( "Exception in invoke", ex ) ;
325N/A }
325N/A }
325N/A
325N/A public ObjectName objectName() {
325N/A return oname ;
325N/A }
325N/A}