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 EDU.oswego.cs.dl.util.concurrent.Executor;
21  import org.codehaus.activemq.message.WireFormat;
22  import org.codehaus.activemq.transport.TransportChannel;
23  
24  import javax.jms.JMSException;
25  import javax.net.SocketFactory;
26  import java.io.IOException;
27  import java.net.InetAddress;
28  import java.net.Socket;
29  import java.net.URI;
30  
31  /***
32   * A factory of TcpTransportChannelFactory instances using a SocketFactory
33   *
34   * @version $Revision: 1.6 $
35   */
36  public class SfTransportChannelFactory extends TcpTransportChannelFactory {
37  
38      private SocketFactory socketFactory;
39      private Executor executor;
40  
41      public SfTransportChannelFactory(SocketFactory socketFactory) {
42          this.socketFactory = socketFactory;
43      }
44  
45      /***
46       * Create a Channel to a remote Node - e.g. a Broker
47       *
48       * @param wireFormat
49       * @param remoteLocation
50       * @return the TransportChannel bound to the remote node
51       * @throws JMSException
52       */
53      public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
54          Socket socket = null;
55          try {
56              socket = createSocket(remoteLocation);
57          }
58          catch (IOException e) {
59              JMSException jmsEx = new JMSException("Creation of Socket failed: " + e);
60              jmsEx.setLinkedException(e);
61              throw jmsEx;
62          }
63          return populateProperties(new TcpTransportChannel(wireFormat, socket, executor), remoteLocation);
64      }
65  
66      /***
67       * Create a Channel to a remote Node - e.g. a Broker
68       *
69       * @param wireFormat
70       * @param remoteLocation
71       * @param localLocation  -
72       *                       e.g. local InetAddress and local port
73       * @return the TransportChannel bound to the remote node
74       * @throws JMSException
75       */
76      public TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException {
77          Socket socket = null;
78          try {
79              socket = createSocket(remoteLocation, localLocation);
80          }
81          catch (IOException e) {
82              JMSException jmsEx = new JMSException("Creation of Socket failed: " + e);
83              jmsEx.setLinkedException(e);
84              throw jmsEx;
85          }
86          return populateProperties(new TcpTransportChannel(wireFormat, socket, executor), remoteLocation);
87      }
88  
89      protected Socket createSocket(URI remoteLocation) throws IOException {
90          return socketFactory.createSocket(remoteLocation.getHost(), remoteLocation.getPort());
91      }
92  
93      protected Socket createSocket(URI remoteLocation, URI localLocation) throws IOException {
94          return socketFactory.createSocket(remoteLocation.getHost(), remoteLocation.getPort(), InetAddress
95                  .getByName(localLocation.getHost()), localLocation.getPort());
96      }
97  }