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 org.apache.directory.api.util.Network;
023import org.apache.mina.core.service.IoAcceptor;
024
025
026public abstract class AbstractTransport implements Transport
027{
028    /** The server address */
029    private String address;
030
031    /** The service's port */
032    private int port = -1;
033
034    /** A flag set if SSL is enabled */
035    private boolean sslEnabled = false;
036
037    /** The number of threads to use for the IoAcceptor executor */
038    private int nbThreads;
039
040    /** The backlog for the transport services */
041    private int backlog;
042
043    /** The IoAcceptor used to accept requests */
044    protected IoAcceptor acceptor;
045
046    /** The default backlog queue size */
047    protected static final int DEFAULT_BACKLOG_NB = 50;
048
049    /** The default hostname */
050    protected static final String LOCAL_HOST = "localhost";
051
052    /** The default number of threads */
053    protected static final int DEFAULT_NB_THREADS = 3;
054
055
056    /**
057     * Creates an instance of an Abstract Transport class.
058     */
059    public AbstractTransport()
060    {
061        address = null;
062        nbThreads = DEFAULT_NB_THREADS;
063        port = -1;
064        backlog = DEFAULT_BACKLOG_NB;
065    }
066
067
068    /**
069     * Creates an instance of an Abstract Transport class, using localhost
070     * and port.
071     * 
072     * @param port The port
073     */
074    public AbstractTransport( int port )
075    {
076        this.address = Network.LOOPBACK.getHostAddress();
077        this.port = port;
078    }
079
080
081    /**
082     * Creates an instance of an Abstract Transport class, using localhost
083     * and port.
084     * 
085     * @param port The port
086     * @param nbThreads The number of threads to create in the acceptor
087     */
088    public AbstractTransport( int port, int nbThreads )
089    {
090        this.address = Network.LOOPBACK.getHostAddress();
091        this.port = port;
092        this.nbThreads = nbThreads;
093    }
094
095
096    /**
097     * Creates an instance of an Abstract Transport class, using the given address
098     * and port.
099     * 
100     * @param address The address
101     * @param port The port
102     */
103    public AbstractTransport( String address, int port )
104    {
105        this.address = address;
106        this.port = port;
107    }
108
109
110    /**
111     * Creates an instance of the AbstractTransport class on LocalHost
112     * @param port The port
113     * @param nbThreads The number of threads to create in the acceptor
114     * @param backLog The queue size for incoming messages, waiting for the
115     * acceptor to be ready
116     */
117    public AbstractTransport( int port, int nbThreads, int backLog )
118    {
119        this.address = "localHost";
120        this.port = port;
121        this.nbThreads = nbThreads;
122        this.backlog = backLog;
123    }
124
125
126    /**
127     * Creates an instance of the AbstractTransport class 
128     * @param address The address
129     * @param port The port
130     * @param nbThreads The number of threads to create in the acceptor
131     * @param backLog The queue size for incoming messages, waiting for the
132     * acceptor to be ready
133     */
134    public AbstractTransport( String address, int port, int nbThreads, int backLog )
135    {
136        this.address = address;
137        this.port = port;
138        this.nbThreads = nbThreads;
139        this.backlog = backLog;
140    }
141
142
143    /**
144     * {@inheritDoc}
145     */
146    public int getPort()
147    {
148        return port;
149    }
150
151
152    /**
153     * {@inheritDoc}
154     */
155    public void setPort( int port )
156    {
157        this.port = port;
158    }
159
160
161    /**
162     * {@inheritDoc}
163     */
164    public String getAddress()
165    {
166        return address;
167    }
168
169
170    /**
171     * Stores the Address in this transport
172     * 
173     * @param address the Address to store
174     */
175    public void setAddress( String address )
176    {
177        this.address = address;
178    }
179
180
181    /**
182     * {@inheritDoc}
183     */
184    public int getNbThreads()
185    {
186        return nbThreads;
187    }
188
189
190    /**
191     * {@inheritDoc}
192     */
193    public void setNbThreads( int nbThreads )
194    {
195        this.nbThreads = nbThreads;
196    }
197
198
199    /**
200     * {@inheritDoc}
201     */
202    public int getBackLog()
203    {
204        return backlog;
205    }
206
207
208    /**
209     * {@inheritDoc}
210     */
211    public void setBackLog( int backLog )
212    {
213        this.backlog = backLog;
214    }
215
216
217    /**
218     * Enable or disable SSL
219     * @param sslEnabled if <code>true</code>, SSL is enabled.
220     */
221    public void setEnableSSL( boolean sslEnabled )
222    {
223        this.sslEnabled = sslEnabled;
224    }
225
226
227    /**
228     * Enable or disable SSL
229     * @param sslEnabled if <code>true</code>, SSL is enabled.
230     */
231    public void enableSSL( boolean sslEnabled )
232    {
233        this.sslEnabled = sslEnabled;
234    }
235
236
237    /**
238     * @return <code>true</code> id SSL is enabled for this transport
239     */
240    public boolean isSSLEnabled()
241    {
242        return sslEnabled;
243    }
244
245
246    /**
247     * @return <code>true</code> id SSL is enabled for this transport
248     */
249    public boolean getEnableSSL()
250    {
251        return sslEnabled;
252    }
253
254
255    /**
256     * @see Object#toString()
257     */
258    public String toString()
259    {
260        StringBuilder sb = new StringBuilder();
261        sb.append( "[<" ).append( address ).append( ':' ).append( port );
262        sb.append( ">], backlog=" ).append( backlog );
263        sb.append( ", nbThreads = " ).append( nbThreads );
264
265        if ( sslEnabled )
266        {
267            sb.append( ", SSL" );
268        }
269
270        sb.append( ']' );
271
272        return sb.toString();
273    }
274}