1 23 package com.sun.enterprise.server.ss.provider; 24 25 import java.nio.channels.spi.*; 26 import java.nio.channels.*; 27 import java.io.*; 28 import java.net.*; 29 import java.util.*; 30 31 38 public class ASSelectorProvider extends SelectorProvider { 39 40 public static final int PORT_UNBOUND = 0; 41 public static final int PORT_BOUND = 1; 42 public static final int PORT_CONFLICT = 2; 43 44 private SelectorProvider provider = null; 45 46 private HashMap<Integer , Integer > statemap = 47 new HashMap<Integer , Integer >(); 48 private HashMap<Integer , ServerSocket> socketmap = 49 new HashMap<Integer , ServerSocket>(); 50 51 public ASSelectorProvider() { 52 provider = getProvider(); 53 } 54 55 public DatagramChannel openDatagramChannel() throws IOException { 56 return provider.openDatagramChannel(); 57 } 58 59 public Pipe openPipe() throws IOException { 60 return provider.openPipe(); 61 } 62 63 public ServerSocketChannel openServerSocketChannel() throws IOException { 64 ServerSocketChannel ssc = provider.openServerSocketChannel(); 65 return new ASServerSocketChannel(ssc, this); 66 } 67 68 public SocketChannel openSocketChannel() throws IOException { 69 return provider.openSocketChannel(); 72 } 73 74 public AbstractSelector openSelector() throws IOException { 75 AbstractSelector sel = provider.openSelector(); 76 return new ASSelector(sel, this); 77 } 78 79 80 ServerSocket getServerSocket(int port) { 81 return socketmap.get(port); 82 } 83 84 int getPortState(int port) { 85 Integer i = statemap.get(port); 86 if (i == null) { 87 return PORT_UNBOUND; 88 } else { 89 return i.intValue(); 90 } 91 } 92 93 public void setPortState(int port, int state) { 94 statemap.put(port, state); 95 } 96 97 public void clear(int port) { 98 statemap.remove(port); 99 socketmap.remove(port); 100 } 101 102 103 void setServerSocket(ServerSocket ss, int port) { 104 int state = getPortState(port); 105 106 112 113 if (state == PORT_UNBOUND) { 114 socketmap.put(port, ss); 115 setPortState(port, PORT_BOUND); 116 } 117 else if (state == PORT_BOUND) { 118 setPortState(port, PORT_CONFLICT); 119 } 120 } 121 122 private SelectorProvider getProvider() { 123 SelectorProvider sp = null; 124 try { 125 Class clazz = Class.forName( "sun.nio.ch.DefaultSelectorProvider" ); 126 java.lang.reflect.Method createMeth = clazz.getMethod("create", new Class [] {}); 127 sp = (SelectorProvider) createMeth.invoke( null, new Object [] {} ); 128 } catch( Exception e ) { 129 throw new RuntimeException ("Unable to create default SelectorProvider.", e); 130 } 131 132 return sp; 133 } 134 135 136 } 137 138 | Popular Tags |