View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.directory.server.protocol.shared.transport;
20  
21  
22  import java.net.InetSocketAddress;
23  import java.util.List;
24  
25  import org.apache.directory.api.util.Network;
26  import org.apache.mina.core.service.IoAcceptor;
27  import org.apache.mina.transport.socket.SocketAcceptor;
28  import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  
33  /**
34   * The Transport instance for TCP based protocols.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class TcpTransport extends AbstractTransport
39  {
40      /** The SSL 'needClientAuth' flag */
41      private boolean needClientAuth;
42  
43      /** The SSL 'wantClientAuth' flag */
44      private boolean wantClientAuth;
45  
46      /** The list of enabled protocols */
47      private List<String> enabledProtocols;
48  
49      /** The list of enabled ciphers */
50      private List<String> cipherSuite;
51  
52      /** A logger for this class */
53      private static final Logger LOG = LoggerFactory.getLogger( TcpTransport.class );
54  
55  
56      /**
57       * Creates an instance of the TcpTransport class 
58       */
59      public TcpTransport()
60      {
61          super();
62      }
63  
64  
65      /**
66       * Creates an instance of the TcpTransport class on localhost
67       * @param tcpPort The port
68       */
69      public TcpTransport( int tcpPort )
70      {
71          super( null, tcpPort, DEFAULT_NB_THREADS, DEFAULT_BACKLOG_NB );
72  
73          this.acceptor = createAcceptor( null, tcpPort, DEFAULT_NB_THREADS, DEFAULT_BACKLOG_NB );
74  
75          LOG.debug( "TCP Transport created : <*:{},>", tcpPort );
76      }
77  
78  
79      /**
80       * Creates an instance of the TcpTransport class on localhost
81       * @param tcpPort The port
82       * @param nbThreads The number of threads to create in the acceptor
83       */
84      public TcpTransport( int tcpPort, int nbThreads )
85      {
86          super( null, tcpPort, nbThreads, DEFAULT_BACKLOG_NB );
87  
88          this.acceptor = createAcceptor( null, tcpPort, nbThreads, DEFAULT_BACKLOG_NB );
89  
90          LOG.debug( "TCP Transport created : <*:{},>", tcpPort );
91      }
92  
93  
94      /**
95       * Creates an instance of the TcpTransport class 
96       * @param address The address
97       * @param tcpPort The port
98       */
99      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 }