KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > QServerSocketWrapper


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free SoftwareFoundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.vfs;
31
32 import com.caucho.log.Log;
33
34 import java.io.IOException JavaDoc;
35 import java.net.InetAddress JavaDoc;
36 import java.net.ServerSocket JavaDoc;
37 import java.net.Socket JavaDoc;
38 import java.nio.channels.Selector JavaDoc;
39 import java.nio.channels.ServerSocketChannel JavaDoc;
40 import java.nio.channels.spi.SelectorProvider JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * Abstract socket to handle both normal sockets and bin/resin sockets.
46  */

47 public class QServerSocketWrapper extends QServerSocket {
48   private static final Logger JavaDoc log = Log.open(QServerSocketWrapper.class);
49   
50   private ServerSocket JavaDoc _ss;
51   private boolean _tcpNoDelay = true;
52   private int _connectionSocketTimeout = 65000;
53   
54   public QServerSocketWrapper()
55   {
56   }
57
58   public QServerSocketWrapper(ServerSocket JavaDoc ss)
59   {
60     init(ss);
61   }
62
63   public void init(ServerSocket JavaDoc ss)
64   {
65     _ss = ss;
66   }
67
68   public void setTcpNoDelay(boolean delay)
69   {
70     _tcpNoDelay = delay;
71   }
72
73   public boolean getTcpNoDelay()
74   {
75     return _tcpNoDelay;
76   }
77
78   public void setConnectionSocketTimeout(int socketTimeout)
79   {
80     _connectionSocketTimeout = socketTimeout;
81   }
82   
83   /**
84    * Accepts a new socket.
85    */

86   public boolean accept(QSocket qSocket)
87     throws IOException JavaDoc
88   {
89     QSocketWrapper s = (QSocketWrapper) qSocket;
90
91     Socket JavaDoc socket = _ss.accept();
92
93     if (socket == null)
94       return false;
95
96     // XXX:
97
if (_tcpNoDelay)
98       socket.setTcpNoDelay(true);
99
100     if (_connectionSocketTimeout > 0)
101       socket.setSoTimeout(_connectionSocketTimeout);
102
103     s.init(socket);
104
105     return true;
106   }
107   
108   /**
109    * Creates a new socket object.
110    */

111   public QSocket createSocket()
112     throws IOException JavaDoc
113   {
114     return new QSocketWrapper();
115   }
116
117   public InetAddress JavaDoc getLocalAddress()
118   {
119     return _ss.getInetAddress();
120   }
121
122   public int getLocalPort()
123   {
124     return _ss.getLocalPort();
125   }
126
127   public Selector JavaDoc getSelector()
128   {
129     try {
130       ServerSocketChannel JavaDoc channel = _ss.getChannel();
131
132       if (channel == null)
133         return null;
134       
135       SelectorProvider JavaDoc provider = channel.provider();
136
137       if (provider != null)
138         return provider.openSelector();
139       else
140         return null;
141     } catch (Throwable JavaDoc e) {
142       log.log(Level.WARNING, e.toString(), e);
143       return null;
144     }
145   }
146
147   /**
148    * Closes the underlying socket.
149    */

150   public void close()
151     throws IOException JavaDoc
152   {
153     ServerSocket JavaDoc ss = _ss;
154     _ss = ss;
155
156     if (ss != null) {
157       try {
158         ss.close();
159       } catch (Exception JavaDoc e) {
160       }
161     }
162   }
163   
164   public String JavaDoc toString()
165   {
166     return "ServerSocketWrapper[" + getLocalAddress() + ":" + getLocalPort() + "]";
167   }
168 }
169
170
Popular Tags