donaldp     2002/07/12 23:47:15

  Modified:    .        build.xml
  Added:       src/java/org/apache/avalon/cornerstone/blocks/channels
                        DefaultChannelManager.java
                        DefaultServerChannelFactory.java
                        DefaultSocketChannelFactory.java
               src/java/org/apache/avalon/cornerstone/services/channels
                        ChannelManager.java ServerChannelFactory.java
                        SocketChannelFactory.java
  Log:
  Added support for NIO based socket management.
  
  Submitted by: [EMAIL PROTECTED] (Kurt R. Hoehn)
  
  Revision  Changes    Path
  1.109     +11 -1     jakarta-avalon-cornerstone/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-cornerstone/build.xml,v
  retrieving revision 1.108
  retrieving revision 1.109
  diff -u -r1.108 -r1.109
  --- build.xml 9 Jul 2002 07:48:59 -0000       1.108
  +++ build.xml 13 Jul 2002 06:47:14 -0000      1.109
  @@ -50,13 +50,15 @@
     <property name="dist.name" value="${Name}-${version}"/>
     <property name="dist.base" value="distributions"/>
   
  -  <property name="xerces.jar" value="${tools.dir}/lib/xerces.jar"/>
  +  <property name="xerces.jar" value="${tools.dir}/lib/xerces-2.0.1.jar"/>
  +  <property name="xml-apis.jar" value="${tools.dir}/lib/xml-apis.jar"/>
     <property name="junit.jar" value="${tools.dir}/lib/junit-3.7.jar"/>
     <property name="tools.jar" value="${java.home}/../lib/tools.jar"/>
   
     <path id="project.class.path">
       <pathelement location="${junit.jar}"/>
       <pathelement location="${xerces.jar}"/>
  +    <pathelement location="${xml-apis.jar}"/>
       <pathelement path="${java.class.path}" />
       <fileset dir="${lib.dir}">
         <include name="*.jar" />
  @@ -111,6 +113,10 @@
           classpathref="project.class.path"
           property="LTPJB.present"/>
   
  +    <available property="nio.present"
  +        classname="java.nio.channels.Channels"
  +        classpathref="project.class.path"/>
  +
       <mkdir dir="${build.classes}"/>
   
       <javac srcdir="${java.dir}"
  @@ -140,6 +146,10 @@
                  unless="LTPJB.present" />
         <exclude name="org/apache/avalon/cornerstone/blocks/transport/**/*.java"
                  unless="AltRMI.present" />
  +      <exclude name="org/apache/avalon/cornerstone/blocks/channels/*.java"
  +               unless="nio.present" />
  +      <exclude name="org/apache/avalon/cornerstone/services/channels/*.java"
  +               unless="nio.present" />
       </javac>
     </target>
   
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/blocks/channels/DefaultChannelManager.java
  
  Index: DefaultChannelManager.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.blocks.channels;
  
  import java.util.HashMap;
  import org.apache.avalon.cornerstone.services.channels.ChannelManager;
  import org.apache.avalon.cornerstone.services.channels.ServerChannelFactory;
  import org.apache.avalon.cornerstone.services.channels.SocketChannelFactory;
  import org.apache.avalon.framework.CascadingException;
  import org.apache.avalon.framework.activity.Initializable;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.container.ContainerUtil;
  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;
  
  /**
   * Implementation of ChannelManager.
   *
   * @phoenix:block
   * @phoenix:service 
name="org.apache.avalon.cornerstone.services.channels.ChannelManager"
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Kurt R. Hoehn</a>
   */
  public class DefaultChannelManager
      extends AbstractLogEnabled
      implements ChannelManager, Contextualizable, Configurable, Initializable
  {
      private final HashMap m_serverChannels = new HashMap();
      private final HashMap m_socketChannels = new HashMap();
  
      private Context m_context;
      private Configuration m_configuration;
  
      public void contextualize( final Context context )
          throws ContextException
      {
          m_context = context;
      }
  
      /**
       * Configure the ChannelManager.
       *
       * @param configuration the Configuration
       * @exception ConfigurationException if an error occurs
       */
      public void configure( final Configuration configuration )
          throws ConfigurationException
      {
          m_configuration = configuration;
      }
  
      public void initialize()
          throws Exception
      {
          final Configuration[] serverChannels = m_configuration.getChild( 
"server-channels" ).getChildren( "factory" );
          for( int i = 0; i < serverChannels.length; i++ )
          {
              final Configuration element = serverChannels[ i ];
              final String name = element.getAttribute( "name" );
              final String className = element.getAttribute( "class" );
  
              setupServerChannelFactory( name, className, element );
          }
  
          final Configuration[] socketChannels = m_configuration.getChild( 
"client-channels" ).getChildren( "factory" );
          for( int i = 0; i < socketChannels.length; i++ )
          {
              final Configuration element = socketChannels[ i ];
              final String name = element.getAttribute( "name" );
              final String className = element.getAttribute( "class" );
  
              setupSocketChannelFactory( name, className, element );
          }
      }
  
      protected void setupServerChannelFactory( final String name,
                                                final String className,
                                                final Configuration configuration )
          throws Exception
      {
          final Object object = createFactory( name, className, configuration );
  
          if( !( object instanceof ServerChannelFactory ) )
          {
              throw new Exception( "Error creating factory " + name +
                                   " with class " + className + " as " +
                                   "is does not implement the correct " +
                                   "interface (ServerChannelFactory)" );
          }
  
          m_serverChannels.put( name, object );
      }
  
      protected void setupSocketChannelFactory( final String name,
                                                final String className,
                                                final Configuration configuration )
          throws Exception
      {
          final Object object = createFactory( name, className, configuration );
  
          if( !( object instanceof SocketChannelFactory ) )
          {
              throw new Exception( "Error creating factory " + name +
                                   " with class " + className + " as " +
                                   "is does not implement the correct " +
                                   "interface (SocketChannelFactory)" );
          }
  
          m_socketChannels.put( name, object );
      }
  
      protected Object createFactory( final String name,
                                      final String className,
                                      final Configuration configuration )
          throws Exception
      {
          Object factory;
          try
          {
              final ClassLoader classLoader =
                  (ClassLoader)Thread.currentThread().getContextClassLoader();
              final Class clazz = classLoader.loadClass( className );
              factory = (Object)clazz.newInstance();
          }
          catch( final Exception e )
          {
              final String message =
                  "Error creating factory with class " + className;
              throw new CascadingException( message, e );
          }
  
          ContainerUtil.enableLogging( factory, getLogger() );
          ContainerUtil.contextualize( factory, m_context );
          ContainerUtil.configure( factory, configuration );
          ContainerUtil.initialize( factory );
  
          return factory;
      }
  
      /**
       * Retrieve a server channel factory by name.
       *
       * @param name the name of server channel factory
       * @return the ServerChannelFactory
       * @exception Exception if server channel factory is not available
       */
      public ServerChannelFactory getServerChannelFactory( String name )
          throws Exception
      {
          final ServerChannelFactory factory = 
(ServerChannelFactory)m_serverChannels.get( name );
          if( null != factory )
          {
              return factory;
          }
          else
          {
              final String message =
                  "Unable to locate server channel factory named " + name;
              throw new Exception( message );
          }
      }
  
      /**
       * Retrieve a client socket channel by name.
       *
       * @param name the name of client socket channel factory
       * @return the SocketChannelFactory
       * @exception Exception if socket channel factory is not available
       */
      public SocketChannelFactory getSocketChannelFactory( String name )
          throws Exception
      {
          final SocketChannelFactory factory = 
(SocketChannelFactory)m_socketChannels.get( name );
          if( null != factory )
          {
              return factory;
          }
          else
          {
              final String message =
                  "Unable to locate socket channel factory named " + name;
              throw new Exception( message );
          }
      }
  }
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/blocks/channels/DefaultServerChannelFactory.java
  
  Index: DefaultServerChannelFactory.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.blocks.channels;
  
  import java.io.IOException;
  import java.net.InetSocketAddress;
  import java.nio.channels.SelectionKey;
  import java.nio.channels.Selector;
  import java.nio.channels.ServerSocketChannel;
  import org.apache.avalon.cornerstone.services.channels.ServerChannelFactory;
  
  /**
   * Factory implementation for vanilla server socket channels.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Kurt R. Hoehn</a>
   */
  public class DefaultServerChannelFactory
      implements ServerChannelFactory
  {
      /**
       * Creates a server socket channel on a particular network interface.
       *
       * @param address the network interface to bind to.
       * @return the created ServerSocketChannel
       * @exception IOException if an error occurs
       */
      public ServerSocketChannel createServerChannel( InetSocketAddress address )
          throws IOException
      {
          final ServerSocketChannel ssc = ServerSocketChannel.open();
          ssc.socket().bind( address );
  
          return ssc;
      }
  
      /**
       * Creates a server socket channel on specified port.
       *
       * @param port the server port
       * @return the created ServerSocketChannel
       * @exception IOException if an error occurs
       */
      public ServerSocketChannel createServerChannel( int port )
          throws IOException
      {
          final InetSocketAddress address = new InetSocketAddress( port );
          return createServerChannel( address );
      }
  }
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/blocks/channels/DefaultSocketChannelFactory.java
  
  Index: DefaultSocketChannelFactory.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.blocks.channels;
  
  import java.io.IOException;
  import java.net.InetSocketAddress;
  import java.nio.channels.SocketChannel;
  import org.apache.avalon.cornerstone.services.channels.SocketChannelFactory;
  
  /**
   * Factory implementation for vanilla socket channels.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Kurt R. Hoehn</a>
   */
  public class DefaultSocketChannelFactory
      implements SocketChannelFactory
  {
      /**
       * Create a socket channel and connect to remote address specified.
       *
       * @param address the remote address
       * @return the socket channel
       * @exception IOException if an error occurs
       */
      public SocketChannel createSocketChannel( final InetSocketAddress address )
          throws IOException
      {
          final SocketChannel channel = SocketChannel.open();
          channel.connect( address );
  
          return channel;
      }
  
      /**
       * Create a socket channel and connect to remote address specified
       * from host and port.
       *
       * @param host the remote host
       * @param port the remote port
       * @return the socket channel
       * @exception IOException if an error occurs
       */
      public SocketChannel createSocketChannel( String host, int port )
          throws IOException
      {
          return createSocketChannel( new InetSocketAddress( host, port ) );
      }
  }
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/services/channels/ChannelManager.java
  
  Index: ChannelManager.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.services.channels;
  
  /**
   * Service to manage the channel factories.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Kurt R. Hoehn</a>
   */
  public interface ChannelManager
  {
      String ROLE = ChannelManager.class.getName();
  
      /**
       * Retrieve a server channel factory by name.
       *
       * @param name the name of server channel factory
       * @return the ServerChannelFactory
       * @exception Exception if server socket factory is not available
       */
      public ServerChannelFactory getServerChannelFactory( String name )
          throws Exception;
  
      /**
       * Retrieve a client socket channel factory by name.
       *
       * @param name the name of client socket channel factory
       * @return the SocketChannelFactory
       * @exception Exception if socket channel factory is not available
       */
      public SocketChannelFactory getSocketChannelFactory( String name )
          throws Exception;
  }
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/services/channels/ServerChannelFactory.java
  
  Index: ServerChannelFactory.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.services.channels;
  
  import java.io.IOException;
  import java.net.InetSocketAddress;
  import java.nio.channels.Selector;
  import java.nio.channels.ServerSocketChannel;
  
  /**
   * The interface used to create server socket channels.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Kurt R. Hoehn</a>
   */
  public interface ServerChannelFactory
  {
      /**
       * Creates a server socket channel on a particular network interface.
       *
       * @param address the network interface to bind to.
       * @return the created ServerSocketChannel
       * @exception IOException if an error occurs
       */
      public ServerSocketChannel createServerChannel( InetSocketAddress address )
          throws IOException;
  
      /**
       * Creates a server socket channel on specified port.
       *
       * @param port the server port
       * @return the created ServerSocketChannel
       * @exception IOException if an error occurs
       */
      public ServerSocketChannel createServerChannel( int port )
          throws IOException;
  }
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/services/channels/SocketChannelFactory.java
  
  Index: SocketChannelFactory.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.services.channels;
  
  import java.io.IOException;
  import java.net.InetSocketAddress;
  import java.nio.channels.SocketChannel;
  
  /**
   * The interface used to create client socket channels.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Kurt R. Hoehn</a>
   */
  public interface SocketChannelFactory
  {
      /**
       * Create a socket channel and connect to remote address specified.
       *
       * @param address the remote address
       * @return the socket channel
       * @exception IOException if an error occurs
       */
      SocketChannel createSocketChannel( InetSocketAddress address )
          throws IOException;
  
      /**
       * Create a socket channel and connect to remote address specified
       * from host and port.
       *
       * @param host the remote host
       * @param port the remote port
       * @return the socket channel
       * @exception IOException if an error occurs
       */
      SocketChannel createSocketChannel( String host, int port )
          throws IOException;
  }
  
  
  

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

Reply via email to