1 package org.tanukisoftware.wrapper; 2 3 27 28 import java.io.IOException ; 29 import java.io.InterruptedIOException ; 30 import java.lang.reflect.InvocationTargetException ; 31 import java.lang.reflect.Method ; 32 import java.net.InetAddress ; 33 import java.net.Socket ; 34 import java.net.SocketException ; 35 import java.net.ServerSocket ; 36 import java.util.Hashtable ; 37 38 import org.tanukisoftware.wrapper.WrapperManager; 39 40 93 public class WrapperActionServer 94 implements Runnable 95 { 96 97 public final static byte COMMAND_SHUTDOWN = (byte)'S'; 98 99 public final static byte COMMAND_HALT_EXPECTED = (byte)'H'; 100 101 public final static byte COMMAND_RESTART = (byte)'R'; 102 103 public final static byte COMMAND_DUMP = (byte)'D'; 104 105 public final static byte COMMAND_HALT_UNEXPECTED = (byte)'U'; 106 107 public final static byte COMMAND_ACCESS_VIOLATION = (byte)'V'; 108 109 public final static byte COMMAND_APPEAR_HUNG = (byte)'G'; 110 111 112 private InetAddress m_bindAddr; 113 114 115 private int m_port; 116 117 118 private Thread m_runner; 119 120 121 private boolean m_runnerStop = false; 122 123 124 private ServerSocket m_serverSocket; 125 126 127 private Hashtable m_actions = new Hashtable (); 128 129 132 139 public WrapperActionServer( int port, InetAddress bindAddress ) 140 { 141 m_port = port; 142 m_bindAddr = bindAddress; 143 } 144 145 152 public WrapperActionServer( int port ) 153 { 154 this( port, null ); 155 } 156 157 160 163 public void run() 164 { 165 if ( Thread.currentThread() != m_runner ) 166 { 167 throw new IllegalStateException ( "Private method." ); 168 } 169 170 try 171 { 172 while ( !m_runnerStop ) 173 { 174 try 175 { 176 int command; 177 Socket socket = m_serverSocket.accept(); 178 try 179 { 180 socket.setSoTimeout( 15000 ); 183 184 command = socket.getInputStream().read(); 186 } 187 finally 188 { 189 socket.close(); 190 } 191 192 if ( command >= 0 ) 193 { 194 Runnable action; 195 synchronized( m_actions ) 196 { 197 action = (Runnable )m_actions.get( new Integer ( command ) ); 198 } 199 200 if ( action != null ) 201 { 202 try 203 { 204 action.run(); 205 } 206 catch ( Throwable t ) 207 { 208 System.out.println( 209 "WrapperActionServer: Error processing action." ); 210 t.printStackTrace(); 211 } 212 } 213 } 214 } 215 catch ( Throwable t ) 216 { 217 if ( m_runnerStop 221 && ( ( t instanceof InterruptedException ) 222 || ( t instanceof SocketException ) 223 || ( t instanceof InterruptedIOException ) ) ) 224 { 225 } 227 else 228 { 229 System.out.println( 230 "WrapperActionServer: Unexpeced error." ); 231 t.printStackTrace(); 232 233 try 235 { 236 Thread.sleep( 5000 ); 237 } 238 catch ( InterruptedException e ) 239 { 240 } 242 } 243 } 244 } 245 } 246 finally 247 { 248 synchronized( this ) 249 { 250 m_runner = null; 251 252 this.notify(); 254 } 255 } 256 } 257 258 261 268 public void start() 269 throws IOException 270 { 271 m_serverSocket = new ServerSocket ( m_port, 5, m_bindAddr ); 273 274 m_runner = new Thread ( this, "WrapperActionServer_runner" ); 275 m_runner.setDaemon( true ); 276 m_runner.start(); 277 } 278 279 282 public void stop() 283 throws Exception 284 { 285 Thread runner = m_runner; 286 m_runnerStop = true; 287 runner.interrupt(); 288 289 ServerSocket serverSocket = m_serverSocket; 291 if ( serverSocket != null ) 292 { 293 try 294 { 295 serverSocket.close(); 296 } 297 catch ( IOException e ) 298 { 299 } 301 } 302 303 synchronized( this ) 304 { 305 while( m_runner != null ) 306 { 307 try 308 { 309 this.wait(); 311 } 312 catch ( InterruptedException e ) 313 { 314 } 316 } 317 } 318 } 319 320 332 public void registerAction( byte command, Runnable action ) 333 { 334 synchronized( m_actions ) 335 { 336 m_actions.put( new Integer ( command ), action ); 337 } 338 } 339 340 344 public void unregisterAction( byte command ) 345 { 346 synchronized( m_actions ) 347 { 348 m_actions.remove( new Integer ( command ) ); 349 } 350 } 351 352 357 public void enableShutdownAction( boolean enable ) 358 { 359 if ( enable ) 360 { 361 registerAction( COMMAND_SHUTDOWN, new Runnable () 362 { 363 public void run() 364 { 365 WrapperManager.stop( 0 ); 366 } 367 } ); 368 } 369 else 370 { 371 unregisterAction( COMMAND_SHUTDOWN ); 372 } 373 } 374 375 382 public void enableHaltExpectedAction( boolean enable ) 383 { 384 if ( enable ) 385 { 386 registerAction( COMMAND_HALT_EXPECTED, new Runnable () 387 { 388 public void run() 389 { 390 WrapperManager.stopImmediate( 0 ); 391 } 392 } ); 393 } 394 else 395 { 396 unregisterAction( COMMAND_HALT_EXPECTED ); 397 } 398 } 399 400 405 public void enableRestartAction( boolean enable ) 406 { 407 if ( enable ) 408 { 409 registerAction( COMMAND_RESTART, new Runnable () 410 { 411 public void run() 412 { 413 WrapperManager.restart(); 414 } 415 } ); 416 } 417 else 418 { 419 unregisterAction( COMMAND_RESTART ); 420 } 421 } 422 423 428 public void enableThreadDumpAction( boolean enable ) 429 { 430 if ( enable ) 431 { 432 registerAction( COMMAND_DUMP, new Runnable () 433 { 434 public void run() 435 { 436 WrapperManager.requestThreadDump(); 437 } 438 } ); 439 } 440 else 441 { 442 unregisterAction( COMMAND_DUMP ); 443 } 444 } 445 446 453 public void enableHaltUnexpectedAction( boolean enable ) 454 { 455 if ( enable ) 456 { 457 registerAction( COMMAND_HALT_UNEXPECTED, new Runnable () 458 { 459 public void run() 460 { 461 Method haltMethod; 464 try 465 { 466 haltMethod = 467 Runtime .class.getMethod( "halt", new Class [] { Integer.TYPE } ); 468 } 469 catch ( NoSuchMethodException e ) 470 { 471 System.out.println( "halt not supported by current JVM." ); 472 haltMethod = null; 473 } 474 475 if ( haltMethod != null ) 476 { 477 Runtime runtime = Runtime.getRuntime(); 478 try 479 { 480 haltMethod.invoke( runtime, new Object [] { new Integer ( 0 ) } ); 481 } 482 catch ( IllegalAccessException e ) 483 { 484 System.out.println( 485 "Unable to call runitme.halt: " + e.getMessage() ); 486 } 487 catch ( InvocationTargetException e ) 488 { 489 System.out.println( 490 "Unable to call runitme.halt: " + e.getMessage() ); 491 } 492 } 493 } 494 } ); 495 } 496 else 497 { 498 unregisterAction( COMMAND_HALT_UNEXPECTED ); 499 } 500 } 501 502 511 public void enableAccessViolationAction( boolean enable ) 512 { 513 if ( enable ) 514 { 515 registerAction( COMMAND_ACCESS_VIOLATION, new Runnable () 516 { 517 public void run() 518 { 519 WrapperManager.accessViolationNative(); 520 } 521 } ); 522 } 523 else 524 { 525 unregisterAction( COMMAND_ACCESS_VIOLATION ); 526 } 527 } 528 529 540 public void enableAppearHungAction( boolean enable ) 541 { 542 if ( enable ) 543 { 544 registerAction( COMMAND_APPEAR_HUNG, new Runnable () 545 { 546 public void run() 547 { 548 WrapperManager.appearHung(); 549 } 550 } ); 551 } 552 else 553 { 554 unregisterAction( COMMAND_APPEAR_HUNG ); 555 } 556 } 557 } 558 559 | Popular Tags |