J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | package com.sun.jmx.snmp.daemon; |
| 28 | |
| 29 | |
| 30 | |
| 31 | // java import |
| 32 | // |
| 33 | import java.io.*; |
| 34 | import java.util.logging.Level; |
| 35 | |
| 36 | // jmx import |
| 37 | // |
| 38 | import javax.management.MBeanServer; |
| 39 | import javax.management.ObjectName; |
| 40 | |
| 41 | // jmx RI import |
| 42 | // |
| 43 | import static com.sun.jmx.defaults.JmxProperties.SNMP_ADAPTOR_LOGGER; |
| 44 | |
| 45 | /** |
| 46 | * The <CODE>ClientHandler</CODE> class is the base class of each |
| 47 | * adaptor.<p> |
| 48 | */ |
| 49 | |
| 50 | abstract class ClientHandler implements Runnable { |
| 51 | |
| 52 | public ClientHandler(CommunicatorServer server, int id, MBeanServer f, ObjectName n) { |
| 53 | adaptorServer = server ; |
| 54 | requestId = id ; |
| 55 | mbs = f ; |
| 56 | objectName = n ; |
| 57 | interruptCalled = false ; |
| 58 | dbgTag = makeDebugTag() ; |
| 59 | //if (mbs == null ){ |
| 60 | //thread = new Thread (this) ; |
| 61 | thread = createThread(this); |
| 62 | |
| 63 | //} else { |
| 64 | //thread = mbs.getThreadAllocatorSrvIf().obtainThread(objectName,this) ; |
| 65 | //} |
| 66 | // Note: the thread will be started by the subclass. |
| 67 | } |
| 68 | |
| 69 | // thread service |
| 70 | Thread createThread(Runnable r) { |
| 71 | return new Thread(this); |
| 72 | } |
| 73 | |
| 74 | public void interrupt() { |
| 75 | SNMP_ADAPTOR_LOGGER.entering(dbgTag, "interrupt"); |
| 76 | interruptCalled = true ; |
| 77 | if (thread != null) { |
| 78 | thread.interrupt() ; |
| 79 | } |
| 80 | SNMP_ADAPTOR_LOGGER.exiting(dbgTag, "interrupt"); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | public void join() { |
| 85 | if (thread != null) { |
| 86 | try { |
| 87 | thread.join() ; |
| 88 | } |
| 89 | catch(InterruptedException x) { |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public void run() { |
| 95 | |
| 96 | try { |
| 97 | // |
| 98 | // Notify the server we are now active |
| 99 | // |
| 100 | adaptorServer.notifyClientHandlerCreated(this) ; |
| 101 | |
| 102 | // |
| 103 | // Call protocol specific sequence |
| 104 | // |
| 105 | doRun() ; |
| 106 | } |
| 107 | finally { |
| 108 | // |
| 109 | // Now notify the adaptor server that the handler is terminating. |
| 110 | // This is important because the server may be blocked waiting for |
| 111 | // a handler to terminate. |
| 112 | // |
| 113 | adaptorServer.notifyClientHandlerDeleted(this) ; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // |
| 118 | // The protocol-dependent part of the request |
| 119 | // |
| 120 | public abstract void doRun() ; |
| 121 | |
| 122 | protected CommunicatorServer adaptorServer = null ; |
| 123 | protected int requestId = -1 ; |
| 124 | protected MBeanServer mbs = null ; |
| 125 | protected ObjectName objectName = null ; |
| 126 | protected Thread thread = null ; |
| 127 | protected boolean interruptCalled = false ; |
| 128 | protected String dbgTag = null ; |
| 129 | |
| 130 | protected String makeDebugTag() { |
| 131 | return "ClientHandler[" + adaptorServer.getProtocol() + ":" + adaptorServer.getPort() + "][" + requestId + "]"; |
| 132 | } |
| 133 | } |