KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > ss > provider > ASServerSocketImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.server.ss.provider;
24
25 import java.io.IOException JavaDoc;
26 import java.net.InetSocketAddress JavaDoc;
27 import java.net.ServerSocket JavaDoc;
28 import java.net.Socket JavaDoc;
29 import java.net.SocketException JavaDoc;
30 import java.nio.channels.Selector JavaDoc;
31 import java.nio.channels.ServerSocketChannel JavaDoc;
32 import java.nio.channels.AsynchronousCloseException JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 import com.sun.enterprise.server.ss.spi.ASSocketFacadeUtils;
38 import com.sun.logging.LogDomains;
39
40 /**
41  * NIO based SocketImpl implementation used by java.net.ServerSocket.
42  */

43 public class ASServerSocketImpl extends java.net.SocketImpl JavaDoc {
44     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
45     private java.net.InetAddress JavaDoc hostToBind;
46     private int portToBind;
47
48     private ServerSocketChannel JavaDoc ssc;
49     private ServerSocket JavaDoc ss;
50     private Selector JavaDoc selector;
51     private Hashtable JavaDoc options = new Hashtable JavaDoc();
52     
53     public int available() throws IOException JavaDoc {
54     throw new UnsupportedOperationException JavaDoc(
55             "available() not supported in ASServerSocketImpl");
56     }
57
58     public void connect(java.lang.String JavaDoc s,int i) throws IOException JavaDoc {
59     throw new UnsupportedOperationException JavaDoc(
60             "connect() not supported in ASServerSocketImpl");
61     }
62
63     public void connect(java.net.InetAddress JavaDoc ia,int i)
64         throws IOException JavaDoc {
65     throw new UnsupportedOperationException JavaDoc(
66             "connect() not supported in ASServerSocketImpl");
67     }
68
69     public void connect(java.net.SocketAddress JavaDoc sa,int i)
70         throws IOException JavaDoc {
71     throw new UnsupportedOperationException JavaDoc(
72             "connect() not supported in ASServerSocketImpl");
73     }
74
75     public java.io.InputStream JavaDoc getInputStream() throws IOException JavaDoc {
76     throw new UnsupportedOperationException JavaDoc(
77             "getInputStream() not supported in ASServerSocketImpl");
78     }
79
80     public java.io.OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
81     throw new UnsupportedOperationException JavaDoc(
82             "getOutputStream() not supported in ASServerSocketImpl");
83     }
84
85     public void shutdownInput() throws IOException JavaDoc {
86     throw new UnsupportedOperationException JavaDoc(
87             "shutdownInput() not supported in ASServerSocketImpl");
88     }
89
90     public void shutdownOutput() throws IOException JavaDoc {
91     throw new UnsupportedOperationException JavaDoc(
92             "shutdownOutput() not supported in ASServerSocketImpl");
93     }
94
95     public boolean supportsUrgentData() {
96     throw new UnsupportedOperationException JavaDoc(
97             "supportsUrgentData() not supported in ASServerSocketImpl");
98     }
99
100     public void sendUrgentData(int i) throws IOException JavaDoc {
101     throw new UnsupportedOperationException JavaDoc(
102             "sendUrgentData() not supported in ASServerSocketImpl");
103     }
104
105     public void close() throws IOException JavaDoc {
106         if (ss != null && !ss.isClosed()) {
107             try {
108                 ServerSocketChannel JavaDoc channelToClose = ssc;
109                 ServerSocket JavaDoc socketToClose = ss;
110                 if (ssc instanceof ASServerSocketChannel) {
111                     ASServerSocketChannel assc =
112                     (ASServerSocketChannel) ssc;
113                     channelToClose = (ServerSocketChannel JavaDoc) assc.getActualChannel();
114                     socketToClose = ssc.socket();
115                 }
116                 ASSocketFacadeUtils.getASSocketService().close(portToBind,
117                                             socketToClose, channelToClose);
118             } catch (IOException JavaDoc e) {
119                 if ( logger.isLoggable(Level.FINE) ) {
120                     logger.log(Level.FINE, ""+ e.getMessage(),e);
121                 }
122             }
123         }
124     }
125
126     public void create(boolean stream) throws IOException JavaDoc {
127     // No-op: stream is always true when called from ServerSocket
128
}
129
130
131     public void bind(java.net.InetAddress JavaDoc host, int port) throws IOException JavaDoc {
132     hostToBind = host;
133     portToBind = port;
134
135     // actual binding happens in listen() below, because listen() is
136
// called after bind() by java.net.ServerSocket.java.
137
}
138
139     public void listen(int backlog) throws IOException JavaDoc {
140
141     // Check for services that are not lazily initialized
142
if (!ASSocketFacadeUtils.getASSocketService().exists(portToBind)) {
143         ssc = ServerSocketChannel.open();
144         ss = ssc.socket();
145         } else {
146             ssc = ASSocketFacadeUtils.getASSocketService().
147                                              getServerSocketChannel(portToBind);
148             ss = ASSocketFacadeUtils.getASSocketService().
149                                              getServerSocket(portToBind);
150     }
151
152         InetSocketAddress JavaDoc isa = new InetSocketAddress JavaDoc(hostToBind, portToBind);
153     ss.bind(isa, backlog);
154
155         localport = ss.getLocalPort();
156         address = ss.getInetAddress();
157     }
158
159     public void accept(java.net.SocketImpl JavaDoc si) throws IOException JavaDoc {
160         try {
161         Socket JavaDoc sock = ss.accept();
162         ((ASClientSocketImpl)si).setClientSocket(sock);
163         } catch (AsynchronousCloseException JavaDoc ase) {
164             SocketException JavaDoc se = new SocketException JavaDoc(ase.getMessage());
165             se.initCause(ase);
166             throw se;
167         }
168     }
169
170     public void setOption(int opt,java.lang.Object JavaDoc val ) throws SocketException JavaDoc{
171         if ( logger.isLoggable(Level.FINE) ) {
172              logger.log(java.util.logging.Level.FINE, "In ASServerSocketImpl.setOption, opt = "
173                         +opt+" val = "+val, new Exception JavaDoc());
174         }
175
176     //Consider only those options that are settable in a ServerSocket
177
switch (opt) {
178
179     case SO_TIMEOUT:
180         if (val == null || (!(val instanceof Integer JavaDoc)))
181         throw new SocketException JavaDoc("Bad parameter for SO_TIMEOUT");
182         int tmp = ((Integer JavaDoc) val).intValue();
183         if (tmp < 0)
184         throw new IllegalArgumentException JavaDoc("timeout < 0");
185         ss.setSoTimeout( tmp );
186         break;
187     
188     case SO_RCVBUF:
189         if (val == null || !(val instanceof Integer JavaDoc) ||
190         !(((Integer JavaDoc)val).intValue() > 0)) {
191         throw new SocketException JavaDoc("bad parameter for SO_SNDBUF " +
192                       "or SO_RCVBUF");
193         }
194         ss.setReceiveBufferSize( ((Integer JavaDoc) val).intValue() );
195         break;
196     
197     case SO_REUSEADDR:
198         if (val == null || !(val instanceof Boolean JavaDoc))
199             throw new SocketException JavaDoc("bad parameter for SO_REUSEADDR");
200             if (ss != null)
201             ss.setReuseAddress( ((Boolean JavaDoc)val).booleanValue() );
202         break;
203     default:
204         throw new SocketException JavaDoc("unrecognized TCP option: " + opt);
205     }
206     }
207
208     public Object JavaDoc getOption(int opt) throws SocketException JavaDoc {
209        switch (opt) {
210         case SO_TIMEOUT:
211             try {
212                 return new Integer JavaDoc(ss.getSoTimeout());
213             } catch( IOException JavaDoc ioe ) {
214                 throw new SocketException JavaDoc(ioe.getMessage()) ;
215             }
216         case SO_RCVBUF:
217             return new Integer JavaDoc(ss.getReceiveBufferSize());
218         case SO_REUSEADDR:
219             return new Boolean JavaDoc(ss.getReuseAddress());
220         default:
221             throw new SocketException JavaDoc("unrecognized TCP option: " + opt);
222         }
223
224     }
225 }
226
227
Popular Tags