1 29 30 package com.caucho.server.host; 31 32 import com.caucho.lifecycle.Lifecycle; 33 import com.caucho.loader.EnvironmentClassLoader; 34 import com.caucho.log.Log; 35 import com.caucho.make.AlwaysModified; 36 import com.caucho.server.cluster.Server; 37 import com.caucho.server.deploy.DeployContainer; 38 import com.caucho.server.dispatch.DispatchBuilder; 39 import com.caucho.server.dispatch.DispatchServer; 40 import com.caucho.server.dispatch.ErrorFilterChain; 41 import com.caucho.server.dispatch.Invocation; 42 import com.caucho.server.dispatch.ServletMapping; 43 import com.caucho.server.e_app.EarConfig; 44 import com.caucho.server.webapp.RewriteInvocation; 45 import com.caucho.server.webapp.WebApp; 46 import com.caucho.server.webapp.WebAppConfig; 47 import com.caucho.util.L10N; 48 import com.caucho.vfs.Path; 49 import com.caucho.vfs.Vfs; 50 51 import javax.servlet.FilterChain ; 52 import java.util.ArrayList ; 53 import java.util.HashMap ; 54 import java.util.logging.Level ; 55 import java.util.logging.Logger ; 56 57 60 public class HostContainer implements DispatchBuilder { 61 static final Logger log = Log.open(HostContainer.class); 62 static final L10N L = new L10N(HostContainer.class); 63 64 private EnvironmentClassLoader _classLoader; 66 67 private DispatchServer _dispatchServer; 68 69 private WebApp _errorWebApp; 70 71 private String _url = ""; 72 73 private Path _rootDir; 75 76 private RewriteInvocation _rewriteInvocation; 78 79 private ArrayList <HostConfig> _hostDefaultList = new ArrayList <HostConfig>(); 81 82 private DeployContainer<HostController> _hostDeploy 84 = new DeployContainer<HostController>(); 85 86 private HashMap <String ,HostController> _hostMap 88 = new HashMap <String ,HostController>(); 89 90 private ArrayList <HostConfig> _hostRegexpList = new ArrayList <HostConfig>(); 92 93 private ArrayList <WebAppConfig> _webAppDefaultList 95 = new ArrayList <WebAppConfig>(); 96 97 private ArrayList <EarConfig> _earDefaultList 99 = new ArrayList <EarConfig>(); 100 101 private Throwable _configException; 103 104 private final Lifecycle _lifecycle = new Lifecycle(); 106 107 110 public HostContainer() 111 { 112 _classLoader = new EnvironmentClassLoader(); 113 114 _rootDir = Vfs.lookup(); 115 } 116 117 120 public ClassLoader getClassLoader() 121 { 122 return _classLoader; 123 } 124 125 128 public void setClassLoader(EnvironmentClassLoader classLoader) 129 { 130 _classLoader = classLoader; 131 } 132 133 136 public void setURL(String url) 137 { 138 _url = url; 139 } 140 141 144 public String getURL() 145 { 146 return _url; 147 } 148 149 152 public void setDispatchServer(DispatchServer server) 153 { 154 _dispatchServer = server; 155 } 156 157 160 public DispatchServer getDispatchServer() 161 { 162 return _dispatchServer; 163 } 164 165 168 public Path getRootDirectory() 169 { 170 return _rootDir; 171 } 172 173 176 public void setRootDirectory(Path path) 177 { 178 _rootDir = path; 179 } 180 181 185 public void setRootDir(Path path) 186 { 187 setRootDirectory(path); 188 } 189 190 193 public void addHostDefault(HostConfig init) 194 { 195 _hostDefaultList.add(init); 196 } 197 198 201 public ArrayList <HostConfig> getHostDefaultList() 202 { 203 return _hostDefaultList; 204 } 205 206 209 public HostExpandDeployGenerator createHostDeploy() 210 { 211 return new HostExpandDeployGenerator(_hostDeploy, this); 212 } 213 214 217 public void addHostDeploy(HostExpandDeployGenerator hostDeploy) 218 { 219 _hostDeploy.add(hostDeploy); 220 } 221 222 225 public void addHost(HostConfig hostConfig) 226 throws Exception 227 { 228 if (hostConfig.getRegexp() != null) { 229 _hostDeploy.add(new HostRegexpDeployGenerator(_hostDeploy, this, hostConfig)); 230 return; 231 } 232 233 HostSingleDeployGenerator deploy; 234 deploy = new HostSingleDeployGenerator(_hostDeploy, this, hostConfig); 235 236 _hostDeploy.add(deploy); 237 } 238 239 242 public void addWebAppDefault(WebAppConfig init) 243 { 244 _webAppDefaultList.add(init); 245 } 246 247 250 public ArrayList <WebAppConfig> getWebAppDefaultList() 251 { 252 return _webAppDefaultList; 253 } 254 255 258 public void addEarDefault(EarConfig init) 259 { 260 _earDefaultList.add(init); 261 } 262 263 266 public ArrayList <EarConfig> getEarDefaultList() 267 { 268 return _earDefaultList; 269 } 270 271 274 public RewriteInvocation createRewriteDispatch() 275 { 276 if (_rewriteInvocation == null) { 277 _rewriteInvocation = new RewriteInvocation(); 278 } 279 280 return _rewriteInvocation; 281 } 282 283 286 public void clearCache() 287 { 288 _hostMap.clear(); 289 _dispatchServer.clearCache(); 290 } 291 292 295 public void buildInvocation(Invocation invocation) 296 throws Throwable 297 { 298 String rawHost = invocation.getHost(); 299 int rawPort = invocation.getPort(); 300 301 String hostName; 302 303 if (rawHost == null) 304 hostName = ""; 305 else 306 hostName = DomainName.fromAscii(rawHost); 307 308 invocation.setHostName(hostName); 309 310 if (_rewriteInvocation != null) { 311 String url; 312 313 if (invocation.isSecure()) 314 url = "https://" + hostName + invocation.getURI(); 315 else 316 url = "http://" + hostName + invocation.getURI(); 317 318 FilterChain chain = _rewriteInvocation.map(url, invocation); 319 320 if (chain != null) { 321 Server server = (Server) _dispatchServer; 322 invocation.setWebApp(server.getErrorWebApp()); 323 invocation.setFilterChain(chain); 324 return; 325 } 326 } 327 328 Host host = getHost(hostName, rawPort); 329 330 if (host != null) { 331 host.buildInvocation(invocation); 332 } 333 else { 334 FilterChain chain = new ErrorFilterChain(404); 335 invocation.setFilterChain(chain); 336 invocation.setWebApp(getErrorWebApp()); 337 invocation.setDependency(AlwaysModified.create()); 338 } 339 } 340 341 public ArrayList <HostController> getHostList() 342 { 343 return _hostDeploy.getControllers(); 344 } 345 346 349 public Host getHost(String hostName, int port) 350 { 351 try { 352 HostController controller = findHost(hostName, port); 353 354 if (controller != null) 355 return controller.request(); 356 else 357 return null; 358 } catch (Throwable e) { 359 log.log(Level.WARNING, e.toString(), e); 360 361 return null; 362 } 363 } 364 365 368 private HostController findHost(String rawHost, int rawPort) 369 throws Exception 370 { 371 if (rawHost == null) 372 rawHost = ""; 373 374 int p = rawHost.indexOf(':'); 375 376 String shortHost = rawHost; 377 378 if (p > 0) 379 shortHost = rawHost.substring(0, p); 380 381 String fullHost = shortHost + ':' + rawPort; 382 383 HostController hostController = null; 384 385 synchronized (_hostMap) { 386 hostController = _hostMap.get(fullHost); 387 388 if (hostController != null && ! hostController.isDestroyed()) 389 return hostController; 390 } 391 392 if (hostController == null || hostController.isDestroyed()) 393 hostController = _hostMap.get(shortHost); 394 395 if (hostController == null || hostController.isDestroyed()) 396 hostController = findHostController(fullHost); 397 398 if (hostController == null || hostController.isDestroyed()) 399 hostController = findHostController(shortHost); 400 401 if (hostController == null || hostController.isDestroyed()) 402 hostController = findHostController(""); 403 404 synchronized (_hostMap) { 405 if (hostController != null && ! hostController.isDestroyed()) 406 _hostMap.put(fullHost, hostController); 407 else { 408 hostController = null; 409 _hostMap.remove(fullHost); 410 } 411 } 412 413 return hostController; 414 } 415 416 423 private HostController findHostController(String hostName) 424 throws Exception 425 { 426 return _hostDeploy.findController(hostName); 427 } 428 429 432 public WebApp getErrorWebApp() 433 { 434 if (_errorWebApp == null) { 435 Thread thread = Thread.currentThread(); 436 ClassLoader loader = thread.getContextClassLoader(); 437 try { 438 thread.setContextClassLoader(_classLoader); 439 440 _errorWebApp = new WebApp(); 441 _errorWebApp.setAppDir(getRootDirectory()); 442 com.caucho.server.dispatch.ServletMapping file; 443 file = new ServletMapping(); 444 file.addURLPattern("/"); 445 file.setServletName("resin-file"); 446 file.setServletClass("com.caucho.servlets.FileServlet"); 447 file.init(); 448 _errorWebApp.addServletMapping(file); 449 450 for (WebAppConfig config : _webAppDefaultList) { 451 try { 452 config.getBuilderProgram().configure(_errorWebApp); 453 } catch (Throwable e) { 454 log.log(Level.WARNING, e.toString(), e); 455 } 456 } 457 458 _errorWebApp.init(); 459 _errorWebApp.start(); 460 } catch (Throwable e) { 461 log.log(Level.WARNING, e.toString(), e); 462 } finally { 463 thread.setContextClassLoader(loader); 464 } 465 } 466 467 return _errorWebApp; 468 } 469 470 473 public void start() 474 { 475 if (! _lifecycle.toStarting()) 476 return; 477 478 _classLoader.start(); 479 480 _lifecycle.toActive(); 481 482 _hostDeploy.start(); 483 } 484 485 488 public void stop() 489 { 490 if (! _lifecycle.toStop()) 491 return; 492 493 _hostDeploy.stop(); 494 495 _classLoader.stop(); 496 } 497 498 501 public void destroy() 502 { 503 stop(); 504 505 if (! _lifecycle.toDestroy()) 506 return; 507 508 _hostDeploy.destroy(); 509 510 _classLoader.destroy(); 511 } 512 } 513 | Popular Tags |