001 /**
002 *
003 * Copyright 2004 Protique Ltd
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 **/
018 package org.activemq.transport.tcp;
019
020 import EDU.oswego.cs.dl.util.concurrent.Executor;
021 import org.activemq.io.WireFormat;
022 import org.activemq.transport.TransportChannel;
023
024 import javax.jms.JMSException;
025 import javax.net.SocketFactory;
026 import java.io.IOException;
027 import java.net.InetAddress;
028 import java.net.Socket;
029 import java.net.URI;
030
031 /**
032 * A factory of TcpTransportChannelFactory instances using a SocketFactory
033 *
034 * @version $Revision: 1.1.1.1 $
035 */
036 public class SfTransportChannelFactory extends TcpTransportChannelFactory {
037
038 private SocketFactory socketFactory;
039 private Executor executor;
040
041 public SfTransportChannelFactory(SocketFactory socketFactory) {
042 this.socketFactory = socketFactory;
043 }
044
045 /**
046 * Create a Channel to a remote Node - e.g. a Broker
047 *
048 * @param wireFormat
049 * @param remoteLocation
050 * @return the TransportChannel bound to the remote node
051 * @throws JMSException
052 */
053 public TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
054 Socket socket = null;
055 try {
056 socket = createSocket(remoteLocation);
057 }
058 catch (IOException e) {
059 JMSException jmsEx = new JMSException("Creation of Socket failed: " + e);
060 jmsEx.setLinkedException(e);
061 throw jmsEx;
062 }
063 return populateProperties(new TcpTransportChannel(wireFormat, socket, executor), remoteLocation);
064
065 }
066
067 /**
068 * Create a Channel to a remote Node - e.g. a Broker
069 *
070 * @param wireFormat
071 * @param remoteLocation
072 * @param localLocation -
073 * e.g. local InetAddress and local port
074 * @return the TransportChannel bound to the remote node
075 * @throws JMSException
076 */
077 public TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException {
078 Socket socket = null;
079 try {
080 socket = createSocket(remoteLocation, localLocation);
081 }
082 catch (IOException e) {
083 JMSException jmsEx = new JMSException("Creation of Socket failed: " + e);
084 jmsEx.setLinkedException(e);
085 throw jmsEx;
086 }
087 return populateProperties(new TcpTransportChannel(wireFormat, socket, executor), remoteLocation);
088 }
089
090 protected Socket createSocket(URI remoteLocation) throws IOException {
091 return socketFactory.createSocket(remoteLocation.getHost(), remoteLocation.getPort());
092 }
093
094 protected Socket createSocket(URI remoteLocation, URI localLocation) throws IOException {
095 return socketFactory.createSocket(remoteLocation.getHost(), remoteLocation.getPort(), InetAddress
096 .getByName(localLocation.getHost()), localLocation.getPort());
097 }
098 }