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