001
014
015 package com.liferay.portal.kernel.nio.intraband.welder.socket;
016
017 import com.liferay.portal.kernel.nio.intraband.Intraband;
018 import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
019 import com.liferay.portal.kernel.nio.intraband.welder.BaseWelder;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.InetAddressUtil;
022 import com.liferay.portal.kernel.util.PropsKeys;
023 import com.liferay.portal.kernel.util.PropsUtil;
024 import com.liferay.portal.kernel.util.SocketUtil;
025 import com.liferay.portal.kernel.util.SocketUtil.ServerSocketConfigurator;
026
027 import java.io.IOException;
028
029 import java.net.InetSocketAddress;
030 import java.net.ServerSocket;
031 import java.net.Socket;
032 import java.net.SocketException;
033
034 import java.nio.channels.ServerSocketChannel;
035 import java.nio.channels.SocketChannel;
036
037
040 public class SocketWelder extends BaseWelder {
041
042 public SocketWelder() throws IOException {
043
044
045
046
047
048
049
050 bufferSize = Configuration.bufferSize;
051 keepAlive = Configuration.keepAlive;
052 reuseAddress = Configuration.reuseAddress;
053 soLinger = Configuration.soLinger;
054 soTimeout = Configuration.soTimeout;
055 tcpNoDelay = Configuration.tcpNoDelay;
056
057 serverSocketChannel = SocketUtil.createServerSocketChannel(
058 InetAddressUtil.getLoopbackInetAddress(),
059 Configuration.serverStartPort,
060 new SocketWelderServerSocketConfigurator());
061
062 ServerSocket serverSocket = serverSocketChannel.socket();
063
064 serverPort = serverSocket.getLocalPort();
065 }
066
067 @Override
068 protected void doDestroy() throws IOException {
069 socketChannel.close();
070 }
071
072 @Override
073 protected RegistrationReference weldClient(Intraband intraband)
074 throws IOException {
075
076 socketChannel = SocketChannel.open();
077
078 _configureSocket(socketChannel.socket());
079
080 socketChannel.connect(
081 new InetSocketAddress(
082 InetAddressUtil.getLoopbackInetAddress(), serverPort));
083
084 return intraband.registerChannel(socketChannel);
085 }
086
087 @Override
088 protected RegistrationReference weldServer(Intraband intraband)
089 throws IOException {
090
091 socketChannel = serverSocketChannel.accept();
092
093 serverSocketChannel.close();
094
095 _configureSocket(socketChannel.socket());
096
097 return intraband.registerChannel(socketChannel);
098 }
099
100 protected final int bufferSize;
101 protected final boolean keepAlive;
102 protected final boolean reuseAddress;
103 protected final int serverPort;
104 protected final transient ServerSocketChannel serverSocketChannel;
105 protected transient SocketChannel socketChannel;
106 protected final int soLinger;
107 protected final int soTimeout;
108 protected final boolean tcpNoDelay;
109
110 protected static class Configuration {
111
112 protected static final int bufferSize = GetterUtil.getInteger(
113 PropsUtil.get(PropsKeys.INTRABAND_WELDER_SOCKET_BUFFER_SIZE));
114 protected static final boolean keepAlive = GetterUtil.getBoolean(
115 PropsUtil.get(PropsKeys.INTRABAND_WELDER_SOCKET_KEEP_ALIVE));
116 protected static final boolean reuseAddress = GetterUtil.getBoolean(
117 PropsUtil.get(PropsKeys.INTRABAND_WELDER_SOCKET_REUSE_ADDRESS));
118 protected static final int serverStartPort = GetterUtil.getInteger(
119 PropsUtil.get(PropsKeys.INTRABAND_WELDER_SOCKET_SERVER_START_PORT));
120 protected static final int soLinger = GetterUtil.getInteger(
121 PropsUtil.get(PropsKeys.INTRABAND_WELDER_SOCKET_SO_LINGER));
122 protected static final int soTimeout = GetterUtil.getInteger(
123 PropsUtil.get(PropsKeys.INTRABAND_WELDER_SOCKET_SO_TIMEOUT));
124 protected static final boolean tcpNoDelay = GetterUtil.getBoolean(
125 PropsUtil.get(PropsKeys.INTRABAND_WELDER_SOCKET_TCP_NO_DELAY));
126
127 }
128
129 protected class SocketWelderServerSocketConfigurator
130 implements ServerSocketConfigurator {
131
132 @Override
133 public void configure(ServerSocket serverSocket)
134 throws SocketException {
135
136 serverSocket.setReceiveBufferSize(bufferSize);
137 serverSocket.setReuseAddress(reuseAddress);
138 serverSocket.setSoTimeout(soTimeout);
139 }
140
141 }
142
143 private void _configureSocket(Socket socket) throws SocketException {
144 socket.setKeepAlive(keepAlive);
145 socket.setReceiveBufferSize(bufferSize);
146 socket.setReuseAddress(reuseAddress);
147 socket.setSendBufferSize(bufferSize);
148
149 if (soLinger <= 0) {
150 socket.setSoLinger(false, soLinger);
151 }
152 else {
153 socket.setSoLinger(true, soLinger);
154 }
155
156 socket.setSoTimeout(soTimeout);
157 socket.setTcpNoDelay(tcpNoDelay);
158 }
159
160 }