commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cos...@apache.org
Subject cvs commit: jakarta-commons/modeler/src/java/org/apache/commons/modeler/util DomUtil.java
Date Sun, 29 Dec 2002 18:01:42 GMT
costin      2002/12/29 10:01:42

  Modified:    modeler/src/java/org/apache/commons/modeler Registry.java
               modeler/src/java/org/apache/commons/modeler/ant
                        RegistryTask.java
               modeler/src/java/org/apache/commons/modeler/modules
                        MbeansSource.java
               modeler/src/java/org/apache/commons/modeler/util
                        DomUtil.java
  Added:       modeler/src/java/org/apache/commons/modeler Main.java
  Log:
  Various fixes to the new code.
  
  Added a Main - for testing and to bootstrap ( it's easier to use the
  ant tasks, but it may be usefull to create the mbeans without requiring ant )
  
  Revision  Changes    Path
  1.11      +9 -5      jakarta-commons/modeler/src/java/org/apache/commons/modeler/Registry.java
  
  Index: Registry.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/Registry.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Registry.java	26 Dec 2002 18:21:01 -0000	1.10
  +++ Registry.java	29 Dec 2002 18:01:42 -0000	1.11
  @@ -286,7 +286,6 @@
           String moduleType=type + "Source";
           String sourceClassName=System.getProperty("org.apache.commons.modeler.source",
                   "org.apache.commons.modeler.modules." + moduleType);
  -        // "org.apache.commons.modeler.modules.MBeansDescriptorsDigesterSource")
   
           Class c=Class.forName( sourceClassName );
           DescriptorSource ds=(DescriptorSource)c.newInstance();
  @@ -337,11 +336,16 @@
           }
           ManagedBean managed = registry.findManagedBean(type);
           if( managed==null ) {
  -            // XXX use introspection ( or check super classes ?? )
  -            // I think introspection + super classes ( i.e. use descriptions, etc)
  +            // TODO: check package and parent packages
  +
  +            // TODO: check super-class
  +
  +            // introspection
  +            managed=createManagedBean(domain, bean.getClass(), type);
  +            addManagedBean(managed);
           }
   
  -        // XXX The real mbean is created and registered
  +        // The real mbean is created and registered
           ModelMBean mbean = managed.createMBean(bean);
   
           if( name==null ) {
  
  
  
  1.1                  jakarta-commons/modeler/src/java/org/apache/commons/modeler/Main.java
  
  Index: Main.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/Main.java,v
1.1 2002/12/29 18:01:42 costin Exp $
   * $Revision: 1.1 $
   * $Date: 2002/12/29 18:01:42 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package org.apache.commons.modeler;
  
  
  import org.apache.commons.modeler.util.IntrospectionUtils;
  
  import java.io.FileInputStream;
  
  
  /**
   *  Small main that loads mbeans.
   *
   *  Requires: commons-logging-api.jar, jaxp ( including DOM ), jmx
   *
   *  Arguments:
   *   -file FILE
   *       Will load mbeans from the file
   *   -type TYPE
   *       Type of the mbeans file
   *
   * @author Costin Manolache
   */
  
  public class Main
  {
      String file;
      String home;
      String type="Mbeans";
  
      public void setFile( String f ) {
          this.file=f;
      }
  
      // shortcut
      public void setF( String f ) {
          this.file=f;
      }
  
      public void setType( String type ) {
          this.type=type;
      }
  
      public void execute( )
          throws Exception
      {
          if( home==null ) {
              home=IntrospectionUtils.guessInstall("install.dir", "home.dir",
                  "commons-modeler.jar", "org.apache.commons.modeler.Main");
          }
  
          if( file==null ) throw new Exception( "No file, use -file file.xml");
  
          Registry reg=Registry.getRegistry();
  
          reg.loadDescriptors(file, type, new FileInputStream( file ));
      }
  
      public static void main( String args[] ) {
          try {
              Main main=new Main();
              IntrospectionUtils.processArgs(main, args);
  
              main.execute();
          } catch( Exception ex ) {
              ex.printStackTrace();
          }
  
      }
  }
  
  
  
  1.3       +29 -2     jakarta-commons/modeler/src/java/org/apache/commons/modeler/ant/RegistryTask.java
  
  Index: RegistryTask.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/ant/RegistryTask.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- RegistryTask.java	26 Dec 2002 18:09:29 -0000	1.2
  +++ RegistryTask.java	29 Dec 2002 18:01:42 -0000	1.3
  @@ -67,6 +67,7 @@
   import java.util.*;
   import org.apache.commons.modeler.BaseRegistry;
   import org.apache.commons.modeler.ManagedBean;
  +import org.apache.tools.ant.BuildException;
   
   /**
    * Load descriptors into registry.
  @@ -80,13 +81,39 @@
   
       String resource;
       String file;
  +    String type="MbeansDescriptorsDOM";
   
  +    /** Set the resource type that will be loaded
  +     *
  +     * @param type
  +     */
  +    public void setType( String type ) {
  +        this.type=type;
  +    }
  +
  +    public void setFile( String file ) {
  +        this.file=file;
  +    }
  +
  +    public void setResource( String res ) {
  +        this.resource=res;
  +    }
   
       public void execute() throws Exception {
  +        InputStream is=null;
  +        String id=null;
  +
           if( resource != null ) {
  -            InputStream is=this.getClass().getClassLoader().getResourceAsStream(resource);
  -            BaseRegistry.getBaseRegistry().loadDescriptors( resource, is, "mbeans-descriptors"
);
  +            is=this.getClass().getClassLoader().getResourceAsStream(resource);
  +            id=resource;
  +        } else if( file != null ) {
  +            is=new FileInputStream( file );
  +            id=file;
  +        } else {
  +            throw new BuildException( "Resource or file attribute required");
           }
  +
  +        BaseRegistry.getBaseRegistry().loadDescriptors( resource, is, type );
       }
   }
    
  
  
  
  1.2       +77 -17    jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansSource.java
  
  Index: MbeansSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansSource.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MbeansSource.java	26 Dec 2002 18:17:12 -0000	1.1
  +++ MbeansSource.java	29 Dec 2002 18:01:42 -0000	1.2
  @@ -7,8 +7,7 @@
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  -import javax.management.MBeanServer;
  -import javax.management.ObjectName;
  +import javax.management.*;
   import java.io.InputStream;
   
   
  @@ -40,6 +39,8 @@
   
               if( firstMbeanN==null ) {
                   // maybe we have a single mlet
  +                if( log.isDebugEnabled() )
  +                    log.debug("No child " + descriptorsN);
                   firstMbeanN=descriptorsN;
               }
   
  @@ -50,9 +51,17 @@
                    mbeanN= DomUtil.getNext(mbeanN, null, Node.ELEMENT_NODE))
               {
                   String nodeName=mbeanN.getNodeName();
  +
                   if( "mbean".equals(nodeName) || "MLET".equals(nodeName)) {
                       String code=DomUtil.getAttribute( mbeanN, "code" );
  -                    String name=DomUtil.getAttribute( mbeanN, "name" );
  +                    String objectName=DomUtil.getAttribute( mbeanN, "objectName" );
  +                    if( objectName==null ) {
  +                        objectName=DomUtil.getAttribute( mbeanN, "name" );
  +                    }
  +
  +                    if( log.isDebugEnabled())
  +                        log.debug( "Processing mbean objectName=" + objectName +
  +                                " code=" + code);
   
                       // args can be grouped in constructor or direct childs
                       Node constructorN=DomUtil.getChild(mbeanN, "constructor");
  @@ -60,32 +69,45 @@
   
                       processArg(constructorN);
   
  +                    try {
  +                        ObjectName oname=new ObjectName(objectName);
  +                        server.createMBean(code, oname);
  +                        // XXX Arguments, loader !!!
  +                    } catch( Exception ex ) {
  +                        log.error( "Error creating mbean " + objectName, ex);
  +                    }
  +
                       Node firstAttN=DomUtil.getChild(mbeanN, "attribute");
                       for (Node descN = firstAttN; descN != null;
                            descN = DomUtil.getNext( descN ))
                       {
  -                        String attName=DomUtil.getAttribute(descN, "name");
  -                        String value=DomUtil.getAttribute(descN, "value");
  -                        if( value==null ) {
  -                            // The value may be specified as CDATA
  -                            value=DomUtil.getContent(descN);
  -                        }
  +                        processAttribute(server, descN, objectName);
                       }
   
  -                } else if("mbeans-descriptors".equals(nodeName) ) {
  -                    // TODO support descriptors fragments inside <mbeans>
                   } else if("classpath".equals(nodeName) ) {
                       // TODO support classpath ( standard )
                   } else if("jmx-attribute".equals(nodeName) ) {
  -                    // Do we need this ?
  +                    // <jmx-attribute objectName="..." name="..." value="..."/>
  +                    String objectName=DomUtil.getAttribute(mbeanN, "objectName");
  +                    processAttribute(server, mbeanN, objectName);
                   } else if("jmx-operation".equals(nodeName) ) {
  -                    String name=DomUtil.getAttribute(mbeanN, "name");
  -                    String operation=DomUtil.getAttribute(mbeanN, "operation");
  +                    String name=DomUtil.getAttribute(mbeanN, "objectName");
  +                    if( name==null )
  +                        name=DomUtil.getAttribute(mbeanN, "name");
   
  -                    ObjectName oname=new ObjectName(name);
  +                    String operation=DomUtil.getAttribute(mbeanN, "operation");
   
  -                    processArg( mbeanN );
  -                    server.invoke( oname, operation, null, null);
  +                    if( log.isDebugEnabled())
  +                        log.debug( "Processing invoke objectName=" + name +
  +                                " code=" + operation);
  +                    try {
  +                        ObjectName oname=new ObjectName(name);
  +
  +                        processArg( mbeanN );
  +                        server.invoke( oname, operation, null, null);
  +                    } catch (Exception e) {
  +                        log.error( "Error in invoke " + name + " " + operation);
  +                    }
                   }
   
                   ManagedBean managed=new ManagedBean();
  @@ -109,6 +131,44 @@
           } catch( Exception ex ) {
               log.error( "Error reading mbeans ", ex);
           }
  +    }
  +
  +    private void processAttribute(MBeanServer server,
  +                                  Node descN, String objectName ) {
  +        String attName=DomUtil.getAttribute(descN, "name");
  +        String value=DomUtil.getAttribute(descN, "value");
  +        String type=DomUtil.getAttribute(descN, "type");
  +        if( value==null ) {
  +            // The value may be specified as CDATA
  +            value=DomUtil.getContent(descN);
  +        }
  +        try {
  +            if( log.isDebugEnabled())
  +                log.debug("Set attribute " + objectName + " " + attName +
  +                        " " + value);
  +            ObjectName oname=new ObjectName(objectName);
  +            Object valueO=getValueObject( value, type);
  +            server.setAttribute(oname, new Attribute(attName, valueO));
  +        } catch( Exception ex) {
  +            log.error("Error processing attribute " + objectName + " " +
  +                    attName + " " + value, ex);
  +        }
  +
  +    }
  +
  +    // XXX We should know the type from the mbean metadata
  +    private Object getValueObject( String valueS, String type )
  +            throws MalformedObjectNameException
  +    {
  +        if( type==null )
  +            return valueS;
  +        if( "int".equals( type ) || "java.lang.Integer".equals(type) ) {
  +            return new Integer( valueS);
  +        }
  +        if( "ObjectName".equals( type ) || "javax.management.ObjectName".equals(type) )
{
  +            return new ObjectName( valueS);
  +        }
  +        return valueS;
       }
   
       private void processArg(Node mbeanN) {
  
  
  
  1.2       +5 -0      jakarta-commons/modeler/src/java/org/apache/commons/modeler/util/DomUtil.java
  
  Index: DomUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/util/DomUtil.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DomUtil.java	26 Dec 2002 18:12:39 -0000	1.1
  +++ DomUtil.java	29 Dec 2002 18:01:42 -0000	1.2
  @@ -106,6 +106,9 @@
                   name.equals( node.getNodeName() ) ) {
                   return node;
               }
  +            if( name == null ) {
  +                return node;
  +            }
           }
           return null;
       }
  @@ -148,6 +151,8 @@
                node = node.getNextSibling()) {
               if( node.getNodeType() != type ) continue;
               //System.out.println("getNode: " + name + " " + node.getNodeName());
  +            if( name==null )
  +                return node;
               if( name.equals( node.getNodeName() ) ) {
                   return node;
               }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message