1 29 30 package com.caucho.server.host; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.config.types.PathBuilder; 34 import com.caucho.el.EL; 35 import com.caucho.log.Log; 36 import com.caucho.management.server.HostMXBean; 37 import com.caucho.server.deploy.DeployController; 38 import com.caucho.server.deploy.DeployControllerAdmin; 39 import com.caucho.server.deploy.EnvironmentDeployController; 40 import com.caucho.server.e_app.EarConfig; 41 import com.caucho.server.webapp.WebAppConfig; 42 import com.caucho.util.L10N; 43 import com.caucho.vfs.Depend; 44 import com.caucho.vfs.Dependency; 45 import com.caucho.vfs.Path; 46 import com.caucho.vfs.Vfs; 47 48 import java.util.ArrayList ; 49 import java.util.HashMap ; 50 import java.util.Map ; 51 import java.util.logging.Level ; 52 import java.util.logging.Logger ; 53 import java.util.regex.Matcher ; 54 import java.util.regex.Pattern ; 55 56 59 public class HostController 60 extends EnvironmentDeployController<Host,HostConfig> 61 { 62 private static final Logger log = Log.open(HostController.class); 63 private static final L10N L = new L10N(HostController.class); 64 65 private HostContainer _container; 66 67 private String _hostName; 69 private String _regexpName; 71 72 private Pattern _regexp; 73 private String _rootDirectoryPattern; 74 75 private ArrayList <String > _entryHostAliases 77 = new ArrayList <String >(); 78 private ArrayList <Pattern > _entryHostAliasRegexps 79 = new ArrayList <Pattern >(); 80 81 private ArrayList <String > _hostAliases = new ArrayList <String >(); 82 83 private final Var _hostVar = new Var(); 85 private final HostAdmin _admin = new HostAdmin(this); 86 87 private ArrayList <Dependency> _dependList = new ArrayList <Dependency>(); 88 89 HostController(String id, 90 HostConfig config, 91 HostContainer container, 92 Map <String ,Object > varMap) 93 { 94 super(id, config); 95 96 setHostName(id); 97 98 if (varMap != null) 99 getVariableMap().putAll(varMap); 100 101 getVariableMap().put("host", _hostVar); 102 103 setContainer(container); 104 105 setRootDirectory(config.calculateRootDirectory(getVariableMap())); 106 } 107 108 HostController(String id, Path rootDirectory, HostContainer container) 109 { 110 super(id, rootDirectory); 111 112 addHostAlias(id); 113 setHostName(id); 114 115 getVariableMap().put("name", id); 116 getVariableMap().put("host", _hostVar); 117 118 setContainer(container); 119 } 120 121 public void setContainer(HostContainer container) 122 { 123 _container = container; 124 125 if (_container != null) { 126 for (HostConfig defaultConfig : _container.getHostDefaultList()) 127 addConfigDefault(defaultConfig); 128 } 129 } 130 131 134 public String getName() 135 { 136 String name = super.getId(); 137 138 if (name != null) 139 return name; 140 else 141 return getHostName(); 142 } 143 144 147 public String getHostName() 148 { 149 return _hostName; 150 } 151 152 155 public void setHostName(String name) 156 { 157 if (name != null) 158 name = name.trim(); 159 160 if (name == null || name.equals("*")) 161 name = ""; 162 163 name = name.toLowerCase(); 164 165 _hostName = name; 166 } 167 168 171 public void setRegexpName(String name) 172 { 173 _regexpName = name.toLowerCase(); 174 } 175 176 179 public void addHostAlias(String name) 180 { 181 if (name != null) 182 name = name.trim(); 183 184 if (name == null || name.equals("*")) 185 name = ""; 187 name = name.toLowerCase(); 188 189 if (! _entryHostAliases.contains(name)) 190 _entryHostAliases.add(name); 191 192 addExtHostAlias(name); 193 } 194 195 198 public void addExtHostAlias(String name) 199 { 200 if (! _hostAliases.contains(name)) 201 _hostAliases.add(name); 202 } 203 204 207 public ArrayList <String > getHostAliases() 208 { 209 return _hostAliases; 210 } 211 212 215 public void setRegexp(Pattern regexp) 216 { 217 _regexp = regexp; 218 } 219 220 223 public void setRootDirectoryPattern(String rootDirectoryPattern) 224 { 225 _rootDirectoryPattern = rootDirectoryPattern; 226 } 227 228 231 public void addDepend(Path depend) 232 { 233 if (! _dependList.contains(depend)) 234 _dependList.add(new Depend(depend)); 235 } 236 237 240 public HostMXBean getAdmin() 241 { 242 return _admin; 243 } 244 245 248 protected DeployControllerAdmin getDeployAdmin() 249 { 250 return _admin; 251 } 252 253 256 protected void initBegin() 257 { 258 try { 259 try { 260 if (getConfig() == null || getHostName() != null) { 261 } 262 else if (getConfig().getHostName() != null) 263 setHostName(EL.evalString(getConfig().getHostName(), 264 EL.getEnvironment())); 265 } catch (Exception e) { 266 log.log(Level.WARNING, e.toString(), e); 267 } 268 269 if (_regexpName != null && _hostName == null) 270 _hostName = _regexpName; 271 272 if (_hostName == null) 273 _hostName = ""; 274 275 ArrayList <String > aliases = null; 276 277 if (getConfig() != null) { 278 aliases = getConfig().getHostAliases(); 279 280 _entryHostAliasRegexps.addAll(getConfig().getHostAliasRegexps()); 281 } 282 283 for (int i = 0; aliases != null && i < aliases.size(); i++) { 284 String alias = aliases.get(i); 285 286 alias = EL.evalString(alias, EL.getEnvironment()); 287 288 addHostAlias(alias); 289 } 290 } catch (Exception e) { 291 log.log(Level.WARNING, e.toString(), e); 292 } 293 294 super.initBegin(); 295 } 296 297 300 protected String getMBeanId() 301 { 302 String name = _hostName; 303 304 if (name == null) 305 name = ""; 306 else if (name.indexOf(':') >= 0) 307 name = name.replace(':', '-'); 308 309 if (name.equals("")) 310 return "default"; 311 else 312 return name; 313 } 314 315 318 public boolean isNameMatch(String name) 319 { 320 if (_hostName.equalsIgnoreCase(name)) 321 return true; 322 323 for (int i = _hostAliases.size() - 1; i >= 0; i--) { 324 if (name.equalsIgnoreCase(_hostAliases.get(i))) 325 return true; 326 } 327 328 for (int i = _entryHostAliasRegexps.size() - 1; i >= 0; i--) { 329 Pattern alias = _entryHostAliasRegexps.get(i); 330 331 if (alias.matcher(name).find()) 332 return true; 333 } 334 335 if (_regexp != null) { 336 338 Matcher matcher = _regexp.matcher(name); 339 340 if (matcher.matches()) { 341 Path rootDirectory = calculateRoot(matcher); 342 343 if (getRootDirectory().equals(rootDirectory)) 344 return true; 345 } 346 } 347 348 return false; 349 } 350 351 private Path calculateRoot(Matcher matcher) 352 { 353 355 Thread thread = Thread.currentThread(); 356 ClassLoader oldLoader = thread.getContextClassLoader(); 357 358 try { 359 thread.setContextClassLoader(getParentClassLoader()); 360 361 if (_rootDirectoryPattern == null) { 362 return Vfs.lookup(); 364 } 365 366 int length = matcher.end() - matcher.start(); 367 368 ArrayList <String > vars = new ArrayList <String >(); 369 370 HashMap <String ,Object > varMap = new HashMap <String ,Object >(); 371 372 for (int j = 0; j <= matcher.groupCount(); j++) { 373 vars.add(matcher.group(j)); 374 varMap.put("host" + j, matcher.group(j)); 375 } 376 377 varMap.put("regexp", vars); 378 varMap.put("host", new TestVar(matcher.group(0), vars)); 379 380 Path path = PathBuilder.lookupPath(_rootDirectoryPattern, varMap); 381 382 return path; 383 } catch (Exception e) { 384 log.log(Level.FINE, e.toString(), e); 385 386 return Vfs.lookup(_rootDirectoryPattern); 388 } finally { 389 thread.setContextClassLoader(oldLoader); 390 } 391 } 392 393 396 protected HostController merge(HostController newController) 397 { 398 if (getConfig() != null && getConfig().getRegexp() != null) 399 return newController; 400 else if (newController.getConfig() != null && 401 newController.getConfig().getRegexp() != null) 402 return this; 403 else { 404 Thread thread = Thread.currentThread(); 405 ClassLoader oldLoader = thread.getContextClassLoader(); 406 407 try { 408 thread.setContextClassLoader(getParentClassLoader()); 409 410 HostController mergedController 411 = new HostController(newController.getHostName(), 412 getRootDirectory(), 413 _container); 414 415 mergedController.mergeController(this); 416 mergedController.mergeController(newController); 417 418 if (! getHostName().equals(newController.getHostName())) { 419 ConfigException e; 420 421 e = new ConfigException(L.l("Illegal merge of {0} and {1}. Both hosts have the same root-directory '{2}'.", 422 getHostName(), 423 newController.getHostName(), 424 getRootDirectory())); 425 426 log.log(Level.FINE, e.toString(), e); 427 428 mergedController.setConfigException(e); 429 } 430 431 return mergedController; 432 } catch (Throwable e) { 433 log.log(Level.FINE, e.toString(), e); 434 435 return null; 436 } finally { 437 thread.setContextClassLoader(oldLoader); 438 } 439 } 440 } 441 442 445 protected void mergeController(DeployController oldControllerV) 446 { 447 super.mergeController(oldControllerV); 448 449 HostController oldController = (HostController) oldControllerV; 450 451 _entryHostAliases.addAll(oldController._entryHostAliases); 452 if (! oldController.getHostName().equals("")) 453 _entryHostAliases.add(oldController.getHostName()); 454 _entryHostAliasRegexps.addAll(oldController._entryHostAliasRegexps); 455 _hostAliases.addAll(oldController._hostAliases); 456 457 if (_regexp == null) { 458 _regexp = oldController._regexp; 459 _rootDirectoryPattern = oldController._rootDirectoryPattern; 460 } 461 } 462 463 466 protected Host instantiateDeployInstance() 467 { 468 return new Host(_container, this, _hostName); 469 } 470 471 474 protected void configureInstance(Host host) 475 throws Throwable 476 { 477 _hostAliases.clear(); 478 _hostAliases.addAll(_entryHostAliases); 479 480 getVariableMap().put("host-root", getRootDirectory()); 481 482 if (_container != null) { 483 for (EarConfig config : _container.getEarDefaultList()) 484 host.addEarDefault(config); 485 486 for (WebAppConfig config : _container.getWebAppDefaultList()) 487 host.addWebAppDefault(config); 488 } 489 490 super.configureInstance(host); 491 } 492 493 protected void extendJMXContext(Map <String ,String > context) 494 { 495 context.put("Host", getMBeanId()); 496 } 497 498 501 public boolean equals(Object o) 502 { 503 if (! (o instanceof HostController)) 504 return false; 505 506 HostController entry = (HostController) o; 507 508 return _hostName.equals(entry._hostName); 509 } 510 511 514 public String toString() 515 { 516 return "HostController[" + getName() + "]"; 517 } 518 519 522 public class Var { 523 public String getName() 524 { 525 return HostController.this.getName(); 526 } 527 528 public String getHostName() 529 { 530 return HostController.this.getHostName(); 531 } 532 533 public String getUrl() 534 { 535 Host host = getDeployInstance(); 536 537 if (host != null) 538 return host.getURL(); 539 else if (_hostName.equals("")) 540 return ""; 541 else if (_hostName.startsWith("http:") || 542 _hostName.startsWith("https:")) 543 return _hostName; 544 else 545 return "http://" + _hostName; 546 } 547 548 public ArrayList getRegexp() 549 { 550 return (ArrayList ) getVariableMap().get("regexp"); 551 } 552 553 public Path getRoot() 554 { 555 Host host = getDeployInstance(); 556 557 if (host != null) 558 return host.getRootDirectory(); 559 else 560 return HostController.this.getRootDirectory(); 561 } 562 563 566 public Path getRootDir() 567 { 568 return getRoot(); 569 } 570 571 574 public Path getRootDirectory() 575 { 576 return getRoot(); 577 } 578 579 public Path getDocumentDirectory() 580 { 581 Host host = getDeployInstance(); 582 583 if (host != null) 584 return host.getDocumentDirectory(); 585 else 586 return null; 587 } 588 589 public Path getDocDir() 590 { 591 return getDocumentDirectory(); 592 } 593 594 public Path getWarDirectory() 595 { 596 Host host = getDeployInstance(); 597 598 if (host != null) 599 return host.getWarDir(); 600 else 601 return null; 602 } 603 604 public Path getWarDir() 605 { 606 return getWarDirectory(); 607 } 608 609 public Path getWarExpandDirectory() 610 { 611 Host host = getDeployInstance(); 612 613 if (host != null) 614 return host.getWarExpandDir(); 615 else 616 return null; 617 } 618 619 public Path getWarExpandDir() 620 { 621 return getWarExpandDirectory(); 622 } 623 624 public String toString() 625 { 626 return "Host[" + getId() + "]"; 627 } 628 } 629 630 633 public class TestVar { 634 private String _name; 635 private ArrayList <String > _regexp; 636 637 TestVar(String name, ArrayList <String > regexp) 638 { 639 _name = name; 640 _regexp = regexp; 641 } 642 643 public String getName() 644 { 645 return _name; 646 } 647 648 public String getHostName() 649 { 650 return _name; 651 } 652 653 public ArrayList <String > getRegexp() 654 { 655 return _regexp; 657 } 658 } 659 } 660 | Popular Tags |