Source code: org/activemq/transport/tcp/SfTransportChannelFactory.java
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.activemq.transport.tcp;
19
20 import EDU.oswego.cs.dl.util.concurrent.Executor;
21 import org.activemq.io.WireFormat;
22 import org.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.1.1.1 $
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 /**
68 * Create a Channel to a remote Node - e.g. a Broker
69 *
70 * @param wireFormat
71 * @param remoteLocation
72 * @param localLocation -
73 * e.g. local InetAddress and local port
74 * @return the TransportChannel bound to the remote node
75 * @throws JMSException
76 */
77 public TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException {
78 Socket socket = null;
79 try {
80 socket = createSocket(remoteLocation, localLocation);
81 }
82 catch (IOException e) {
83 JMSException jmsEx = new JMSException("Creation of Socket failed: " + e);
84 jmsEx.setLinkedException(e);
85 throw jmsEx;
86 }
87 return populateProperties(new TcpTransportChannel(wireFormat, socket, executor), remoteLocation);
88 }
89
90 protected Socket createSocket(URI remoteLocation) throws IOException {
91 return socketFactory.createSocket(remoteLocation.getHost(), remoteLocation.getPort());
92 }
93
94 protected Socket createSocket(URI remoteLocation, URI localLocation) throws IOException {
95 return socketFactory.createSocket(remoteLocation.getHost(), remoteLocation.getPort(), InetAddress
96 .getByName(localLocation.getHost()), localLocation.getPort());
97 }
98 }