User: oberg   
  Date: 00/11/07 04:22:58

  Modified:    src/main/org/jboss/jdbc HypersonicDatabaseMBean.java
                        HypersonicDatabase.java
  Log:
  Now runs Hypersonic in-VM(!)
  Added settable parameters, with decent defaults.
  Now stores database files in db/hypersonic/<configurable name>
  Name of MBean service is configurable through jboss.conf if desired
  
  Revision  Changes    Path
  1.2       +12 -1     jboss/src/main/org/jboss/jdbc/HypersonicDatabaseMBean.java
  
  Index: HypersonicDatabaseMBean.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/jdbc/HypersonicDatabaseMBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HypersonicDatabaseMBean.java      2000/04/22 14:30:13     1.1
  +++ HypersonicDatabaseMBean.java      2000/11/07 12:22:57     1.2
  @@ -12,7 +12,7 @@
    *      
    *   @see <related>
    *   @author Rickard �berg ([EMAIL PROTECTED])
  - *   @version $Revision: 1.1 $
  + *   @version $Revision: 1.2 $
    */
   public interface HypersonicDatabaseMBean
      extends org.jboss.util.ServiceMBean
  @@ -21,4 +21,15 @@
      public static final String OBJECT_NAME = ":service=Hypersonic";
       
      // Public --------------------------------------------------------
  +   public void setDatabase(String name);
  +   public String getDatabase();
  +
  +   public void setPort(int port);
  +   public int getPort();
  +
  +   public void setSilent(boolean silent);
  +   public boolean getSilent();
  +   
  +   public void setTrace(boolean trace);
  +   public boolean getTrace();
   }
  
  
  
  1.4       +105 -39   jboss/src/main/org/jboss/jdbc/HypersonicDatabase.java
  
  Index: HypersonicDatabase.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/jdbc/HypersonicDatabase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- HypersonicDatabase.java   2000/09/28 01:17:07     1.3
  +++ HypersonicDatabase.java   2000/11/07 12:22:57     1.4
  @@ -21,22 +21,30 @@
   import org.jboss.util.ServiceMBeanSupport;
   
   /**
  - *   <description> 
  + *   Integration with Hypersonic SQL (http://hsql.oron.ch/). Starts a Hypersonic 
database in-VM.
  + *
  + *   Note that once started it cannot be shutdown.
    *      
  - *   @see <related>
  + *   @see HypersonicDatabaseMBean
    *   @author Rickard �berg ([EMAIL PROTECTED])
  - *   @version $Revision: 1.3 $
  + *   @version $Revision: 1.4 $
    */
   public class HypersonicDatabase
      extends ServiceMBeanSupport
  -   implements HypersonicDatabaseMBean, MBeanRegistration
  +   implements HypersonicDatabaseMBean, MBeanRegistration, NotificationListener
   {
      // Constants -----------------------------------------------------
       
      // Attributes ----------------------------------------------------
      Thread runner;
      Process proc;
  +   MBeanServer server;
      
  +   String name = "jboss"; // Database name will be appended to "<db.properties 
location>/hypersonic/"
  +   int port = 1476; // Default port
  +   boolean silent = false;
  +   boolean trace = true;
  +   
      // Static --------------------------------------------------------
   
      // Constructors --------------------------------------------------
  @@ -45,10 +53,52 @@
      }
      
      // Public --------------------------------------------------------
  +   // Settings
  +   public void setDatabase(String name)
  +   {
  +      this.name = name;
  +   }
  +   
  +   public String getDatabase()
  +   {
  +      return name;
  +   }
  +
  +   public void setPort(int port)
  +   {
  +      this.port = port;
  +   }
  +   
  +   public int getPort()
  +   {
  +      return port;
  +   }
  +
  +   public void setSilent(boolean silent)
  +   {
  +      this.silent = silent;
  +   }
  +   
  +   public boolean getSilent()
  +   {
  +      return silent;
  +   }
  +   
  +   public void setTrace(boolean trace)
  +   {
  +      this.trace = trace;
  +   }
  +   
  +   public boolean getTrace()
  +   {
  +      return trace;
  +   }
  +   
      public ObjectName getObjectName(MBeanServer server, ObjectName name)
         throws javax.management.MalformedObjectNameException
      {
  -      return new ObjectName(OBJECT_NAME);
  +      this.server = server;
  +      return name == null ? new ObjectName(OBJECT_NAME) : name;
      }
      
      public String getName()
  @@ -56,57 +106,73 @@
         return "Hypersonic";
      }
      
  -   public void initService()
  +   public void startService()
         throws Exception
      {
  -      final Log log = this.log;
  +      // Register as log listener
  +      server.addNotificationListener(new 
ObjectName(server.getDefaultDomain(),"service","Log"),this,null,null);
  +   
  +      // Start DB in new thread, or else it will block us
         runner = new Thread(new Runnable()
         {
            public void run()
            {
  -            try
  -            {
  -               proc = Runtime.getRuntime().exec("java -classpath 
../lib/ext/hsql.jar org.hsql.Server");
  -               
  -               DataInputStream din = new DataInputStream(proc.getInputStream());
  -               String line;
  -               while((line = din.readLine()) != null)
  -               {
  -                  // Notify that something happened
  -                  synchronized(runner)
  -                  {
  -                     runner.notifyAll();
  -                  }
  -                  
  -                  if (!line.equals("Press [Ctrl]+[C] to abort"))
  -                     log.debug(line);
  -               }
  -                  
  -               runner = null;
  -               proc = null;
  -            } catch (IOException e)
  +            // Get DB directory
  +            URL dbLocator = getClass().getResource("/db.properties");
  +            File dbDir = new File(dbLocator.getFile()).getParentFile();
  +            File dbName = new File(dbDir, "hypersonic/"+name);
  +            
  +            // Create startup arguments
  +            String[] args = new String[]
               {
  -               log.error("Hypersonic database failed");
  -            }
  +               "-database", dbName.toString(),
  +               "-port", port+"",
  +               "-silent", silent+"",
  +               "-trace", trace+""
  +            };
  +            
  +            // Start server
  +            org.hsql.Server.main(args);
  +            
  +            // Now wait for "Server x.x is running" message
            }
         });
         
  -      synchronized (runner)
  +      // Wait for startup message
  +      try
         {
  -         runner.start();
  -         runner.wait(30000); // Wait for database to start; timeout = not started
  +         synchronized (runner)
  +         {
  +            runner.start();
  +            runner.wait(30000); // Wait for database to start; timeout = not started
  +         }
  +      } finally
  +      {
  +         try
  +         {
  +            server.removeNotificationListener(new 
ObjectName(server.getDefaultDomain(),"service","Log"), this);
  +         } catch (ListenerNotFoundException e)
  +         {
  +            // Ignore
  +         }
         }
         log.log("Database started");
      }
      
  -   public void stopService()
  +   // NotificationListener implementation ---------------------------
  +   public void handleNotification(Notification n,
  +                                  java.lang.Object handback)
      {
  -      if (runner != null)
  +      if (n.getUserData().toString().equals(getName()))
         {
  -         runner.stop();
  -         proc.destroy();
  -         runner = null;
  -         proc = null;
  +         if (n.getMessage().endsWith("is running"))
  +         {
  +            // Notify other thread that DB is now started
  +            synchronized(runner)
  +            {
  +               runner.notify();
  +            }
  +         }
         }
      }
      // Protected -----------------------------------------------------
  
  
  

Reply via email to