User: oberg   
  Date: 00/12/07 07:48:15

  Added:       src/main/org/jboss/util ServiceControl.java
                        ServiceControlMBean.java Shutdown.java
                        ShutdownMBean.java
  Log:
  Added ServiceControl
  Added Shutdown
  Added jaws.dtd
  
  Revision  Changes    Path
  1.1                  jboss/src/main/org/jboss/util/ServiceControl.java
  
  Index: ServiceControl.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.util;
  
  import java.io.*;
  import java.net.*;
  import java.util.*;
  
  import javax.management.*;
  
  import org.jboss.logging.Log;
  
  /**
   *   <description> 
   *      
   *   @see <related>
   *   @author Rickard �berg ([EMAIL PROTECTED])
   *   @version $Revision: 1.1 $
   */
  public class ServiceControl
     implements ServiceControlMBean, MBeanRegistration, NotificationListener
  {
     // Constants -----------------------------------------------------
     public static final String OBJECT_NAME = ":service=ServiceControl";
      
     // Attributes ----------------------------------------------------
     Log log = new Log("Service Control");
     
     List mbeans = new ArrayList();
     MBeanServer server;
     
     // Static --------------------------------------------------------
  
     // Constructors --------------------------------------------------
     
     // Service implementation ----------------------------------------
     public void init()
        throws Exception
     {
        log.log("Initializing "+mbeans.size()+" MBeans");
        
        List mbeansCopy = new ArrayList(mbeans);
        Iterator enum = mbeansCopy.iterator();
        int serviceCounter = 0;
        while (enum.hasNext())
        {
           ObjectName name = (ObjectName)enum.next();
           try
           {
              server.invoke(name, "init", new Object[0], new String[0]);
              serviceCounter++;
              
              // Register start/stop listener
              server.addNotificationListener(name,
                                            this,
                                            null,
                                            name);
           } catch (ReflectionException e)
           {
             // Not a service - ok 
           } catch (Exception e)
           {
              log.error("Could not initialize "+name);
              log.exception(e);
           }
        }
        log.log("Initialized "+mbeansCopy.size()+" services");
     }
     
     public void start()
        throws Exception
     {
        log.log("Starting "+mbeans.size()+" MBeans");
        
        List mbeansCopy = new ArrayList(mbeans);
        Iterator enum = mbeansCopy.iterator();
        int serviceCounter = 0;
        while (enum.hasNext())
        {
           ObjectName name = (ObjectName)enum.next();
           
           try
           {
              server.invoke(name, "start", new Object[0], new String[0]);
              serviceCounter++;
           } catch (ReflectionException e)
           {
             // Not a service - ok 
           } catch (Throwable e)
           {
              log.error("Could not start "+name);
              
              if (e instanceof RuntimeErrorException)
              {
                 e = ((RuntimeErrorException)e).getTargetError();
              }
              
              log.exception(e);
           }
        }
        log.log("Started "+mbeansCopy.size()+" services");
     }
     
     public void stop()
     {
        
     }
     
     public void destroy()
     {
        
     }
     
     // MBeanRegistration implementation ------------------------------
     public ObjectName preRegister(MBeanServer server, ObjectName name)
        throws java.lang.Exception
     {
        this.server = server;
        return name == null ? new ObjectName(OBJECT_NAME) : name;
     }
     
     public void postRegister(java.lang.Boolean registrationDone)
     {
        try
        {
           server.addNotificationListener(new 
ObjectName("JMImplementation:type=MBeanServerDelegate"),
                                         this,
                                         null,
                                         null);
           log.log("Registered with server");
        } catch (Exception e)
        {
           log.error("Could not register with server");
           log.exception(e);
        }
     }
     
     public void preDeregister()
        throws java.lang.Exception
     {
        server.removeNotificationListener(new 
ObjectName("JMImplementation:type=MBeanServerDelegate"), this);
        log.log("Deregistered from server");
     }
     
     public void postDeregister()
     {
     }
     
     // NotificationListener implementation ---------------------------
     public void handleNotification(Notification notification,
                                 java.lang.Object handback)
     {
        if (notification instanceof AttributeChangeNotification)
        {
           AttributeChangeNotification attrChg = 
(AttributeChangeNotification)notification;
  //         
log.log(handback+":"+attrChg.getAttributeName()+":"+attrChg.getNewValue());
        } else
        {
           MBeanServerNotification reg = (MBeanServerNotification)notification;
        
           if (reg.getType().equals(MBeanServerNotification.REGISTRATION_NOTIFICATION))
           {
              mbeans.add(reg.getMBeanName());
           } else if 
(reg.getType().equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION))
           {
              mbeans.remove(reg.getMBeanName());
           }
        }
     }
  }
  
  
  
  
  1.1                  jboss/src/main/org/jboss/util/ServiceControlMBean.java
  
  Index: ServiceControlMBean.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.util;
  
  /**
   *   <description> 
   *      
   *   @see <related>
   *   @author Rickard �berg ([EMAIL PROTECTED])
   *   @version $Revision: 1.1 $
   */
  public interface ServiceControlMBean
     extends Service
  {
     // Public --------------------------------------------------------
  }
  
  
  
  
  1.1                  jboss/src/main/org/jboss/util/Shutdown.java
  
  Index: Shutdown.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.util;
  
  import java.io.*;
  import java.net.*;
  import java.util.*;
  
  import javax.management.*;
  
  import org.jboss.logging.Log;
  
  /**
   *   <description> 
   *      
   *   @see <related>
   *   @author Rickard �berg ([EMAIL PROTECTED])
   *   @version $Revision: 1.1 $
   */
  public class Shutdown
     implements MBeanRegistration, ShutdownMBean
  {
     // Constants -----------------------------------------------------
     public static final String OBJECT_NAME = ":type=Shutdown";
      
     // Attributes ----------------------------------------------------
     Log log = new Log("Shutdown");
     
     List mbeans = new ArrayList();
     MBeanServer server;
     
     // Public  -------------------------------------------------------
     public void shutdown()
     {
        System.exit(0); // This will execute the shutdown hook
     }
     
     // MBeanRegistration implementation ------------------------------
     public ObjectName preRegister(final MBeanServer server, ObjectName name)
        throws java.lang.Exception
     {
        this.server = server;
        try
        {
           Runtime.getRuntime().addShutdownHook(new Thread()
           {
              public void run()
              {
                 System.out.println("Shutting down");
                 
                 // Make sure all services are down properly
                 shutdownServices();
                 
                 System.out.println("Shutdown complete");
              }
           });
           log.log("Shutdown hook added");
        } catch (Throwable e)
        {
           log.error("Could not add shutdown hook");
        }
        return name == null ? new ObjectName(OBJECT_NAME) : name;
     }
     
     public void postRegister(java.lang.Boolean registrationDone)
     {
     }
     
     public void preDeregister()
        throws java.lang.Exception
     {
     }
     
     public void postDeregister()
     {
     }
     
     protected void shutdownServices()
     {
        try
        {
           // Stop services
           server.invoke(new ObjectName(":service=ServiceControl"), "stop", new 
Object[0] , new String[0]);
        } catch (Exception e)
        {
           e.printStackTrace();
        }
  
        try
        {
           // Destroy services
           server.invoke(new ObjectName(":service=ServiceControl"), "destroy", new 
Object[0] , new String[0]);
        } catch (Exception e)
        {
           e.printStackTrace();
        }
     }
  }
  
  
  
  
  1.1                  jboss/src/main/org/jboss/util/ShutdownMBean.java
  
  Index: ShutdownMBean.java
  ===================================================================
  /*
   * JBoss, the OpenSource EJB server
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.util;
  
  /**
   *   <description> 
   *      
   *   @see <related>
   *   @author Rickard �berg ([EMAIL PROTECTED])
   *   @version $Revision: 1.1 $
   */
  public interface ShutdownMBean
  {
     // Public --------------------------------------------------------
     public void shutdown();
  }
  
  
  
  
  
  
  
  

Reply via email to