FileDocCategorySizeDatePackage
Connection.javaAPI DocExample10412Tue May 29 16:57:08 BST 2007com.sun.xml.ws.transport.tcp.io

Connection

public final class Connection extends Object
author
Alexey Stashok

Fields Summary
private static final Logger
logger
private static com.sun.xml.ws.transport.tcp.pool.ByteBufferStreamPool
byteBufferInputStreamPool
private static com.sun.xml.ws.transport.tcp.pool.ByteBufferStreamPool
byteBufferOutputStreamPool
private SocketChannel
socketChannel
private WeakReference
inputStreamRef
private FramedMessageInputStream
inputStream
private FramedMessageOutputStream
outputStream
private boolean
isDirectMode
is message framed or direct mode is used
private int
messageId
private int
channelId
private int
contentId
Constructors Summary
public Connection(SocketChannel socketChannel)

    
        
        inputStream = byteBufferInputStreamPool.take();
        outputStream = byteBufferOutputStreamPool.take();
        setSocketChannel(socketChannel);
    
Methods Summary
public voidclose()

        if (inputStream != null) {
            byteBufferInputStreamPool.release(inputStream);
            inputStream = null;
        }
        
        if (outputStream != null) {
            byteBufferOutputStreamPool.release(outputStream);
            outputStream = null;
        }
        
        socketChannel.close();
    
public static com.sun.xml.ws.transport.tcp.io.Connectioncreate(java.lang.String host, int port)

        if (logger.isLoggable(Level.FINE)) {
            logger.log(Level.FINE, MessagesMessages.WSTCP_1051_CONNECTION_OPEN_TCP_SOCKET(host, port));
        }
        final SocketChannel socketChannel = SocketChannel.open();
        final Socket socket = socketChannel.socket();
        socket.connect(new InetSocketAddress(host, port));
        socketChannel.configureBlocking(false);
        
        final Connection connection = new Connection(socketChannel);
        
        final ByteBuffer byteBuffer = ByteBufferFactory.allocateView(TCPConstants.DEFAULT_FRAME_SIZE, TCPConstants.DEFAULT_USE_DIRECT_BUFFER);
        byteBuffer.position(0);
        byteBuffer.limit(0);
        
        connection.setInputStreamByteBuffer(byteBuffer);
        
        return connection;
    
protected voidfinalize()

        close();
        super.finalize();
    
public voidflush()

        outputStream.flushLast();
    
public intgetChannelId()
Get channel id

        return channelId;
    
public intgetContentId()
Get request/response contentId

        return contentId;
    
public java.util.MapgetContentProperties()
Get request content properties

        return inputStream.getContentProperties();
    
public java.lang.StringgetHost()

        return getHost(socketChannel);
    
public static java.lang.StringgetHost(java.nio.channels.SocketChannel socketChannel)

        return socketChannel.socket().getInetAddress().getHostAddress();
    
public java.lang.StringgetLocalHost()

        return getLocalHost(socketChannel);
    
public static java.lang.StringgetLocalHost(java.nio.channels.SocketChannel socketChannel)

        return socketChannel.socket().getLocalAddress().getHostAddress();
    
public intgetLocalPort()

        return getLocalPort(socketChannel);
    
public static intgetLocalPort(java.nio.channels.SocketChannel socketChannel)

        return socketChannel.socket().getLocalPort();
    
public intgetMessageId()
Get request/response messageId of 1st frame

        return messageId;
    
public intgetPort()

        return getPort(socketChannel);
    
public static intgetPort(java.nio.channels.SocketChannel socketChannel)

        return socketChannel.socket().getPort();
    
public java.nio.channels.SocketChannelgetSocketChannel()

        return socketChannel;
    
public booleanisDirectMode()

        return isDirectMode;
    
public java.io.InputStreamopenInputStream()

        final BufferedMessageInputStream is = new BufferedMessageInputStream(inputStream);
        inputStreamRef = new WeakReference<BufferedMessageInputStream>(is);
        return is;
    
public java.io.OutputStreamopenOutputStream()

        outputStream.setChannelId(channelId);
        outputStream.setMessageId(messageId);
        outputStream.setContentId(contentId);
        
        return outputStream;
    
public voidprepareForReading()

        if (inputStreamRef != null) {
            final BufferedMessageInputStream is = inputStreamRef.get();
            // if InputStream is used by some lazy reader - buffer message
            if (inputStream.isMessageInProcess() && is != null && !is.isClosed()) {
                is.bufferMessage();
                logger.log(Level.FINEST, MessagesMessages.WSTCP_1050_CONNECTION_BUFFERING_IS(is.getBufferedSize()));
            }
        }
        
        // double check input stream doesnt have earlier read message
        if (inputStream.isMessageInProcess()) {
            inputStream.skipToEndOfMessage();
        }
        
        inputStream.reset();
        outputStream.reset();
        
        inputStream.forceHeaderRead();
        
        channelId = inputStream.getChannelId();
        messageId = inputStream.getMessageId();
        
        if (FrameType.isFrameContainsParams(messageId)) {
            contentId = inputStream.getContentId();
        }
    
public voidsetChannelId(int channelId)
Set channel id

        this.channelId = channelId;
    
public voidsetContentId(int contentId)
Set request/response contentId

        this.contentId = contentId;
    
public voidsetContentProperty(int key, java.lang.String value)
Set response content properties

        outputStream.setContentProperty(key, value);
    
public voidsetDirectMode(boolean isDirectMode)

        this.isDirectMode = isDirectMode;
        inputStream.setDirectMode(isDirectMode);
        outputStream.setDirectMode(isDirectMode);
    
public voidsetInputStreamByteBuffer(java.nio.ByteBuffer messageBuffer)
Set messageBuffer for InputStream some message part could be preread before

        inputStream.setByteBuffer(messageBuffer);
    
public voidsetMessageId(int messageId)
Set request/response messageId of 1st frame

        this.messageId = messageId;
    
public voidsetSocketChannel(java.nio.channels.SocketChannel socketChannel)

        this.socketChannel = socketChannel;
        inputStream.setSocketChannel(socketChannel);
        outputStream.setSocketChannel(socketChannel);
    
public java.lang.StringtoString()

        return "host: " + getHost() + " port: " + getPort();