View Javadoc

1   /*
2    *   @(#) $Id: Main.java 332218 2005-11-10 03:52:42Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   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, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.mina.examples.echoserver;
20  
21  import org.apache.mina.common.TransportType;
22  import org.apache.mina.examples.echoserver.ssl.BogusSSLContextFactory;
23  import org.apache.mina.io.IoAcceptor;
24  import org.apache.mina.io.filter.IoLoggingFilter;
25  import org.apache.mina.io.filter.SSLFilter;
26  import org.apache.mina.registry.Service;
27  import org.apache.mina.registry.ServiceRegistry;
28  import org.apache.mina.registry.SimpleServiceRegistry;
29  
30  /***
31   * (<b>Entry point</b>) Echo server
32   * 
33   * @author The Apache Directory Project (dev@directory.apache.org)
34   * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $
35   */
36  public class Main
37  {
38      /*** Choose your favorite port number. */
39      private static final int PORT = 8080;
40      
41      /*** Set this to true if you want to make the server SSL */
42      private static final boolean USE_SSL = false;
43  
44      public static void main( String[] args ) throws Exception
45      {
46          ServiceRegistry registry = new SimpleServiceRegistry();
47          
48          // Add SSL filter if SSL is enabled.
49          if( USE_SSL )
50          {
51              addSSLSupport( registry );
52          }
53          
54          addLogger( registry );
55          
56          // Bind
57          Service service = new Service( "echo", TransportType.SOCKET, PORT );
58          registry.bind( service, new EchoProtocolHandler() );
59  
60          System.out.println( "Listening on port " + PORT );
61      }
62  
63      private static void addSSLSupport( ServiceRegistry registry )
64          throws Exception
65      {
66          SSLFilter sslFilter =
67              new SSLFilter( BogusSSLContextFactory.getInstance( true ) );
68          IoAcceptor acceptor = registry.getIoAcceptor( TransportType.SOCKET );
69          acceptor.getFilterChain().addLast( "sslFilter", sslFilter );
70          System.out.println( "SSL ON" );
71      }
72      
73      private static void addLogger( ServiceRegistry registry )
74      {
75          IoAcceptor acceptor = registry.getIoAcceptor( TransportType.SOCKET );
76          acceptor.getFilterChain().addLast( "logger", new IoLoggingFilter() );
77          System.out.println( "Logging ON" );
78      }
79  }