1 23 package com.sun.enterprise.server.ss.provider; 24 25 import java.io.IOException ; 26 import java.net.InetSocketAddress ; 27 import java.net.ServerSocket ; 28 import java.net.Socket ; 29 import java.net.SocketException ; 30 import java.nio.channels.Selector ; 31 import java.nio.channels.ServerSocketChannel ; 32 import java.nio.channels.AsynchronousCloseException ; 33 import java.util.Hashtable ; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 36 37 import com.sun.enterprise.server.ss.spi.ASSocketFacadeUtils; 38 import com.sun.logging.LogDomains; 39 40 43 public class ASServerSocketImpl extends java.net.SocketImpl { 44 private static Logger logger = LogDomains.getLogger(LogDomains.CORE_LOGGER); 45 private java.net.InetAddress hostToBind; 46 private int portToBind; 47 48 private ServerSocketChannel ssc; 49 private ServerSocket ss; 50 private Selector selector; 51 private Hashtable options = new Hashtable (); 52 53 public int available() throws IOException { 54 throw new UnsupportedOperationException ( 55 "available() not supported in ASServerSocketImpl"); 56 } 57 58 public void connect(java.lang.String s,int i) throws IOException { 59 throw new UnsupportedOperationException ( 60 "connect() not supported in ASServerSocketImpl"); 61 } 62 63 public void connect(java.net.InetAddress ia,int i) 64 throws IOException { 65 throw new UnsupportedOperationException ( 66 "connect() not supported in ASServerSocketImpl"); 67 } 68 69 public void connect(java.net.SocketAddress sa,int i) 70 throws IOException { 71 throw new UnsupportedOperationException ( 72 "connect() not supported in ASServerSocketImpl"); 73 } 74 75 public java.io.InputStream getInputStream() throws IOException { 76 throw new UnsupportedOperationException ( 77 "getInputStream() not supported in ASServerSocketImpl"); 78 } 79 80 public java.io.OutputStream getOutputStream() throws IOException { 81 throw new UnsupportedOperationException ( 82 "getOutputStream() not supported in ASServerSocketImpl"); 83 } 84 85 public void shutdownInput() throws IOException { 86 throw new UnsupportedOperationException ( 87 "shutdownInput() not supported in ASServerSocketImpl"); 88 } 89 90 public void shutdownOutput() throws IOException { 91 throw new UnsupportedOperationException ( 92 "shutdownOutput() not supported in ASServerSocketImpl"); 93 } 94 95 public boolean supportsUrgentData() { 96 throw new UnsupportedOperationException ( 97 "supportsUrgentData() not supported in ASServerSocketImpl"); 98 } 99 100 public void sendUrgentData(int i) throws IOException { 101 throw new UnsupportedOperationException ( 102 "sendUrgentData() not supported in ASServerSocketImpl"); 103 } 104 105 public void close() throws IOException { 106 if (ss != null && !ss.isClosed()) { 107 try { 108 ServerSocketChannel channelToClose = ssc; 109 ServerSocket socketToClose = ss; 110 if (ssc instanceof ASServerSocketChannel) { 111 ASServerSocketChannel assc = 112 (ASServerSocketChannel) ssc; 113 channelToClose = (ServerSocketChannel ) assc.getActualChannel(); 114 socketToClose = ssc.socket(); 115 } 116 ASSocketFacadeUtils.getASSocketService().close(portToBind, 117 socketToClose, channelToClose); 118 } catch (IOException 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 { 127 } 129 130 131 public void bind(java.net.InetAddress host, int port) throws IOException { 132 hostToBind = host; 133 portToBind = port; 134 135 } 138 139 public void listen(int backlog) throws IOException { 140 141 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 isa = new InetSocketAddress (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 si) throws IOException { 160 try { 161 Socket sock = ss.accept(); 162 ((ASClientSocketImpl)si).setClientSocket(sock); 163 } catch (AsynchronousCloseException ase) { 164 SocketException se = new SocketException (ase.getMessage()); 165 se.initCause(ase); 166 throw se; 167 } 168 } 169 170 public void setOption(int opt,java.lang.Object val ) throws SocketException { 171 if ( logger.isLoggable(Level.FINE) ) { 172 logger.log(java.util.logging.Level.FINE, "In ASServerSocketImpl.setOption, opt = " 173 +opt+" val = "+val, new Exception ()); 174 } 175 176 switch (opt) { 178 179 case SO_TIMEOUT: 180 if (val == null || (!(val instanceof Integer ))) 181 throw new SocketException ("Bad parameter for SO_TIMEOUT"); 182 int tmp = ((Integer ) val).intValue(); 183 if (tmp < 0) 184 throw new IllegalArgumentException ("timeout < 0"); 185 ss.setSoTimeout( tmp ); 186 break; 187 188 case SO_RCVBUF: 189 if (val == null || !(val instanceof Integer ) || 190 !(((Integer )val).intValue() > 0)) { 191 throw new SocketException ("bad parameter for SO_SNDBUF " + 192 "or SO_RCVBUF"); 193 } 194 ss.setReceiveBufferSize( ((Integer ) val).intValue() ); 195 break; 196 197 case SO_REUSEADDR: 198 if (val == null || !(val instanceof Boolean )) 199 throw new SocketException ("bad parameter for SO_REUSEADDR"); 200 if (ss != null) 201 ss.setReuseAddress( ((Boolean )val).booleanValue() ); 202 break; 203 default: 204 throw new SocketException ("unrecognized TCP option: " + opt); 205 } 206 } 207 208 public Object getOption(int opt) throws SocketException { 209 switch (opt) { 210 case SO_TIMEOUT: 211 try { 212 return new Integer (ss.getSoTimeout()); 213 } catch( IOException ioe ) { 214 throw new SocketException (ioe.getMessage()) ; 215 } 216 case SO_RCVBUF: 217 return new Integer (ss.getReceiveBufferSize()); 218 case SO_REUSEADDR: 219 return new Boolean (ss.getReuseAddress()); 220 default: 221 throw new SocketException ("unrecognized TCP option: " + opt); 222 } 223 224 } 225 } 226 227 | Popular Tags |