1 7 8 package java.nio.channels.spi; 9 10 import java.io.FileDescriptor ; 11 import java.io.IOException ; 12 import java.net.ServerSocket ; 13 import java.net.Socket ; 14 import java.nio.channels.*; 15 import java.security.AccessController ; 16 import java.security.PrivilegedAction ; 17 import java.util.Iterator ; 18 import sun.misc.Service; 19 import sun.misc.ServiceConfigurationError; 20 import sun.security.action.GetPropertyAction; 21 22 23 53 54 public abstract class SelectorProvider { 55 56 private static final Object lock = new Object (); 57 private static SelectorProvider provider = null; 58 59 66 protected SelectorProvider() { 67 SecurityManager sm = System.getSecurityManager(); 68 if (sm != null) 69 sm.checkPermission(new RuntimePermission ("selectorProvider")); 70 } 71 72 private static boolean loadProviderFromProperty() { 73 String cn = System.getProperty("java.nio.channels.spi.SelectorProvider"); 74 if (cn == null) 75 return false; 76 try { 77 Class c = Class.forName(cn, true, 78 ClassLoader.getSystemClassLoader()); 79 provider = (SelectorProvider )c.newInstance(); 80 return true; 81 } catch (ClassNotFoundException x) { 82 throw new ServiceConfigurationError(x); 83 } catch (IllegalAccessException x) { 84 throw new ServiceConfigurationError(x); 85 } catch (InstantiationException x) { 86 throw new ServiceConfigurationError(x); 87 } catch (SecurityException x) { 88 throw new ServiceConfigurationError(x); 89 } 90 } 91 92 private static boolean loadProviderAsService() { 93 Iterator i = Service.providers(SelectorProvider .class, 94 ClassLoader.getSystemClassLoader()); 95 for (;;) { 96 try { 97 if (!i.hasNext()) 98 return false; 99 provider = (SelectorProvider )i.next(); 100 return true; 101 } catch (ServiceConfigurationError sce) { 102 if (sce.getCause() instanceof SecurityException ) { 103 continue; 105 } 106 throw sce; 107 } 108 } 109 } 110 111 146 public static SelectorProvider provider() { 147 synchronized (lock) { 148 if (provider != null) 149 return provider; 150 return (SelectorProvider )AccessController 151 .doPrivileged(new PrivilegedAction () { 152 public Object run() { 153 if (loadProviderFromProperty()) 154 return provider; 155 if (loadProviderAsService()) 156 return provider; 157 provider = sun.nio.ch.DefaultSelectorProvider.create(); 158 return provider; 159 } 160 }); 161 } 162 } 163 164 169 public abstract DatagramChannel openDatagramChannel() 170 throws IOException ; 171 172 177 public abstract Pipe openPipe() 178 throws IOException ; 179 180 185 public abstract AbstractSelector openSelector() 186 throws IOException ; 187 188 193 public abstract ServerSocketChannel openServerSocketChannel() 194 throws IOException ; 195 196 201 public abstract SocketChannel openSocketChannel() 202 throws IOException ; 203 204 262 public Channel inheritedChannel() throws IOException { 263 return null; 264 } 265 266 } 267 | Popular Tags |