tbennett    2004/03/03 23:23:24

  Modified:    merlin/facilities/db/hsql/src/java/org/apache/avalon/db/hsql
                        HsqldbServer.java
  Log:
  CVS: ----------------------------------------------------------------------
  CVS: Submitted by: Timothy Bennett <[EMAIL PROTECTED]>
  
  + Cleaned up formatting
  + Changed base directory to urn:avalon:home
  
  Revision  Changes    Path
  1.3       +137 -81   
avalon/merlin/facilities/db/hsql/src/java/org/apache/avalon/db/hsql/HsqldbServer.java
  
  Index: HsqldbServer.java
  ===================================================================
  RCS file: 
/home/cvs/avalon/merlin/facilities/db/hsql/src/java/org/apache/avalon/db/hsql/HsqldbServer.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- HsqldbServer.java 24 Feb 2004 22:39:29 -0000      1.2
  +++ HsqldbServer.java 4 Mar 2004 07:23:24 -0000       1.3
  @@ -1,16 +1,16 @@
  -/* 
  +/*
    * Copyright 2004 Apache Software Foundation
    * Licensed  under the  Apache License,  Version 2.0  (the "License");
    * you may not use  this file  except in  compliance with the License.
  - * You may obtain a copy of the License at 
  - * 
  + * You may obtain a copy of the License at
  + *
    *   http://www.apache.org/licenses/LICENSE-2.0
  - * 
  + *
    * Unless required by applicable law or agreed to in writing, software
    * distributed  under the  License is distributed on an "AS IS" BASIS,
    * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
    * implied.
  - * 
  + *
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */
  @@ -35,8 +35,6 @@
   import org.apache.avalon.framework.context.Context;
   import org.apache.avalon.framework.context.ContextException;
   import org.apache.avalon.framework.context.Contextualizable;
  -import org.apache.avalon.framework.logger.AbstractLogEnabled;
  -import org.apache.avalon.framework.logger.LogEnabled;
   import org.apache.avalon.framework.logger.Logger;
   import org.hsqldb.HsqlServerFactory;
   import org.hsqldb.HsqlSocketRequestHandler;
  @@ -48,16 +46,42 @@
    *
    * @avalon.component name="server" lifestyle="singleton"
    * @avalon.service type="org.apache.avalon.db.DatabaseService"
  - * @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a>
  + * @author  <a href="mailto:[EMAIL PROTECTED]">Timothy Bennett</a>
    */
  -public class HsqldbServer 
  +public class HsqldbServer
     implements Contextualizable, Configurable, Initializable,
  -  Startable, DatabaseService 
  +  Startable, DatabaseService
   {
       //---------------------------------------------------------
  +    // static
  +    //---------------------------------------------------------
  +
  +    /**
  +     * Default HSQL database server TCP socket listening port.
  +     * The default value is the HSQL standard port 9001.
  +     */
  +    private static final int DEFAULT_PORT = 9001;
  +    /**
  +     * Default host name to bind the HSQL database server TCP socket
  +     * to.  The default value is the localhost, that is 127.0.0.1.
  +     */
  +    private static final String DEFAULT_HOST = "127.0.0.1";
  +    /**
  +     * TCP listening socket backlog.
  +     */
  +    private static final int BACKLOG = 50;
  +    /**
  +     * TCP listening socket connection timeout (msecs).
  +     */
  +    private static final int TIMEOUT = 500;
  +
  +    //---------------------------------------------------------
       // immutable state
       //---------------------------------------------------------
   
  +    /**
  +     * The logging channel for this component.
  +     */
       private final Logger m_logger;
   
       //---------------------------------------------------------
  @@ -85,7 +109,7 @@
       private int m_port;
   
       /**
  -     * Host to bind the database TCP/IP listener to. Default is localhost. 
  +     * Host to bind the database TCP/IP listener to. Default is localhost.
        */
       private InetAddress m_host;
   
  @@ -108,7 +132,13 @@
       // constructor
       //---------------------------------------------------------
   
  -    public HsqldbServer( Logger logger )
  +    /**
  +     * Creates a new instance of the HSQL database server with the
  +     * logging channel supplied by the container.
  +     *
  +     * @param logger the container-supplied logging channel
  +     */
  +    public HsqldbServer( final Logger logger )
       {
           m_logger = logger;
       }
  @@ -119,16 +149,16 @@
   
      /**
       * Contextualization of the server by the container.
  -    * 
  +    *
       * @param context the supplied server context
       * @exception ContextException if a contextualization error occurs
  -    * @avalon.entry key="urn:composition:dir" type="java.io.File"
  +    * @avalon.entry key="urn:avalon:home" type="java.io.File" alias="app.home"
       */
  -    public void contextualize(Context context) 
  -      throws ContextException 
  +    public void contextualize( final Context context )
  +      throws ContextException
       {
           // cache the working base directory
  -        m_basedir = getBaseDirectory(context);
  +        m_basedir = getBaseDirectory( context );
       }
   
       //---------------------------------------------------------
  @@ -137,36 +167,37 @@
   
       /**
        * Configuration of the server by the container.
  -     * 
  +     *
        * @param config the supplied server configuration
        * @exception ConfigurationException if a configuration error occurs
        */
  -    public void configure(Configuration config) 
  -      throws ConfigurationException 
  -    {    
  +    public void configure( final Configuration config )
  +      throws ConfigurationException
  +    {
           // get the database listening port/host
  -        m_port = config.getChild("port").getValueAsInteger(9001);
  -        String host = config.getChild("host").getValue("127.0.0.1");
  +        m_port = config.getChild( "port" ).getValueAsInteger( DEFAULT_PORT );
  +        String host = config.getChild( "host" ).getValue( DEFAULT_HOST );
           try
           {
  -            m_host = InetAddress.getByName(host);
  -        } 
  -        catch (UnknownHostException uhe) 
  +            m_host = InetAddress.getByName( host );
  +        }
  +        catch ( UnknownHostException uhe )
           {
  -            if (getLogger().isErrorEnabled()) 
  +            if ( getLogger().isErrorEnabled() )
               {
                   getLogger().error(
  -                  "Error configuring HSQLDB component: " 
  -                    + uhe.getMessage(), 
  +                  "Error configuring HSQLDB component: "
  +                    + uhe.getMessage(),
                     uhe );
               }
               throw new ConfigurationException( uhe.getMessage(), uhe );
           }
  -        
  +
           // get the HSQLDB server parameters...
  -        m_dbName = new File(m_basedir, 
config.getChild("db-name").getValue("avalon-hsqldb"));
  -        m_dbDebug = config.getChild("debug").getValueAsBoolean(false);
  -        m_dbSilent = config.getChild("silent").getValueAsBoolean(true);
  +        m_dbName = new File( m_basedir,
  +            config.getChild( "db-name" ).getValue( "avalon-hsqldb" ) );
  +        m_dbDebug = config.getChild( "debug" ).getValueAsBoolean( false );
  +        m_dbSilent = config.getChild( "silent" ).getValueAsBoolean( true );
           
           // how are we configured?
           this.toString();
  @@ -178,6 +209,9 @@
   
       /**
        * Initialization of the component by the container.
  +     * 
  +     * @throws Exception if an error occurs while initializing
  +     * the component
        */
       public void initialize() throws Exception 
       {
  @@ -190,6 +224,9 @@
   
       /**
        * Start the server.
  +     * 
  +     * @throws Exception if an error occurs while starting the 
  +     * component
        */
       public void start() throws Exception 
       {
  @@ -200,11 +237,11 @@
                 HsqlServerFactory.createHsqlServer(
                   m_dbName.getAbsolutePath(), 
                   m_dbDebug, 
  -                m_dbSilent);
  +                m_dbSilent );
           } 
  -        catch (SQLException sqle) 
  +        catch ( SQLException sqle ) 
           {
  -            if (m_logger.isErrorEnabled()) 
  +            if ( m_logger.isErrorEnabled() ) 
               {
                   final String error = 
                     "Error starting HSQL database: " 
  @@ -215,33 +252,39 @@
           }
           
           // start up primary socket server 
  -        final ServerSocket serverSocket = new ServerSocket(m_port, 50, m_host);
  -        serverSocket.setSoTimeout(500);
  +        final ServerSocket serverSocket = new ServerSocket( m_port,
  +            BACKLOG, m_host );
  +        serverSocket.setSoTimeout( TIMEOUT );
           m_serverSocketThread = 
  -          new Thread(new Runnable() {
  -            public void run() { runServer(serverSocket); }
  -          });
  +          new Thread( new Runnable() {
  +            public void run()
  +            {
  +                runServer( serverSocket );
  +            }
  +          } );
           m_serverSocketThread.start();
           
  -        if (m_logger.isInfoEnabled()) 
  +        if ( m_logger.isInfoEnabled() ) 
           {
  -            m_logger.info("Hypersonic SQL listening on port " + m_port);
  +            m_logger.info( "Hypersonic SQL listening on port " + m_port );
           }
       }
   
       /**
        * Stop the server.
  +     * 
  +     * @throws Exception if an error occurs while stopping the component
        */
       public void stop() throws Exception 
       {
           // tell HSQLDB that all connections are being shutdown...
  -        if (m_hsqlSocketRequestHandler != null) 
  +        if ( m_hsqlSocketRequestHandler != null ) 
           {
               m_hsqlSocketRequestHandler.signalCloseAllServerConnections();
           }
           
           // shutdown server socket...
  -        if (m_serverSocketThread != null) 
  +        if ( m_serverSocketThread != null ) 
           {
               m_serverSocketThread.interrupt();
           }
  @@ -253,24 +296,24 @@
        * 
        * @param serverSocket the serverSocket to manage
        */
  -    private void runServer(ServerSocket serverSocket) 
  +    private void runServer( ServerSocket serverSocket ) 
       {
  -        if (m_logger.isInfoEnabled()) 
  +        if ( m_logger.isInfoEnabled() ) 
           {
  -            m_logger.info("Running database connection server...");
  +            m_logger.info( "Running database connection server..." );
           }
           
           ThreadedExecutor runner = new ThreadedExecutor();
           try 
           {
               // accept connections until this thread is interrupted...
  -            while (!Thread.interrupted()) 
  +            while ( !Thread.interrupted() ) 
               {
                   try
                   {
  -                    // when a connection is made, spin off a thread to process the
  -                    // connection, and immediately go back to listening for other
  -                    // connections
  +                    // when a connection is made, spin off a thread to
  +                    // process the connection, and immediately go back to
  +                    // listening for other connections
                       final Socket connection = serverSocket.accept();
                       runner.execute( 
                         new Runnable() 
  @@ -284,9 +327,9 @@
                                 connection.getInetAddress().getHostAddress();
                               
                               // hand-off the socket connection to HSQLDB...
  -                            m_hsqlSocketRequestHandler.handleConnection(connection);
  +                            m_hsqlSocketRequestHandler.handleConnection( connection 
);
   
  -                            if (m_logger.isDebugEnabled()) 
  +                            if ( m_logger.isDebugEnabled() ) 
                               {
                                   final String message = 
                                     "database connection from " 
  @@ -297,19 +340,19 @@
                                   m_logger.debug( message );
                               }
                           }
  -                    });
  +                    } );
                   } 
  -                catch (InterruptedIOException iioe) 
  +                catch ( InterruptedIOException iioe ) 
                   {
                       // Ignore these - thrown when accept() times out
                   }
  -                catch (Exception e) 
  +                catch ( Exception e ) 
                   {
                       // IOException - if an I/O error occurs when waiting 
                       // for a connection.
                       // SecurityException - if a security manager exists 
                       // and its checkListen method doesn't allow the operation.
  -                    if( getLogger().isWarnEnabled() ) 
  +                    if ( getLogger().isWarnEnabled() ) 
                       {
                           getLogger().warn( e.getMessage(), e );
                       }
  @@ -318,17 +361,17 @@
           } 
           finally 
           {
  -            if (runner != null) 
  +            if ( runner != null ) 
               {
                   runner = null;
               }
  -            if (serverSocket != null) 
  +            if ( serverSocket != null ) 
               {
                   try 
                   {
                       serverSocket.close();
                   } 
  -                catch (IOException ignore) 
  +                catch ( IOException ignore ) 
                   {
                       // ignore...
                   }
  @@ -341,13 +384,28 @@
        * Return the working base directory.
        * 
        * @param context the supplied server context
  -     * @return a <code>File</code> object representing the working base directory
  +     * @return a <code>File</code> object representing the working
  +     * base directory
        * @exception ContextException if a contextualization error occurs
        */
  -    private File getBaseDirectory( Context context ) 
  +    private File getBaseDirectory( final Context context ) 
         throws ContextException 
       {
  -        return (File) context.get("urn:composition:dir");
  +        File home = (File) context.get( "urn:avalon:home" );
  +        if ( !home.exists() )
  +        {
  +            boolean success = home.mkdirs();
  +            if ( !success )
  +            {
  +                throw new ContextException( "Unable to create component "
  +                    + "home directory: " + home.getAbsolutePath() );
  +            }
  +        }
  +        if ( getLogger().isInfoEnabled() )
  +        {
  +            getLogger().info( "Setting home directory to: " + home );
  +        }
  +        return home;
       }
       
      /**
  @@ -364,25 +422,23 @@
        * 
        * @return a <code>String</code> representation of this component
        */
  -    public String toString() 
  +    public String toString()
       {
           StringBuffer buffer = new StringBuffer();
  -        buffer.append("[HsqldbServer:");
  -        buffer.append(" m_logger: ");
  -        buffer.append(m_logger);
  -        buffer.append(" m_basedir: ");
  -        buffer.append(m_basedir);
  -        buffer.append(" m_port: ");
  -        buffer.append(m_port);
  -        buffer.append(" m_host: ");
  -        buffer.append(m_host);
  -        buffer.append(" m_dbName: ");
  -        buffer.append(m_dbName);
  -        buffer.append(" m_dbDebug: ");
  -        buffer.append(m_dbDebug);
  -        buffer.append(" m_dbSilent: ");
  -        buffer.append(m_dbSilent);
  -        buffer.append("]");
  +        buffer.append( "[HsqldbServer:" );
  +        buffer.append( " m_basedir: " );
  +        buffer.append( m_basedir );
  +        buffer.append( " m_port: " );
  +        buffer.append( m_port );
  +        buffer.append( " m_host: " );
  +        buffer.append( m_host );
  +        buffer.append( " m_dbName: " );
  +        buffer.append( m_dbName );
  +        buffer.append( " m_dbDebug: " );
  +        buffer.append( m_dbDebug );
  +        buffer.append( " m_dbSilent: " );
  +        buffer.append( m_dbSilent );
  +        buffer.append( "]" );
           return buffer.toString();
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to