View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.channel.socket.oio;
17  
18  import static org.jboss.netty.channel.Channels.*;
19  
20  import java.io.IOException;
21  import java.net.InetSocketAddress;
22  import java.net.ServerSocket;
23  import java.util.concurrent.locks.Lock;
24  import java.util.concurrent.locks.ReentrantLock;
25  
26  import org.jboss.netty.channel.AbstractServerChannel;
27  import org.jboss.netty.channel.ChannelException;
28  import org.jboss.netty.channel.ChannelFactory;
29  import org.jboss.netty.channel.ChannelPipeline;
30  import org.jboss.netty.channel.ChannelSink;
31  import org.jboss.netty.channel.socket.DefaultServerSocketChannelConfig;
32  import org.jboss.netty.channel.socket.ServerSocketChannel;
33  import org.jboss.netty.channel.socket.ServerSocketChannelConfig;
34  import org.jboss.netty.logging.InternalLogger;
35  import org.jboss.netty.logging.InternalLoggerFactory;
36  
37  class OioServerSocketChannel extends AbstractServerChannel
38                               implements ServerSocketChannel {
39  
40      private static final InternalLogger logger =
41          InternalLoggerFactory.getInstance(OioServerSocketChannel.class);
42  
43      final ServerSocket socket;
44      final Lock shutdownLock = new ReentrantLock();
45      private final ServerSocketChannelConfig config;
46  
47      OioServerSocketChannel(
48              ChannelFactory factory,
49              ChannelPipeline pipeline,
50              ChannelSink sink) {
51  
52          super(factory, pipeline, sink);
53  
54          try {
55              socket = new ServerSocket();
56          } catch (IOException e) {
57              throw new ChannelException(
58                      "Failed to open a server socket.", e);
59          }
60  
61          try {
62              socket.setSoTimeout(1000);
63          } catch (IOException e) {
64              try {
65                  socket.close();
66              } catch (IOException e2) {
67                  if (logger.isWarnEnabled()) {
68                      logger.warn(
69                              "Failed to close a partially initialized socket.", e2);
70                  }
71              }
72              throw new ChannelException(
73                      "Failed to set the server socket timeout.", e);
74          }
75  
76          config = new DefaultServerSocketChannelConfig(socket);
77  
78          fireChannelOpen(this);
79      }
80  
81      public ServerSocketChannelConfig getConfig() {
82          return config;
83      }
84  
85      public InetSocketAddress getLocalAddress() {
86          return (InetSocketAddress) socket.getLocalSocketAddress();
87      }
88  
89      public InetSocketAddress getRemoteAddress() {
90          return null;
91      }
92  
93      public boolean isBound() {
94          return isOpen() && socket.isBound();
95      }
96  
97      @Override
98      protected boolean setClosed() {
99          return super.setClosed();
100     }
101 }