1. /*
  2. * @(#)ReaderThread.java 1.11 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * Licensed Materials - Property of IBM
  9. * RMI-IIOP v1.0
  10. * Copyright IBM Corp. 1998 1999 All Rights Reserved
  11. *
  12. * US Government Users Restricted Rights - Use, duplication or
  13. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  14. */
  15. package com.sun.corba.se.internal.iiop;
  16. import java.io.IOException;
  17. import com.sun.corba.se.internal.orbutil.ORBUtility;
  18. import com.sun.corba.se.internal.core.GIOPVersion;
  19. /**
  20. * A thread class to handle reading incoming messages off the wire
  21. */
  22. final class ReaderThread extends Thread
  23. {
  24. private boolean keepRunning = true;
  25. private boolean debug ;
  26. private void dprint( String msg )
  27. {
  28. ORBUtility.dprint( this, msg ) ;
  29. }
  30. private IIOPConnection c;
  31. public ReaderThread(ThreadGroup g, IIOPConnection c, String name, boolean debug )
  32. {
  33. super(g, name);
  34. this.c = c;
  35. this.debug = debug ;
  36. }
  37. public ReaderThread(IIOPConnection c, String name, boolean debug )
  38. {
  39. super(name);
  40. this.c = c;
  41. this.debug = debug ;
  42. }
  43. public IIOPConnection getCurrentConnection(){
  44. return c;
  45. }
  46. /**
  47. */
  48. public void run() {
  49. while (keepRunning) {
  50. try {
  51. c.processInput();
  52. } catch (IOException ex) {
  53. if (debug) {
  54. dprint( "IOException in createInputStream: " + ex ) ;
  55. ex.printStackTrace() ;
  56. }
  57. // Close the connection, inform all threads
  58. // and stop this thread.
  59. c.purge_calls(Connection.CONN_ABORT, true, false);
  60. keepRunning = false;
  61. /*
  62. // This doesn't seem to be thrown. A thread.stop is called (which should
  63. // be changed.
  64. } catch (IIOPConnection.DeleteConn dc) {
  65. if (debug) {
  66. dprint( "DeleteConn thrown while reading request: " + dc ) ;
  67. dc.printStackTrace() ;
  68. }
  69. // Close the connection, inform all threads
  70. // and stop this thread.
  71. c.purge_calls (dc.minorCode, true, false);
  72. */
  73. } catch (ThreadDeath td) {
  74. if (debug) {
  75. dprint( "ThreadDeath thrown while reading request: " + td ) ;
  76. td.printStackTrace() ;
  77. }
  78. try {
  79. // Close the connection and inform all threads.
  80. c.purge_calls(Connection.CONN_ABORT, false, false);
  81. } finally {
  82. throw td;
  83. }
  84. } catch (Throwable ex) {
  85. if (debug) {
  86. dprint( "Exception thrown while reading request: " + ex ) ;
  87. ex.printStackTrace() ;
  88. }
  89. try {
  90. if (ex instanceof org.omg.CORBA.INTERNAL) {
  91. c.sendMessageError(GIOPVersion.DEFAULT_VERSION);
  92. }
  93. } catch (IOException e) {}
  94. // Close the connection and inform all threads. The
  95. // ReaderThread exits. If we don't close the connection,
  96. // clients will hang forever.
  97. c.purge_calls(Connection.CONN_ABORT, false, false);
  98. keepRunning = false;
  99. return;
  100. }
  101. }
  102. }
  103. /**
  104. * reader.shutdown() does the following
  105. * 1. Sets keepRunning to false, which will terminate the thread
  106. * 2. Closes the connection.InputStream() which may result in IOException
  107. * if thread is running, which would ultimately result in purging the
  108. * connection.
  109. * 3. Attempts to start the thread. This may have any of the following
  110. * effects
  111. * 3-1: IllegalThreadStateException if the thread is already running,
  112. * if this happens we simply neglect the exception.
  113. * 3-2: Just starts and stops the thread because of keepRunning == false
  114. * condition. This helps in garbage collection if the thread was
  115. * instantiated and was never started.
  116. */
  117. synchronized void shutdown() {
  118. keepRunning = false;
  119. try {
  120. java.io.InputStream inStream = c.getInputStream();
  121. inStream.close();
  122. } catch( Exception e ) {
  123. // We neglect exception, because it is just an attempt to
  124. // unblock the c.processInput() call.
  125. }
  126. // This extra step is neccessary to make sure that the Thread will
  127. // be deleted from the Threadgroup if it is not previously started.
  128. // There will be a chance of Memory leak if the Thread object is
  129. // instantantiated, but never started.
  130. try {
  131. this.start( );
  132. } catch( IllegalThreadStateException e ) {
  133. // Just neglect the exception, this exception was raised because
  134. // the thread was already started. If it was already started,
  135. // thread will be shutdown because 'keepRunning' is set to false.
  136. }
  137. }
  138. }