1 23 24 package com.sun.enterprise.admin.servermgmt.pe; 25 26 import java.util.Map ; 27 import java.util.ArrayList ; 28 import java.io.File ; 29 import java.lang.reflect.Method ; 30 31 import com.sun.enterprise.util.ProcessExecutor; 32 import com.sun.enterprise.util.SystemPropertyConstants; 33 import com.sun.enterprise.util.io.FileUtils; 34 import com.sun.enterprise.util.i18n.StringManager; 35 import com.sun.enterprise.admin.servermgmt.InstancesManager; 36 import com.sun.enterprise.admin.servermgmt.InstanceException; 37 import com.sun.enterprise.admin.servermgmt.pe.InstanceTimer; 38 import com.sun.enterprise.admin.servermgmt.pe.TimerCallback; 39 40 import com.sun.enterprise.admin.servermgmt.RepositoryConfig; 41 import com.sun.enterprise.admin.servermgmt.RepositoryManager; 42 import com.sun.enterprise.admin.servermgmt.DomainConfig; 43 import com.sun.enterprise.admin.common.Status; 44 import com.sun.enterprise.admin.server.core.channel.RMIClient; 45 46 48 public class PEInstancesManager extends RepositoryManager implements InstancesManager 49 { 50 53 private static final StringManager _strMgr = 54 StringManager.getManager(PEInstancesManager.class); 55 56 private final RepositoryConfig _config; 57 58 public PEInstancesManager(RepositoryConfig config) 59 { 60 super(); 61 _config = config; 62 } 63 64 protected RepositoryConfig getConfig() 65 { 66 return _config; 67 } 68 69 public void createInstance() 70 throws InstanceException 71 { 72 throw new UnsupportedOperationException ( 73 _strMgr.getString("notSupported")); 74 } 75 76 public void deleteInstance() throws InstanceException 77 { 78 throw new UnsupportedOperationException ( 79 _strMgr.getString("notSupported")); 80 } 81 82 83 90 public Process startInstance() throws InstanceException { 91 return startInstance(null, null); 92 } 93 94 95 103 public Process startInstance(String [] interativeOptions) throws InstanceException { 104 String [] commandLineArgs=null; 105 return startInstance(interativeOptions, commandLineArgs); 106 } 107 108 109 118 public Process startInstance(String [] interativeOptions, String [] commandLineArgs) 119 throws InstanceException 120 { 121 preStart(); 122 123 Boolean v = (Boolean )getConfig().get(DomainConfig.K_VERBOSE); 125 boolean verbose = false; 126 if ( v != null ) { 127 verbose = v.booleanValue(); 128 } 129 130 Boolean d = (Boolean )getConfig().get(DomainConfig.K_DEBUG); 131 boolean debug = false; 132 if ( d != null ) { 133 debug = d.booleanValue(); 134 } 135 136 String [] command=null; 137 File script = getFileLayout(getConfig()).getStartServ(); 138 String nativeLauncher=System.getProperty(SystemPropertyConstants.NATIVE_LAUNCHER); 139 Process process=null; 140 ArrayList alCmd=new ArrayList (); 141 alCmd.add(script.getAbsolutePath()); 143 144 148 if ( System.getProperty("com.sun.aas.processLauncher") == null && verbose ) { 150 try { 151 155 System.setProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY, 157 getConfig().getRepositoryRoot() + File.separator + 158 getConfig().getRepositoryName()); 159 ArrayList args = new ArrayList (); 160 args.add("s1as-server"); 161 args.add("-Dcom.sun.aas.instanceName=server"); 164 args.add("start"); 165 if ( debug ) { 166 args.add("debug"); 167 } 168 if ( verbose ) { 169 args.add("verbose"); 170 } 171 172 if (commandLineArgs != null) { 174 for(int ii=0; ii < commandLineArgs.length; ii++) { 175 args.add(commandLineArgs[ii]); 176 } 177 } 178 179 String [] argStrings = (String [])args.toArray( 181 new String [args.size()]); 182 183 186 Class launcherClass = Class.forName("LauncherBootstrap"); 187 Class [] paramClasses = new Class [] { String [].class }; 188 Object [] argsArray = new Object [] { argStrings }; 189 Method mainMethod = launcherClass.getMethod("main", paramClasses); 190 191 mainMethod.invoke(null, argsArray); 194 195 } catch ( Exception ex ) { 196 throw new InstanceException( 197 getMessages().getInstanceStartupExceptionMessage( 198 getConfig().getDisplayName()), ex); 199 } 200 201 } else if (System.getProperty("com.sun.aas.processLauncher") != null && verbose) { 202 if (nativeLauncher != null && nativeLauncher.equals("true")) { 204 alCmd.add("native"); 206 } 207 if (debug) alCmd.add("debug"); 208 if (verbose) alCmd.add("verbose"); 209 if (commandLineArgs != null) { 211 for(int ii=0; ii < commandLineArgs.length; ii++) { 212 alCmd.add(commandLineArgs[ii]); 213 } 214 } 215 216 command=new String [alCmd.size()]; 218 command=(String [])alCmd.toArray(command); 219 220 try { 221 ProcessExecutor exec = new ProcessExecutor(command, interativeOptions); 223 exec.setVerbose(verbose); 225 exec.execute(false, false); 227 process=exec.getSubProcess(); 228 int exitValue=process.waitFor(); 230 System.exit(exitValue); 231 } catch (Exception e) { 232 throw new InstanceException(_strMgr.getString("procExecError"), e); 233 } 234 235 } else { 236 if (nativeLauncher != null && nativeLauncher.equals("true")) { 238 alCmd.add("native"); 240 } 241 242 if (debug) { 244 alCmd.add("debug"); 245 } 246 247 if (commandLineArgs != null) { 249 for(int ii=0; ii < commandLineArgs.length; ii++) { 250 alCmd.add(commandLineArgs[ii]); 251 } 252 } 253 254 command=new String [alCmd.size()]; 256 command=(String [])alCmd.toArray(command); 257 258 ProcessExecutor processExec=startInstanceExecute(command, interativeOptions); 261 process=processExec.getSubProcess(); 262 waitUntilStarting(processExec); 263 waitUntilStarted(); 264 postStart(); 265 } 266 267 return process; 268 } 269 270 274 protected ProcessExecutor startInstanceExecute(String [] command, String [] interativeOptions) throws InstanceException { 275 return execute(command, interativeOptions); 276 } 277 278 279 280 public void stopInstance() throws InstanceException 281 { 282 preStop(); 283 execute(getFileLayout(getConfig()).getStopServ()); 284 waitUntilStopped(); 285 postStop(); 286 } 287 288 public String [] listInstances() throws InstanceException 289 { 290 throw new UnsupportedOperationException ( 291 _strMgr.getString("notSupported")); 292 } 293 294 public boolean isInstanceStarting() throws InstanceException 295 { 296 return (getInstanceStatus() == 297 Status.kInstanceStartingCode); 298 } 299 300 public boolean isInstanceRunning() throws InstanceException 301 { 302 return (Status.kInstanceRunningCode == 303 getInstanceStatus()); 304 } 305 306 310 public boolean isInstanceFailed() throws InstanceException 311 { 312 return (Status.kInstanceFailedCode == 313 getInstanceStatus()); 314 } 315 316 public boolean isInstanceNotRunning() throws InstanceException 317 { 318 return (Status.kInstanceNotRunningCode == 319 getInstanceStatus()); 320 } 321 322 public boolean isRestartNeeded() throws InstanceException 323 { 324 boolean isRestartNeeded = false; 325 try 326 { 327 isRestartNeeded = getRMIClient().isRestartNeeded(); 328 } 329 catch (Exception e) 330 { 331 throw new InstanceException(e.getMessage(), e); 332 } 333 return isRestartNeeded; 334 } 335 336 339 protected void preStart() throws InstanceException 340 { 341 final int state = getInstanceStatus(); 342 if (Status.kInstanceNotRunningCode != state) 343 { 344 throw new InstanceException( 345 getMessages().getCannotStartInstanceInvalidStateMessage( 346 getConfig().getDisplayName(), 347 Status.getStatusString(state))); 348 } 349 } 350 351 void postStart() throws InstanceException 352 { 353 if (isInstanceFailed()) { 354 int port = getConflictedPort(); 355 abortServer(); 356 throw new InstanceException( 357 getMessages().getStartupFailedMessage( 358 getConfig().getDisplayName(), port )); 359 } 360 if (!isInstanceRunning() && 361 !isInstanceNotRunning()) 362 { 363 367 try { 368 stopInstance(); 369 } catch (Exception e) { 370 throw new InstanceException( 371 getMessages().getStartInstanceTimeOutMessage( 372 getConfig().getDisplayName()), e); 373 } 374 throw new InstanceException( 375 getMessages().getStartInstanceTimeOutMessage( 376 getConfig().getDisplayName())); 377 } 378 if (isInstanceNotRunning()) { 379 throw new InstanceException( 380 getMessages().getStartupFailedMessage( 381 getConfig().getDisplayName())); 382 } 383 setRMIClient(null); 384 } 385 386 390 void preStop() throws InstanceException 391 { 392 final int state = getInstanceStatus(); 393 398 if (state != Status.kInstanceRunningCode) 401 { 402 throw new InstanceException( 403 getMessages().getCannotStopInstanceInvalidStateMessage( 404 getConfig().getDisplayName(), 405 Status.getStatusString(state))); 406 } 407 } 408 409 void postStop() throws InstanceException 410 { 411 if (!isInstanceNotRunning()) 412 { 413 throw new InstanceException( 414 getMessages().getCannotStopInstanceMessage( 415 getConfig().getDisplayName())); 416 } 417 setRMIClient(null); 418 } 419 420 421 protected void waitUntilStarting(ProcessExecutor processExec) throws InstanceException 424 { 425 waitUntilStarting(processExec, 180); 426 } 427 428 protected void waitUntilStarting(ProcessExecutor processExec, int timeoutSeconds) throws InstanceException 429 { 430 InstanceTimer timer = new InstanceTimer(timeoutSeconds, 0, 431 new TimerCallback() 432 { 433 public boolean check() throws Exception 434 { 435 return isInstanceStarting() || 436 isInstanceRunning() || 437 isInstanceFailed(); 438 } 439 } 440 ); 441 timer.run(); 443 444 if (getInstanceStatus() == Status.kInstanceNotRunningCode) 445 { 446 throw new InstanceException( 447 getMessages().getTimeoutStartingMessage( 448 getConfig().getDisplayName())); 449 } 450 } 451 452 protected void waitUntilStarted() throws InstanceException 455 { 456 waitUntilStarted(1200); 458 } 459 460 461 protected void waitUntilStarted(int timeoutSeconds) throws InstanceException 462 { 463 InstanceTimer timer = new InstanceTimer(timeoutSeconds, 0, 464 new TimerCallback() 465 { 466 public boolean check() throws Exception 467 { 468 return (isInstanceRunning() || 469 isInstanceFailed() || 470 isInstanceNotRunning()); 471 } 472 } 473 ); 474 timer.run(); } 476 477 void waitUntilStopped() throws InstanceException 478 { 479 final int timeOutSeconds = 60; 480 InstanceTimer timer = new InstanceTimer(timeOutSeconds, 0, 481 new TimerCallback() 482 { 483 public boolean check() throws Exception 484 { 485 return isInstanceNotRunning(); 486 } 487 } 488 ); 489 timer.run(); } 491 492 void execute(File script) throws InstanceException 493 { 494 try 495 { 496 ProcessExecutor exec = new ProcessExecutor( 497 new String [] {script.getAbsolutePath()}); 498 exec.execute(); 499 } 500 catch (Exception e) 501 { 502 throw new InstanceException(_strMgr.getString("procExecError"), e); 503 } 504 } 505 506 ProcessExecutor execute(String [] command) throws InstanceException { 507 return execute(command, null); 508 } 509 510 ProcessExecutor execute(String [] command, String [] interativeOptions) throws InstanceException 511 { 512 try 513 { 514 ProcessExecutor exec = new ProcessExecutor(command, interativeOptions); 515 String nativeLauncher=System.getProperty(SystemPropertyConstants.NATIVE_LAUNCHER); 516 if (nativeLauncher != null && nativeLauncher.equals("true")) { 517 exec.execute(false, false); 521 } else { 522 exec.execute(); 524 } 525 526 527 return exec; 530 } 531 catch (Exception e) 532 { 533 throw new InstanceException(_strMgr.getString("procExecError"), e); 534 } 535 } 536 537 protected PEFileLayout getFileLayout() 538 { 539 return super.getFileLayout(getConfig()); 540 } 541 542 545 int getConflictedPort() { 546 int port = 0; 547 try { 548 port = getRMIClient().getConflictedPort(); 549 } 550 catch (Exception ex) { 551 } 553 return port; 554 } 555 556 559 void abortServer() { 560 try { 561 getRMIClient().triggerServerExit(); 562 } 563 catch (Throwable t) { 564 } 567 } 568 569 public int getInstanceStatus() throws InstanceException 570 { 571 int status = Integer.MIN_VALUE; 572 try { 573 status = getRMIClient().getInstanceStatusCode(); 574 } 575 catch (Exception ex) { 576 583 } 586 return status; 587 } 588 589 private RMIClient rmiClient = null; 590 591 private RMIClient getRMIClient() 592 { 593 if (rmiClient == null) 594 { 595 String stubPath = getFileLayout().getStubFile().getAbsolutePath(); 596 String seedPath = getFileLayout().getSeedFile().getAbsolutePath(); 597 rmiClient = new RMIClient(false, stubPath, seedPath); 598 } 599 return rmiClient; 600 } 601 602 private void setRMIClient(RMIClient client) 603 { 604 this.rmiClient = client; 605 } 606 } 607 | Popular Tags |