KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > remote > internal > ServerCommunicatorAdmin


1 /*
2  * @(#)ServerCommunicatorAdmin.java 1.18 05/01/04
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.jmx.remote.internal;
9
10 import java.io.IOException JavaDoc;
11
12 import com.sun.jmx.remote.util.ClassLogger;
13
14 public abstract class ServerCommunicatorAdmin {
15     public ServerCommunicatorAdmin(long timeout) {
16     if (logger.traceOn()) {
17         logger.trace("Constructor",
18              "Creates a new ServerCommunicatorAdmin object "+
19              "with the timeout "+timeout);
20     }
21
22     this.timeout = timeout;
23
24     timestamp = 0;
25         if (timeout < Long.MAX_VALUE) {
26             Runnable JavaDoc timeoutTask = new Timeout();
27             final Thread JavaDoc t = new Thread JavaDoc(timeoutTask);
28             t.setName("JMX server connection timeout " + t.getId());
29             // If you change this name you will need to change a unit test
30
// (NoServerTimeoutTest)
31
t.setDaemon(true);
32             t.start();
33         }
34     }
35
36     /**
37      * Tells that a new request message is received.
38      * A caller of this method should always call the method
39      * <code>rspOutgoing</code> to inform that a response is sent out
40      * for the received request.
41      * @return the value of the termination flag:
42      * <ul><code>true</code> if the connection is already being terminated,
43      * <br><code>false</code> otherwise.</ul>
44      */

45     public boolean reqIncoming() {
46     if (logger.traceOn()) {
47         logger.trace("reqIncoming", "Receive a new request.");
48     }
49
50     synchronized(lock) {
51         if (terminated) {
52         logger.warning("reqIncoming",
53                    "The server has decided to close " +
54                    "this client connection.");
55         }
56         ++currentJobs;
57
58         return terminated;
59     }
60     }
61
62     /**
63      * Tells that a response is sent out for a received request.
64      * @return the value of the termination flag:
65      * <ul><code>true</code> if the connection is already being terminated,
66      * <br><code>false</code> otherwise.</ul>
67      */

68     public boolean rspOutgoing() {
69     if (logger.traceOn()) {
70         logger.trace("reqIncoming", "Finish a request.");
71     }
72
73     synchronized(lock) {
74         if (--currentJobs == 0) {
75         timestamp = System.currentTimeMillis();
76         logtime("Admin: Timestamp=",timestamp);
77         // tells the adminor to restart waiting with timeout
78
lock.notify();
79         }
80         return terminated;
81     }
82     }
83
84     /**
85      * Called by this class to tell an implementation to do stop.
86      */

87     protected abstract void doStop();
88
89     /**
90      * Terminates this object.
91      * Called only by outside, so do not need to call doStop
92      */

93     public void terminate() {
94         if (logger.traceOn()) {
95         logger.trace("terminate",
96              "terminate the ServerCommunicatorAdmin object.");
97     }
98
99     synchronized(lock) {
100         if (terminated) {
101         return;
102         }
103
104         terminated = true;
105
106         // tell Timeout to terminate
107
lock.notify();
108     }
109     }
110
111 // --------------------------------------------------------------
112
// private classes
113
// --------------------------------------------------------------
114
private class Timeout implements Runnable JavaDoc {
115     public void run() {
116         boolean stopping = false;
117
118         synchronized(lock) {
119         if (timestamp == 0) timestamp = System.currentTimeMillis();
120         logtime("Admin: timeout=",timeout);
121         logtime("Admin: Timestamp=",timestamp);
122         
123         while(!terminated) {
124             try {
125             // wait until there is no more job
126
while(!terminated && currentJobs != 0) {
127                 if (logger.traceOn()) {
128                 logger.trace("Timeout-run",
129                          "Waiting without timeout.");
130                 }
131                 
132                 lock.wait();
133             }
134
135             if (terminated) return;
136
137             final long remaining =
138                 timeout - (System.currentTimeMillis() - timestamp);
139                 
140             logtime("Admin: remaining timeout=",remaining);
141
142             if (remaining > 0) {
143                 
144                 if (logger.traceOn()) {
145                 logger.trace("Timeout-run",
146                          "Waiting with timeout: "+
147                          remaining + " ms remaining");
148                 }
149
150                 lock.wait(remaining);
151             }
152
153             if (currentJobs > 0) continue;
154
155             final long elapsed =
156                 System.currentTimeMillis() - timestamp;
157             logtime("Admin: elapsed=",elapsed);
158
159             if (!terminated && elapsed > timeout) {
160                 if (logger.traceOn()) {
161                 logger.trace("Timeout-run",
162                          "timeout elapsed");
163                 }
164                 logtime("Admin: timeout elapsed! "+
165                     elapsed+">",timeout);
166                 // stopping
167
terminated = true;
168                 
169                 stopping = true;
170                 break;
171             }
172             } catch (InterruptedException JavaDoc ire) {
173             logger.warning("Timeout-run","Unexpected Exception: "+
174                        ire);
175             logger.debug("Timeout-run",ire);
176             return;
177             }
178         }
179         }
180
181         if (stopping) {
182         if (logger.traceOn()) {
183             logger.trace("Timeout-run", "Call the doStop.");
184         }
185         
186         doStop();
187         }
188     }
189     }
190
191     private void logtime(String JavaDoc desc,long time) {
192     timelogger.trace("synchro",desc+time);
193     }
194
195 // --------------------------------------------------------------
196
// private variables
197
// --------------------------------------------------------------
198
private long timestamp;
199
200     private final int[] lock = new int[0];
201     private int currentJobs = 0;
202
203     private long timeout;
204
205     // state issue
206
private boolean terminated = false;
207
208     private static final ClassLogger logger =
209     new ClassLogger("javax.management.remote.misc",
210             "ServerCommunicatorAdmin");
211     private static final ClassLogger timelogger =
212     new ClassLogger("javax.management.remote.timeout",
213             "ServerCommunicatorAdmin");
214 }
215
Popular Tags