User: andreas 
  Date: 00/11/19 15:28:59

  Added:       examples/jboss.admin/src/org/jboss/jBossAdmin MainPane.java
                        RemoteResource.java RemoteServer.java
                        ResourceManagerFactoryImpl.java
                        ResourceManagerImpl.java
  Log:
  First draft for a jBoss Administration GUI with EJX.
  At the moment it just uses the JMX Connector to
  connect to the jBoss server and list all the services but
  you cannot do anything more that this for now.
  
  Revision  Changes    Path
  1.1                  ejx/examples/jboss.admin/src/org/jboss/jBossAdmin/MainPane.java
  
  Index: MainPane.java
  ===================================================================
  /*
   * jBoss, the OpenSource EJB server
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.jBossAdmin;
  
  import java.awt.BorderLayout;
  import java.awt.Component;
  import java.beans.Customizer;
  import java.beans.beancontext.BeanContextSupport;
  import java.beans.beancontext.BeanContextChildComponentProxy;
  import java.util.Collection;
  import java.util.Iterator;
  
  import javax.management.MBeanServer;
  import javax.management.MBeanServerFactory;
  import javax.management.ObjectInstance;
  import javax.management.ObjectName;
  import javax.swing.JTabbedPane;
  
  import com.dreambean.awt.GenericCustomizer;
  
  import org.jboss.jmx.interfaces.JMXConnector;
  
  /**
  * Class containing the Bean Context Support and
  * the viewer GUI component.
  * It creates the necessary environment to lookup
  * remote MBeanServers from which the user can choose
  * one.
  * The inner Viewer Class will then create a GUI showing
  * this list of remote MBeans.
  *
  * @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
  * @version $Revision: 1.1 $
  **/
  public class MainPane
     extends BeanContextSupport
     implements BeanContextChildComponentProxy
  {
        // Constants -----------------------------------------------------
        
        // Attributes ----------------------------------------------------
        // There is only one Customizer for all the instances
        private Customizer mCustomizer;
  
        private String mJNDIServer = "";
        private String mMBeanServerQuery = "";
      private MBeanServer     mServer;
      private ObjectInstance  mFactory;
        
        
        // Static --------------------------------------------------------
        
        // Constructors --------------------------------------------------
        public MainPane() {
                super();
          try {
              // Starts the MBeanServer and instanciate the Connector Factory
              // First create a MBeanServer and let it start
              mServer = MBeanServerFactory.createMBeanServer();
              // Then register the logger
              mServer.createMBean(
                  "org.jboss.logging.Logger",
                  new ObjectName( "DefaultDomain:name=Logger" )
              );
              // Then register the connector factory
              mFactory = mServer.createMBean(
                  "org.jboss.jmx.client.ConnectorFactoryService",
                  new ObjectName( "DefaultDomain:name=ConnectorFactory" )
              );
          }
          catch( Exception e ) {
              e.printStackTrace();
          }
        }
        
        // Public --------------------------------------------------------
        /*
        * There are the properties the Main Pane offer and which can
        * then be set by GUI component defined by the Bean Context
        * ATTENTION: All the properties in the BeanInfo XML file need
        * here a public getter and setter method with the appropriate
        * type.
        */
        public String getJNDIServer() {
                return mJNDIServer;
        }
        public void setJNDIServer( String pToSet ) {
                mJNDIServer = pToSet;
        }
        public String getMBeanServerQuery() {
                return mMBeanServerQuery;
        }
        public void setMBeanServerQuery( String pToSet ) {
                mMBeanServerQuery = pToSet;
        }
  
        public void performSearch()
          throws Exception
        {
                System.out.println( "Before a search on JNDI Server: " + mJNDIServer
              + ", MBeanServerQuery: " + mMBeanServerQuery );
        }
  
        // BeanContextChildComponentProxy implementation -----------------
        /**
        * Creates and returns the GUI component which is used to display
        * the properties and information of this instance by a Generic
        * Customiser.
        *
        * @return                               GUI Component displaying this instances
        *                                               properties
        **/
        public Component getComponent() {
                if( mCustomizer == null ) {
                        mCustomizer = new Viewer();
                        mCustomizer.setObject( this );
                }
                return (Component) mCustomizer;
        }
        /**
        * Displays the component of the outer class
        **/
        public class Viewer
                extends JTabbedPane
                implements Customizer
        {
                // Customizer implementation  ------------------------------------
                public void setObject( Object pDataObject ) {
                        // Init UI
                        addTab(
                                "JNDI - Server", 
                                new GenericCustomizer( pDataObject )
                        );
              try {
                  // Take the first server and its first protoccol and create a
                  // connection to the remote MBeanServer
                  // Now let's list the available servers, protocols
                  Collection lServers = (Collection) mServer.invoke(
                      mFactory.getObjectName(),
                      "getServers",
                      new String[] {
                          null
                      },
                      new String[] {
                          "java.lang.String"
                      }
                  );
                  Iterator i = lServers.iterator();
                  if( !i.hasNext() ) {
                      return;
                  }
                  String lServer = (String) i.next();
                  Collection lProtocols = (Collection) mServer.invoke(
                      mFactory.getObjectName(),
                      "getProtocols",
                      new String[] {
                          lServer,
                      },
                      new String[] {
                          "java.lang.String"
                      }
                  );
                  i = lProtocols.iterator();
                  if( !i.hasNext() ) {
                      return;
                  }
                  String lProtocol = (String) i.next();
                  // Get the connector
                  JMXConnector lConnector = (JMXConnector) mServer.invoke(
                      mFactory.getObjectName(),
                      "createConnection",
                      new Object[] {
                          lServer,
                          lProtocol
                      },
                      new String[] {
                          "java.lang.String",
                          "java.lang.String"
                      }
                  );
                  // Create the Remote Server instance, add it to the GUI and set the 
connector
                  RemoteServer lRemote = new RemoteServer();
                  addTab(
                      "Default: Servers",
                      lRemote.getComponent()
                  );
                  lRemote.setConnector( lConnector );
              }
              catch( Exception e ) {
                  e.printStackTrace();
              }
                }
        }
  }
  
  
  
  1.1                  
ejx/examples/jboss.admin/src/org/jboss/jBossAdmin/RemoteResource.java
  
  Index: RemoteResource.java
  ===================================================================
  /*
   * Copyright 1999 by dreamBean Software,
   * All rights reserved.
   */
  package org.jboss.jBossAdmin;
  
  import java.awt.*;
  import java.beans.*;
  import java.beans.beancontext.*;
  import java.io.*;
  import java.util.*;
  import java.lang.reflect.*;
  
  import javax.management.ObjectInstance;
  import javax.management.ObjectName;
  
  import com.dreambean.awt.BeanContextViewer;
  import com.dreambean.awt.GenericCustomizer;
  import com.dreambean.ejx.Util;
  
  import org.jboss.jmx.interfaces.JMXConnector;
  
  /**
  *   <description> 
  *      
  * @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
  * @version $Revision: 1.1 $
  **/
  public class RemoteResource
      extends BeanContextSupport
      implements BeanContextChildComponentProxy
  {
      // Constants -----------------------------------------------------
      
      // Attributes ----------------------------------------------------
        private JMXConnector mConnector;
      private ObjectInstance mService;
      
      private Customizer c;
      
      // Static --------------------------------------------------------
      
      // Constructors --------------------------------------------------
      
      // Public --------------------------------------------------------
      public void setReference( JMXConnector pConnector, ObjectInstance pReference ) {
                mConnector = pConnector;
          mService = pReference;
      }
  
        public String getName() {
          String lName = "";
          try {
              lName = (String) mConnector.getAttribute(
                        mService.getObjectName(),
                        "Name"
                    );
          }
          catch( Exception e ) {
              e.printStackTrace();
          }
          return lName;
        }
        
        public String getDescription() {
          String lDescription = "";
          try {
              lDescription = (String) mConnector.getAttribute(
                  mService.getObjectName(),
                  "Description"
              );
          }
          catch( Exception e ) {
              e.printStackTrace();
          }
          return lDescription;
        }
  
      public String toString() {
          return ( mService == null ? "" : mService.getObjectName().toString() );
      }
      
      /* AS ??
      public EnterpriseBean getEjb(String name)
        {
                Iterator enum = super.iterator();
                while(enum.hasNext())
                {
                        EnterpriseBean ejb = (EnterpriseBean)enum.next();
                        if (ejb.getEjbName().equals(name))
                                return ejb;
                }
                throw new IllegalArgumentException("No such bean");
        }
  */
  
      public Iterator iterator() {
          return Util.sortByClass( super.iterator() );
      }
  /*    
      public Iterator getServices() {
          return Util.getChildrenByClass( super.iterator(), RemoteService.class );
      }
  */
      
     /**
      *   This is a wizard-style method that tries to find all CMP-fields in 
EntityBeans.
      *
      */
  /* AS ??
      public void findCMPFields(String idName)
     {
        Iterator entities = getEntities();
        while (entities.hasNext())
        {
           Entity entity = (Entity)entities.next();
           
           // Check if container-managed
           if (entity.getPersistenceType().equals("Bean"))
              continue;
           
           // Build comma-separated list of current fields prefixed with "," and 
suffixed with ","
           Iterator currentFields = entity.getCMPFields();
           String list = ",";
           while(currentFields.hasNext())
           {
              list += ((CMPField)currentFields.next()).getFieldName() + ",";
           }
           
           ClassLoader cl = 
((EjbResourceManager)getBeanContext().getBeanContext()).getClassLoader();
           try
           {
              Class clazz = cl.loadClass(entity.getEjbClass());
              Field[] fields = clazz.getFields();
              for (int i = 0; i < fields.length; i++)
              {
                 String fieldName = fields[i].getName();
                 
                 // Check so that it doesn't exist yet - we don't want duplicates
                 if (list.indexOf(","+fieldName+",") == -1)
                    entity.createCMPField(fieldName);
                 
                 // Check for id-field
                 if (fieldName.equals(idName) && 
entity.getPrimaryKeyField().equals(""))
                 {
                    entity.setPrimaryKeyField(idName);
                    entity.setPrimaryKeyClass(fields[i].getType().getName());
                 }
              }
           } catch (Throwable e)
           {
              e.printStackTrace();
           }
           
        }
        
     }
     
     public void addEnvironmentEntry(String name, String type, String value)
        throws Exception
     {
        Iterator beans = iterator();
        while (beans.hasNext())
        {
           Object bean = beans.next();
           if (bean instanceof Entity)
           {
              Entity entity = (Entity)bean;
              EnvironmentEntry entry = entity.addEnvironmentEntry();
              entry.setName(name);
              entry.setType(type);
              entry.setValue(value);
           } else if (bean instanceof Session)
           {
              Session session = (Session)bean;
              EnvironmentEntry entry = session.addEnvironmentEntry();
              entry.setName(name);
              entry.setType(type);
              entry.setValue(value);
           }
        }
     }
     
     public void updateEnvironmentEntry(String name, String value)
        throws Exception
     {
        Iterator beans = iterator();
        while (beans.hasNext())
        {
           EnterpriseBean bean = (EnterpriseBean)beans.next();
           Iterator entries = bean.getEnvironmentEntries();
           while (entries.hasNext())
           {
              EnvironmentEntry entry = (EnvironmentEntry)entries.next();
              if (entry.getName().equals(name))
              {
                 entry.setValue(value);
                 break;
              }
           }
        }
     }
  */
     
      // BeanContextContainerProxy implementation -----------------
      public Component getComponent() {
          if( c == null ) {
              c = new GenericCustomizer();
              c.setObject( this );
          }
          return (Component) c;
      }
      
     // Package protected ---------------------------------------------
      
     // Protected -----------------------------------------------------
      
     // Private -------------------------------------------------------
  
     // Inner classes -------------------------------------------------
  }
  
  
  
  1.1                  
ejx/examples/jboss.admin/src/org/jboss/jBossAdmin/RemoteServer.java
  
  Index: RemoteServer.java
  ===================================================================
  /*
   * Copyright 1999 by dreamBean Software,
   * All rights reserved.
   */
  package org.jboss.jBossAdmin;
  
  import java.awt.*;
  import java.beans.*;
  import java.beans.beancontext.*;
  import java.io.*;
  import java.util.*;
  import java.lang.reflect.*;
  
  import javax.management.ObjectInstance;
  
  import com.dreambean.awt.BeanContextViewer;
  import com.dreambean.ejx.Util;
  
  import org.jboss.jmx.interfaces.JMXConnector;
  
  /**
  *   <description> 
  *      
  * @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
  * @version $Revision: 1.1 $
  **/
  public class RemoteServer
      extends BeanContextSupport
      implements BeanContextChildComponentProxy
  {
      // Constants -----------------------------------------------------
      
      // Attributes ----------------------------------------------------
      private JMXConnector mConnector;
      
      private Customizer c;
      
      // Static --------------------------------------------------------
      
      // Constructors --------------------------------------------------
      
      // Public --------------------------------------------------------
      public void setConnector( JMXConnector pConnector ) {
          try {
              if( pConnector != null ) {
                  mConnector = pConnector;
                  // Go through all services (MBeans) and add them to the list
                  Collection lServices = mConnector.queryMBeans( null, null );
                  Iterator i = lServices.iterator();
                  while( i.hasNext() ) {
                      ObjectInstance lResource = (ObjectInstance) i.next();
                      createResource( lResource );
                  }
              }
          }
          catch( Exception e ) {
              e.printStackTrace();
          }
      }
      
      public void createResource( ObjectInstance pResource )
          throws IOException, ClassNotFoundException
      {
          System.out.println( "Create new Resource: " + pResource.getObjectName() );
          addResource().setReference( mConnector, pResource );
      }
  
      public RemoteResource addResource()
          throws IOException, ClassNotFoundException
      {
          return (RemoteResource) instantiateChild( 
"org.jboss.jBossAdmin.RemoteResource" );
      }
      
  /* AS ??
      public EnterpriseBean getEjb(String name)
        {
                Iterator enum = super.iterator();
                while(enum.hasNext())
                {
                        EnterpriseBean ejb = (EnterpriseBean)enum.next();
                        if (ejb.getEjbName().equals(name))
                                return ejb;
                }
                throw new IllegalArgumentException("No such bean");
        }
  */
  
      public Iterator iterator() {
          return Util.sortByClass( super.iterator() );
      }
      
      public Iterator getResources() {
          return Util.getChildrenByClass( super.iterator(), RemoteResource.class );
      }
      
     /**
      *   This is a wizard-style method that tries to find all CMP-fields in 
EntityBeans.
      *
      */
  /* AS ??
      public void findCMPFields(String idName)
     {
        Iterator entities = getEntities();
        while (entities.hasNext())
        {
           Entity entity = (Entity)entities.next();
           
           // Check if container-managed
           if (entity.getPersistenceType().equals("Bean"))
              continue;
           
           // Build comma-separated list of current fields prefixed with "," and 
suffixed with ","
           Iterator currentFields = entity.getCMPFields();
           String list = ",";
           while(currentFields.hasNext())
           {
              list += ((CMPField)currentFields.next()).getFieldName() + ",";
           }
           
           ClassLoader cl = 
((EjbResourceManager)getBeanContext().getBeanContext()).getClassLoader();
           try
           {
              Class clazz = cl.loadClass(entity.getEjbClass());
              Field[] fields = clazz.getFields();
              for (int i = 0; i < fields.length; i++)
              {
                 String fieldName = fields[i].getName();
                 
                 // Check so that it doesn't exist yet - we don't want duplicates
                 if (list.indexOf(","+fieldName+",") == -1)
                    entity.createCMPField(fieldName);
                 
                 // Check for id-field
                 if (fieldName.equals(idName) && 
entity.getPrimaryKeyField().equals(""))
                 {
                    entity.setPrimaryKeyField(idName);
                    entity.setPrimaryKeyClass(fields[i].getType().getName());
                 }
              }
           } catch (Throwable e)
           {
              e.printStackTrace();
           }
           
        }
        
     }
     
     public void addEnvironmentEntry(String name, String type, String value)
        throws Exception
     {
        Iterator beans = iterator();
        while (beans.hasNext())
        {
           Object bean = beans.next();
           if (bean instanceof Entity)
           {
              Entity entity = (Entity)bean;
              EnvironmentEntry entry = entity.addEnvironmentEntry();
              entry.setName(name);
              entry.setType(type);
              entry.setValue(value);
           } else if (bean instanceof Session)
           {
              Session session = (Session)bean;
              EnvironmentEntry entry = session.addEnvironmentEntry();
              entry.setName(name);
              entry.setType(type);
              entry.setValue(value);
           }
        }
     }
     
     public void updateEnvironmentEntry(String name, String value)
        throws Exception
     {
        Iterator beans = iterator();
        while (beans.hasNext())
        {
           EnterpriseBean bean = (EnterpriseBean)beans.next();
           Iterator entries = bean.getEnvironmentEntries();
           while (entries.hasNext())
           {
              EnvironmentEntry entry = (EnvironmentEntry)entries.next();
              if (entry.getName().equals(name))
              {
                 entry.setValue(value);
                 break;
              }
           }
        }
     }
  */
     
      // BeanContextContainerProxy implementation -----------------
      public Component getComponent() {
          if (c == null) {
              c = new BeanContextViewer();
              c.setObject(this);
          }
          return (Component)c;
      }
      
     // Package protected ---------------------------------------------
      
     // Protected -----------------------------------------------------
      
     // Private -------------------------------------------------------
  
     // Inner classes -------------------------------------------------
  }
  
  
  
  1.1                  
ejx/examples/jboss.admin/src/org/jboss/jBossAdmin/ResourceManagerFactoryImpl.java
  
  Index: ResourceManagerFactoryImpl.java
  ===================================================================
  /*
   * jBoss, the OpenSource EJB server
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.jBossAdmin;
  
  import java.io.File;
  import java.net.URL;
  import javax.swing.filechooser.FileFilter;
  
  import com.dreambean.ejx.ResourceManager;
  import com.dreambean.ejx.ResourceManagerFactory;
  
  /**
  * ResourceManagerFactory implemenation which allows EJX
  * plugin to select an existing file or create one in the
  * given directory.
  * <BR>
  * The purpose of this class is to deliver a file filter to
  * select a file and to create a file manage implemenation
  * when the file is selected or a directory to put the new
  * file into.
  *      
  * @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
  * @version $Revision: 1.1 $
  **/
  public class ResourceManagerFactoryImpl
        extends FileFilter
        implements ResourceManagerFactory
  {
        // Public --------------------------------------------------------
        
        // FileFilter implementation -------------------------------------
        public boolean accept(File f)
        {
                return f.getName().equals( "jboss.admin.xml" ) || f.isDirectory();
        }
        
        public String getDescription() { 
                return toString(); 
        }
  
        // ResourceManagerFactory implementation -----------------------------
        public ResourceManager createResourceManager() {
                return new ResourceManagerImpl( this );
        }
        
        public FileFilter getFileFilter() {
                return this;
        }
        
        public String toString() {
                return "jBoss Admin";
        }
  }
  
  
  
  1.1                  
ejx/examples/jboss.admin/src/org/jboss/jBossAdmin/ResourceManagerImpl.java
  
  Index: ResourceManagerImpl.java
  ===================================================================
  /*
   * jBoss, the OpenSource EJB server
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.jBossAdmin;
  
  import java.awt.BorderLayout;
  import java.awt.Component;
  import java.beans.beancontext.BeanContextServicesSupport;
  import java.net.URL;
  
  import javax.swing.JTabbedPane;
  
  import com.dreambean.ejx.ResourceManager;
  import com.dreambean.ejx.ResourceManagerFactory;
  
  /**
  * ResourceManager handles the load and save of a given file
  * and create a new GUI component to display in EJX
  * when EJX is asking for.
  *      
  * @author <A href="mailto:[EMAIL PROTECTED]">Andreas "Mad" Schaefer</A>
  * @version $Revision: 1.1 $
  **/
  public class ResourceManagerImpl
        extends BeanContextServicesSupport
        implements ResourceManager
  {
        // Attributes ----------------------------------------------------
        /** The factory which created this instance **/
        private ResourceManagerFactory mFactory;
        
        // Constructors --------------------------------------------------
        /**
        * Creates this file manager and store the Factory created
        * this instance
        *
        * @param pCaller                                        File Manager Factory 
created this instance
        **/
        ResourceManagerImpl( ResourceManagerFactory pCaller ) {
                mFactory = pCaller;
        }
        
        // Public --------------------------------------------------------
        
        // ResourceManager implementation ------------------------------------
        public boolean isChanged() {
                return true;
        }
        
        public void createNew() {
        }
        
        public void load( URL pResource )
                throws Exception
        {
        }
        
        public void save( URL pResource )
                throws Exception
        {
        }
        
        public URL getResource() {
                return null;
        }
        
        public void setResource( URL pResource ) {
        }
        
        public ResourceManagerFactory getFactory() {
                return mFactory;
        }
  
        // BeanContextChildComponentProxy implementation -----------------
        public Component getComponent() {
                // Create the Property Container and return its GUI component
                return new MainPane().getComponent();
        }
  }
  
  
  

Reply via email to