User: andreas 
  Date: 00/11/29 18:50:36

  Modified:    examples/jboss.admin/src/org/jboss/jBossAdmin
                        GenericMBeanCustomizer.java
  Added:       examples/jboss.admin/src/org/jboss/jBossAdmin
                        GenericMBeanMethodDialog.java Util.java
  Log:
  New Generic Method Input dialog for MBeans
  
  Revision  Changes    Path
  1.3       +8 -35     
ejx/examples/jboss.admin/src/org/jboss/jBossAdmin/GenericMBeanCustomizer.java
  
  Index: GenericMBeanCustomizer.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/ejx/examples/jboss.admin/src/org/jboss/jBossAdmin/GenericMBeanCustomizer.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- GenericMBeanCustomizer.java       2000/11/28 17:29:24     1.2
  +++ GenericMBeanCustomizer.java       2000/11/30 02:50:35     1.3
  @@ -43,7 +43,6 @@
   
   import javax.swing.*;
   
  -import com.dreambean.awt.GenericMethodDialog;
   import com.dreambean.awt.GenericPropertyCustomizer;
   
   import org.jboss.jmx.interfaces.JMXConnector;
  @@ -55,7 +54,7 @@
   * of the nature of the manageable interface of the MBean.
   *
   * @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
  -* @version    $Revision: 1.2 $ 
  +* @version    $Revision: 1.3 $ 
   **/
   public class GenericMBeanCustomizer
        extends JScrollPane
  @@ -142,39 +141,10 @@
                                addSubtitle( "Attributes" );
                                while( i.hasNext() ) {
                                        MBeanAttributeInfo lFeature = 
(MBeanAttributeInfo) i.next();
  -                                     String lType = lFeature.getType();
  -                                     Class lClass = null;
  -                                     if( lType.equals( "int" ) ) {
  -                                             lClass = Integer.TYPE;
  -                                     }
  -                                     else if( lType.equals( "short" ) ) {
  -                                             lClass = Short.TYPE;
  -                                     }
  -                                     else if( lType.equals( "long" ) ) {
  -                                             lClass = Long.TYPE;
  -                                     }
  -                                     else if( lType.equals( "byte" ) ) {
  -                                             lClass = Byte.TYPE;
  -                                     }
  -                                     else if( lType.equals( "char" ) ) {
  -                                             lClass = Character.TYPE;
  -                                     }
  -                                     else if( lType.equals( "float" ) ) {
  -                                             lClass = Float.TYPE;
  -                                     }
  -                                     else if( lType.equals( "double" ) ) {
  -                                             lClass = Double.TYPE;
  -                                     }
  -                                     else if( lType.equals( "boolean" ) ) {
  -                                             lClass = Boolean.TYPE;
  -                                     }
  -                                     if( lClass == null ) {
  -                                             lClass = Class.forName( lType );
  -                                     }
                                        PropertyEditor lEditor = 
PropertyEditorManager.findEditor(
  -                                             lClass
  +                                             Util.getClass( lFeature.getType() )
                                        );
  -                                     System.out.println( "Got class: " + lClass + 
", editor: " + lEditor );
  +                                     System.out.println( "Got class: " + 
Util.getClass( lFeature.getType() ) + ", editor: " + lEditor );
                                        // No editor is available therefore use just 
one displaying the
                                        // string representation and allowing no 
editing
                                        if( lEditor == null ) {
  @@ -770,9 +740,12 @@
                                }
                        }
                        else {
  -/* AS Make a new class using MBeanOperationInfo instead of "md"
  -                             new GenericMethodDialog( object, md, ( Frame ) lFrame 
);
  -*/
  +                             new GenericMBeanMethodDialog(
  +                                     mResource.getConnector(),
  +                                     mResource.getName(),
  +                                     mInfo,
  +                                     ( Frame ) lFrame
  +                             );
                        }
                }
        }
  
  
  
  1.1                  
ejx/examples/jboss.admin/src/org/jboss/jBossAdmin/GenericMBeanMethodDialog.java
  
  Index: GenericMBeanMethodDialog.java
  ===================================================================
  /*
   * Copyright 1999 by dreamBean Software,
   * All rights reserved.
   */
  package org.jboss.jBossAdmin;
  
  import java.beans.*;
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import java.lang.reflect.*;
  
  import java.util.Arrays;
  import java.util.Iterator;
  import java.util.Vector;
  
  import javax.management.InstanceNotFoundException;
  import javax.management.MBeanOperationInfo;
  import javax.management.MBeanParameterInfo;
  import javax.management.ObjectName;
  
  import com.dreambean.awt.GenericPropertyCustomizer;
  
  import org.jboss.jmx.interfaces.JMXConnector;
  
  /**
  * General method invoker dialog for MBeans. It creates property editors for all
  * parameters and an "Ok" and "Cancel" button. If the user presses "Ok" the
  * corresponding method is invoked with the given parameters. 
  *
  * @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
  * @author Rickard �berg ([EMAIL PROTECTED]): Writer of the original 
GenericMethodDialog class.
  * @version $Revision: 1.1 $
  */
  public class GenericMBeanMethodDialog
        extends JDialog
        implements ActionListener
  {
        // Attributes ----------------------------------------------------
        private MBeanOperationInfo mOperation;
        private JMXConnector mConnector;
        private ObjectName mService;
  
        private Vector mEditors = new Vector();
        
        // Constructor ---------------------------------------------------
        
        /**
        *   Create a new dialog.
        *
        * @param pConnector Connector to the remote MBean Server
        * @param pService Remote Service Bean on which the method should be invoked
        * @param pOperation Operation to be invoked if accepted
        * @param pOwner Owner of the dialog controlling its appearance
        **/
        public GenericMBeanMethodDialog(
                JMXConnector pConnector,
                ObjectName pService,
                MBeanOperationInfo pOperation,
                Frame pOwner
        ) {
                super( pOwner, true );
  
                // Check the parameters
                if( pConnector == null ) {
                        throw new IllegalArgumentException( "Connector must be 
defined" );
                }
                if( pService == null ) {
                        throw new IllegalArgumentException( "Service must be defined" 
);
                }
                if( pOperation == null ) {
                        throw new IllegalArgumentException( "Operation must be 
defined" );
                }
                mConnector = pConnector;
                mService = pService;
                mOperation = pOperation;
  
                JPanel con = (JPanel) getContentPane();
                con.setLayout( new GridBagLayout() );
                
                try {
                        GridBagConstraints c = new GridBagConstraints();
                        c.insets = new Insets(5,5,5,5);
                        c.anchor = GridBagConstraints.NORTH;
                        c.weighty = 1;
                        
                        // Header
                        JLabel lName = new JLabel( mOperation.getName() );
                        c.gridwidth = GridBagConstraints.REMAINDER;
                        ((GridBagLayout)con.getLayout()).setConstraints( lName, c );
                        con.add( lName );
                        
                        // Parameters
                        Iterator i = Arrays.asList( mOperation.getSignature() 
).iterator();
                        while( i.hasNext() ) {
                                MBeanParameterInfo lParameter = (MBeanParameterInfo) 
i.next();
                                PropertyEditor lEditor = 
PropertyEditorManager.findEditor(
                                        Util.getClass( lParameter.getType() )
                                );
  
                                if( lEditor != null ) {
                                        lName = new JLabel( lParameter.getName() + 
":", JLabel.RIGHT );
                                        c.gridwidth = GridBagConstraints.RELATIVE;
                                        c.fill = GridBagConstraints.NONE;
                                        c.weightx = 0;
                                        ( (GridBagLayout) con.getLayout() 
).setConstraints( lName, c );
                                        con.add( lName );
                                        
                                        // Set initial value
                                        if( lEditor.getTags() != null ) {
                                                // Set to first value
                                                lEditor.setAsText( 
lEditor.getTags()[0] );
                                        }
                        
                                        Component lEditorComp;
                                        if( lEditor.supportsCustomEditor() ) {
                                                lEditorComp = 
lEditor.getCustomEditor();
                                        }
                                        else {
                                                String[] lTags = lEditor.getTags();
                                                if( lTags != null ) {
                                                        lEditorComp = new 
GenericPropertyCustomizer( lEditor, lTags );
                                                }
                                                else {                  
                                                        lEditorComp = new 
GenericPropertyCustomizer( lEditor );
                                                }
                                        }
                                        
                                        c.gridwidth = GridBagConstraints.REMAINDER;
                                        c.fill = GridBagConstraints.HORIZONTAL;
                                        c.weightx = 1;
                                        
((GridBagLayout)con.getLayout()).setConstraints( lEditorComp, c );
                                        con.add( lEditorComp );
                                        
                                        mEditors.addElement( new Editor( lEditor, 
lParameter.getType() ) );
                                }
                                c.weighty = 1;
                        }
                        
                        // Button
                        c.gridwidth = GridBagConstraints.REMAINDER;
                        JPanel p = new JPanel();
                        JButton ok = new JButton( "Ok" );
                        p.add( ok );
                        ok.addActionListener( this );
                        JButton cancel = new JButton("Cancel");
                        p.add( cancel );
                        cancel.addActionListener( this );
                        ( (GridBagLayout) con.getLayout()).setConstraints( p, c );
                        con.add( p );
                        
                        pack();
                        if( getWidth() < 300 ) {
                                setSize( new Dimension( 300,getHeight() ) );
                        }
                        setLocationRelativeTo( pOwner );
                        setVisible( true );
                }
                catch( Exception e ) {
                        System.out.println( "Exception occurred" );
                        e.printStackTrace();
                }
        }
  
        // Public --------------------------------------------------------
        
        // ActionListener implementation ---------------------------------
        /**
        *   Ok or Cancel has been pressed.
        *
        * @param   e  the event
        */
        public void actionPerformed( ActionEvent e ) {
                if( e.getActionCommand().equals( "Ok" ) ) {
                        Object[] params = new Object[ mEditors.size() ];
                        String[] lTypes = new String[ mEditors.size() ];
                        Iterator i = mEditors.iterator();
                        int j = 0;
                        while( i.hasNext() ) {
                                Editor lEditor = (Editor) i.next();
                                params[ j ] = lEditor.getEditor().getValue();
                                lTypes[ j ] = lEditor.getType();
                                j++;
                        }
                
                        try     {
                                Object lReturn = mConnector.invoke(
                                        mService,
                                        mOperation.getName(),
                                        params,
                                        lTypes
                                );
                                if( lReturn != null ) {
                                        JOptionPane.showMessageDialog(
                                                this,
                                                lReturn.toString(),
                                                "Result",
                                                JOptionPane.INFORMATION_MESSAGE
                                        );
                                }
                        }
                        catch( InstanceNotFoundException ex ) {
                                ex.printStackTrace();
                                JOptionPane.showMessageDialog(
                                        this,
                                        ex.getMessage(),
                                        "Error",
                                        JOptionPane.ERROR_MESSAGE
                                );
                        } 
                        catch( Exception ex ) {
                                System.err.println(ex);
                                JOptionPane.showMessageDialog(
                                        this,
                                        "An exception occured. Check log for details",
                                        "Error",
                                        JOptionPane.ERROR_MESSAGE
                                );
                        }
                }
                setVisible(false);
        }
        
        private class Editor {
                private PropertyEditor mEditor;
                private String mType;
                
                public Editor( PropertyEditor pEditor, String pType ) {
                        mEditor = pEditor;
                        mType = pType;
                }
                public PropertyEditor getEditor() {
                        return mEditor;
                }
                public String getType() {
                        return mType;
                }
        }
  }
  
  
  
  1.1                  ejx/examples/jboss.admin/src/org/jboss/jBossAdmin/Util.java
  
  Index: Util.java
  ===================================================================
  /*
   * jBoss, the OpenSource EJB server
   * 
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.jBossAdmin;
  
  /**
  * Utility class for this Admin
  *
  * @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
  * @version    $Revision: 1.1 $ 
  **/
  public class Util {
        /**
        * Returns the Class instance for the given class name
        *
        * @param pFullPathClassName Full qualified class name
        * @return Class instance found or null
        **/
        public static Class getClass( String pFullPathClassName ) {
                Class lClass = null;
                if( pFullPathClassName.equals( "int" ) ) {
                        lClass = Integer.TYPE;
                }
                else if( pFullPathClassName.equals( "short" ) ) {
                        lClass = Short.TYPE;
                }
                else if( pFullPathClassName.equals( "long" ) ) {
                        lClass = Long.TYPE;
                }
                else if( pFullPathClassName.equals( "byte" ) ) {
                        lClass = Byte.TYPE;
                }
                else if( pFullPathClassName.equals( "char" ) ) {
                        lClass = Character.TYPE;
                }
                else if( pFullPathClassName.equals( "float" ) ) {
                        lClass = Float.TYPE;
                }
                else if( pFullPathClassName.equals( "double" ) ) {
                        lClass = Double.TYPE;
                }
                else if( pFullPathClassName.equals( "boolean" ) ) {
                        lClass = Boolean.TYPE;
                }
                if( lClass == null ) {
                        try {
                                lClass = Class.forName( pFullPathClassName );
                        }
                        catch( Exception e ) {
                                e.printStackTrace();
                        }
                }
                return lClass;
        }
  }
  
  
  

Reply via email to