User: vincent 
  Date: 00/08/24 19:30:26

  Modified:    src/java/org/spydermq/server Main.java
  Added:       src/java/org/spydermq/server StartServer.java
  Log:
  Factored out starting up the SpyderMQ server so that it can be started
  both standalone and inside another application (e.g. JBoss).
  
  Revision  Changes    Path
  1.11      +35 -210   spyderMQ/src/java/org/spydermq/server/Main.java
  
  Index: Main.java
  ===================================================================
  RCS file: /products/cvs/ejboss/spyderMQ/src/java/org/spydermq/server/Main.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Main.java 2000/06/19 21:52:01     1.10
  +++ Main.java 2000/08/25 02:30:25     1.11
  @@ -6,27 +6,8 @@
    * Distributable under GPL license.
    * See terms of license at gnu.org.
    */
  -
  -import org.spydermq.JMSServer;
  -import org.spydermq.security.SecurityManager;
  -import org.spydermq.distributed.interfaces.DistributedJMSServer;
  -import org.spydermq.distributed.interfaces.DistributedJMSServerSetup;
  -import org.spydermq.distributed.interfaces.DistributedTopicConnectionFactory;
  -import org.spydermq.distributed.interfaces.DistributedQueueConnectionFactory;
  -import org.spydermq.distributed.JMSServerFactory;
  -import org.spydermq.distributed.SpyTopicConnectionFactory;
  -import org.spydermq.distributed.SpyQueueConnectionFactory;
   import org.spydermq.Log;
   
  -import javax.jms.TopicConnectionFactory;
  -import javax.jms.QueueConnectionFactory;
  -import javax.jms.JMSException;
  -import javax.jms.Topic;
  -import javax.jms.Queue;
  -
  -import org.jnp.server.NamingServer;
  -import javax.naming.InitialContext;
  -import javax.naming.Context;
   import java.util.Properties;
   import java.io.InputStream;
   import java.util.StringTokenizer;
  @@ -44,197 +25,41 @@
    *   @author Norbert Lataille ([EMAIL PROTECTED])
    *   @author Rich Johns ([EMAIL PROTECTED])
    *
  - *   @version $Revision: 1.10 $
  + *   @version $Revision: 1.11 $
    */
  -public class Main implements Runnable
  +public class Main
   {
  -     SpyTopicConnectionFactory topicConnectionFactory;
  -     SpyQueueConnectionFactory queueConnectionFactory;
  -     DistributedTopicConnectionFactory distributedTopicConnectionFactory;
  -     DistributedQueueConnectionFactory distributedQueueConnectionFactory;
  -     DistributedJMSServerSetup theDistributedServerSetup;
  -     DistributedJMSServer theDistributedServer;
  -     SecurityManager securityManager;
  -     JMSServer theServer;
  -     MBeanServer mBeanServer;
  -
  -     //DEBUG -- For the RemoteControl object
  -     private static RemoteControlImpl remoteControl;
  -     
  -     public void run()
  -     {
  -             Log.notice("SpyderMQ [v0.5]");
  -             
  -             try { 
  -                     
  -                     //Load the property file
  -                     InputStream in = 
getClass().getClassLoader().getResource("spyderMQ.properties").openStream();
  -                     Properties cfg=new Properties();
  -                     cfg.load(in);
  -                     in.close();
  -
  -                     // By default we will start a JNDI Server. We won't
  -                     // if user explicitly tells us not to.
  -                     String noStartJNDI = (String)cfg.get("DoNotStartJNDI" );
  -                     if( (noStartJNDI == null) || (noStartJNDI.length() == 0) )
  -                     {
  -                             //Start the JNDI server
  -                             Log.notice( "Starting JNDI Server (JNP)" );
  -                             new org.jnp.server.Main().start();
  -                     }
  -                     else
  -                     {
  -                             // Need to warn user because if they fail to start
  -                             // a JNDI server prior to starting spyder, they
  -                             // will be confused by the resulting error.
  -                             Log.notice("[Warning]: SpyderMQ.properties specifys 
NOT to start a JNDI Server.");
  -                             Log.notice("           If a JNDI Server is not running 
SpyderMQ will not start.");
  -                     }
  -                     
  -                     //Create an MBeanServer
  -                     mBeanServer = new MBeanServer();
  -
  -                     // Create MLet
  -                     MLet mlet = new MLet();
  -                     mBeanServer.registerMBean(mlet, new 
ObjectName(mBeanServer.getDefaultDomain(), "service", "MLet"));
  -                              
  -                     // Set MLet as classloader for this app
  -                     Thread.currentThread().setContextClassLoader(mlet);
  -
  -                     // Read default configuration
  -                     URL mletConf = 
getClass().getClassLoader().getResource("spyderMQ.plugin");
  -                     Set beans = (Set)mlet.getMBeansFromURL(mletConf);
  -                     Iterator enum = beans.iterator();
  -                     while (enum.hasNext())
  -                     {
  -                        Object obj = enum.next();
  -                        if (obj instanceof Exception) Log.error(obj);
  -                     }
  -
  -                     //Start the adaptor
  -                     mBeanServer.invoke(new 
ObjectName("Adaptor:name=html,port=8082"), "start", new Object[0], new String[0]);
  -                     
  -                     //Get an InitialContext
  -                     InitialContext ctx=new InitialContext();
  -
  -                     //Create a SecurityManager object
  -                     securityManager=new SecurityManager();
  -
  -                     //Create the JMSServer object
  -                     theServer = new JMSServer(securityManager);
  -                     theDistributedServerSetup = 
JMSServerFactory.createJMSServer(theServer,cfg);
  -                     theDistributedServer=theDistributedServerSetup.createClient();
  -                     String 
connectionReceiverCN=(String)cfg.get("ConnectionReceiverClassName");
  -                     mBeanServer.registerMBean(theServer, new 
ObjectName(JMSServer.OBJECT_NAME));
  -                     mBeanServer.registerMBean(theDistributedServerSetup, new 
ObjectName("JMS:service=DistributedJMSServerSetup"));
  -             
  -                     //Get the Topic properties
  -                     String 
topicConnectionFactoryCN=(String)cfg.get("DistributedTopicConnectionFactoryClassName");
  -                     if 
(topicConnectionFactoryCN==null||connectionReceiverCN==null) throw new 
RuntimeException("Missing configuration parameters");
  -
  -                     //Create the distributedTopicConnectionFactory object          
         
  -                     distributedTopicConnectionFactory = 
(DistributedTopicConnectionFactory)Class.forName(topicConnectionFactoryCN).newInstance();
  -                     
distributedTopicConnectionFactory.setServer(theDistributedServer);
  -                     
distributedTopicConnectionFactory.setCRClassName(connectionReceiverCN);
  -                     
distributedTopicConnectionFactory.setSecurityManager(securityManager);
  -                     mBeanServer.registerMBean(distributedTopicConnectionFactory, 
new ObjectName("JMS:service=DistributedTopicConnectionFactory"));
  -             
  -                     //Create the topicConnectionFactory object                     
 
  -                     topicConnectionFactory = new 
SpyTopicConnectionFactory(distributedTopicConnectionFactory);
  -
  -                     //create the known topics
  -                     Context subcontext=ctx.createSubcontext("topic");
  -                     String topics=(String)cfg.get("knownTopics");
  -                     
  -                     if (topics!=null) {
  -                             
  -                             StringTokenizer st = new StringTokenizer(topics,", ");
  -                     
  -                             while (st.hasMoreElements()) {
  -                                     String name=(String)st.nextElement();
  -                                     Topic t=theServer.newTopic(name);
  -                                     subcontext.rebind(name,t);
  -                             }
  -                             
  -                     } else Log.notice("Warning: no known Topics !"); 
  -                     
  -                     //Get the queue properties
  -                     String 
queueConnectionFactoryCN=(String)cfg.get("DistributedQueueConnectionFactoryClassName");
  -                     if (queueConnectionFactoryCN==null) throw new 
RuntimeException("Missing configuration parameter");
  -
  -                     //Create the distributedTopicConnectionFactory object          
         
  -                     distributedQueueConnectionFactory = 
(DistributedQueueConnectionFactory)Class.forName(queueConnectionFactoryCN).newInstance();
  -                     
distributedQueueConnectionFactory.setServer(theDistributedServer);
  -                     
distributedQueueConnectionFactory.setCRClassName(connectionReceiverCN);
  -                     
distributedQueueConnectionFactory.setSecurityManager(securityManager);
  -                     mBeanServer.registerMBean(distributedQueueConnectionFactory, 
new ObjectName("JMS:service=DistributedQueueConnectionFactory"));
  -
  -                     //Create the topicConnectionFactory object                     
 
  -                     queueConnectionFactory = new 
SpyQueueConnectionFactory(distributedQueueConnectionFactory);
  -
  -                     //create the known queues
  -                     subcontext=ctx.createSubcontext("queue");
  -                     String queues=(String)cfg.get("knownQueues");
  -                     
  -                     if (queues!=null) {
  -                             
  -                             StringTokenizer st = new StringTokenizer(queues,", ");
  -                     
  -                             while (st.hasMoreElements()) {
  -                                     String name=(String)st.nextElement();
  -                                     Queue q=theServer.newQueue(name);
  -                                     subcontext.rebind(name,q);
  -                             }
  -                             
  -                     } else Log.notice("Warning: no known Queues !"); 
  -
  -                     //Set the known Ids
  -                     String ids=(String)cfg.get("knownIds");
  -
  -                     if (ids!=null) {
  -                             
  -                             StringTokenizer st = new StringTokenizer(ids,", ");
  -                     
  -                             while (st.hasMoreElements()) {
  -                                     String read=(String)st.nextElement();
  -                                     int pos=read.indexOf(':');
  -                                     if (pos==-1) throw new JMSException("Bad 
configuration file (missing separator in knownIds)");
  -                                     String name=read.substring(0,pos);
  -                                     String passwd=read.substring(pos+1);
  -                                     pos=passwd.indexOf(':');
  -                                                                             
  -                                     if (pos==-1) {
  -                                             Log.log("[JMSServer] new user : Login 
= "+name+", Id = [none]");                                                        
  -                                             
securityManager.addUser(name,passwd,null);
  -                                     } else {
  -                                             String ID=passwd.substring(pos+1);
  -                                             Log.log("[JMSServer] new user : Login 
= "+name+", Id = "+ID);                                                   
  -                                             
securityManager.addUser(name,passwd.substring(0,pos),ID);
  -                                     }
  -                                     
  -                             }
  -                             
  -                     } else Log.notice("Warning: no known Ids !"); 
  -                     
  -                     //(re)bind the connection factories in the JNDI namespace
  -                     ctx.rebind("TopicConnectionFactory",topicConnectionFactory);
  -                     ctx.rebind("QueueConnectionFactory",queueConnectionFactory);
  -                     
  -                     //DEBUG -- Create a RemoteControl object
  -                     //Administration calls will soon be done using JMX.
  -                     remoteControl=new RemoteControlImpl(theServer);
  -                     ctx.rebind("RemoteControl",remoteControl);
  -
  -             } catch (Exception e) { 
  -                     Log.error("Cannot start the JMS server ! "+e.getMessage()); 
  -                     Log.error(e); 
  -             } 
  -             
  -     }
  -     
  -     public static void main(String[] args) throws Exception
  -     {    
  -             new Main().run();
  -     }
  -     
  +    public Main(String [] args) throws Exception {
  +        MBeanServer mBeanServer = null;
  +
  +        //Create an MBeanServer
  +        mBeanServer = new MBeanServer();
  +        
  +        // Create MLet
  +        MLet mlet = new MLet();
  +        mBeanServer.registerMBean(mlet, new 
ObjectName(mBeanServer.getDefaultDomain(), "service", "MLet"));
  +        
  +        // Set MLet as classloader for this app
  +        Thread.currentThread().setContextClassLoader(mlet);
  +        
  +        // Read default configuration
  +        URL mletConf = getClass().getClassLoader().getResource("spyderMQ.plugin");
  +        Set beans = (Set)mlet.getMBeansFromURL(mletConf);
  +        Iterator enum = beans.iterator();
  +        while (enum.hasNext())
  +            {
  +                Object obj = enum.next();
  +                if (obj instanceof Exception) Log.error(obj);
  +            }
  +        
  +        //Start the adaptor
  +        mBeanServer.invoke(new ObjectName("Adaptor:name=html,port=8082"), "start", 
new Object[0], new String[0]);
  +
  +        new StartServer().start(mBeanServer);
  +    }
  +
  +    public static void main(String[] args) throws Exception
  +    {    
  +        new Main(args);
  +    }
   }
  
  
  
  1.1                  spyderMQ/src/java/org/spydermq/server/StartServer.java
  
  Index: StartServer.java
  ===================================================================
  package org.spydermq.server;
  
  /*
   * spyderMQ, the OpenSource JMS implementation
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
  
  import org.spydermq.JMSServer;
  import org.spydermq.security.SecurityManager;
  import org.spydermq.distributed.interfaces.DistributedJMSServer;
  import org.spydermq.distributed.interfaces.DistributedJMSServerSetup;
  import org.spydermq.distributed.interfaces.DistributedTopicConnectionFactory;
  import org.spydermq.distributed.interfaces.DistributedQueueConnectionFactory;
  import org.spydermq.distributed.JMSServerFactory;
  import org.spydermq.distributed.SpyTopicConnectionFactory;
  import org.spydermq.distributed.SpyQueueConnectionFactory;
  import org.spydermq.Log;
  
  import javax.jms.TopicConnectionFactory;
  import javax.jms.QueueConnectionFactory;
  import javax.jms.JMSException;
  import javax.jms.Topic;
  import javax.jms.Queue;
  
  import org.jnp.server.NamingServer;
  import javax.naming.InitialContext;
  import javax.naming.Context;
  import java.util.Properties;
  import java.io.InputStream;
  import java.util.StringTokenizer;
  
  import javax.management.MBeanServer;
  import javax.management.ObjectName;
  import javax.management.NotCompliantMBeanException;
  import javax.management.MBeanRegistrationException;
  import javax.management.InstanceAlreadyExistsException;
  import javax.management.loading.*;
  import java.net.*;
  import java.util.Set;
  import java.util.LinkedList;
  import java.util.Iterator;
  
  /**
   *  Class used to start a JMS service.  This can be called from
   *  inside another application to start JMS.
   *
   *    @author Norbert Lataille ([EMAIL PROTECTED])
   *    @author Rich Johns ([EMAIL PROTECTED])
   *      @author Vincent Sheffer ([EMAIL PROTECTED])
   *
   *    @version $Revision: 1.1 $
   */
  public class StartServer implements Runnable
  {
      private JMSServer theServer;
      private MBeanServer mBeanServer;
      private LinkedList serviceList = new LinkedList();
  
      SpyTopicConnectionFactory topicConnectionFactory;
      SpyQueueConnectionFactory queueConnectionFactory;
      DistributedTopicConnectionFactory distributedTopicConnectionFactory;
      DistributedQueueConnectionFactory distributedQueueConnectionFactory;
      DistributedJMSServerSetup theDistributedServerSetup;
      DistributedJMSServer theDistributedServer;
      SecurityManager securityManager;
  
      //DEBUG -- For the RemoteControl object
      private static RemoteControlImpl remoteControl;
  
      /**
       * Start the JMS server running in it's own thread.
       *
       * @param  server  An <code>MBeanServer</code> to 
       *                 add the JMS related JMX services to.
       */    
      public void start(MBeanServer server) {
          Thread thread = null;
  
          this.mBeanServer = server;
          if (thread == null) {
              thread = new Thread(this);
              thread.start();
          }
      }
  
      /**
       * Stop the JMS service.  This stops the thread and 
       * unregisters the JMX services related to JMS.
       */
      public void stop() {
          theServer.stopServer();
  
          /*
           * Need to unregister all of the JMS related JMX services.
           * TODO:  Don't know how to do this just yet.
           */
          while (!theServer.isStopped()) {
              try {
                  Thread.sleep(100);
              } catch (InterruptedException e) {
              }
          }
  
          /*
           * Now that we know the JMS server has stopped we 
           * can unregister the JMS services.
           */
          unregisterServices();
      }
  
      public StartServer()
      {
      }
  
      /**
       * Register a JMX service.  This is a wrapper method for
       * <code>MBeanServer.registerMBean</code> that adds the
       * service name to a list.  When the JMS service is stopped
       * all of the services that SpyderMQ started are unregistered.
       *
       * @param  obj      The <code>Object</code> to register.
       * @param  objName  The name of the service.  It is this name
       *                  that gets added to the list of services.
       */
      private void registerService(Object obj, ObjectName objName) 
      throws NotCompliantMBeanException, MBeanRegistrationException,
             InstanceAlreadyExistsException {
          this.mBeanServer.registerMBean(obj, objName);
          if (!serviceList.contains(objName)) {
              serviceList.add(objName);
          }
      }
  
      /**
       * Unregister all of the JMX services from the <code>MBeanServer</code>.
       */
      private void unregisterServices() {
          Iterator iter = null;
  
          iter = serviceList.iterator();
          while (iter.hasNext()) {
              ObjectName objName = null;
  
              objName = (ObjectName) iter.next();
              Log.notice("Unregistering '" + objName + "'");
              try {
                  this.mBeanServer.unregisterMBean(objName);
              } catch (Exception e) {
                  Log.error("Cannot unregister the JMS service '" +
                            objName + "': " + e.getMessage()); 
                  Log.error(e); 
              }
          }
      }
  
      public void run() {
          Log.notice("SpyderMQ [v0.5]");
          
          try { 
              
              //Load the property file
              InputStream in = 
getClass().getClassLoader().getResource("spyderMQ.properties").openStream();
              Properties cfg=new Properties();
              cfg.load(in);
              in.close();
              
              // By default we will start a JNDI Server. We won't
              // if user explicitly tells us not to.
              String noStartJNDI = (String)cfg.get("DoNotStartJNDI" );
              if((noStartJNDI == null) || 
                 (noStartJNDI.trim().length() == 0)) {
                  //Start the JNDI server
  
                  Log.notice( "Starting JNDI Server (JNP)" );
                  new org.jnp.server.Main().start();
              } else {
                  // Need to warn user because if they fail to start
                  // a JNDI server prior to starting spyder, they
                  // will be confused by the resulting error.
                  Log.notice("[Warning]: SpyderMQ.properties specifys NOT to start a 
JNDI Server.");
                  Log.notice("           If a JNDI Server is not running SpyderMQ will 
not start.");
              }
              
              //Get an InitialContext
              InitialContext ctx=new InitialContext();
              
              //Create a SecurityManager object
              securityManager=new SecurityManager();
              
              //Create the JMSServer object
              theServer = new JMSServer(securityManager);
              theDistributedServerSetup = 
JMSServerFactory.createJMSServer(theServer,cfg);
              theDistributedServer=theDistributedServerSetup.createClient();
              String 
connectionReceiverCN=(String)cfg.get("ConnectionReceiverClassName");
              registerService(theServer, new ObjectName(JMSServer.OBJECT_NAME));
              registerService(theDistributedServerSetup, 
                              new ObjectName("JMS:service=DistributedJMSServerSetup"));
              
              //Get the Topic properties
              String 
topicConnectionFactoryCN=(String)cfg.get("DistributedTopicConnectionFactoryClassName");
              if (topicConnectionFactoryCN == null ||
                  connectionReceiverCN == null) {
                  throw new RuntimeException("Missing configuration parameters");
              }
              
              //Create the distributedTopicConnectionFactory object                    
 
              distributedTopicConnectionFactory = 
(DistributedTopicConnectionFactory)Class.forName(topicConnectionFactoryCN).newInstance();
              distributedTopicConnectionFactory.setServer(theDistributedServer);
              distributedTopicConnectionFactory.setCRClassName(connectionReceiverCN);
              distributedTopicConnectionFactory.setSecurityManager(securityManager);
              registerService(distributedTopicConnectionFactory, 
                              new 
ObjectName("JMS:service=DistributedTopicConnectionFactory"));
              
              //Create the topicConnectionFactory object                        
              topicConnectionFactory = new 
SpyTopicConnectionFactory(distributedTopicConnectionFactory);
              
              //create the known topics
              Context subcontext=ctx.createSubcontext("topic");
              String topics=(String)cfg.get("knownTopics");
              
              if (topics!=null) {
                  
                  StringTokenizer st = new StringTokenizer(topics,", ");
                  
                  while (st.hasMoreElements()) {
                      String name=(String)st.nextElement();
                      Topic t=theServer.newTopic(name);
                      subcontext.rebind(name,t);
                  }
                  
              } else Log.notice("Warning: no known Topics !"); 
              
              //Get the queue properties
              String 
queueConnectionFactoryCN=(String)cfg.get("DistributedQueueConnectionFactoryClassName");
              if (queueConnectionFactoryCN==null) throw new RuntimeException("Missing 
configuration parameter");
              
              //Create the distributedTopicConnectionFactory object                    
 
              distributedQueueConnectionFactory = 
(DistributedQueueConnectionFactory)Class.forName(queueConnectionFactoryCN).newInstance();
              distributedQueueConnectionFactory.setServer(theDistributedServer);
              distributedQueueConnectionFactory.setCRClassName(connectionReceiverCN);
              distributedQueueConnectionFactory.setSecurityManager(securityManager);
              registerService(distributedQueueConnectionFactory, 
                              new 
ObjectName("JMS:service=DistributedQueueConnectionFactory"));
              
              //Create the topicConnectionFactory object                        
              queueConnectionFactory = new 
SpyQueueConnectionFactory(distributedQueueConnectionFactory);
              
              //create the known queues
              subcontext=ctx.createSubcontext("queue");
              String queues=(String)cfg.get("knownQueues");
              
              if (queues!=null) {
                  
                  StringTokenizer st = new StringTokenizer(queues,", ");
                  
                  while (st.hasMoreElements()) {
                      String name=(String)st.nextElement();
                      Queue q=theServer.newQueue(name);
                      subcontext.rebind(name,q);
                  }
                  
              } else {
                  Log.notice("Warning: no known Queues !"); 
              }
              
              //Set the known Ids
              String ids=(String)cfg.get("knownIds");
              
              if (ids!=null) {
                  
                  StringTokenizer st = new StringTokenizer(ids,", ");
                  
                  while (st.hasMoreElements()) {
                      String read=(String)st.nextElement();
                      int pos=read.indexOf(':');
                      if (pos==-1) throw new JMSException("Bad configuration file 
(missing separator in knownIds)");
                      String name=read.substring(0,pos);
                      String passwd=read.substring(pos+1);
                      pos=passwd.indexOf(':');
                      
                      if (pos==-1) {
                          Log.log("[JMSServer] new user : Login = "+name+", Id = 
[none]");                                                      
                          securityManager.addUser(name,passwd,null);
                      } else {
                          String ID=passwd.substring(pos+1);
                          Log.log("[JMSServer] new user : Login = "+name+", Id = 
"+ID);                                                 
                          securityManager.addUser(name,passwd.substring(0,pos),ID);
                      }
                      
                  }
                  
              } else {
                  Log.notice("Warning: no known Ids !"); 
              }
              
              //(re)bind the connection factories in the JNDI namespace
              ctx.rebind("TopicConnectionFactory",topicConnectionFactory);
              ctx.rebind("QueueConnectionFactory",queueConnectionFactory);
              
              //DEBUG -- Create a RemoteControl object
              //Administration calls will soon be done using JMX.
              remoteControl=new RemoteControlImpl(theServer);
              ctx.rebind("RemoteControl",remoteControl);
              
          } catch (Exception e) { 
              Log.error("Cannot start the JMS server ! "+e.getMessage()); 
              Log.error(e); 
          } 
          
      }
  }
  
  
  

Reply via email to