001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.directory.server.protocol.shared.transport; 020 021 022import java.net.InetSocketAddress; 023import java.util.List; 024 025import org.apache.directory.api.util.Network; 026import org.apache.mina.core.service.IoAcceptor; 027import org.apache.mina.transport.socket.SocketAcceptor; 028import org.apache.mina.transport.socket.nio.NioSocketAcceptor; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032 033/** 034 * The Transport instance for TCP based protocols. 035 * 036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 037 */ 038public class TcpTransport extends AbstractTransport 039{ 040 /** The SSL 'needClientAuth' flag */ 041 private boolean needClientAuth; 042 043 /** The SSL 'wantClientAuth' flag */ 044 private boolean wantClientAuth; 045 046 /** The list of enabled protocols */ 047 private List<String> enabledProtocols; 048 049 /** The list of enabled ciphers */ 050 private List<String> cipherSuite; 051 052 /** A logger for this class */ 053 private static final Logger LOG = LoggerFactory.getLogger( TcpTransport.class ); 054 055 056 /** 057 * Creates an instance of the TcpTransport class 058 */ 059 public TcpTransport() 060 { 061 super(); 062 } 063 064 065 /** 066 * Creates an instance of the TcpTransport class on localhost 067 * @param tcpPort The port 068 */ 069 public TcpTransport( int tcpPort ) 070 { 071 super( null, tcpPort, DEFAULT_NB_THREADS, DEFAULT_BACKLOG_NB ); 072 073 this.acceptor = createAcceptor( null, tcpPort, DEFAULT_NB_THREADS, DEFAULT_BACKLOG_NB ); 074 075 LOG.debug( "TCP Transport created : <*:{},>", tcpPort ); 076 } 077 078 079 /** 080 * Creates an instance of the TcpTransport class on localhost 081 * @param tcpPort The port 082 * @param nbThreads The number of threads to create in the acceptor 083 */ 084 public TcpTransport( int tcpPort, int nbThreads ) 085 { 086 super( null, tcpPort, nbThreads, DEFAULT_BACKLOG_NB ); 087 088 this.acceptor = createAcceptor( null, tcpPort, nbThreads, DEFAULT_BACKLOG_NB ); 089 090 LOG.debug( "TCP Transport created : <*:{},>", tcpPort ); 091 } 092 093 094 /** 095 * Creates an instance of the TcpTransport class 096 * @param address The address 097 * @param tcpPort The port 098 */ 099 public TcpTransport( String address, int tcpPort ) 100 { 101 super( address, tcpPort, DEFAULT_NB_THREADS, DEFAULT_BACKLOG_NB ); 102 this.acceptor = createAcceptor( address, tcpPort, DEFAULT_NB_THREADS, DEFAULT_BACKLOG_NB ); 103 104 LOG.debug( "TCP Transport created : <{}:{}>", address, tcpPort ); 105 } 106 107 108 /** 109 * Creates an instance of the TcpTransport class on localhost 110 * @param tcpPort The port 111 * @param nbThreads The number of threads to create in the acceptor 112 * @param backLog The queue size for incoming messages, waiting for the 113 * acceptor to be ready 114 */ 115 public TcpTransport( int tcpPort, int nbThreads, int backLog ) 116 { 117 super( Network.LOOPBACK.getHostAddress(), tcpPort, nbThreads, backLog ); 118 this.acceptor = createAcceptor( null, tcpPort, nbThreads, backLog ); 119 120 LOG.debug( "TCP Transport created : <*:{},>", tcpPort ); 121 } 122 123 124 /** 125 * Creates an instance of the TcpTransport class 126 * @param address The address 127 * @param tcpPort The port 128 * @param nbThreads The number of threads to create in the acceptor 129 * @param backLog The queue size for incoming messages, waiting for the 130 * acceptor to be ready 131 */ 132 public TcpTransport( String address, int tcpPort, int nbThreads, int backLog ) 133 { 134 super( address, tcpPort, nbThreads, backLog ); 135 this.acceptor = createAcceptor( address, tcpPort, nbThreads, backLog ); 136 137 LOG.debug( "TCP Transport created : <{}:{},>", address, tcpPort ); 138 } 139 140 141 /** 142 * Initialize the Acceptor if needed 143 */ 144 public void init() 145 { 146 acceptor = createAcceptor( getAddress(), getPort(), getNbThreads(), getBackLog() ); 147 } 148 149 150 /** 151 * Helper method to create an IoAcceptor 152 */ 153 private IoAcceptor createAcceptor( String address, int port, int nbThreads, int backLog ) 154 { 155 NioSocketAcceptor acceptor = new NioSocketAcceptor( nbThreads ); 156 acceptor.setReuseAddress( true ); 157 acceptor.setBacklog( backLog ); 158 159 InetSocketAddress socketAddress = null; 160 161 // The address can be null here, if one want to connect using the wildcard address 162 if ( address == null ) 163 { 164 // Create a socket listening on the wildcard address 165 socketAddress = new InetSocketAddress( port ); 166 } 167 else 168 { 169 socketAddress = new InetSocketAddress( address, port ); 170 } 171 172 acceptor.setDefaultLocalAddress( socketAddress ); 173 174 return acceptor; 175 } 176 177 178 /** 179 * @return The associated SocketAcceptor 180 */ 181 public SocketAcceptor getAcceptor() 182 { 183 if ( ( acceptor != null ) && acceptor.isDisposed() ) 184 { 185 acceptor = createAcceptor( getAddress(), getPort(), getNbThreads(), getBackLog() ); 186 } 187 188 return acceptor == null ? null : ( SocketAcceptor ) acceptor; 189 } 190 191 192 /** 193 * Set the needClientAuth SSL flag 194 * 195 * @param needClientAuth the flag to set 196 */ 197 public void setNeedClientAuth( boolean needClientAuth ) 198 { 199 this.needClientAuth = needClientAuth; 200 } 201 202 203 /** 204 * @return <code>true</code> if the NeedClientAuth SSL flag is set 205 */ 206 public boolean isNeedClientAuth() 207 { 208 return needClientAuth; 209 } 210 211 212 /** 213 * Set the wantClientAuth SSL flag 214 * 215 * @param wantClientAuth the flag to set 216 */ 217 public void setWantClientAuth( boolean wantClientAuth ) 218 { 219 this.wantClientAuth = wantClientAuth; 220 } 221 222 223 /** 224 * @return <code>true</code> if the WantClientAuth SSL flag is set 225 */ 226 public boolean isWantClientAuth() 227 { 228 return wantClientAuth; 229 } 230 231 232 /** 233 * @return The list of enabled protocols 234 */ 235 public List<String> getEnabledProtocols() 236 { 237 return enabledProtocols; 238 } 239 240 241 /** 242 * Set the list of enabled protocols 243 * 244 * @param enabledProtocols The list of enabled protocols 245 */ 246 public void setEnabledProtocols( List<String> enabledProtocols ) 247 { 248 this.enabledProtocols = enabledProtocols; 249 } 250 251 252 /** 253 * @return The list of enabled ciphers 254 */ 255 public List<String> getCipherSuite() 256 { 257 return cipherSuite; 258 } 259 260 261 /** 262 * Set the list of enabled ciphers 263 * 264 * @param cipherSuite The list of enabled ciphers 265 */ 266 public void setEnabledCiphers( List<String> cipherSuite ) 267 { 268 this.cipherSuite = cipherSuite; 269 } 270 271 272 /** 273 * @see Object#toString() 274 */ 275 @Override 276 public String toString() 277 { 278 return "TcpTransport" + super.toString(); 279 } 280}