1 package org.tanukisoftware.wrapper; 2 3 45 46 import java.lang.reflect.InvocationTargetException ; 47 import java.lang.reflect.Method ; 48 import java.lang.reflect.Modifier ; 49 50 86 public class WrapperSimpleApp 87 implements WrapperListener, Runnable 88 { 89 92 private Method m_mainMethod; 93 94 97 private String [] m_appArgs; 98 99 103 private boolean m_mainStarted; 104 105 109 private boolean m_mainComplete; 110 111 114 private Integer m_mainExitCode; 115 116 119 private boolean m_startComplete; 120 121 124 129 protected WrapperSimpleApp( String args[] ) 130 { 131 132 Class wmClass = WrapperManager.class; 134 135 if ( args.length < 1 ) 137 { 138 showUsage(); 139 WrapperManager.stop( 1 ); 140 return; } 142 143 Class mainClass; 145 try 146 { 147 mainClass = Class.forName( args[0] ); 148 } 149 catch ( ClassNotFoundException e ) 150 { 151 System.out.println( "WrapperSimpleApp: Unable to locate the class " + args[0] + ": " 152 + e ); 153 showUsage(); 154 WrapperManager.stop( 1 ); 155 return; } 157 catch ( LinkageError e ) 158 { 159 System.out.println( "WrapperSimpleApp: Unable to locate the class " + args[0] + ": " 160 + e ); 161 showUsage(); 162 WrapperManager.stop( 1 ); 163 return; } 165 166 try 168 { 169 m_mainMethod = mainClass.getMethod( "main", new Class [] { String [].class } ); 173 } 174 catch ( NoSuchMethodException e ) 175 { 176 System.out.println( 177 "WrapperSimpleApp: Unable to locate a public static main method in class " 178 + args[0] + ": " + e ); 179 showUsage(); 180 WrapperManager.stop( 1 ); 181 return; } 183 catch ( SecurityException e ) 184 { 185 System.out.println( 186 "WrapperSimpleApp: Unable to locate a public static main method in class " 187 + args[0] + ": " + e ); 188 showUsage(); 189 WrapperManager.stop( 1 ); 190 return; } 192 193 int modifiers = m_mainMethod.getModifiers(); 195 if ( !( Modifier.isPublic( modifiers ) && Modifier.isStatic( modifiers ) ) ) 196 { 197 System.out.println( "WrapperSimpleApp: The main method in class " + args[0] 198 + " must be declared public and static." ); 199 showUsage(); 200 WrapperManager.stop( 1 ); 201 return; } 203 204 String [] appArgs = new String [args.length - 1]; 206 System.arraycopy( args, 1, appArgs, 0, appArgs.length ); 207 208 WrapperManager.start( this, appArgs ); 213 214 } 217 218 221 224 public void run() 225 { 226 synchronized( this ) 228 { 229 m_mainStarted = true; 230 notifyAll(); 231 } 232 233 Throwable t = null; 234 try 235 { 236 if ( WrapperManager.isDebugEnabled() ) 237 { 238 System.out.println( "WrapperSimpleApp: invoking main method" ); 239 } 240 m_mainMethod.invoke( null, new Object [] { m_appArgs } ); 241 if ( WrapperManager.isDebugEnabled() ) 242 { 243 System.out.println( "WrapperSimpleApp: main method completed" ); 244 } 245 246 synchronized(this) 247 { 248 m_mainComplete = true; 251 this.notifyAll(); 252 } 253 254 return; 255 } 256 catch ( IllegalAccessException e ) 257 { 258 t = e; 259 } 260 catch ( IllegalArgumentException e ) 261 { 262 t = e; 263 } 264 catch ( InvocationTargetException e ) 265 { 266 t = e.getTargetException(); 267 if ( t == null ) 268 { 269 t = e; 270 } 271 } 272 273 System.out.println(); 276 System.out.println( "WrapperSimpleApp: Encountered an error running main: " + t ); 277 278 t.printStackTrace(); 282 283 synchronized(this) 284 { 285 if ( m_startComplete ) 286 { 287 WrapperManager.stop( 1 ); 289 return; } 291 else 292 { 293 m_mainComplete = true; 295 m_mainExitCode = new Integer ( 1 ); 296 this.notifyAll(); 297 return; 298 } 299 } 300 } 301 302 305 314 public Integer start( String [] args ) 315 { 316 boolean waitForStartMain = WrapperSystemPropertyUtil.getBooleanProperty( 318 WrapperSimpleApp.class.getName() + ".waitForStartMain", false ); 319 int maxStartMainWait = WrapperSystemPropertyUtil.getIntProperty( 320 WrapperSimpleApp.class.getName() + ".maxStartMainWait", 2 ); 321 maxStartMainWait = Math.max( 1, maxStartMainWait ); 322 323 int maxLoops; 325 if ( waitForStartMain ) 326 { 327 maxLoops = Integer.MAX_VALUE; 328 if ( WrapperManager.isDebugEnabled() ) 329 { 330 System.out.println( "WrapperSimpleApp: start(args) Will wait indefinitely " 331 + "for the main method to complete." ); 332 } 333 } 334 else 335 { 336 maxLoops = maxStartMainWait; if ( WrapperManager.isDebugEnabled() ) 338 { 339 System.out.println( "WrapperSimpleApp: start(args) Will wait up to " + maxLoops 340 + " seconds for the main method to complete." ); 341 } 342 } 343 344 Thread mainThread = new Thread ( this, "WrapperSimpleAppMain" ); 345 synchronized(this) 346 { 347 m_appArgs = args; 348 mainThread.start(); 349 350 while ( !m_mainStarted ) 353 { 354 try 355 { 356 this.wait( 1000 ); 357 } 358 catch ( InterruptedException e ) 359 { 360 } 362 } 363 364 int loops = 0; 366 while ( ( loops < maxLoops ) && ( !m_mainComplete ) ) 367 { 368 try 369 { 370 this.wait( 1000 ); 371 } 372 catch ( InterruptedException e ) 373 { 374 } 376 377 if ( !m_mainComplete ) 378 { 379 WrapperManager.signalStarting( 5000 ); 382 } 383 384 loops++; 385 } 386 387 m_startComplete = true; 392 393 if ( WrapperManager.isDebugEnabled() ) 396 { 397 System.out.println( "WrapperSimpleApp: start(args) end. Main Completed=" 398 + m_mainComplete + ", exitCode=" + m_mainExitCode ); 399 } 400 return m_mainExitCode; 401 } 402 } 403 404 407 public int stop( int exitCode ) 408 { 409 if ( WrapperManager.isDebugEnabled() ) 410 { 411 System.out.println( "WrapperSimpleApp: stop(" + exitCode + ")" ); 412 } 413 414 return exitCode; 418 } 419 420 427 public void controlEvent( int event ) 428 { 429 if ( ( event == WrapperManager.WRAPPER_CTRL_LOGOFF_EVENT ) 430 && WrapperManager.isLaunchedAsService() ) 431 { 432 if ( WrapperManager.isDebugEnabled() ) 434 { 435 System.out.println( "WrapperSimpleApp: controlEvent(" + event + ") Ignored" ); 436 } 437 } 438 else 439 { 440 if ( WrapperManager.isDebugEnabled() ) 441 { 442 System.out.println( "WrapperSimpleApp: controlEvent(" + event + ") Stopping" ); 443 } 444 WrapperManager.stop( 0 ); 445 } 447 } 448 449 452 455 protected void showUsage() 456 { 457 System.out.println(); 458 System.out.println( 459 "WrapperSimpleApp Usage:" ); 460 System.out.println( 461 " java org.tanukisoftware.wrapper.WrapperSimpleApp {app_class} [app_arguments]" ); 462 System.out.println(); 463 System.out.println( 464 "Where:" ); 465 System.out.println( 466 " app_class: The fully qualified class name of the application to run." ); 467 System.out.println( 468 " app_arguments: The arguments that would normally be passed to the" ); 469 System.out.println( 470 " application." ); 471 } 472 473 476 483 public static void main( String args[] ) 484 { 485 new WrapperSimpleApp( args ); 486 } 487 } 488 489 | Popular Tags |