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     * Initialize the Acceptor if needed
145     */
146    public abstract void init();
147
148
149    /**
150     * {@inheritDoc}
151     */
152    public int getPort()
153    {
154        return port;
155    }
156
157
158    /**
159     * {@inheritDoc}
160     */
161    public void setPort( int port )
162    {
163        this.port = port;
164    }
165
166
167    /**
168     * {@inheritDoc}
169     */
170    public String getAddress()
171    {
172        return address;
173    }
174
175
176    /**
177     * Stores the Address in this transport
178     * 
179     * @param address the Address to store
180     */
181    public void setAddress( String address )
182    {
183        this.address = address;
184    }
185
186
187    /**
188     * {@inheritDoc}
189     */
190    public abstract IoAcceptor getAcceptor();
191
192
193    /**
194     * {@inheritDoc}
195     */
196    public int getNbThreads()
197    {
198        return nbThreads;
199    }
200
201
202    /**
203     * {@inheritDoc}
204     */
205    public void setNbThreads( int nbThreads )
206    {
207        this.nbThreads = nbThreads;
208    }
209
210
211    /**
212     * {@inheritDoc}
213     */
214    public int getBackLog()
215    {
216        return backlog;
217    }
218
219
220    /**
221     * {@inheritDoc}
222     */
223    public void setBackLog( int backLog )
224    {
225        this.backlog = backLog;
226    }
227
228
229    /**
230     * Enable or disable SSL
231     * @param sslEnabled if <code>true</code>, SSL is enabled.
232     */
233    public void setEnableSSL( boolean sslEnabled )
234    {
235        this.sslEnabled = sslEnabled;
236    }
237
238
239    /**
240     * Enable or disable SSL
241     * @param sslEnabled if <code>true</code>, SSL is enabled.
242     */
243    public void enableSSL( boolean sslEnabled )
244    {
245        this.sslEnabled = sslEnabled;
246    }
247
248
249    /**
250     * @return <code>true</code> id SSL is enabled for this transport
251     */
252    public boolean isSSLEnabled()
253    {
254        return sslEnabled;
255    }
256
257
258    /**
259     * @return  <code>true</code> id SSL is enabled for this transport
260     */
261    public boolean getEnableSSL()
262    {
263        return sslEnabled;
264    }
265
266
267    /**
268     * @see Object#toString()
269     */
270    public String toString()
271    {
272        StringBuilder sb = new StringBuilder();
273        sb.append( "[<" ).append( address ).append( ':' ).append( port );
274        sb.append( ">], backlog=" ).append( backlog );
275        sb.append( ", nbThreads = " ).append( nbThreads );
276
277        if ( sslEnabled )
278        {
279            sb.append( ", SSL" );
280        }
281
282        sb.append( ']' );
283
284        return sb.toString();
285    }
286}