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.transport.socket.nio;
21  
22  import org.apache.mina.common.ExceptionMonitor;
23  import org.apache.mina.common.IoAcceptorConfig;
24  import org.apache.mina.common.IoSessionConfig;
25  import org.apache.mina.common.RuntimeIOException;
26  import org.apache.mina.common.support.BaseIoAcceptorConfig;
27  
28  import java.io.IOException;
29  import java.net.ServerSocket;
30  
31  /**
32   * An {@link IoAcceptorConfig} for {@link SocketAcceptor}.
33   *
34   * @author The Apache Directory Project (mina-dev@directory.apache.org)
35   * @version $Rev: 555855 $, $Date: 2007-07-13 05:19:00 +0200 (Fri, 13 Jul 2007) $
36   */
37  public class SocketAcceptorConfig extends BaseIoAcceptorConfig {
38      private SocketSessionConfig sessionConfig = new SocketSessionConfigImpl();
39  
40      private int backlog = 50;
41  
42      private boolean reuseAddress;
43  
44      /**
45       * Creates a new instance.
46       * 
47       * @throws RuntimeIOException if failed to get the default configuration
48       */
49      public SocketAcceptorConfig() {
50          ServerSocket s = null;
51          try {
52              s = new ServerSocket();
53              reuseAddress = s.getReuseAddress();
54          } catch (IOException e) {
55              throw new RuntimeIOException(
56                      "Failed to get the default configuration.", e);
57          } finally {
58              if (s != null) {
59                  try {
60                      s.close();
61                  } catch (IOException e) {
62                      ExceptionMonitor.getInstance().exceptionCaught(e);
63                  }
64              }
65          }
66  
67          sessionConfig.setReuseAddress(true);
68      }
69  
70      public IoSessionConfig getSessionConfig() {
71          return sessionConfig;
72      }
73  
74      /**
75       * @see ServerSocket#getReuseAddress()
76       */
77      public boolean isReuseAddress() {
78          return reuseAddress;
79      }
80  
81      /**
82       * @see ServerSocket#setReuseAddress(boolean)
83       */
84      public void setReuseAddress(boolean reuseAddress) {
85          this.reuseAddress = reuseAddress;
86      }
87  
88      public int getBacklog() {
89          return backlog;
90      }
91  
92      public void setBacklog(int backlog) {
93          this.backlog = backlog;
94      }
95  }