1 23 24 31 package com.sun.enterprise.admin.server.core.channel; 32 33 import java.io.File ; 34 import java.io.FileInputStream ; 35 import java.io.IOException ; 36 import java.io.ObjectInputStream ; 37 import java.rmi.RemoteException ; 38 import java.rmi.ServerException ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 import java.util.logging.Handler ; 42 43 import com.sun.enterprise.admin.common.constant.AdminConstants; 44 import com.sun.enterprise.admin.common.ServerInstanceStatus; 45 import com.sun.enterprise.admin.common.Status; 46 import com.sun.enterprise.admin.event.AdminEvent; 47 import com.sun.enterprise.admin.event.AdminEventResult; 48 49 import com.sun.enterprise.util.i18n.StringManager; 51 52 55 public class RMIClient implements Runnable { 56 57 private File stubFile; 58 private File seedFile; 59 private long stubFileTs = 0; 60 private byte[] key; 61 private RemoteAdminChannel stub; 62 private boolean autoRefresh; 63 private long autoRefreshInterval; 64 private Thread autoRefreshThread; 65 66 77 RMIClient(String stubFile, String seedFile) { 78 if (stubFile == null || seedFile == null) { 79 warn(CLIENT_NULLARGS_ERRCODE); 80 throw new IllegalArgumentException (CLIENT_NULLARGS_ERRMSG); 81 } 82 this.stubFile = new File (stubFile); 83 this.seedFile = new File (seedFile); 84 if (this.stubFile.exists()) { 85 stub = readStub(); 86 } 87 if (AdminChannel.getClientAutoRefreshEnabled()) { 88 startAutoRefreshThread( 89 AdminChannel.getClientAutoRefreshInterval()); 90 } 91 } 92 93 96 public RMIClient(boolean isDebug, String stubFile, String seedFile) { 97 if (! isDebug) { 98 logger.setLevel(java.util.logging.Level.SEVERE); 99 } 100 if (stubFile == null || seedFile == null) { 101 throw new IllegalArgumentException (CLIENT_NULLARGS_ERRMSG); 102 } 103 this.stubFile = new File (stubFile); 104 this.seedFile = new File (seedFile); 105 if (this.stubFile.exists()) { 106 stub = readStub(); 107 } 108 } 109 110 114 void startAutoRefreshThread(long interval) { 115 if (interval <= 0) { 116 throw new IllegalArgumentException (INVALID_AUTO_REFRESH_INTERVAL); 117 } 118 autoRefresh = true; 119 autoRefreshInterval = interval; 120 if (autoRefreshThread != null && autoRefreshThread.isAlive()) { 121 return; 122 } else { 123 autoRefreshThread = new Thread (this); 124 autoRefreshThread.start(); 125 } 126 } 127 128 131 void stopAutoRefreshThread() { 132 autoRefresh = false; 133 } 134 135 138 public void run() { 139 while (autoRefresh) { 140 try { 141 Thread.sleep(autoRefreshInterval); 142 } catch (InterruptedException ie) { 143 warn(AUTO_REFRESH_INTR); 144 autoRefresh = false; 145 } 146 if (autoRefresh) { 147 checkServerStatus(); 148 } 149 } 150 } 151 152 155 public AdminEventResult sendNotification(AdminEvent event) { 156 boolean doRetry = true; 158 AdminEventResult result = null; 159 if (stub != null) { 160 try { 161 result = stub.sendNotification(key, event); 162 doRetry = false; 163 } catch (ServerException re) { 164 if ((re.detail != null) && 165 (re.detail instanceof java.lang.IllegalArgumentException 166 || re.detail instanceof java.lang.SecurityException )) { 167 doRetry = false; 168 warn(EVENT_NOTIFY_ERROR); 169 debug(re.detail); 170 } else { 171 if (re.detail != null) { 172 debug(re.detail); 173 } 174 debug(re); 175 } 176 } catch (RemoteException re) { 177 if (re.detail != null) { 178 debug(re.detail); 179 } 180 debug(re); 181 } 182 } 183 if (doRetry) { 184 boolean gotNew = checkServerStatus(); 187 if (stub != null && gotNew) { 188 try { 189 result = stub.sendNotification(key, event); 190 } catch (RemoteException re) { 191 warn(EVENT_RENOTIFY_ERROR); 192 if (re.detail != null) { 193 debug(re.detail); 194 } 195 debug(re); 196 } 197 } 198 } 199 if (result == null) { 200 result = new AdminEventResult(event.getSequenceNumber()); 202 result.setResultCode(AdminEventResult.TRANSMISSION_ERROR); 203 if (stub == null) { 204 result.addMessage(event.getEffectiveDestination(), 205 "Remote Stub is null"); 206 } 207 } 208 return result; 209 } 210 211 217 public boolean isAlive() { 218 return isAlive(false); 219 } 220 221 230 public boolean isAlive(boolean refreshStub) { 231 232 if (refreshStub) { 233 boolean gotNew = checkServerStatus(); 234 } 235 236 boolean isAlive = true; 237 238 if (stub != null) { 239 try { 240 stub.pingServer(key); 241 } 242 catch(RemoteException re) { 243 debug(re); 244 isAlive = false; 245 } 246 } 247 else { 248 isAlive = false; 249 } 250 return ( isAlive ); 251 } 252 253 263 public int getInstanceStatusCode() { 264 boolean gotNew = checkServerStatus(); 265 int statusCode = Status.kInstanceNotRunningCode; 266 if (stub != null) { 267 try { 268 statusCode = stub.getServerStatusCode(key); 269 } catch (RemoteException re) { 270 debug(CHANNEL_COMM_ERROR, stubFile.getName()); 271 if (re.detail != null) { 272 trace(re.detail); 273 } 274 trace(re); 275 } catch (IllegalArgumentException iae) { 276 debug(CHANNEL_COMM_ERROR, stubFile.getName()); 277 trace(iae); 278 byte[] newKey = null; 284 try { 285 newKey = readSeed(); 286 } catch (IOException ioe) { 287 debug(FILE_READ_ERROR, seedFile.getName()); 288 trace(ioe); 289 } 290 if (newKey != null) { 291 key = newKey; 292 } 293 throw iae; 294 } 295 } 296 return statusCode; 297 } 298 299 304 public int getConflictedPort() { 305 int conflictedPort = 0; 306 boolean gotNew = checkServerStatus(); 307 if (stub != null) { 308 try { 309 conflictedPort = stub.getConflictedPort(key); 310 } catch (RemoteException re) { 311 debug(CHANNEL_COMM_ERROR, stubFile.getName()); 312 if (re.detail != null) { 313 trace(re.detail); 314 } 315 trace(re); 316 } 317 } 318 return conflictedPort; 319 } 320 321 325 public void triggerServerExit() { 326 boolean gotNew = checkServerStatus(); 327 if (stub != null) { 328 try { 329 stub.triggerServerExit(key); 330 } catch (RemoteException re) { 331 debug(CHANNEL_COMM_ERROR, stubFile.getName()); 332 if (re.detail != null) { 333 trace(re.detail); 334 } 335 trace(re); 336 } 337 } 338 } 339 340 347 public boolean isRestartNeeded() { 348 boolean restartNeeded = false; 349 boolean gotNew = checkServerStatus(); 350 if (stub != null) { 351 try { 352 restartNeeded = stub.isRestartNeeded(key); 353 } catch (RemoteException re) { 354 debug(CHANNEL_COMM_ERROR, stubFile.getName()); 355 if (re.detail != null) { 356 trace(re.detail); 357 } 358 trace(re); 359 } 360 } 361 return restartNeeded; 362 } 363 364 371 public void setRestartNeeded(boolean restartNeeded) { 372 boolean gotNew = checkServerStatus(); 373 if (stub != null) { 374 try { 375 stub.setRestartNeeded(key, restartNeeded); 376 } catch (RemoteException re) { 377 debug(CHANNEL_COMM_ERROR, stubFile.getName()); 378 if (re.detail != null) { 379 trace(re.detail); 380 } 381 trace(re); 382 } 383 } 384 } 385 386 389 private boolean checkServerStatus() { 390 boolean gotNew = false; 391 if (stubFile.exists()) { 392 long ts = stubFile.lastModified(); 393 if (ts > stubFileTs) { 394 if (stubFile.canRead()) { 395 RemoteAdminChannel obj = readStub(); 396 if (obj != null) { 397 gotNew = true; 398 stub = obj; 399 } 400 } else { 401 warn(FILE_READ_ERROR, stubFile.getName()); 402 } 403 } 404 } else { 405 if (stub != null) { 406 stub = null; 407 } 408 } 409 return gotNew; 410 } 411 412 415 private RemoteAdminChannel readStub() { 416 RemoteAdminChannel obj = null; 417 FileInputStream fis = null; 418 try { 419 stubFileTs = stubFile.lastModified(); 420 fis = new FileInputStream (stubFile); 421 ObjectInputStream ois = new ObjectInputStream (fis); 422 obj = (RemoteAdminChannel)ois.readObject(); 423 } catch (IOException ioe) { 424 warn(CLIENT_INIT_ERROR); 425 debug(ioe); 426 } catch (ClassNotFoundException cnfe) { 427 warn(CLIENT_INIT_ERROR); 428 debug(cnfe); 429 } finally { 430 if (fis != null) { 431 try { 432 fis.close(); 433 } catch (IOException ioe) { 434 } 435 } 436 } 437 try { 438 key = readSeed(); 439 } catch (IOException ioe) { 440 warn(CLIENT_INIT_ERROR); 441 debug(ioe); 442 obj = null; 443 } 444 if (obj == null) { 445 stubFileTs = 0; 446 } 447 return obj; 448 } 449 450 453 private byte[] readSeed() throws IOException { 454 byte[] seed = null; 455 if (seedFile.exists() && seedFile.canRead()) { 456 long seedFileTs = seedFile.lastModified(); 461 if (seedFileTs >= stubFileTs) { 462 FileInputStream fis = null; 463 try { 464 fis = new FileInputStream (seedFile); 465 seed = new byte[AdminChannel.SEED_LENGTH]; 466 fis.read(seed); 467 } catch (IOException ioe) { 468 warn(AdminChannel.KEY_READ_ERROR); 469 debug(ioe); 470 seed = null; 471 } finally { 472 if (fis != null) { 473 try { 474 fis.close(); 475 } catch (IOException ioe) { 476 } 477 } 478 } 479 } else { 480 debug(SEED_FILE_OLDER, 481 new Long [] {new Long (seedFileTs), new Long (stubFileTs)}); 482 } 483 } else { 484 warn(AdminChannel.KEY_READ_ERROR); 485 debug(FILE_READ_ERROR, seedFile.getName()); 486 } 487 if (seed == null) { 488 String msg = localStrings.getString( "admin.server.core.channel.unable_initializing_key", seedFile ); 489 throw new IOException ( msg ); 490 } 491 return seed; 492 } 493 494 505 public boolean hasRestartedSince(long ts) { 506 return hasRestartedSince(ts, false); 507 } 508 509 520 public boolean hasRestartedSince(long ts, boolean refreshStub) { 521 if (refreshStub) { 522 boolean gotNew = checkServerStatus(); 523 } 524 boolean restarted = false; 525 if (stubFile != null) { 526 if (stubFileTs > ts) { 527 restarted = true; 528 } 529 } 530 return restarted; 531 } 532 533 537 static void trace(Throwable t) { 538 logger.log(Level.FINEST, t.getMessage(), t); 539 } 540 541 static void warn(String s) { 542 logger.warning(s); 543 } 544 545 static void warn(String msgkey, String obj1) { 546 logger.log(Level.WARNING, msgkey, obj1); 547 } 548 549 static void debug(String s) { 550 logger.fine(s); 551 } 552 553 static void debug(String msgkey, String obj1) { 554 logger.log(Level.FINE, msgkey, obj1); 555 } 556 557 static void debug(String msgkey, Object [] objarr) { 558 logger.log(Level.FINE, msgkey, objarr); 559 } 560 561 static void debug(Throwable t) { 562 logger.log(Level.FINE, t.getMessage(), t); 563 } 564 565 private static StringManager localStrings = 567 StringManager.getManager( RMIClient.class ); 568 569 private static final String CLIENT_NULLARGS_ERRCODE = 570 "channel.client_nullargs"; 571 private static final String CLIENT_NULLARGS_ERRMSG = 572 localStrings.getString( "admin.server.core.channel.attempt_initializing_channel_client_with_null_arguments" ); 573 private static final String CLIENT_INIT_ERROR = "channel.client_init_error"; 574 private static final String EVENT_NOTIFY_ERROR = 575 "channel.event_notify_error"; 576 private static final String EVENT_RENOTIFY_ERROR = 577 "channel.event_renotify_error"; 578 private static final String AUTO_REFRESH_INTR = "channel.auto_refresh_intr"; 579 private static final String CHANNEL_COMM_ERROR = "channel.comm_error"; 580 private static final String INVALID_AUTO_REFRESH_INTERVAL = 581 localStrings.getString( 582 "admin.server.core.channel.invalid_auto_refresh_interval"); 583 584 static final String FILE_READ_ERROR = "channel.file_read_error"; 585 static final String SEED_FILE_OLDER = "channel.seed_file_older"; 586 587 static Logger logger = Logger.getLogger(AdminConstants.kLoggerName); 588 } 589 | Popular Tags |