1 21 22 package org.opensubsystems.core.application; 23 24 import java.io.IOException ; 25 import java.lang.reflect.Method ; 26 import java.net.BindException ; 27 import java.util.Iterator ; 28 import java.util.Properties ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 32 import org.mortbay.http.HttpServer; 33 import org.mortbay.http.SocketListener; 34 import org.mortbay.jetty.servlet.WebApplicationContext; 35 import org.mortbay.util.InetAddrPort; 36 import org.mortbay.util.MultiException; 37 import org.opensubsystems.core.error.OSSConfigException; 38 import org.opensubsystems.core.error.OSSException; 39 import org.opensubsystems.core.error.OSSInternalErrorException; 40 import org.opensubsystems.core.util.Config; 41 import org.opensubsystems.core.util.GlobalConstants; 42 import org.opensubsystems.core.util.Log; 43 import org.opensubsystems.core.util.ProductInfo; 44 import org.opensubsystems.core.www.WebCommonConstants; 45 import org.opensubsystems.core.www.WebUtils; 46 47 75 public abstract class Server 76 { 77 79 92 public static final String SERVER_START_RETRYCOUNT 93 = "oss.server.start.retrycount"; 94 95 97 103 public static final int SERVER_START_RETRYCOUNT_DEFAULT = 3; 104 105 107 110 protected int m_iWebServerPort; 111 112 115 protected HttpServer m_hsServer; 116 117 120 protected boolean m_bStarted; 121 122 124 127 private static Logger s_logger = Log.getInstance(Server.class); 128 129 136 protected static int s_iMaxServerStartRetry; 137 138 140 143 static 144 { 145 Properties prpSettings; 147 148 prpSettings = Config.getInstance().getPropertiesSafely(); 149 150 s_iMaxServerStartRetry = Config.getIntPropertyInRange( 151 prpSettings, 152 SERVER_START_RETRYCOUNT, 153 SERVER_START_RETRYCOUNT_DEFAULT, 154 "Number of retries to restart web" 155 + " server if used port is not available", 156 0, 159 Integer.MAX_VALUE); 160 } 161 162 170 public Server( 171 ProductInfo product 172 ) 173 { 174 m_iWebServerPort = WebUtils.getConfiguredPort(); 175 m_bStarted = false; 176 177 GlobalConstants.setCurrentProduct(product); 178 } 179 180 185 public int getWebServerPort( 186 ) 187 { 188 return m_iWebServerPort; 189 } 190 191 196 public void setWebServerPort( 197 int iWebServerPort 198 ) 199 { 200 if (GlobalConstants.ERROR_CHECKING) 201 { 202 if ((iWebServerPort < WebCommonConstants.HTTP_PORT_MIN) 203 || (iWebServerPort > WebCommonConstants.HTTP_PORT_MAX)) 204 { 205 throw new IllegalArgumentException ("New webserver port " + iWebServerPort 206 + " is outside of valid range (" 207 + WebCommonConstants.HTTP_PORT_MIN 208 + " - " 209 + WebCommonConstants.HTTP_PORT_MAX 210 + ")."); 211 } 212 } 213 m_iWebServerPort = iWebServerPort; 214 } 215 216 224 public void init( 225 ) throws OSSException, 226 IOException 227 { 228 m_hsServer = new HttpServer(); 230 231 InetAddrPort iaddrServerAdress; 233 SocketListener hlListener; 234 235 239 iaddrServerAdress = new InetAddrPort(m_iWebServerPort); 242 hlListener = new SocketListener(iaddrServerAdress); 243 hlListener.setMinThreads(80); 252 hlListener.setMaxThreads(200); 253 hlListener.setMaxIdleTimeMs(180000); 254 hlListener.setLowResourcePersistTimeMs(20000); 255 m_hsServer.addListener(hlListener); 256 257 259 addWebApplications(m_hsServer); 260 } 261 262 268 public void start( 269 ) throws MultiException, 270 OSSException 271 { 272 try 273 { 274 m_hsServer.start(); 275 m_bStarted = true; 276 s_logger.info("Server is ready on port " + getWebServerPort()); 277 } 278 catch (MultiException meExc) 279 { 280 throw meExc; 282 } 283 catch (Exception eExc) 284 { 285 MultiException meExc = new MultiException(); 286 meExc.add(eExc); 287 throw meExc; 288 } 289 } 290 291 296 public boolean isStarted( 297 ) 298 { 299 return m_bStarted; 300 } 301 302 308 public void stop( 309 ) throws OSSException, 310 InterruptedException 311 { 312 try 313 { 314 m_hsServer.stop(); 315 m_bStarted = false; 316 } 317 finally 318 { 319 m_hsServer.destroy(); 320 } 321 } 322 323 325 334 protected static void startServer( 335 final Server srvServer 336 ) throws OSSException 337 { 338 int iRetry = 0; 339 boolean bServerRunning = false; 340 int iNewWebServerPort; 341 Iterator itrExceptions; 342 Exception eTemp; 343 344 try 350 { 351 Method shutdownHook = Runtime .class.getMethod("addShutdownHook", 352 new Class [] {Thread .class}); 353 354 Thread thrdHook = new Thread () 355 { 356 public void run() 357 { 358 if (srvServer.isStarted()) 361 { 362 try 363 { 364 stopServer(srvServer); 365 } 366 catch (OSSException ossExc) 367 { 368 s_logger.log(Level.WARNING, 369 "Unexpected exception.", 370 ossExc); 371 } 372 373 try 376 { 377 Thread.sleep(1000); 378 } 379 catch (Throwable throwable) 380 { 381 s_logger.log(Level.WARNING, 382 "Unexpected exception.", 383 throwable); 384 } 385 } 386 } 387 }; 388 shutdownHook.invoke(Runtime.getRuntime(), new Object []{thrdHook}); 389 } 390 catch (Throwable throwable) 391 { 392 s_logger.log(Level.WARNING, "No shutdown hook in JVM.", throwable); 393 } 394 395 do 396 { 397 s_logger.info("Initializing server."); 398 try 399 { 400 srvServer.init(); 401 } 402 catch (IOException ioeExc) 403 { 404 throw new OSSInternalErrorException( 405 "Cannot initialize server. Please check your settings.", 406 ioeExc); 407 } 408 409 s_logger.info("Starting server."); 410 try 411 { 412 iRetry++; 413 srvServer.start(); 414 bServerRunning = true; 415 } 416 catch (MultiException meExc) 417 { 418 for (itrExceptions = meExc.getExceptions().iterator(); itrExceptions.hasNext();) 420 { 421 eTemp = (Exception )itrExceptions.next(); 422 if (eTemp instanceof BindException ) 423 { 424 if (iRetry < s_iMaxServerStartRetry) 426 { 427 iNewWebServerPort = generateNewWebPort(srvServer.getWebServerPort()); 428 s_logger.warning("Cannot start server on port " 429 + srvServer.getWebServerPort() 430 + ". Original message: " 431 + eTemp.getMessage() 432 + ". Server will try to start on new port " 433 + iNewWebServerPort + "."); 434 srvServer.setWebServerPort(iNewWebServerPort); 436 break; 438 } 439 else 440 { 441 throw new OSSConfigException( 442 "Cannot find port to start server even after " 443 + iRetry + " retries. Please check your settings."); 444 } 445 } 446 else 447 { 448 throw new OSSInternalErrorException( 449 "Cannot start server. Please check your settings.", 450 eTemp); 451 } 452 } 453 } 454 finally 455 { 456 if (!bServerRunning) 457 { 458 try 459 { 460 s_logger.log(Level.FINEST, "Stopping the unsuccessful server."); 462 srvServer.stop(); 463 } 464 catch (Throwable throwable) 465 { 466 s_logger.log(Level.WARNING, "Unexpected exception.", throwable); 467 } 468 } 469 } 470 } 471 while ((!bServerRunning) && (iRetry < s_iMaxServerStartRetry)); 472 } 473 474 480 protected static void stopServer( 481 Server srvServer 482 ) throws OSSException 483 { 484 s_logger.info("Server is stopping."); 485 try 486 { 487 srvServer.stop(); 488 } 489 catch (InterruptedException ieExc) 490 { 491 } 493 s_logger.info("Server is stopped."); 494 } 495 496 502 protected static int generateNewWebPort( 503 int iOldPort 504 ) 505 { 506 int iReturn; 507 508 if (iOldPort == WebCommonConstants.HTTP_PORT_DEFAULT) 509 { 510 iReturn = WebCommonConstants.HTTP_PORT_BACKUP; 512 } 513 else 514 { 515 iReturn = iOldPort + 1; 519 } 520 521 return iReturn; 522 } 523 524 529 protected abstract void addWebApplications( 530 HttpServer hsServer 531 ); 532 533 544 protected void initContext( 545 String strURLPrefix, 546 WebApplicationContext wacContext 547 ) 548 { 549 } 551 } 552 | Popular Tags |