1. /*
  2. * @(#)EventHandlerBase.java 1.9 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.impl.transport;
  8. import java.nio.channels.SelectionKey;
  9. import org.omg.CORBA.INTERNAL;
  10. import com.sun.corba.se.pept.transport.Acceptor;
  11. import com.sun.corba.se.pept.transport.Connection;
  12. import com.sun.corba.se.pept.transport.EventHandler;
  13. import com.sun.corba.se.spi.orb.ORB;
  14. import com.sun.corba.se.spi.orbutil.threadpool.NoSuchThreadPoolException;
  15. import com.sun.corba.se.spi.orbutil.threadpool.NoSuchWorkQueueException;
  16. import com.sun.corba.se.spi.orbutil.threadpool.Work;
  17. import com.sun.corba.se.impl.orbutil.ORBUtility;
  18. public abstract class EventHandlerBase
  19. implements
  20. EventHandler
  21. {
  22. protected ORB orb;
  23. protected Work work;
  24. protected boolean useWorkerThreadForEvent;
  25. protected boolean useSelectThreadToWait;
  26. protected SelectionKey selectionKey;
  27. ////////////////////////////////////////////////////
  28. //
  29. // EventHandler methods
  30. //
  31. public void setUseSelectThreadToWait(boolean x)
  32. {
  33. useSelectThreadToWait = x;
  34. }
  35. public boolean shouldUseSelectThreadToWait()
  36. {
  37. return useSelectThreadToWait;
  38. }
  39. public void setSelectionKey(SelectionKey selectionKey)
  40. {
  41. this.selectionKey = selectionKey;
  42. }
  43. public SelectionKey getSelectionKey()
  44. {
  45. return selectionKey;
  46. }
  47. /*
  48. * NOTE:
  49. * This is not thread-safe by design.
  50. * Only one thread should call it - a reader/listener/select thread.
  51. * Not stateless: interest ops, registration.
  52. */
  53. public void handleEvent()
  54. {
  55. if (orb.transportDebugFlag) {
  56. dprint(".handleEvent->: " + this);
  57. }
  58. getSelectionKey().interestOps(getSelectionKey().interestOps() &
  59. (~ getInterestOps()));
  60. if (shouldUseWorkerThreadForEvent()) {
  61. Throwable throwable = null;
  62. try {
  63. if (orb.transportDebugFlag) {
  64. dprint(".handleEvent: addWork to pool: " + 0);
  65. }
  66. orb.getThreadPoolManager().getThreadPool(0)
  67. .getWorkQueue(0).addWork(getWork());
  68. } catch (NoSuchThreadPoolException e) {
  69. throwable = e;
  70. } catch (NoSuchWorkQueueException e) {
  71. throwable = e;
  72. }
  73. // REVISIT: need to close connection.
  74. if (throwable != null) {
  75. if (orb.transportDebugFlag) {
  76. dprint(".handleEvent: " + throwable);
  77. }
  78. INTERNAL i = new INTERNAL("NoSuchThreadPoolException");
  79. i.initCause(throwable);
  80. throw i;
  81. }
  82. } else {
  83. if (orb.transportDebugFlag) {
  84. dprint(".handleEvent: doWork");
  85. }
  86. getWork().doWork();
  87. }
  88. if (orb.transportDebugFlag) {
  89. dprint(".handleEvent<-: " + this);
  90. }
  91. }
  92. public boolean shouldUseWorkerThreadForEvent()
  93. {
  94. return useWorkerThreadForEvent;
  95. }
  96. public void setUseWorkerThreadForEvent(boolean x)
  97. {
  98. useWorkerThreadForEvent = x;
  99. }
  100. public void setWork(Work work)
  101. {
  102. this.work = work;
  103. }
  104. public Work getWork()
  105. {
  106. return work;
  107. }
  108. private void dprint(String msg)
  109. {
  110. ORBUtility.dprint("EventHandlerBase", msg);
  111. }
  112. }
  113. // End of file.