9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.corba.se.impl.transport; 27 28 import java.io.IOException; 29 import java.nio.channels.ClosedChannelException; 30 import java.nio.channels.SelectableChannel; 31 import java.nio.channels.SelectionKey; 32 import java.nio.channels.Selector; 33 import java.util.ArrayList; 34 import java.util.HashMap; 35 import java.util.Map; 36 import java.util.Iterator; 37 import java.util.List; 38 39 import com.sun.corba.se.pept.broker.Broker; 40 import com.sun.corba.se.pept.transport.Acceptor; 41 import com.sun.corba.se.pept.transport.Connection; 42 import com.sun.corba.se.pept.transport.EventHandler; 43 import com.sun.corba.se.pept.transport.ListenerThread; 44 import com.sun.corba.se.pept.transport.ReaderThread; 45 46 import com.sun.corba.se.spi.logging.CORBALogDomains; 47 import com.sun.corba.se.spi.orb.ORB; 48 import com.sun.corba.se.spi.orbutil.threadpool.Work; 49 import com.sun.corba.se.spi.orbutil.threadpool.NoSuchThreadPoolException; 50 import com.sun.corba.se.spi.orbutil.threadpool.NoSuchWorkQueueException; 51 52 import com.sun.corba.se.impl.logging.ORBUtilSystemException; 53 import com.sun.corba.se.impl.orbutil.ORBUtility; 54 55 /** 56 * @author Harold Carr 57 */ 58 class SelectorImpl 222 setClosed(true); 223 224 Iterator i; 225 226 // Kill listeners. 227 228 i = listenerThreads.values().iterator(); 229 while (i.hasNext()) { 230 ListenerThread listenerThread = (ListenerThread) i.next(); 231 listenerThread.close(); 232 } 233 234 // Kill readers. 235 236 i = readerThreads.values().iterator(); 237 while (i.hasNext()) { 238 ReaderThread readerThread = (ReaderThread) i.next(); 239 readerThread.close(); 240 } 241 242 // Selector 243 244 try { 245 if (selector != null) { 246 // wakeup Selector thread to process close request 247 selector.wakeup(); 248 } 249 } catch (Throwable t) { 250 if (orb.transportDebugFlag) { 251 dprint(".close: selector.close: " + t); 252 } 253 } 254 } 255 256 /////////////////////////////////////////////////// 257 // 258 // Thread methods. 259 // 260 261 public void run() 311 } 312 } 313 if (timeout == 0 && orb.transportDebugFlag) { 314 dprint(".run: End of selection cycle"); 315 } 316 } catch (Throwable t) { 317 // IMPORTANT: ignore all errors so the select thread keeps running. 318 // Otherwise a guaranteed hang. 319 if (orb.transportDebugFlag) { 320 dprint(".run: ignoring", t); 321 } 322 } 323 } 324 } 325 326 ///////////////////////////////////////////////////// 327 // 328 // Implementation. 329 // 330 331 private synchronized boolean isClosed () 332 { 333 return closed; 334 } 335 336 private synchronized void setClosed(boolean closed) 337 { 338 this.closed = closed; 339 } 340 341 private void startSelector() 342 { 343 try { 344 selector = Selector.open(); 345 } catch (IOException e) { 346 if (orb.transportDebugFlag) { 347 dprint(".startSelector: Selector.open: IOException: " + e); 348 } 349 // REVISIT - better handling/reporting 350 RuntimeException rte = | 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.corba.se.impl.transport; 27 28 import java.io.IOException; 29 import java.net.ServerSocket; 30 import java.nio.channels.ClosedChannelException; 31 import java.nio.channels.SelectableChannel; 32 import java.nio.channels.ServerSocketChannel; 33 import java.nio.channels.SelectionKey; 34 import java.nio.channels.Selector; 35 import java.util.ArrayList; 36 import java.util.HashMap; 37 import java.util.Map; 38 import java.util.Iterator; 39 import java.util.List; 40 41 42 import com.sun.corba.se.pept.broker.Broker; 43 import com.sun.corba.se.pept.transport.Acceptor; 44 import com.sun.corba.se.pept.transport.Connection; 45 import com.sun.corba.se.pept.transport.EventHandler; 46 import com.sun.corba.se.pept.transport.ListenerThread; 47 import com.sun.corba.se.pept.transport.ReaderThread; 48 49 import com.sun.corba.se.spi.logging.CORBALogDomains; 50 import com.sun.corba.se.spi.orb.ORB; 51 import com.sun.corba.se.spi.orbutil.threadpool.Work; 52 import com.sun.corba.se.spi.orbutil.threadpool.NoSuchThreadPoolException; 53 import com.sun.corba.se.spi.orbutil.threadpool.NoSuchWorkQueueException; 54 55 import com.sun.corba.se.impl.logging.ORBUtilSystemException; 56 import com.sun.corba.se.impl.orbutil.ORBUtility; 57 58 /** 59 * @author Harold Carr 60 */ 61 class SelectorImpl 225 setClosed(true); 226 227 Iterator i; 228 229 // Kill listeners. 230 231 i = listenerThreads.values().iterator(); 232 while (i.hasNext()) { 233 ListenerThread listenerThread = (ListenerThread) i.next(); 234 listenerThread.close(); 235 } 236 237 // Kill readers. 238 239 i = readerThreads.values().iterator(); 240 while (i.hasNext()) { 241 ReaderThread readerThread = (ReaderThread) i.next(); 242 readerThread.close(); 243 } 244 245 clearDeferredRegistrations(); 246 247 // Selector 248 249 try { 250 if (selector != null) { 251 // wakeup Selector thread to process close request 252 selector.wakeup(); 253 } 254 } catch (Throwable t) { 255 if (orb.transportDebugFlag) { 256 dprint(".close: selector.close: " + t); 257 } 258 } 259 } 260 261 /////////////////////////////////////////////////// 262 // 263 // Thread methods. 264 // 265 266 public void run() 316 } 317 } 318 if (timeout == 0 && orb.transportDebugFlag) { 319 dprint(".run: End of selection cycle"); 320 } 321 } catch (Throwable t) { 322 // IMPORTANT: ignore all errors so the select thread keeps running. 323 // Otherwise a guaranteed hang. 324 if (orb.transportDebugFlag) { 325 dprint(".run: ignoring", t); 326 } 327 } 328 } 329 } 330 331 ///////////////////////////////////////////////////// 332 // 333 // Implementation. 334 // 335 336 private void clearDeferredRegistrations() { 337 synchronized (deferredRegistrations) { 338 int deferredListSize = deferredRegistrations.size(); 339 if (orb.transportDebugFlag) { 340 dprint(".clearDeferredRegistrations:deferred list size == " + deferredListSize); 341 } 342 for (int i = 0; i < deferredListSize; i++) { 343 EventHandler eventHandler = 344 (EventHandler)deferredRegistrations.get(i); 345 if (orb.transportDebugFlag) { 346 dprint(".clearDeferredRegistrations: " + eventHandler); 347 } 348 SelectableChannel channel = eventHandler.getChannel(); 349 SelectionKey selectionKey = null; 350 351 try { 352 if (orb.transportDebugFlag) { 353 dprint(".clearDeferredRegistrations:close channel == " 354 + channel); 355 dprint(".clearDeferredRegistrations:close channel class == " 356 + channel.getClass().getName()); 357 } 358 channel.close(); 359 if (channel instanceof ServerSocketChannel) { 360 dprint(".clearDeferredRegistrations:close ServerSocketCHannel == " 361 + channel); 362 ServerSocket ss = ((ServerSocketChannel) channel) 363 .socket(); 364 ss.close(); 365 selectionKey = eventHandler.getSelectionKey(); 366 if (selectionKey != null) { 367 selectionKey.cancel(); 368 selectionKey.attach(null); 369 } 370 } 371 } catch (IOException ioEx) { 372 if (orb.transportDebugFlag) { 373 dprint(".handleDeferredRegistrations: " + ioEx); 374 } 375 } 376 } 377 deferredRegistrations.clear(); 378 } 379 } 380 381 private synchronized boolean isClosed () 382 { 383 return closed; 384 } 385 386 private synchronized void setClosed(boolean closed) 387 { 388 this.closed = closed; 389 } 390 391 private void startSelector() 392 { 393 try { 394 selector = Selector.open(); 395 } catch (IOException e) { 396 if (orb.transportDebugFlag) { 397 dprint(".startSelector: Selector.open: IOException: " + e); 398 } 399 // REVISIT - better handling/reporting 400 RuntimeException rte = |