SfTransportServerChannelFactory xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.transport.tcp;
    19
    20  import org.codehaus.activemq.message.WireFormat;
    21  import org.codehaus.activemq.transport.TransportServerChannel;
    22  import org.codehaus.activemq.transport.TransportServerChannelFactory;
    23
    24  import javax.jms.JMSException;
    25  import javax.net.ServerSocketFactory;
    26  import java.io.IOException;
    27  import java.net.InetAddress;
    28  import java.net.ServerSocket;
    29  import java.net.URI;
    30
    31  /***
    32   * An implementation of TransportServerChannelFactory which uses a ServerSocketFactory
    33   * to create ServerSocket instances
    34   *
    35   * @version $Revision: 1.4 $
    36   */
    37  public class SfTransportServerChannelFactory implements TransportServerChannelFactory {
    38
    39      protected static final int BACKLOG = 500;
    40
    41      private ServerSocketFactory serverSocketFactory;
    42
    43      public SfTransportServerChannelFactory(ServerSocketFactory socketFactory) {
    44          this.serverSocketFactory = socketFactory;
    45      }
    46
    47      /***
    48       * Bind a ServerChannel to an address
    49       *
    50       * @param wireFormat
    51       * @param bindAddress
    52       * @return the TransportChannel bound to the remote node
    53       * @throws JMSException
    54       */
    55      public TransportServerChannel create(WireFormat wireFormat, URI bindAddress) throws JMSException {
    56          ServerSocket serverSocket = null;
    57          try {
    58              serverSocket = createServerSocket(bindAddress);
    59          }
    60          catch (IOException e) {
    61              JMSException jmsEx = new JMSException("Creation of ServerSocket failed: " + e);
    62              jmsEx.setLinkedException(e);
    63              throw jmsEx;
    64          }
    65          return new TcpTransportServerChannel(wireFormat, serverSocket);
    66      }
    67
    68      protected ServerSocket createServerSocket(URI bind) throws IOException {
    69          String host = bind.getHost();
    70          host = (host == null || host.length() == 0) ? "localhost" : host;
    71
    72          InetAddress addr = InetAddress.getByName(host);
    73          if (addr.equals(InetAddress.getLocalHost())) {
    74              return serverSocketFactory.createServerSocket(bind.getPort(), BACKLOG);
    75          }
    76          else {
    77              return serverSocketFactory.createServerSocket(bind.getPort(), BACKLOG, addr);
    78          }
    79      }
    80
    81  }