1 29 30 package com.caucho.server.host; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.config.SchemaBean; 34 import com.caucho.lifecycle.Lifecycle; 35 import com.caucho.loader.EnvironmentBean; 36 import com.caucho.loader.EnvironmentClassLoader; 37 import com.caucho.loader.EnvironmentLocal; 38 import com.caucho.make.AlwaysModified; 39 import com.caucho.management.server.HostMXBean; 40 import com.caucho.server.cluster.Cluster; 41 import com.caucho.server.cluster.Server; 42 import com.caucho.server.deploy.EnvironmentDeployInstance; 43 import com.caucho.server.dispatch.DispatchServer; 44 import com.caucho.server.dispatch.ExceptionFilterChain; 45 import com.caucho.server.dispatch.Invocation; 46 import com.caucho.server.port.Port; 47 import com.caucho.server.webapp.WebAppContainer; 48 import com.caucho.util.L10N; 49 import com.caucho.vfs.Dependency; 50 import com.caucho.vfs.Path; 51 52 import java.util.ArrayList ; 53 import java.util.logging.Level ; 54 import java.util.logging.Logger ; 55 56 59 public class Host extends WebAppContainer 60 implements EnvironmentBean, Dependency, SchemaBean, 61 EnvironmentDeployInstance 62 { 63 private static final Logger log = Logger.getLogger(Host.class.getName()); 64 private static final L10N L = new L10N(Host.class); 65 66 private static EnvironmentLocal<Host> _hostLocal 67 = new EnvironmentLocal<Host>("caucho.host"); 68 69 private HostContainer _parent; 70 71 private HostController _controller; 73 74 private String _hostName = ""; 76 private String _url; 78 79 private String _serverName = ""; 80 private int _serverPort = 0; 81 82 private String _secureHostName; 84 85 private boolean _isDefaultHost; 86 87 private ArrayList <String > _aliasList = new ArrayList <String >(); 89 90 private Throwable _configException; 91 92 private boolean _isRootDirSet; 93 private boolean _isDocDirSet; 94 95 private final Lifecycle _lifecycle; 96 97 private String _configETag = null; 98 99 102 public Host(HostContainer parent, HostController controller, String hostName) 103 { 104 super(new EnvironmentClassLoader()); 105 106 try { 107 _controller = controller; 108 109 setParent(parent); 110 setHostName(hostName); 111 112 _hostLocal.set(this, getClassLoader()); 113 } catch (Throwable e) { 114 _configException = e; 115 } finally { 116 _lifecycle = new Lifecycle(log, toString(), Level.INFO); 117 } 118 } 119 120 123 public static Host getLocal() 124 { 125 return _hostLocal.get(); 126 } 127 128 131 private void setHostName(String name) 132 throws ConfigException 133 { 134 _hostName = name; 135 136 if (name.equals("")) 137 _isDefaultHost = true; 138 139 addHostAlias(name); 140 141 getEnvironmentClassLoader().setId("host:" + name); 142 143 145 int p = name.indexOf("://"); 146 147 if (p >= 0) 148 name = name.substring(p + 3); 149 150 _serverName = name; 151 152 p = name.lastIndexOf(':'); 153 if (p > 0) { 154 _serverName = name.substring(0, p); 155 156 boolean isPort = true; 157 int port = 0; 158 for (p++; p < name.length(); p++) { 159 char ch = name.charAt(p); 160 161 if ('0' <= ch && ch <= '9') 162 port = 10 * port + ch - '0'; 163 else 164 isPort = false; 165 } 166 167 if (isPort) 168 _serverPort = port; 169 } 170 } 171 172 175 public String getName() 176 { 177 return _controller.getName(); 178 } 179 180 184 public String getHostName() 185 { 186 return _hostName; 187 } 188 189 192 public Host getHost() 193 { 194 return this; 195 } 196 197 200 public String getSecureHostName() 201 { 202 return _secureHostName; 203 } 204 205 208 public void setSecureHostName(String secureHostName) 209 { 210 _secureHostName = secureHostName; 211 } 212 213 216 public String getSchema() 217 { 218 return "com/caucho/server/host/host.rnc"; 219 } 220 221 224 public String getURL() 225 { 226 if (_url != null) 227 return _url; 228 else if (_hostName == null || _hostName.equals("")) { 229 Server server = getServer(); 230 231 if (server == null) 232 return ""; 233 234 for (Port port : server.getPorts()) { 235 if ("http".equals(port.getProtocolName())) { 236 String address = port.getAddress(); 237 if (address == null) 238 address = "localhost"; 239 240 return "http://" + address + ":" + port.getPort(); 241 } 242 } 243 244 for (Port port : server.getPorts()) { 245 if ("https".equals(port.getProtocolName())) { 246 String address = port.getAddress(); 247 if (address == null) 248 address = "localhost"; 249 250 return "https://" + address + ":" + port.getPort(); 251 } 252 } 253 254 return ""; 255 } 256 else if (_hostName.startsWith("http:") || 257 _hostName.startsWith("https:")) 258 return _hostName; 259 else 260 return "http://" + _hostName; 261 } 262 263 266 public void addHostAlias(String name) 267 { 268 name = name.toLowerCase(); 269 270 if (! _aliasList.contains(name)) 271 _aliasList.add(name); 272 273 if (name.equals("") || name.equals("*")) 274 _isDefaultHost = true; 275 276 277 _controller.addExtHostAlias(name); 278 } 279 280 283 public ArrayList <String > getAliasList() 284 { 285 return _aliasList; 286 } 287 288 291 public boolean isDefaultHost() 292 { 293 return _isDefaultHost; 294 } 295 296 299 private void setParent(HostContainer parent) 300 { 301 _parent = parent; 302 303 setDispatchServer(parent.getDispatchServer()); 304 305 if (! _isRootDirSet) { 306 setRootDirectory(parent.getRootDirectory()); 307 _isRootDirSet = false; 308 } 309 } 310 311 314 public EnvironmentClassLoader getEnvironmentClassLoader() 315 { 316 return (EnvironmentClassLoader) getClassLoader(); 317 } 318 319 322 public void setRootDirectory(Path rootDir) 323 { 324 super.setRootDirectory(rootDir); 325 _isRootDirSet = true; 326 327 if (! _isDocDirSet) { 328 setDocumentDirectory(rootDir); 329 _isDocDirSet = false; 330 } 331 } 332 333 336 public void setDocumentDirectory(Path docDir) 337 { 338 super.setDocumentDirectory(docDir); 339 _isDocDirSet = true; 340 } 341 342 345 public void setConfigException(Throwable e) 346 { 347 if (e != null) { 348 _configException = e; 350 getEnvironmentClassLoader().addDependency(AlwaysModified.create()); 351 } 352 } 353 354 357 public Throwable getConfigException() 358 { 359 return _configException; 360 } 361 362 365 public Server getServer() 366 { 367 if (_parent != null) { 368 DispatchServer server = _parent.getDispatchServer(); 369 370 if (server instanceof Server) 371 return (Server) server; 372 } 373 374 return null; 375 } 376 377 380 public Cluster getCluster() 381 { 382 Server server = getServer(); 383 384 if (server != null) 385 return server.getCluster(); 386 else 387 return null; 388 } 389 390 393 public String getConfigETag() 394 { 395 return _configETag; 396 } 397 398 401 public void setConfigETag(String etag) 402 { 403 _configETag = etag; 404 } 405 406 409 public HostMXBean getAdmin() 410 { 411 return _controller.getAdmin(); 412 } 413 414 417 public void start() 418 { 419 if (! _lifecycle.toStarting()) 420 return; 421 422 if (getURL().equals("") && _parent != null) { 423 _url = _parent.getURL(); 424 } 425 426 EnvironmentClassLoader loader; 427 loader = getEnvironmentClassLoader(); 428 429 loader.setId("host:" + getURL()); 430 431 Thread thread = Thread.currentThread(); 432 ClassLoader oldLoader = thread.getContextClassLoader(); 433 434 try { 435 thread.setContextClassLoader(loader); 436 437 super.start(); 438 439 loader.start(); 440 441 if (_parent != null) 442 _parent.clearCache(); 443 } finally { 444 _lifecycle.toActive(); 445 446 thread.setContextClassLoader(oldLoader); 447 } 448 } 449 450 453 public void clearCache() 454 { 455 super.clearCache(); 456 457 setConfigETag(null); 458 } 459 460 463 public void buildInvocation(Invocation invocation) 464 throws Exception 465 { 466 invocation.setHostName(_serverName); 467 invocation.setPort(_serverPort); 468 469 Thread thread = Thread.currentThread(); 470 ClassLoader oldLoader = thread.getContextClassLoader(); 471 472 try { 473 thread.setContextClassLoader(getClassLoader()); 474 475 if (_configException == null) 476 super.buildInvocation(invocation); 477 else { 478 invocation.setFilterChain(new ExceptionFilterChain(_configException)); 479 invocation.setDependency(AlwaysModified.create()); 480 } 481 } finally { 482 thread.setContextClassLoader(oldLoader); 483 } 484 } 485 486 489 public boolean isModified() 490 { 491 return (isDestroyed() || getEnvironmentClassLoader().isModified()); 492 } 493 494 497 public boolean isModifiedNow() 498 { 499 return (isDestroyed() || getEnvironmentClassLoader().isModifiedNow()); 500 } 501 502 505 public boolean isDeployError() 506 { 507 return _configException != null; 508 } 509 510 513 public boolean isDeployIdle() 514 { 515 return false; 516 } 517 518 521 public boolean stop() 522 { 523 Thread thread = Thread.currentThread(); 524 ClassLoader oldLoader = thread.getContextClassLoader(); 525 526 try { 527 EnvironmentClassLoader envLoader = getEnvironmentClassLoader(); 528 thread.setContextClassLoader(envLoader); 529 530 if (! _lifecycle.toStopping()) 531 return false; 532 533 super.stop(); 534 535 envLoader.stop(); 536 537 return true; 538 } finally { 539 _lifecycle.toStop(); 540 541 thread.setContextClassLoader(oldLoader); 542 } 543 } 544 545 548 public void destroy() 549 { 550 stop(); 551 552 if (isDestroyed()) 553 return; 554 555 Thread thread = Thread.currentThread(); 556 ClassLoader oldLoader = thread.getContextClassLoader(); 557 EnvironmentClassLoader classLoader = getEnvironmentClassLoader(); 558 559 thread.setContextClassLoader(classLoader); 560 561 try { 562 super.destroy(); 563 } finally { 564 thread.setContextClassLoader(oldLoader); 565 566 classLoader.destroy(); 567 } 568 } 569 570 public String toString() 571 { 572 return "Host[" + getHostName() + "]"; 573 } 574 } 575 | Popular Tags |