KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > daemon > ClientHandler


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
12
13 package com.sun.jmx.snmp.daemon;
14
15
16
17 // java import
18
//
19
import java.io.*;
20
21 // jmx import
22
//
23
import javax.management.MBeanServer JavaDoc;
24 import javax.management.ObjectName JavaDoc;
25
26 // jmx RI import
27
//
28
import com.sun.jmx.trace.Trace;
29
30 /**
31  * The <CODE>ClientHandler</CODE> class is the base class of each
32  * adaptor.<p>
33  */

34
35 abstract class ClientHandler implements Runnable JavaDoc {
36
37     public ClientHandler(CommunicatorServer server, int id, MBeanServer JavaDoc f, ObjectName JavaDoc n) {
38         adaptorServer = server ;
39         requestId = id ;
40         mbs = f ;
41         objectName = n ;
42         interruptCalled = false ;
43         dbgTag = makeDebugTag() ;
44     //if (mbs == null ){
45
//thread = new Thread (this) ;
46
thread = createThread(this);
47
48     //} else {
49
//thread = mbs.getThreadAllocatorSrvIf().obtainThread(objectName,this) ;
50
//}
51
// Note: the thread will be started by the subclass.
52
}
53
54     // thread service
55
Thread JavaDoc createThread(Runnable JavaDoc r) {
56     return new Thread JavaDoc(this);
57     }
58
59     public void interrupt() {
60         if (isTraceOn()) {
61             trace("interrupt","start") ;
62         }
63         interruptCalled = true ;
64     if (thread != null) {
65             thread.interrupt() ;
66     }
67         if (isTraceOn()) {
68             trace("interrupt","end") ;
69         }
70     }
71   
72   
73     public void join() {
74     if (thread != null) {
75         try {
76             thread.join() ;
77         }
78         catch(InterruptedException JavaDoc x) {
79         }
80     }
81     }
82   
83     public void run() {
84
85         try {
86             //
87
// Notify the server we are now active
88
//
89
adaptorServer.notifyClientHandlerCreated(this) ;
90
91             //
92
// Call protocol specific sequence
93
//
94
doRun() ;
95         }
96         finally {
97             //
98
// Now notify the adaptor server that the handler is terminating.
99
// This is important because the server may be blocked waiting for
100
// a handler to terminate.
101
//
102
adaptorServer.notifyClientHandlerDeleted(this) ;
103         }
104     }
105   
106     //
107
// The protocol-dependent part of the request
108
//
109
public abstract void doRun() ;
110   
111     protected CommunicatorServer adaptorServer = null ;
112     protected int requestId = -1 ;
113     protected MBeanServer JavaDoc mbs = null ;
114     protected ObjectName JavaDoc objectName = null ;
115     protected Thread JavaDoc thread = null ;
116     protected boolean interruptCalled = false ;
117     protected String JavaDoc dbgTag = null ;
118
119     protected boolean isTraceOn() {
120         return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP);
121     }
122
123     protected void trace(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
124         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_ADAPTOR_SNMP, clz, func, info);
125     }
126
127     protected boolean isDebugOn() {
128         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP);
129     }
130
131     protected void debug(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
132         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, clz, func, info);
133     }
134
135     protected void trace(String JavaDoc func, String JavaDoc info) {
136         trace(dbgTag, func, info);
137     }
138
139     protected void debug(String JavaDoc func, String JavaDoc info) {
140         debug(dbgTag, func, info);
141     }
142
143     protected String JavaDoc makeDebugTag() {
144         return "ClientHandler[" + adaptorServer.getProtocol() + ":" + adaptorServer.getPort() + "][" + requestId + "]";
145     }
146 }
147
Popular Tags