1 23 24 package com.sun.enterprise.server; 25 26 import java.io.IOException ; 27 import java.io.File ; 28 import java.io.PrintStream ; 29 import java.io.FileOutputStream ; 30 import java.util.logging.Level ; 31 import java.util.Locale ; 32 import org.apache.catalina.LifecycleException; 33 import org.apache.catalina.core.StandardHost; 34 import com.sun.appserv.server.ServerLifecycle; 35 import com.sun.appserv.server.ServerLifecycleException; 36 import com.sun.enterprise.config.ConfigFactory; 37 import com.sun.enterprise.config.ConfigContext; 38 import com.sun.enterprise.config.ConfigException; 39 import com.sun.enterprise.web.WebContainer; 40 import java.util.logging.Logger ; 41 import com.sun.logging.LogDomains; 42 import com.sun.enterprise.server.logging.*; 43 import com.sun.enterprise.server.logging.stats.ErrorStatistics; 44 45 import com.sun.enterprise.admin.server.core.channel.RMIClient; 46 import com.sun.enterprise.admin.event.ShutdownEvent; 47 import com.sun.enterprise.server.Shutdown; 48 import com.sun.enterprise.admin.event.AdminEventListenerRegistry; 49 import com.sun.enterprise.admin.event.AdminEventResult; 50 51 import com.sun.enterprise.server.pluggable.PluggableFeatureFactory; 52 import com.sun.enterprise.server.pluggable.PluggableFeatureFactoryImpl; 53 54 import com.sun.enterprise.util.i18n.StringManager; 55 import com.sun.appserv.server.util.Version; 56 import com.sun.enterprise.admin.common.Status; 57 58 import com.sun.enterprise.util.ASenvPropertyReader; 59 import com.sun.enterprise.util.SystemPropertyConstants; 60 61 import com.sun.enterprise.launcher.PELaunchFilter; 62 63 import com.sun.enterprise.jms.JmsProviderLifecycle; 64 65 import com.sun.enterprise.security.store.IdentityManager; 66 import com.sun.enterprise.server.ss.ASLazyKernel; 67 import com.sun.enterprise.admin.server.core.channel.RRStateFactory; 68 69 70 import com.sun.enterprise.admin.server.core.jmx.SunoneInterceptor; 71 72 import com.sun.enterprise.server.ondemand.OnDemandServer; 73 74 import com.sun.enterprise.security.audit.AuditManager; 75 import com.sun.enterprise.security.audit.AuditManagerFactory; 76 77 80 81 public class PEMain { 82 83 static { 84 ErrorStatistics.registerStartupTime(); 86 } 87 88 90 93 public PEMain() { 94 _loader = getClass().getClassLoader(); 96 Thread.currentThread().setContextClassLoader(_loader); 97 AdminEventListenerRegistry.addShutdownEventListener(new Shutdown ()); 98 _instance = this; 99 } 100 101 103 public static boolean shutdownStarted = false; 104 105 public static boolean shutdownThreadInvoked = false; 106 107 private static ApplicationServer _server = null; 108 109 private final static String STOP = "stop"; 110 111 private static PEMain _instance = null; 112 113 private final static String SERVER_INSTANCE = 114 System.getProperty("com.sun.aas.instanceName"); 115 116 117 private static StringManager localStrings = 118 StringManager.getManager(PEMain.class); 119 120 ServerContext context = null; 121 122 private static Logger _logger = null; 126 127 private ClassLoader _loader = null; 128 129 private boolean isStarted = false; 130 131 private final static String instance_root = 132 System.getProperty("com.sun.aas.instanceRoot"); 133 134 private static AuditManager auditManager = 135 AuditManagerFactory.getAuditManagerInstance(); 136 137 private static Logger getLogger() { 141 if (_logger == null) { 142 _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER); 143 } 144 return _logger; 145 } 146 147 149 public static void main(String [] args) { 150 151 152 boolean verbose = false; 154 boolean dbg = false; 155 156 setSystemLocale(); 158 159 if(args[0].trim().equals(STOP)) { 160 if (isInstanceRunning()) { 162 PEMain.shutdown(); 163 return; 164 165 } else { 166 getLogger().log(Level.INFO, "instance.notRunning"); 167 return; 168 } 169 } 170 171 try { 172 173 String verboseMode=System.getProperty("com.sun.aas.verboseMode", "false"); 179 197 198 199 try { 201 if (IdentityManager.getUser() == null) { 203 IdentityManager.populateFromInputStream(); 204 } 205 } catch (IOException e) { 206 getLogger().log(Level.WARNING, "pemain.failureOnReadingSecurityIdentity", e); 207 } 208 if (getLogger().isLoggable(Level.FINE)) { 209 getLogger().log(Level.FINE, IdentityManager.getFormatedContents()); 210 } 211 212 213 214 ASenvPropertyReader reader = new ASenvPropertyReader( 217 System.getProperty(SystemPropertyConstants.CONFIG_ROOT_PROPERTY)); 218 reader.setSystemProperties(); 219 220 if (isInstanceAlreadyStarted()) { 222 getLogger().log(Level.SEVERE, "instance.alreadyRunning"); 223 System.exit(0); 224 } 225 226 getLogger().log(Level.FINE, "instance.start", SERVER_INSTANCE); 227 228 _server = new OnDemandServer(); 230 231 String cstr = localStrings.getStringWithDefault( "pemain.start", 233 "Sun Java System Application Server", 234 new String [] {Version.getFullVersion()}); 235 getLogger().log(Level.INFO, cstr); 236 237 String debugOptions = System.getProperty( 239 PELaunchFilter.DEBUG_OPTIONS); 240 if ( debugOptions != null && !debugOptions.equals("") ) { 241 String transport = PELaunchFilter.getDebugProperty( 242 debugOptions, "transport"); 243 String addr = PELaunchFilter.getDebugProperty( 244 debugOptions, "address"); 245 String str = localStrings.getStringWithDefault( 246 "pemain.debugger.message", 247 "Application server is listening at address " + addr + " for debugger to attach using transport " + transport, 248 new Object [] {addr, transport}); 249 250 System.err.println(str); 251 } 252 253 PEMain peMain = new PEMain(); 254 255 ShutdownThread shutdownThread = new ShutdownThread(); 257 Runtime runtime = Runtime.getRuntime(); 258 runtime.addShutdownHook(shutdownThread); 259 260 peMain.run(System.getProperty(Constants.IAS_ROOT)); 261 262 getLogger().log(Level.INFO, "pemain.startup.complete"); 263 264 if (auditManager.isAuditOn()){ 265 auditManager.serverStarted(); 266 } 267 268 } catch(Exception e) { 269 getLogger().log(Level.SEVERE, "pemain.error", e.getMessage()); 270 System.exit(1); 271 } 272 273 } 274 275 276 279 280 public static ApplicationServer getApplicationServer() { 281 return _server; 282 } 283 284 public synchronized static PEMain getInstance() { 285 return _instance; 286 } 287 288 291 public void run(String rootDir) { 292 293 try { 294 RRStateFactory.removeStateFile(); 295 } catch (Exception e) { 296 getLogger().log(Level.FINE, "Could not remove restart required state file", e); 297 } 298 299 try { 300 context = createServerContext(rootDir); 301 302 } catch (ConfigException ce) { 303 getLogger().log(Level.SEVERE, "j2eerunner.cannotCreateServerContext",ce); 304 } 305 306 307 new SystemOutandErrHandler(); 309 310 _server.setServerContext(context); 311 312 try { 314 316 _server.onInitialization(context); 317 318 if (getLogger().isLoggable(Level.FINE)) { 319 getLogger().log(Level.FINE, 320 "application.config_file" + 321 context.getServerConfigURL()); 322 getLogger().log(Level.FINE, 323 "application.default_locale" + 324 java.util.Locale.getDefault()); 325 } 326 _server.onStartup(); 327 328 _server.onReady(); 329 330 try { 333 SunoneInterceptor.getMBeanServerInstance().queryNames(null, null); 334 } catch (Exception ex) { 335 336 } 337 338 } catch (Exception ee) { 339 getLogger().log(Level.SEVERE, "j2eerunner.initError", ee); 340 getLogger().log(Level.SEVERE, "pemain.startup.failed"); 341 getLogger().log(Level.INFO, "shutdown.started"); 342 try { 343 _server.onShutdown(); 344 } catch (ServerLifecycleException e) { 345 getLogger().log(Level.SEVERE, "j2eerunner.initError", e); 346 } 347 try { 348 _server.onTermination(); 349 } catch (ServerLifecycleException e) { 350 getLogger().log(Level.SEVERE, "j2eerunner.initError", e); 351 } 352 getLogger().log(Level.SEVERE,"pemain.server.startup.failed.exit"); 353 System.exit(1); 354 } 355 synchronized ( this ) { 356 isStarted = true; 357 this.notifyAll(); 358 } 359 } 360 361 public boolean isStartingUp() { 362 return !isStarted; 363 } 364 365 366 368 369 373 public static void shutdown() { 374 try { 375 RMIClient rmiClient = new RMIClient( 376 true, 377 getStubFilePath(), 378 getSeedFilePath()); 379 380 ShutdownEvent shutdownEvent = new ShutdownEvent(SERVER_INSTANCE); 381 getLogger().log(Level.INFO, 382 "sending notification to server..." + 383 SERVER_INSTANCE); 384 AdminEventResult result = rmiClient.sendNotification(shutdownEvent); 385 getLogger().log(Level.INFO, 386 "server.shutdown_complete"); 387 388 } catch(Exception e) { 389 getLogger().log(Level.SEVERE, "j2eerunner.initError", e); 390 } 391 } 392 393 394 397 public static String getStubFilePath() { 398 399 return instance_root + 400 File.separatorChar + 401 "config" + 402 File.separatorChar + 403 "admch"; 404 } 405 406 409 public static String getSeedFilePath() { 410 411 return instance_root + 412 File.separatorChar + 413 "config" + File.separatorChar + 414 "admsn"; 415 } 416 417 420 private static int getInstanceStatus() { 421 String stubFile = getStubFilePath(); 422 String seedFile = getSeedFilePath(); 423 RMIClient rmiClient = new RMIClient(true, stubFile, seedFile); 424 return rmiClient.getInstanceStatusCode(); 425 } 426 427 430 private static boolean isInstanceRunning() { 431 return (getInstanceStatus() == Status.kInstanceRunningCode); 432 } 433 434 437 private static boolean isInstanceStartingOrRunning() { 438 int statusCode = getInstanceStatus(); 439 return (statusCode == Status.kInstanceStartingCode 440 || statusCode == Status.kInstanceRunningCode); 441 } 442 443 446 public static boolean isInstanceAlreadyStarted() { 447 return isInstanceStartingOrRunning(); 448 } 449 450 451 455 private ServerContext createServerContext(String rootDir) throws ConfigException { 456 457 String [] args = new String [0]; 459 ServerContextImpl context = new ServerContextImpl(); 460 461 context.setCmdLineArgs(args); 462 context.setInstallRoot(rootDir); 463 context.setInstanceName(SERVER_INSTANCE); 464 try{ 465 String serverXml = context.getServerConfigURL(); 466 ConfigContext cfgContext = 468 ConfigFactory.createConfigContext(serverXml, 469 true, 470 false, 471 true); 472 context.setConfigContext(cfgContext); 473 } catch (Exception ex){ 474 475 if (!(ex instanceof ConfigException)){ 476 getLogger().log(Level.SEVERE,"j2eerunner.server_context_excp",ex); 477 if (_logger == null){ 478 System.err.println("Exception in creating server context"); 479 ex.printStackTrace(); 480 } 481 } 482 throw new ConfigException(ex.getMessage()); 483 } 484 485 PluggableFeatureFactory ff = PluggableFeatureFactoryImpl.getFactory(); 486 487 context.setPluggableFeatureFactory(ff); 488 return context; 489 } 490 491 492 503 static void setSystemLocale() { 504 String locale = 505 System.getProperty(SystemPropertyConstants.DEFAULT_LOCALE_PROPERTY); 506 if(locale != null && !"".equals(locale) ) { 507 try { 508 String [] tokens = locale.split("_",3); 509 switch(tokens.length) { 510 case 0: 511 break; 512 case 1: 513 Locale.setDefault(new Locale (tokens[0])); 514 break; 515 case 2: 516 Locale.setDefault(new Locale (tokens[0],tokens[1])); 517 break; 518 default: 519 Locale.setDefault(new Locale (tokens[0],tokens[1],tokens[2])); 520 break; 521 } 522 } catch(Exception e) { 523 getLogger().log(Level.WARNING, "locale.setdefault.error",locale); 524 } 525 } 526 } 527 528 private static class ShutdownThread extends Thread { 529 530 public ShutdownThread() { 531 } 532 533 public void run() { 534 shutdownThreadInvoked = true; 535 if (PEMain.shutdownStarted == false) { 536 PEMain.shutdown(); 537 } 538 } 539 540 } 541 542 } 543 | Popular Tags |