User: schaefera Date: 01/06/25 11:28:52 Modified: src/main/org/jboss/jmx/server XMLAdaptorImpl.java XMLTestService.java Log: Fixed some problems and add the "create-mbean" support. Revision Changes Path 1.2 +267 -68 jboss/src/main/org/jboss/jmx/server/XMLAdaptorImpl.java Index: XMLAdaptorImpl.java =================================================================== RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/jmx/server/XMLAdaptorImpl.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- XMLAdaptorImpl.java 2001/06/23 08:48:42 1.1 +++ XMLAdaptorImpl.java 2001/06/25 18:28:52 1.2 @@ -6,19 +6,24 @@ */ package org.jboss.jmx.server; +import java.beans.PropertyEditor; +import java.beans.PropertyEditorManager; +import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; import javax.management.Attribute; +import javax.management.AttributeList; import javax.management.ObjectName; -import javax.management.QueryExp; import javax.management.ObjectInstance; -import javax.management.AttributeNotFoundException; -import javax.management.InstanceNotFoundException; -import javax.management.InvalidAttributeValueException; +// import javax.management.AttributeNotFoundException; +// import javax.management.InstanceNotFoundException; +// import javax.management.InvalidAttributeValueException; +import javax.management.MalformedObjectNameException; +import javax.management.MBeanAttributeInfo; import javax.management.MBeanException; -import javax.management.ReflectionException; import javax.management.MBeanServer; +// import javax.management.ReflectionException; import javax.naming.InitialContext; import org.w3c.dom.Document; @@ -31,7 +36,7 @@ * * @author Andreas Schaefer ([EMAIL PROTECTED]) * @created June 22, 2001 -* @version $Revision: 1.1 $ +* @version $Revision: 1.2 $ */ public class XMLAdaptorImpl { @@ -43,7 +48,22 @@ // Static -------------------------------------------------------- - // Constructors -------------------------------------------------- + /** Primitive type name -> class map. */ + private static Hashtable mPrimitives = new Hashtable(); + + /** Setup the primitives map. */ + static { + mPrimitives.put( "boolean", Boolean.TYPE ); + mPrimitives.put( "byte", Byte.TYPE ); + mPrimitives.put( "short", Short.TYPE ); + mPrimitives.put( "int", Integer.TYPE ); + mPrimitives.put( "long", Long.TYPE ); + mPrimitives.put( "float", Float.TYPE ); + mPrimitives.put( "double", Double.TYPE ); + mPrimitives.put( "char", Character.TYPE ); + } + + // Constructors -------------------------------------------------- /** * Constructor for the JMXAdaptorImpl object @@ -59,10 +79,15 @@ public Object[] invokeXML( Document pJmxOperations ) { Vector lReturns = new Vector(); - NodeList lOperations = pJmxOperations.getChildNodes(); - for( int i = 0; i < lOperations.getLength(); i++ ) { - Element lChildElement = (Element) lOperations.item( i ); - lReturns.add( invokeXML( lChildElement ) ); + NodeList lRoot = pJmxOperations.getChildNodes(); + if( lRoot.getLength() > 0 ) { + Element lRootElement = (Element) lRoot.item( 0 ); + System.out.println( "XMLAdaptorImpl.invokeXML(), root: " + lRootElement ); + NodeList lOperations = lRootElement.getChildNodes(); + for( int i = 0; i < lOperations.getLength(); i++ ) { + Element lChildElement = (Element) lOperations.item( i ); + lReturns.add( invokeXML( lChildElement ) ); + } } return (Object[]) lReturns.toArray( new Object[ 0 ] ); } @@ -73,74 +98,248 @@ } // Get the requested operation String lTag = pJmxOperation.getTagName(); + System.out.println( "XMLAdaptorImpl.invokeXML(), Tag: " + lTag ); if( "invoke".equals( lTag ) ) { // Get the operation, Object Name and attributes and invoke it String lOperation = pJmxOperation.getAttribute( "operation" ); - if( !"".equals( lOperation ) ) { + return invoke( + lOperation, + pJmxOperation.getElementsByTagName( "object-name" ), + pJmxOperation.getElementsByTagName( "attribute" ) + ); + } + else if( !"create-mbean".equals( lTag ) ) { NodeList lList = pJmxOperation.getElementsByTagName( "object-name" ); - if( lList.getLength() > 0 ) { - try { - Node lNodeName = lList.item( 0 ); - ObjectName lName = null; - switch( lNodeName.getNodeType() ) { - case Node.ELEMENT_NODE: - Element lElementName = (Element) lNodeName; - lName = new ObjectName( lElementName.getAttribute( "name" ) ); - break; - } - // Get attribute values and types - NodeList lAttributeList = pJmxOperation.getElementsByTagName( "attribute" ); - Object[] lObjects = new Object[ lAttributeList.getLength() ]; - String[] lTypes = new String[ lAttributeList.getLength() ]; - for( int i = 0; i < lAttributeList.getLength(); i++ ) { - Node lAttributeNode = lAttributeList.item( i ); - Element lAttributeElement = (Element) lAttributeNode; - lTypes[ i ] = lAttributeElement.getAttribute( "type" ); - if( "int".equals( lTypes[ i ] ) ) { - lObjects[ i ] = new Integer( lAttributeElement.getAttribute( "value" ) ); - } - else if( "short".equals( lTypes[ i ] ) ) { - lObjects[ i ] = new Short( lAttributeElement.getAttribute( "value" ) ); - } - else if( "byte".equals( lTypes[ i ] ) ) { - lObjects[ i ] = new Byte( lAttributeElement.getAttribute( "value" ) ); - } - else if( "char".equals( lTypes[ i ] ) ) { - lObjects[ i ] = new Character( lAttributeElement.getAttribute( "value" ).charAt( 0 ) ); - } - else if( "long".equals( lTypes[ i ] ) ) { - lObjects[ i ] = new Long( lAttributeElement.getAttribute( "value" ) ); - } - else if( "float".equals( lTypes[ i ] ) ) { - lObjects[ i ] = new Float( lAttributeElement.getAttribute( "value" ) ); - } - else if( "double".equals( lTypes[ i ] ) ) { - lObjects[ i ] = new Double( lAttributeElement.getAttribute( "value" ) ); - } - else if( "boolean".equals( lTypes[ i ] ) ) { - lObjects[ i ] = new Boolean( lAttributeElement.getAttribute( "value" ) ); - } - else if( "java.lang.String".equals( lTypes[ i ] ) ) { - lObjects[ i ] = new String( lAttributeElement.getAttribute( "value" ) ); - } - } - // Invoke the method and return the value - return mServer.invoke( + // Get the operation, Object Name and attributes and invoke it + String lCodebase = pJmxOperation.getAttribute( "code" ); + String lName = pJmxOperation.getAttribute( "name" ); + return createMBean( + lCodebase, + lName, + pJmxOperation.getElementsByTagName( "object-name" ), + pJmxOperation.getElementsByTagName( "constructor" ), + pJmxOperation.getElementsByTagName( "attribute" ) + ); + } + return null; + } + + public ObjectName createMBean( + String pCodebase, + String pName, + NodeList pObjectName, + NodeList pConstructor, + NodeList pAttributes + ) { + ObjectName lReturn = null; + // Check Codebase + if( pCodebase != null && !pCodebase.equals( "" ) ) { + try { + // Create ObjectName + ObjectName lName = null; + if( pName != null && !pName.equals( "" ) ) { + lName = createObjectName( pName ); + } + else if( pObjectName != null && pObjectName.getLength() > 0 ) { + lName = createObjectName( (Element) pObjectName.item( 0 ) ); + } + if( lName != null ) { + ObjectInstance lNew = null; + if( pConstructor.getLength() == 0 ) { + lNew = mServer.createMBean( pCodebase, lName ); + } + else { + // Get the Constructor Values + Object[][] lAttributes = getAttributes( + ( (Element) pConstructor.item( 0 ) ).getElementsByTagName( "argument" ) + ); + lNew = mServer.createMBean( + pCodebase, lName, - lOperation, - lObjects, - lTypes + lAttributes[ 0 ], + (String[]) lAttributes[ 1 ] ); - } - catch( Exception e ) { - e.printStackTrace(); } + // Now loop over the attributes and set them + Object[][] lAttributes = getAttributes( + lNew.getObjectName(), + pAttributes + ); + applyAttributes( + lNew.getObjectName(), + (String[]) lAttributes[ 1 ], + (Object[]) lAttributes[ 0 ] + ); + + lReturn = lNew.getObjectName(); } } + catch( Exception e ) { + e.printStackTrace(); + } } - return null; + return lReturn; } + public Object invoke( String pOperation, NodeList pObjectName, NodeList pAttributes ) { + Object lReturn = null; + System.out.println( "XMLAdaptorImpl.invoke(), Operation: " + pOperation ); + if( pOperation != null && !pOperation.equals( "" ) && + pObjectName != null && pObjectName.getLength() > 0 ) + { + try { + ObjectName lName = createObjectName( (Element) pObjectName.item( 0 ) ); + if( pAttributes != null && pAttributes.getLength() > 0 ) { + Object[][] lAttributes = getAttributes( + pAttributes + ); + // Invoke the method and return the value + lReturn = mServer.invoke( + lName, + pOperation, + lAttributes[ 0 ], + (String[]) lAttributes[ 1 ] + ); + } + else { + // Invoke the method and return the value + lReturn = mServer.invoke( + lName, + pOperation, + new Object[] {}, + new String[] {} + ); + } + } + catch( Exception e ) { + e.printStackTrace(); + } + } + return lReturn; + } // Protected ----------------------------------------------------- -} + + protected ObjectName createObjectName( String pName ) + throws + MalformedObjectNameException + { + return new ObjectName( pName ); + } + + protected ObjectName createObjectName( Element pObjectName ) + throws + MalformedObjectNameException + { + if( pObjectName.hasAttribute( "name" ) ) { + return new ObjectName( pObjectName.getAttribute( "name" ) ); + } + else { + String lDomain = null; + if( pObjectName.hasAttribute( "domain" ) ) { + lDomain = pObjectName.getAttribute( "domain" ); + } + Hashtable lProperties = new Hashtable(); + NodeList lPropertyList = pObjectName.getElementsByTagName( "property" ); + for( int i = 0; i < lPropertyList.getLength(); i++ ) { + Element lProperty = (Element) lPropertyList.item( i ); + if( lProperty.hasAttribute( "key" ) && lProperty.hasAttribute( "value" ) ) { + lProperties.put( lProperty.getAttribute( "key" ), lProperty.getAttribute( "value" ) ); + } + } + return new ObjectName( lDomain, lProperties ); + } + } + protected Object[][] getAttributes( NodeList pAttributes ) { + Object[] lReturn = new Object[ 2 ]; + Object[] lValues = new Object[ pAttributes.getLength() ]; + String[] lTypes = new String[ pAttributes.getLength() ]; + // Loop through argument list and create type and values + for( int i = 0; i < pAttributes.getLength(); i++ ) { + try { + Element lArgument = (Element) pAttributes.item( 0 ); + String lTypeString = lArgument.getAttribute( "type" ); + String lValueString = lArgument.getAttribute( "value" ); + Class lClass = null; + if( mPrimitives.containsKey( lTypeString ) ) { + lClass = (Class) mPrimitives.get( lTypeString ); + } + else { + lClass = Thread.currentThread().getContextClassLoader().loadClass( lTypeString ); + } + PropertyEditor lEditor = PropertyEditorManager.findEditor( lClass ); + lEditor.setAsText( lValueString ); + lValues[ i ] = lEditor.getValue(); + lTypes[ i ] = lClass.getName(); + } + catch( Exception e ) { + e.printStackTrace(); + } + } + lReturn[ 0 ] = lValues; + lReturn[ 1 ] = lTypes; + return (Object[][]) lReturn; + } + protected Object[][] getAttributes( ObjectName pName, NodeList pAttributes ) { + Object[] lReturn = new Object[ 2 ]; + Object[] lValues = new Object[ pAttributes.getLength() ]; + String[] lTypes = new String[ pAttributes.getLength() ]; + + try { + MBeanAttributeInfo[] attributes = mServer.getMBeanInfo( pName ).getAttributes(); + // Loop through argument list and create type and values + for( int i = 0; i < pAttributes.getLength(); i++ ) { + Element lArgument = (Element) pAttributes.item( 0 ); + String lNameString = lArgument.getAttribute( "name" ); + String lValueString = lArgument.getAttribute( "value" ); + for( int k = 0; k < attributes.length; k++ ) { + if( attributes[ k ].getName().equals( lNameString ) ) { + String lTypeString = attributes[ k ].getType(); + Class lClass; + if( mPrimitives.containsKey( lTypeString ) ) { + lClass = (Class) mPrimitives.get( lTypeString ); + } + else { + lClass = Thread.currentThread().getContextClassLoader().loadClass( lTypeString ); + } + PropertyEditor lEditor = PropertyEditorManager.findEditor( lClass ); + lEditor.setAsText( lValueString ); + lValues[ i ] = lEditor.getValue(); + lTypes[ i ] = lClass.getName(); + } + } + } + } + catch( Exception e ) { + e.printStackTrace(); + } + lReturn[ 0 ] = lValues; + lReturn[ 1 ] = lTypes; + return (Object[][]) lReturn; + } + + protected void applyAttributes( + ObjectName pName, + String[] pNames, + Object[] pValues + ) { + try { + if( pName != null && pNames != null && pValues != null ) { + if( pNames.length == pValues.length ) { + AttributeList lList = new AttributeList(); + for( int i = 0; i < pNames.length; i++ ) { + String lName = pNames[ i ]; + if( lName != null && !lName.equals( "" ) ) { + // Create Value from attribute type and given string representation + lList.add( new Attribute( lName, pValues[ i ] ) ); + } + } + mServer.setAttributes( pName, lList ); + } + } + } + catch( Exception e ) { + e.printStackTrace(); + } + } +} 1.2 +3 -3 jboss/src/main/org/jboss/jmx/server/XMLTestService.java Index: XMLTestService.java =================================================================== RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/jmx/server/XMLTestService.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- XMLTestService.java 2001/06/23 08:48:42 1.1 +++ XMLTestService.java 2001/06/25 18:28:52 1.2 @@ -24,7 +24,7 @@ * * @author Andreas Schaefer ([EMAIL PROTECTED]) * @created June 22, 2001 -* @version $Revision: 1.1 $ +* @version $Revision: 1.2 $ */ public class XMLTestService extends ServiceMBeanSupport @@ -75,8 +75,8 @@ // Create Test XML Document Document lTest = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( new StringBufferInputStream( - "<invoke operation=\"stop\"><object-name name=\":service=Scheduler\"/>" + - "</invoke>" + "<jmx><invoke operation=\"stop\"><object-name name=\":service=Scheduler\"/>" + + "</invoke></jmx>" ) ); System.out.println( "Call invokeXML with: " + lTest ); _______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] http://lists.sourceforge.net/lists/listinfo/jboss-development