1 23 package com.sun.enterprise.server.ss.provider; 24 25 import java.io.IOException ; 26 import java.nio.channels.ClosedChannelException ; 27 import java.nio.channels.SelectableChannel ; 28 import java.nio.channels.SelectionKey ; 29 import java.nio.channels.Selector ; 30 import java.nio.channels.ServerSocketChannel ; 31 import java.nio.channels.spi.AbstractSelectableChannel ; 32 import java.nio.channels.spi.AbstractSelector ; 33 import java.nio.channels.spi.SelectorProvider ; 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.enterprise.server.ss.util.ASSet; 39 import com.sun.enterprise.server.ss.util.ASWrapperCreator; 40 import com.sun.logging.LogDomains; 41 42 49 class ASSelector extends AbstractSelector implements ASWrapperCreator{ 50 private static Logger logger = LogDomains.getLogger(LogDomains.CORE_LOGGER); 51 private AbstractSelector sel = null; 52 53 private boolean wakenup = false; 54 private boolean wrapSelectionKeys = false; 55 private int port = 0; 56 57 ASSelector(AbstractSelector sel, SelectorProvider provider) { 58 super(provider); 59 this.sel = sel; 60 } 61 62 protected void implCloseSelector() throws IOException { 63 if ( logger.isLoggable(Level.FINER) ) { 64 logger.log(Level.FINER, "Selector is getting closed :" + sel); 65 } 66 sel.close(); 67 } 68 69 protected SelectionKey register(AbstractSelectableChannel ac, 70 int ops, Object obj) { 71 try { 72 if (ac instanceof ASChannel) { 73 ASChannel asc = (ASChannel) ac; 74 SelectableChannel sc = asc.getActualChannel(); 75 76 if (ac instanceof ASServerSocketChannel && 77 ops == SelectionKey.OP_ACCEPT) { 78 this.port = ((ASServerSocketChannel)ac).getPortNumber(); 79 wrapSelectionKeys = true; 80 } 81 return sc.register(sel, ops, obj); 82 } else { 83 if ( logger.isLoggable(Level.FINEST) ) { 84 logger.log(java.util.logging.Level.FINEST, 85 "In ASSelector.register channel class = " 86 + ac.getClass().getName(), new Exception ()); 87 } 88 return ac.register(sel, ops, obj); 89 } 90 } catch (ClosedChannelException ce) { 91 throw new RuntimeException (ce); 92 } 93 } 94 95 public java.util.Set keys() { 96 if (wrapSelectionKeys && 97 ASSocketFacadeUtils.getASSocketService().isServerStartingUp(port)) { 98 return new ASSet(sel.keys(), this); 99 } else { 100 return sel.keys(); 101 } 102 } 103 104 public java.util.Set selectedKeys() { 105 if (wrapSelectionKeys && 106 ASSocketFacadeUtils.getASSocketService().isServerStartingUp(port)) { 107 return new ASSet(sel.selectedKeys(), this); 108 } else { 109 return sel.selectedKeys(); 110 } 111 112 } 113 114 public Selector wakeup() { 115 wakenup = true; 116 return sel.wakeup(); 117 } 118 119 public int select(long l) throws IOException { 120 return sel.select(l); 121 } 122 123 public int selectNow() throws IOException { 124 return sel.selectNow(); 125 } 126 127 public int select() throws IOException { 128 return sel.select(); 129 } 130 131 boolean wakenUp() { 132 boolean result = wakenup; 133 wakenup = false; 134 return result; 135 } 136 137 public Object wrapIfNecessary(Object next) { 138 SelectionKey selKey = (SelectionKey ) next; 139 if ((selKey.interestOps() & SelectionKey.OP_ACCEPT) != 0) { 140 ASServerSocketChannel channel = new ASServerSocketChannel( 141 (ServerSocketChannel ) selKey.channel(), this.sel.provider()); 142 SelectionKey key = new ASSelectionKey(channel, selKey, this); 143 key.attach(selKey.attachment()); 144 return key; 145 } else { 146 return selKey; 147 } 148 } 149 150 } 151 | Popular Tags |