User: schaefera
  Date: 01/06/23 01:48:42

  Added:       src/main/org/jboss/jmx/server XMLAdaptorImpl.java
                        XMLAdaptorService.java XMLAdaptorServiceMBean.java
                        XMLTestService.java XMLTestServiceMBean.java
  Log:
  First draft for the XML Adaptor. Only invoke is supported. Have a look
  at XMLTestService to see how to use it.
  
  Revision  Changes    Path
  1.1                  jboss/src/main/org/jboss/jmx/server/XMLAdaptorImpl.java
  
  Index: XMLAdaptorImpl.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.jmx.server;
  
  import java.util.Iterator;
  import java.util.Vector;
  
  import javax.management.Attribute;
  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.MBeanException;
  import javax.management.ReflectionException;
  import javax.management.MBeanServer;
  import javax.naming.InitialContext;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  /**
  * XML Adaptor Implementation interpreting the XML wrapped JMX commands.
  *
  * @author Andreas Schaefer ([EMAIL PROTECTED])
  * @created June 22, 2001
  * @version $Revision: 1.1 $
  */
  public class XMLAdaptorImpl
  {
    // Constants -----------------------------------------------------
  
    // Attributes ----------------------------------------------------
    MBeanServer mServer;
  
  
    // Static --------------------------------------------------------
  
    // Constructors --------------------------------------------------
  
    /**
    *  Constructor for the JMXAdaptorImpl object
    *
    *@param pServer MBeanServer this adaptor executes its calls on
    */
    public XMLAdaptorImpl( MBeanServer pServer ) {
      super();
      mServer = pServer;
    }
  
    // Public --------------------------------------------------------
  
    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 ) );
      }
      return (Object[]) lReturns.toArray( new Object[ 0 ] );
    }
  
    public Object invokeXML( Element pJmxOperation ) {
      if( pJmxOperation == null ) {
        return null;
      }
      // Get the requested operation
      String lTag = pJmxOperation.getTagName();
      if( "invoke".equals( lTag ) ) {
        // Get the operation, Object Name and attributes and invoke it
        String lOperation = pJmxOperation.getAttribute( "operation" );
        if( !"".equals( lOperation ) ) {
          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(
                lName,
                lOperation,
                lObjects,
                lTypes
              );
            }
            catch( Exception e ) {
              e.printStackTrace();
            }
          }
        }
      }
      return null;
    }
  
    // Protected -----------------------------------------------------
  }
  
  
  
  
  1.1                  jboss/src/main/org/jboss/jmx/server/XMLAdaptorService.java
  
  Index: XMLAdaptorService.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.jmx.server;
  
  import javax.management.MBeanServer;
  import javax.management.ObjectName;
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.Name;
  import javax.naming.NamingException;
  import javax.naming.NameNotFoundException;
  import javax.naming.Reference;
  import javax.naming.StringRefAddr;
  
  // import org.jboss.logging.Log;
  import org.jboss.naming.NonSerializableFactory;
  
  import org.jboss.util.ServiceMBeanSupport;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  
  /**
  * MBean Wrapper for the XML Adaptor.
  *
  * @author Andreas Schaefer ([EMAIL PROTECTED])
  * @created June 22, 2001
  * @version $Revision: 1.1 $
  */
  public class XMLAdaptorService
    extends ServiceMBeanSupport
    implements XMLAdaptorServiceMBean
  {
    // Attributes ----------------------------------------------------
    XMLAdaptorImpl mAdaptor;
  
    // Constants -----------------------------------------------------
    public static String JNDI_NAME = "jmx:xml";
  
  
    // Static --------------------------------------------------------
  
    // Constructors --------------------------------------------------
  
    // Public --------------------------------------------------------
  
    public Object[] invokeXML( Document pJmxOperations ) {
      return mAdaptor.invokeXML( pJmxOperations );
    }
  
    public Object invokeXML( Element pJmxOperation ) {
      return mAdaptor.invokeXML( pJmxOperation );
    }
  
    public ObjectName getObjectName( MBeanServer pServer, ObjectName pName )
      throws
        javax.management.MalformedObjectNameException
    {
      return pName;
    }
  
    public String getName() {
      return "JMX XML-Adaptor";
    }
  
    // Protected -----------------------------------------------------
    protected void initService()
      throws
        Exception
    {
      mAdaptor = new XMLAdaptorImpl( getServer() );
    }
  
    protected void startService()
      throws
        Exception
    {
      bind( mAdaptor );
    }
  
    protected void stopService() {
      try
      {
        unbind();
      }
      catch( Exception e )
      {
        log.exception( e );
      }
    }
        private void bind( XMLAdaptorImpl pImplementation )
        throws
           NamingException
     {
                Context lContext = new InitialContext();
  
                // Ah ! JBoss Server isn't serializable, so we use a helper class
                NonSerializableFactory.bind( JNDI_NAME, pImplementation );
  
        //AS Don't ask me what I am doing here
                Name lName = lContext.getNameParser("").parse( JNDI_NAME );
                while( lName.size() > 1 ) {
                        String lContextName = lName.get( 0 );
                        try {
                                lContext = (Context) lContext.lookup(lContextName);
                        }
                        catch( NameNotFoundException e )        {
                                lContext = lContext.createSubcontext(lContextName);
                        }
                        lName = lName.getSuffix( 1 );
                }
  
                // The helper class NonSerializableFactory uses address type nns, we 
go on to
                // use the helper class to bind the javax.mail.Session object in JNDI
                StringRefAddr lAddress = new StringRefAddr( "nns", JNDI_NAME );
                Reference lReference = new Reference(
           XMLAdaptorImpl.class.getName(),
           lAddress,
           NonSerializableFactory.class.getName(),
           null
        );
                lContext.bind( lName.get( 0 ), lReference );
  
                log.log( "JBoss XML Adaptor Service '" + JNDI_NAME + "' bound to " + 
JNDI_NAME );
        }
  
        private void unbind() throws NamingException
        {
        new InitialContext().unbind( JNDI_NAME );
        NonSerializableFactory.unbind( JNDI_NAME );
        log.log("JBoss XML Adaptor service '" + JNDI_NAME + "' removed from JNDI" );
        }
  
  }
  
  
  
  
  1.1                  jboss/src/main/org/jboss/jmx/server/XMLAdaptorServiceMBean.java
  
  Index: XMLAdaptorServiceMBean.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.jmx.server;
  
  import org.jboss.util.ServiceMBean;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  
  /**
  * XML Adaptor allows to work on a MBeanServer with
  * data specified in an XML foramt.
  *
  * @author Andreas Schaefer ([EMAIL PROTECTED])
  * @created   June 22, 2001
  * @version   $Revision: 1.1 $
   */
  public interface XMLAdaptorServiceMBean
    extends ServiceMBean
  {
    // Constants -----------------------------------------------------
    public static final String OBJECT_NAME = "Adaptor:name=XML";
  
    // Public --------------------------------------------------------
  
    public Object[] invokeXML( Document pJmxOperations );
  
    public Object invokeXML( Element pJmxOperation );
  
  }
  
  
  
  1.1                  jboss/src/main/org/jboss/jmx/server/XMLTestService.java
  
  Index: XMLTestService.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.jmx.server;
  
  import java.io.StringBufferInputStream;
  import java.util.Iterator;
  
  import javax.management.MBeanServer;
  import javax.management.ObjectName;
  import javax.naming.InitialContext;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  
  import org.jboss.util.ServiceMBeanSupport;
  
  import org.w3c.dom.Document;
  
  /**
  * MBean Wrapper for the XML Test-Adaptor.
  *
  * @author Andreas Schaefer ([EMAIL PROTECTED])
  * @created June 22, 2001
  * @version $Revision: 1.1 $
  */
  public class XMLTestService
    extends ServiceMBeanSupport
    implements XMLTestServiceMBean
  {
    // Attributes ----------------------------------------------------
    private ObjectName lXMLAdaptor;
    
    // Constants -----------------------------------------------------
    public static String JNDI_NAME = "jmx:test:xml";
  
  
    // Static --------------------------------------------------------
  
    // Constructors --------------------------------------------------
  
    // Public --------------------------------------------------------
  
    public ObjectName getObjectName( MBeanServer pServer, ObjectName pName )
      throws
        javax.management.MalformedObjectNameException
    {
      return pName;
    }
  
    public String getName() {
      return "JMX XML-Adaptor Test Suite";
    }
  
    // Protected -----------------------------------------------------
    protected void initService()
      throws
        Exception
    {
    }
  
    protected void startService()
      throws
        Exception
    {
  //    new InitialContext().bind( JNDI_NAME, mAdaptor );
      // Get XML-Adaptor
      System.out.println( "Lookup the XML Adaptor" );
      Iterator i = getServer().queryNames( new ObjectName( "jmx:name=xml" ), null 
).iterator();
      if( i.hasNext() ) {
        ObjectName lName = (ObjectName) i.next();
        System.out.println( "Got object name: " + lName );
        // Create Test XML Document
        Document lTest = 
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
          new StringBufferInputStream(
            "<invoke operation=\"stop\"><object-name name=\":service=Scheduler\"/>" +
            "</invoke>"
          )
        );
        System.out.println( "Call invokeXML with: " + lTest );
        getServer().invoke(
          lName,
          "invokeXML",
          new Object[] { lTest },
          new String[] { Document.class.getName() }
        );
      }
    }
  
    protected void stopService() {
      try
      {
        new InitialContext().unbind( JNDI_NAME );
      }
      catch( Exception e )
      {
        log.exception( e );
      }
    }
  }
  
  
  
  
  1.1                  jboss/src/main/org/jboss/jmx/server/XMLTestServiceMBean.java
  
  Index: XMLTestServiceMBean.java
  ===================================================================
  /*
  * JBoss, the OpenSource EJB server
  *
  * Distributable under LGPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.jmx.server;
  
  import org.jboss.util.ServiceMBean;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  
  /**
  * XML Test-Adaptor allows to work on a MBeanServer with
  * data specified in an XML foramt.
  *
  * @author Andreas Schaefer ([EMAIL PROTECTED])
  * @created   June 22, 2001
  * @version   $Revision: 1.1 $
   */
  public interface XMLTestServiceMBean
    extends ServiceMBean
  {
    // Constants -----------------------------------------------------
    public static final String OBJECT_NAME = "test:name=XML";
  
    // Public --------------------------------------------------------
  
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to