1. /*
  2. * @(#)file ClientHandler.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 1.23
  5. * @(#)lastedit 03/12/19
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. package com.sun.jmx.snmp.daemon;
  12. // java import
  13. //
  14. import java.io.*;
  15. // jmx import
  16. //
  17. import javax.management.MBeanServer;
  18. import javax.management.ObjectName;
  19. // jmx RI import
  20. //
  21. import com.sun.jmx.trace.Trace;
  22. /**
  23. * The <CODE>ClientHandler</CODE> class is the base class of each
  24. * adaptor.<p>
  25. */
  26. abstract class ClientHandler implements Runnable {
  27. public ClientHandler(CommunicatorServer server, int id, MBeanServer f, ObjectName n) {
  28. adaptorServer = server ;
  29. requestId = id ;
  30. mbs = f ;
  31. objectName = n ;
  32. interruptCalled = false ;
  33. dbgTag = makeDebugTag() ;
  34. //if (mbs == null ){
  35. //thread = new Thread (this) ;
  36. thread = createThread(this);
  37. //} else {
  38. //thread = mbs.getThreadAllocatorSrvIf().obtainThread(objectName,this) ;
  39. //}
  40. // Note: the thread will be started by the subclass.
  41. }
  42. // thread service
  43. Thread createThread(Runnable r) {
  44. return new Thread(this);
  45. }
  46. public void interrupt() {
  47. if (isTraceOn()) {
  48. trace("interrupt","start") ;
  49. }
  50. interruptCalled = true ;
  51. if (thread != null) {
  52. thread.interrupt() ;
  53. }
  54. if (isTraceOn()) {
  55. trace("interrupt","end") ;
  56. }
  57. }
  58. public void join() {
  59. if (thread != null) {
  60. try {
  61. thread.join() ;
  62. }
  63. catch(InterruptedException x) {
  64. }
  65. }
  66. }
  67. public void run() {
  68. try {
  69. //
  70. // Notify the server we are now active
  71. //
  72. adaptorServer.notifyClientHandlerCreated(this) ;
  73. //
  74. // Call protocol specific sequence
  75. //
  76. doRun() ;
  77. }
  78. finally {
  79. //
  80. // Now notify the adaptor server that the handler is terminating.
  81. // This is important because the server may be blocked waiting for
  82. // a handler to terminate.
  83. //
  84. adaptorServer.notifyClientHandlerDeleted(this) ;
  85. }
  86. }
  87. //
  88. // The protocol-dependent part of the request
  89. //
  90. public abstract void doRun() ;
  91. protected CommunicatorServer adaptorServer = null ;
  92. protected int requestId = -1 ;
  93. protected MBeanServer mbs = null ;
  94. protected ObjectName objectName = null ;
  95. protected Thread thread = null ;
  96. protected boolean interruptCalled = false ;
  97. protected String dbgTag = null ;
  98. protected boolean isTraceOn() {
  99. return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP);
  100. }
  101. protected void trace(String clz, String func, String info) {
  102. Trace.send(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP, clz, func, info);
  103. }
  104. protected boolean isDebugOn() {
  105. return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP);
  106. }
  107. protected void debug(String clz, String func, String info) {
  108. Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, info);
  109. }
  110. protected void trace(String func, String info) {
  111. trace(dbgTag, func, info);
  112. }
  113. protected void debug(String func, String info) {
  114. debug(dbgTag, func, info);
  115. }
  116. protected String makeDebugTag() {
  117. return "ClientHandler[" + adaptorServer.getProtocol() + ":" + adaptorServer.getPort() + "][" + requestId + "]";
  118. }
  119. }