1 7 8 package com.sun.jmx.remote.internal; 9 10 import java.io.IOException ; 11 import java.io.InterruptedIOException ; 12 13 import com.sun.jmx.remote.util.ClassLogger; 14 import com.sun.jmx.remote.util.EnvHelp; 15 16 public abstract class ClientCommunicatorAdmin { 17 public ClientCommunicatorAdmin(long period) { 18 this.period = period; 19 20 if (period > 0) { 21 checker = new Checker(); 22 23 Thread t = new Thread (checker); 24 t.setDaemon(true); 25 t.start(); 26 } else 27 checker = null; 28 } 29 30 33 public void gotIOException (IOException ioe) throws IOException { 34 restart(ioe); 35 } 36 37 40 protected abstract void checkConnection() throws IOException ; 41 42 45 protected abstract void doStart() throws IOException ; 46 47 50 protected abstract void doStop(); 51 52 55 public void terminate() { 56 synchronized(lock) { 57 if (state == TERMINATED) { 58 return; 59 } 60 61 state = TERMINATED; 62 63 lock.notifyAll(); 64 65 if (checker != null) 66 checker.stop(); 67 } 68 } 69 70 private void restart(IOException ioe) throws IOException { 71 synchronized(lock) { 73 if (state == TERMINATED) { 74 throw new IOException ("The client has been closed."); 75 } else if (state == FAILED) { throw ioe; 77 } else if (state == RE_CONNECTING) { 78 while(state == RE_CONNECTING) { 81 try { 82 lock.wait(); 83 } catch (InterruptedException ire) { 84 InterruptedIOException iioe = new InterruptedIOException (ire.toString()); 86 EnvHelp.initCause(iioe, ire); 87 88 throw iioe; 89 } 90 } 91 92 if (state == TERMINATED) { 93 throw new IOException ("The client has been closed."); 94 } else if (state != CONNECTED) { 95 throw ioe; 97 } 98 } else { 99 state = RE_CONNECTING; 100 lock.notifyAll(); 101 } 102 } 103 104 try { 106 doStart(); 107 synchronized(lock) { 108 if (state == TERMINATED) { 109 throw new IOException ("The client has been closed."); 110 } 111 112 state = CONNECTED; 113 114 lock.notifyAll(); 115 } 116 117 return; 118 } catch (Exception e) { 119 logger.warning("restart", "Failed to restart: " + e); 120 logger.debug("restart",e); 121 122 synchronized(lock) { 123 if (state == TERMINATED) { 124 throw new IOException ("The client has been closed."); 125 } 126 127 state = FAILED; 128 129 lock.notifyAll(); 130 } 131 132 try { 133 doStop(); 134 } catch (Exception eee) { 135 } 138 139 terminate(); 140 141 throw ioe; 142 } 143 } 144 145 private class Checker implements Runnable { 149 public void run() { 150 myThread = Thread.currentThread(); 151 152 while (state != TERMINATED && !myThread.isInterrupted()) { 153 try { 154 Thread.sleep(period); 155 } catch (InterruptedException ire) { 156 } 159 160 if (state == TERMINATED || myThread.isInterrupted()) { 161 break; 162 } 163 164 try { 165 checkConnection(); 166 } catch (Exception e) { 167 synchronized(lock) { 168 if (state == TERMINATED || myThread.isInterrupted()) { 169 break; 170 } 171 } 172 173 e = (Exception )EnvHelp.getCause(e); 174 175 if (e instanceof IOException && 176 !(e instanceof InterruptedIOException )) { 177 try { 178 restart((IOException )e); 179 } catch (Exception ee) { 180 logger.warning("Checker-run", 181 "Failed to check connection: "+ e); 182 logger.warning("Checker-run", "stopping"); 183 logger.debug("Checker-run",e); 184 185 break; 186 } 187 } else { 188 logger.warning("Checker-run", 189 "Failed to check the connection: " + e); 190 logger.debug("Checker-run",e); 191 192 194 break; 195 } 196 } 197 } 198 199 if (logger.traceOn()) { 200 logger.trace("Checker-run", "Finished."); 201 } 202 } 203 204 private void stop() { 205 if (myThread != null && myThread != Thread.currentThread()) { 206 myThread.interrupt(); 207 } 208 } 209 210 private Thread myThread; 211 } 212 213 private final Checker checker; 217 private long period; 218 219 private final static int CONNECTED = 0; 221 private final static int RE_CONNECTING = 1; 222 private final static int FAILED = 2; 223 private final static int TERMINATED = 3; 224 225 private int state = CONNECTED; 226 227 private final int[] lock = new int[0]; 228 229 private static final ClassLogger logger = 230 new ClassLogger("javax.management.remote.misc", 231 "ClientCommunicatorAdmin"); 232 } 233 | Popular Tags |