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   */
20  package org.apache.mina.example.httpserver.codec;
21  
22  import java.net.InetSocketAddress;
23  
24  import org.apache.mina.common.IoAcceptor;
25  import org.apache.mina.filter.LoggingFilter;
26  import org.apache.mina.filter.codec.ProtocolCodecFilter;
27  import org.apache.mina.transport.socket.nio.SocketAcceptor;
28  import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
29  
30  /**
31   * (<b>Entry point</b>) HTTP server
32   * 
33   * @author The Apache Directory Project (mina-dev@directory.apache.org)
34   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13  7월 2007) $
35   */
36  public class Server {
37      /** Default HTTP port */
38      private static int DEFAULT_PORT = 8080;
39  
40      /** Tile server revision number */
41      public static final String VERSION_STRING = "$Revision: 555855 $ $Date: 2007-07-13 12:19:00 +0900 (금, 13  7월 2007) $";
42  
43      public static void main(String[] args) {
44          int port = DEFAULT_PORT;
45  
46          for (int i = 0; i < args.length; i++) {
47              if (args[i].equals("-port")) {
48                  port = Integer.parseInt(args[i + 1]);
49              }
50          }
51  
52          try {
53              // Create an acceptor
54              IoAcceptor acceptor = new SocketAcceptor();
55  
56              // Create a service configuration
57              SocketAcceptorConfig cfg = new SocketAcceptorConfig();
58              cfg.setReuseAddress(true);
59              cfg.getFilterChain().addLast(
60                      "protocolFilter",
61                      new ProtocolCodecFilter(
62                              new HttpServerProtocolCodecFactory()));
63              cfg.getFilterChain().addLast("logger", new LoggingFilter());
64  
65              acceptor
66                      .bind(new InetSocketAddress(port), new ServerHandler(), cfg);
67  
68              System.out.println("Server now listening on port " + port);
69          } catch (Exception ex) {
70              ex.printStackTrace();
71          }
72      }
73  }