1 package org.tanukisoftware.wrapper.test; 2 3 45 46 import java.io.BufferedReader ; 47 import java.io.InputStreamReader ; 48 import java.io.IOException ; 49 import java.lang.reflect.InvocationTargetException ; 50 import java.lang.reflect.Method ; 51 import java.util.Enumeration ; 52 import java.util.Properties ; 53 54 import org.tanukisoftware.wrapper.WrapperManager; 55 import org.tanukisoftware.wrapper.WrapperServiceException; 56 import org.tanukisoftware.wrapper.WrapperWin32Service; 57 import org.tanukisoftware.wrapper.event.WrapperControlEvent; 58 import org.tanukisoftware.wrapper.event.WrapperEvent; 59 import org.tanukisoftware.wrapper.event.WrapperEventListener; 60 61 64 public abstract class AbstractActionApp 65 implements WrapperEventListener 66 { 67 private DeadlockPrintStream m_out; 68 private DeadlockPrintStream m_err; 69 70 private Thread m_runner; 71 private Thread m_consoleRunner; 72 73 private boolean m_users; 74 private boolean m_groups; 75 76 private boolean m_nestedExit; 77 78 private long m_eventMask = 0xffffffffffffffffL; 79 private String m_serviceName = "testWrapper"; 80 81 84 protected AbstractActionApp() { 85 m_runner = new Thread ( "WrapperActionTest_Runner" ) 86 { 87 public void run() 88 { 89 while ( true ) 90 { 91 if ( m_users ) 92 { 93 System.out.println( "The current user is: " 94 + WrapperManager.getUser( m_groups ) ); 95 System.out.println( "The current interactive user is: " 96 + WrapperManager.getInteractiveUser( m_groups ) ); 97 } 98 synchronized( AbstractActionApp.class ) 99 { 100 try 101 { 102 AbstractActionApp.class.wait( 5000 ); 103 } 104 catch ( InterruptedException e ) 105 { 106 } 107 } 108 } 109 } 110 }; 111 m_runner.setDaemon( true ); 112 m_runner.start(); 113 } 114 115 118 132 public void fired( WrapperEvent event ) 133 { 134 System.out.println( "Received event: " + event ); 135 if ( event instanceof WrapperControlEvent ) 136 { 137 System.out.println( " Consume and ignore." ); 138 ((WrapperControlEvent)event).consume(); 139 } 140 } 141 142 145 protected boolean isNestedExit() 146 { 147 return m_nestedExit; 148 } 149 150 protected void setEventMask( long eventMask ) 151 { 152 m_eventMask = eventMask; 153 } 154 155 protected void setServiceName( String serviceName ) 156 { 157 m_serviceName = serviceName; 158 } 159 160 protected void prepareSystemOutErr() 161 { 162 m_out = new DeadlockPrintStream( System.out ); 163 System.setOut( m_out ); 164 m_err = new DeadlockPrintStream( System.err ); 165 System.setErr( m_err ); 166 } 167 protected boolean doAction( String action ) 168 { 169 if ( action.equals( "stop0" ) ) 170 { 171 WrapperManager.stop( 0 ); 172 173 } 174 else if ( action.equals( "stop1" ) ) 175 { 176 WrapperManager.stop( 1 ); 177 178 } 179 else if ( action.equals( "exit0" ) ) 180 { 181 System.exit( 0 ); 182 183 } 184 else if ( action.equals( "exit1" ) ) 185 { 186 System.exit( 1 ); 187 188 } 189 else if ( action.equals( "nestedexit1" ) ) 190 { 191 m_nestedExit = true; 192 WrapperManager.stop( 1 ); 193 194 } 195 else if ( action.equals( "stopimmediate0" ) ) 196 { 197 WrapperManager.stopImmediate( 0 ); 198 } 199 else if ( action.equals( "stopimmediate1" ) ) 200 { 201 WrapperManager.stopImmediate( 1 ); 202 } 203 else if ( action.equals( "stopandreturn0" ) ) 204 { 205 WrapperManager.stopAndReturn( 0 ); 206 } 207 else if ( action.equals( "halt0" ) ) 208 { 209 Method haltMethod; 212 try 213 { 214 haltMethod = Runtime .class.getMethod( "halt", new Class [] { Integer.TYPE } ); 215 } 216 catch ( NoSuchMethodException e ) 217 { 218 System.out.println( "halt not supported by current JVM." ); 219 haltMethod = null; 220 } 221 222 if ( haltMethod != null ) 223 { 224 Runtime runtime = Runtime.getRuntime(); 225 try 226 { 227 haltMethod.invoke( runtime, new Object [] { new Integer ( 0 ) } ); 228 } 229 catch ( IllegalAccessException e ) 230 { 231 System.out.println( "Unable to call runitme.halt: " + e.getMessage() ); 232 } 233 catch ( InvocationTargetException e ) 234 { 235 System.out.println( "Unable to call runitme.halt: " + e.getMessage() ); 236 } 237 } 238 } 239 else if ( action.equals( "halt1" ) ) 240 { 241 Method haltMethod; 244 try 245 { 246 haltMethod = Runtime .class.getMethod( "halt", new Class [] { Integer.TYPE } ); 247 } 248 catch ( NoSuchMethodException e ) 249 { 250 System.out.println( "halt not supported by current JVM." ); 251 haltMethod = null; 252 } 253 254 if ( haltMethod != null ) 255 { 256 Runtime runtime = Runtime.getRuntime(); 257 try 258 { 259 haltMethod.invoke( runtime, new Object [] { new Integer ( 1 ) } ); 260 } 261 catch ( IllegalAccessException e ) 262 { 263 System.out.println( "Unable to call runitme.halt: " + e.getMessage() ); 264 } 265 catch ( InvocationTargetException e ) 266 { 267 System.out.println( "Unable to call runitme.halt: " + e.getMessage() ); 268 } 269 } 270 } 271 else if ( action.equals( "restart" ) ) 272 { 273 WrapperManager.restart(); 274 275 } 276 else if ( action.equals( "restartandreturn" ) ) 277 { 278 WrapperManager.restartAndReturn(); 279 280 } 281 else if ( action.equals( "access_violation" ) ) 282 { 283 WrapperManager.accessViolation(); 284 285 } 286 else if ( action.equals( "access_violation_native" ) ) 287 { 288 WrapperManager.accessViolationNative(); 289 290 } 291 else if ( action.equals( "appear_hung" ) ) 292 { 293 WrapperManager.appearHung(); 294 295 } 296 else if ( action.equals( "dump" ) ) 297 { 298 WrapperManager.requestThreadDump(); 299 300 } 301 else if ( action.equals( "deadlock_out" ) ) 302 { 303 System.out.println( "Deadlocking System.out and System.err ..." ); 304 m_out.setDeadlock( true ); 305 m_err.setDeadlock( true ); 306 307 } 308 else if ( action.equals( "users" ) ) 309 { 310 if ( !m_users ) 311 { 312 System.out.println( "Begin polling the current and interactive users." ); 313 m_users = true; 314 } 315 else if ( m_groups ) 316 { 317 System.out.println( "Stop polling for group info." ); 318 m_groups = false; 319 } 320 else 321 { 322 System.out.println( "Stop polling the current and interactive users." ); 323 m_users = false; 324 } 325 326 synchronized( AbstractActionApp.class ) 327 { 328 AbstractActionApp.class.notifyAll(); 329 } 330 } 331 else if ( action.equals( "groups" ) ) 332 { 333 if ( ( !m_users ) || ( !m_groups ) ) 334 { 335 System.out.println( "Begin polling the current and interactive users with group info." ); 336 m_users = true; 337 m_groups = true; 338 } 339 else 340 { 341 System.out.println( "Stop polling for group info." ); 342 m_groups = false; 343 } 344 345 synchronized( AbstractActionApp.class ) 346 { 347 AbstractActionApp.class.notifyAll(); 348 } 349 } 350 else if ( action.equals( "console" ) ) 351 { 352 if ( m_consoleRunner == null ) 353 { 354 m_consoleRunner = new Thread ( "console-runner" ) 355 { 356 public void run() 357 { 358 System.out.println(); 359 System.out.println( "Start prompting for actions." ); 360 try 361 { 362 BufferedReader r = new BufferedReader (new InputStreamReader (System.in)); 363 String line; 364 try 365 { 366 do { 367 System.out.println("Input an action (return stops prompting):"); 368 line = r.readLine(); 369 if ((line != null) && (!line.equals(""))) { 370 System.out.println("Read action: " + line ); 371 if ( !doAction( line ) ) 372 { 373 System.out.println( "Unknown action: " + line ); 374 } 375 } 376 } while ((line != null) && (!line.equals(""))); 377 } 378 catch ( IOException e ) 379 { 380 e.printStackTrace(); 381 } 382 } 383 finally 384 { 385 System.out.println( "Stop prompting for actions." ); 386 System.out.println(); 387 m_consoleRunner = null; 388 } 389 } 390 }; 391 m_consoleRunner.setDaemon( true ); 392 m_consoleRunner.start(); 393 } 394 } 395 else if ( action.equals( "idle" ) ) 396 { 397 System.out.println( "Run idle." ); 398 m_users = false; 399 m_groups = false; 400 401 synchronized( AbstractActionApp.class ) 402 { 403 AbstractActionApp.class.notifyAll(); 404 } 405 } 406 else if ( action.equals( "properties" ) ) 407 { 408 System.out.println( "Dump System Properties:" ); 409 Properties props = System.getProperties(); 410 for ( Enumeration en = props.propertyNames(); en.hasMoreElements(); ) 411 { 412 String name = (String )en.nextElement(); 413 System.out.println( " " + name + "=" + props.getProperty( name ) ); 414 } 415 System.out.println(); 416 } 417 else if ( action.equals( "configuration" ) ) 418 { 419 System.out.println( "Dump Wrapper Properties:" ); 420 Properties props = WrapperManager.getProperties(); 421 for ( Enumeration en = props.propertyNames(); en.hasMoreElements(); ) 422 { 423 String name = (String )en.nextElement(); 424 System.out.println( " " + name + "=" + props.getProperty( name ) ); 425 } 426 System.out.println(); 427 } 428 else if ( action.equals( "listener" ) ) 429 { 430 System.out.println( "Updating Event Listeners:" ); 431 WrapperManager.removeWrapperEventListener( this ); 432 WrapperManager.addWrapperEventListener( this, m_eventMask ); 433 } 434 else if ( action.equals( "service_list" ) ) 435 { 436 WrapperWin32Service[] services = WrapperManager.listServices(); 437 if ( services == null ) 438 { 439 System.out.println( "Services not supported by current platform." ); 440 } 441 else 442 { 443 System.out.println( "Registered Services:" ); 444 for ( int i = 0; i < services.length; i++ ) 445 { 446 System.out.println( " " + services[i] ); 447 } 448 } 449 } 450 else if ( action.equals( "service_interrogate" ) ) 451 { 452 try 453 { 454 WrapperWin32Service service = WrapperManager.sendServiceControlCode( 455 m_serviceName, WrapperManager.SERVICE_CONTROL_CODE_INTERROGATE ); 456 System.out.println( "Service after interrogate: " + service ); 457 } 458 catch ( WrapperServiceException e ) 459 { 460 e.printStackTrace(); 461 } 462 } 463 else if ( action.equals( "service_start" ) ) 464 { 465 try 466 { 467 WrapperWin32Service service = WrapperManager.sendServiceControlCode( 468 m_serviceName, WrapperManager.SERVICE_CONTROL_CODE_START ); 469 System.out.println( "Service after start: " + service ); 470 } 471 catch ( WrapperServiceException e ) 472 { 473 e.printStackTrace(); 474 } 475 } 476 else if ( action.equals( "service_stop" ) ) 477 { 478 try 479 { 480 WrapperWin32Service service = WrapperManager.sendServiceControlCode( 481 m_serviceName, WrapperManager.SERVICE_CONTROL_CODE_STOP ); 482 System.out.println( "Service after stop: " + service ); 483 } 484 catch ( WrapperServiceException e ) 485 { 486 e.printStackTrace(); 487 } 488 } 489 else if ( action.equals( "service_user" ) ) 490 { 491 try 492 { 493 for ( int i = 128; i < 256; i+=10 ) 494 { 495 WrapperWin32Service service = WrapperManager.sendServiceControlCode( 496 m_serviceName, i ); 497 System.out.println( "Service after user code " + i + ": " + service ); 498 } 499 } 500 catch ( WrapperServiceException e ) 501 { 502 e.printStackTrace(); 503 } 504 } 505 else 506 { 507 return false; 509 510 } 511 512 return true; 513 } 514 } 515 516 | Popular Tags |