1 29 30 package com.caucho.server.webapp; 31 32 import com.caucho.config.types.PathBuilder; 33 import com.caucho.log.Log; 34 import com.caucho.management.j2ee.J2EEManagedObject; 35 import com.caucho.management.j2ee.WebModule; 36 import com.caucho.server.deploy.DeployConfig; 37 import com.caucho.server.deploy.DeployControllerAdmin; 38 import com.caucho.server.deploy.EnvironmentDeployController; 39 import com.caucho.server.host.Host; 40 import com.caucho.server.util.CauchoSystem; 41 import com.caucho.util.L10N; 42 import com.caucho.vfs.Path; 43 44 import javax.servlet.jsp.el.ELException ; 45 import java.io.IOException ; 46 import java.util.ArrayList ; 47 import java.util.Map ; 48 import java.util.logging.Logger ; 49 import java.util.regex.Matcher ; 50 import java.util.regex.Pattern ; 51 54 public class WebAppController 55 extends EnvironmentDeployController<WebApp,WebAppConfig> { 56 private static final L10N L = new L10N(WebAppController.class); 57 private static final Logger log = Log.open(WebAppController.class); 58 59 private WebAppContainer _container; 60 61 private WebAppController _parent; 62 63 private String _contextPath; 65 66 private String _warName; 67 68 private ArrayList <String > _regexpValues; 70 71 private boolean _isInheritSession; 72 private boolean _isDynamicDeploy; 73 74 private ArrayList <Path> _dependPathList = new ArrayList <Path>(); 75 76 private String _sourceType = "unknown"; 77 78 private final Object _statisticsLock = new Object (); 79 80 private volatile long _lifetimeConnectionCount; 81 private volatile long _lifetimeConnectionTime; 82 private volatile long _lifetimeReadBytes; 83 private volatile long _lifetimeWriteBytes; 84 private volatile long _lifetimeClientDisconnectCount; 85 86 private WebAppAdmin _admin = new WebAppAdmin(this); 87 88 public WebAppController() 89 { 90 this("/", null, null); 91 } 92 93 public WebAppController(String contextPath, 94 Path rootDirectory, 95 WebAppContainer container) 96 { 97 super(contextPath, rootDirectory); 98 99 _container = container; 100 101 setContextPath(contextPath); 102 103 getVariableMap().put("app", new Var()); 104 getVariableMap().put("webApp", new Var()); 105 } 106 107 110 public String getContextPath() 111 { 112 return _contextPath; 113 } 114 115 118 public void setContextPath(String contextPath) 119 { 120 if (! contextPath.equals("") && ! contextPath.startsWith("/")) 121 contextPath = "/" + contextPath; 122 123 if (contextPath.endsWith("/")) 124 contextPath = contextPath.substring(0, contextPath.length() - 1); 125 126 _contextPath = contextPath; 127 } 128 129 132 public String getContextPath(String uri) 133 { 134 if (getConfig() == null || getConfig().getURLRegexp() == null) 135 return getContextPath(); 136 137 Pattern regexp = getConfig().getURLRegexp(); 138 Matcher matcher = regexp.matcher(uri); 139 140 int tail = 0; 141 while (tail >= 0 && tail <= uri.length()) { 142 String prefix = uri.substring(0, tail); 143 144 matcher.reset(prefix); 145 146 if (matcher.find() && matcher.start() == 0) 147 return matcher.group(); 148 149 if (tail < uri.length()) { 150 tail = uri.indexOf('/', tail + 1); 151 if (tail < 0) 152 tail = uri.length(); 153 } 154 else 155 break; 156 } 157 158 return _contextPath; 159 } 160 161 164 public void setWarName(String warName) 165 { 166 _warName = warName; 167 } 168 169 172 public String getWarName() 173 { 174 return _warName; 175 } 176 177 180 public String getURL() 181 { 182 if (_container != null) 183 return _container.getURL() + _contextPath; 184 else 185 return _contextPath; 186 } 187 188 191 public WebAppController getParent() 192 { 193 return _parent; 194 } 195 196 199 public WebAppContainer getContainer() 200 { 201 return _container; 202 } 203 204 207 public void setParentWebApp(WebAppController parent) 208 { 209 _parent = parent; 210 } 211 212 215 public Host getHost() 216 { 217 if (_container != null) 218 return _container.getHost(); 219 else 220 return null; 221 } 222 223 226 public String getSourceType() 227 { 228 return _sourceType; 229 } 230 231 234 public void setSourceType(String type) 235 { 236 _sourceType = type; 237 } 238 239 242 public void setRegexpValues(ArrayList <String > values) 243 { 244 _regexpValues = values; 245 } 246 247 250 public boolean isInheritSession() 251 { 252 return _isInheritSession; 253 } 254 255 258 public void setInheritSession(boolean inheritSession) 259 { 260 _isInheritSession = inheritSession; 261 } 262 263 266 public WebApp getWebApp() 267 { 268 return getDeployInstance(); 269 } 270 271 274 public void setDynamicDeploy(boolean isDynamicDeploy) 275 { 276 _isDynamicDeploy = isDynamicDeploy; 277 } 278 279 282 public boolean isDynamicDeploy() 283 { 284 return _isDynamicDeploy; 285 } 286 287 protected String getMBeanTypeName() 288 { 289 return "WebApp"; 290 } 291 292 protected String getMBeanId() 293 { 294 String name = _contextPath; 295 if (_contextPath.equals("")) 296 name = "/"; 297 298 return name; 299 } 300 301 304 @Override 305 protected DeployControllerAdmin getDeployAdmin() 306 { 307 return _admin; 308 } 309 310 @Override 311 protected void initEnd() 312 { 313 super.initEnd(); 314 315 J2EEManagedObject.register(new WebModule(this)); 316 } 317 318 321 public WebAppAdmin getAdmin() 322 { 323 return _admin; 324 } 325 326 329 public boolean isNameMatch(String url) 330 { 331 if (CauchoSystem.isCaseInsensitive()) 332 return url.equalsIgnoreCase(_contextPath); 333 else 334 return url.equals(_contextPath); 335 } 336 337 340 protected WebAppController merge(WebAppController newController) 341 { 342 if (getConfig() != null && getConfig().getURLRegexp() != null) 343 return newController; 344 else if (newController.getConfig() != null && 345 newController.getConfig().getURLRegexp() != null) 346 return this; 347 else { 348 Thread thread = Thread.currentThread(); 349 ClassLoader oldLoader = thread.getContextClassLoader(); 350 351 try { 352 thread.setContextClassLoader(getParentClassLoader()); 353 354 WebAppController mergedController 356 = new WebAppController(getContextPath(), 357 getRootDirectory(), 358 _container); 359 360 mergedController.mergeController(this); 363 mergedController.mergeController(newController); 364 365 return mergedController; 366 } finally { 367 thread.setContextClassLoader(oldLoader); 368 } 369 } 370 } 371 372 375 public boolean destroy() 376 { 377 if (! super.destroy()) 378 return false; 379 380 if (_container != null) 381 _container.removeWebApp(this); 382 383 return true; 384 } 385 386 389 protected void protectedWebApp() 390 throws Exception 391 { 392 Path root = getRootDirectory(); 393 root.lookup("WEB-INF").chmod(0750); 395 root.lookup("META-INF").chmod(0750); 396 } 397 398 401 protected void addDependencies() 402 throws Exception 403 { 404 } 405 406 409 public void addDepend(Path path) 410 { 411 _dependPathList.add(path); 412 } 413 414 417 protected void initBegin() 418 { 419 getVariableMap().put("app-dir", getRootDirectory()); 420 421 super.initBegin(); 422 } 423 424 protected void fillInitList(ArrayList <DeployConfig> initList) 425 { 426 if (_container != null) { 427 for (WebAppConfig config : _container.getWebAppDefaultList()) 428 initList.add(config); 429 } 430 431 super.fillInitList(initList); 432 } 433 434 437 protected WebApp instantiateDeployInstance() 438 { 439 return new Application(this); 440 } 441 442 445 protected void configureInstanceVariables(WebApp app) 446 throws Throwable 447 { 448 app.setRegexp(_regexpValues); 449 app.setDynamicDeploy(isDynamicDeploy()); 450 451 super.configureInstanceVariables(app); 452 } 453 454 protected void extendJMXContext(Map <String ,String > context) 455 { 456 context.put("WebApp", getMBeanId()); 457 } 458 459 protected Path calculateRootDirectory() 460 throws ELException 461 { 462 Path appDir = null; 463 464 if (appDir == null && getConfig() != null) { 465 String path = getConfig().getRootDirectory(); 466 467 if (path != null) 468 appDir = PathBuilder.lookupPath(path); 469 } 470 471 if (appDir == null && _container != null) 472 appDir = _container.getDocumentDirectory().lookup("./" + _contextPath); 473 474 if (appDir == null && getDeployInstance() != null) 475 appDir = getDeployInstance().getAppDir(); 476 477 return appDir; 478 } 479 480 483 protected void removeExpandFile(Path path, String relPath) 484 throws IOException 485 { 486 if (relPath.equals("./WEB-INF/resin-web.xml")) 487 return; 488 489 super.removeExpandFile(path, relPath); 490 } 491 492 public long getLifetimeConnectionCount() 493 { 494 synchronized (_statisticsLock) { 495 return _lifetimeConnectionCount; 496 } 497 } 498 499 public long getLifetimeConnectionTime() 500 { 501 synchronized (_statisticsLock) { 502 return _lifetimeConnectionTime; 503 } 504 } 505 506 public long getLifetimeReadBytes() 507 { 508 synchronized (_statisticsLock) { 509 return _lifetimeReadBytes; 510 } 511 } 512 513 public long getLifetimeWriteBytes() 514 { 515 synchronized (_statisticsLock) { 516 return _lifetimeWriteBytes; 517 } 518 } 519 520 public long getLifetimeClientDisconnectCount() 521 { 522 synchronized (_statisticsLock) { 523 return _lifetimeClientDisconnectCount; 524 } 525 } 526 527 535 public void updateStatistics(long milliseconds, 536 int readBytes, 537 int writeBytes, 538 boolean isClientDisconnect) 539 { 540 synchronized (_statisticsLock) { 541 _lifetimeConnectionCount++; 542 _lifetimeConnectionTime += milliseconds; 543 _lifetimeReadBytes += readBytes; 544 _lifetimeWriteBytes += writeBytes; 545 if (isClientDisconnect) 546 _lifetimeClientDisconnectCount++; 547 } 548 } 549 552 public String toString() 553 { 554 return "WebAppController$" + System.identityHashCode(this) + "[" + getId() + "]"; 555 } 556 557 560 public class Var { 561 public String getUrl() 562 { 563 return WebAppController.this.getURL(); 564 } 565 566 public String getId() 567 { 568 String id = WebAppController.this.getId(); 569 570 if (id != null) 571 return id; 572 else 573 return WebAppController.this.getContextPath(); 574 } 575 576 public String getName() 577 { 578 if (getWarName() != null) 579 return getWarName(); 580 else 581 return getId(); 582 } 583 584 public Path getAppDir() 585 { 586 return WebAppController.this.getRootDirectory(); 587 } 588 589 public Path getDocDir() 590 { 591 return WebAppController.this.getRootDirectory(); 592 } 593 594 public Path getRoot() 595 { 596 return WebAppController.this.getRootDirectory(); 597 } 598 599 public Path getRootDir() 600 { 601 return WebAppController.this.getRootDirectory(); 602 } 603 604 public String getContextPath() 605 { 606 return WebAppController.this.getContextPath(); 607 } 608 609 public ArrayList <String > getRegexp() 610 { 611 return _regexpValues; 612 } 613 614 public String toString() 615 { 616 return "WebApp[" + getURL() + "]"; 617 } 618 } 619 } 620 | Popular Tags |