1 22 package org.jboss.deployers.plugins.structure; 23 24 import java.io.IOException ; 25 import java.io.Serializable ; 26 import java.net.URI ; 27 import java.util.Collections ; 28 import java.util.List ; 29 import java.util.Set ; 30 import java.util.concurrent.CopyOnWriteArraySet ; 31 32 import org.jboss.deployers.plugins.attachments.AttachmentsImpl; 33 import org.jboss.deployers.spi.DeploymentException; 34 import org.jboss.deployers.spi.attachments.Attachments; 35 import org.jboss.deployers.spi.classloader.ClassLoaderFactory; 36 import org.jboss.deployers.spi.deployer.DeploymentUnit; 37 import org.jboss.deployers.spi.structure.DeploymentContext; 38 import org.jboss.deployers.spi.structure.DeploymentContextVisitor; 39 import org.jboss.deployers.spi.structure.DeploymentState; 40 import org.jboss.deployers.spi.structure.StructureDetermined; 41 import org.jboss.logging.Logger; 42 import org.jboss.virtual.VFSUtils; 43 import org.jboss.virtual.VirtualFile; 44 45 51 public class AbstractDeploymentContext 52 implements DeploymentContext, Serializable 53 { 54 private static final long serialVersionUID = 1; 55 56 57 protected Logger log = Logger.getLogger(getClass()); 58 59 60 private String name; 61 62 63 private StructureDetermined structureDetermined = StructureDetermined.NO; 64 65 66 private DeploymentState state; 67 68 69 private DeploymentUnit unit; 70 71 72 private VirtualFile root; 73 74 75 private VirtualFile metaDataLocation; 76 77 78 private List <VirtualFile> classPath; 79 80 81 private transient ClassLoader classLoader; 82 83 84 private transient ClassLoaderFactory classLoaderFactory; 85 86 87 private boolean candidate; 88 89 90 private boolean deployed; 91 92 93 private DeploymentContext parent; 94 95 96 private Set <DeploymentContext> children = new CopyOnWriteArraySet <DeploymentContext>(); 97 98 99 private Set <DeploymentContext> components = new CopyOnWriteArraySet <DeploymentContext>(); 100 101 102 private Attachments predeterminedManagedObjects = new AttachmentsImpl(); 103 104 105 private transient Attachments transientAttachments = new AttachmentsImpl(); 106 107 108 private transient Attachments transientManagedObjects = new AttachmentsImpl(); 109 110 111 private Throwable problem; 112 113 119 public static String getDeploymentName(VirtualFile file) 120 { 121 if (file == null) 122 throw new IllegalArgumentException ("Null file"); 123 try 124 { 125 URI uri = file.toURI(); 126 return uri.toString(); 127 } 128 catch (Exception e) 129 { 130 throw new IllegalArgumentException ("File does not have a valid uri: " + file, e); 131 } 132 } 133 134 140 public AbstractDeploymentContext(String name) 141 { 142 this(name, false); 143 } 144 145 152 public AbstractDeploymentContext(String name, boolean candidate) 153 { 154 if (name == null) 155 throw new IllegalArgumentException ("Null name"); 156 this.name = name; 157 this.candidate = candidate; 158 } 159 160 167 public AbstractDeploymentContext(String name, DeploymentContext parent) 168 { 169 this(name, false, parent); 170 } 171 172 180 public AbstractDeploymentContext(String name, boolean candidate, DeploymentContext parent) 181 { 182 this(name, candidate); 183 if (parent == null) 184 throw new IllegalArgumentException ("Null parent"); 185 setParent(parent); 186 } 187 188 194 public AbstractDeploymentContext(VirtualFile root) 195 { 196 this(getDeploymentName(root), false); 197 setRoot(root); 198 } 199 200 207 public AbstractDeploymentContext(VirtualFile root, boolean candidate) 208 { 209 this(getDeploymentName(root), candidate); 210 setRoot(root); 211 } 212 213 221 public AbstractDeploymentContext(VirtualFile root, boolean candidate, DeploymentContext parent) 222 { 223 this(getDeploymentName(root), candidate, parent); 224 setRoot(root); 225 } 226 227 public String getName() 228 { 229 return name; 230 } 231 232 243 public String getSimpleName() 244 { 245 VirtualFile unitVF = getRoot(); 246 return unitVF.getName(); 247 } 248 249 255 public String getRelativePath() 256 { 257 VirtualFile unitVF = getRoot(); 258 VirtualFile topVF = unitVF; 259 DeploymentContext ctx = getParent(); 260 while( ctx != null ) 261 { 262 topVF = ctx.getRoot(); 263 ctx = ctx.getParent(); 264 } 265 String unitPath = unitVF.getPathName(); 266 String topPath = topVF.getPathName(); 267 String relativePath = unitPath.substring(topPath.length()); 268 return relativePath; 269 } 270 271 public StructureDetermined getStructureDetermined() 272 { 273 return structureDetermined; 274 } 275 276 public void setStructureDetermined(StructureDetermined determined) 277 { 278 if (determined == null) 279 throw new IllegalArgumentException ("Null determined"); 280 this.structureDetermined = determined; 281 } 282 283 public boolean isCandidate() 284 { 285 return candidate; 286 } 287 288 public DeploymentState getState() 289 { 290 return state; 291 } 292 293 public void setState(DeploymentState state) 294 { 295 this.state = state; 296 } 297 298 public DeploymentUnit getDeploymentUnit() 299 { 300 if (unit == null) 301 throw new IllegalStateException ("Deployment unit has not been set"); 302 return unit; 303 } 304 305 public void setDeploymentUnit(DeploymentUnit unit) 306 { 307 this.unit = unit; 308 } 309 310 public VirtualFile getRoot() 311 { 312 return root; 313 } 314 315 320 public void setRoot(VirtualFile root) 321 { 322 this.root = root; 323 } 324 325 public void setMetaDataPath(String path) 326 { 327 if (path == null) 328 setMetaDataLocation(null); 329 try 330 { 331 setMetaDataLocation(root.findChild(path)); 332 } 333 catch (IOException e) 334 { 335 log.debug("Meta data path does not exist: root=" + root.getPathName() + " path=" + path); 336 } 337 } 338 339 public VirtualFile getMetaDataLocation() 340 { 341 return metaDataLocation; 342 } 343 344 public void setMetaDataLocation(VirtualFile location) 345 { 346 this.metaDataLocation = location; 347 if (log.isTraceEnabled() && location != null) 348 log.trace("MetaData location for " + root.getPathName() + " is " + location.getPathName()); 349 } 350 351 public ClassLoader getClassLoader() 352 { 353 return classLoader; 354 } 355 356 public void setClassLoader(ClassLoader classLoader) 357 { 358 this.classLoader = classLoader; 359 if (classLoader != null) 360 log.trace("ClassLoader for " + name + " is " + classLoader); 361 } 362 363 public boolean createClassLoader(ClassLoaderFactory factory) throws DeploymentException 364 { 365 if (factory == null) 366 throw new IllegalArgumentException ("Null factory"); 367 368 ClassLoader cl = getClassLoader(); 369 if (cl != null) 370 return false; 371 372 try 373 { 374 cl = factory.createClassLoader(this); 375 if (cl != null) 376 { 377 setClassLoader(cl); 378 this.classLoaderFactory = factory; 379 } 380 } 381 catch (Throwable t) 382 { 383 throw DeploymentException.rethrowAsDeploymentException("Error creating classloader for " + getName(), t); 384 } 385 return true; 386 } 387 388 public void removeClassLoader() 389 { 390 if (classLoaderFactory == null) 391 return; 392 try 393 { 394 classLoaderFactory.removeClassLoader(this); 395 } 396 catch (Throwable t) 397 { 398 log.warn("Error removing classloader for " + getName(), t); 399 } 400 classLoaderFactory = null; 401 setClassLoader(null); 402 } 403 404 405 public List <VirtualFile> getClassPath() 406 { 407 return classPath; 408 } 409 410 public void setClassPath(List <VirtualFile> paths) 411 { 412 this.classPath = paths; 413 if (log.isTraceEnabled() && paths != null) 414 log.trace("ClassPath for " + root.getPathName() + " is " + VFSUtils.getPathsString(paths)); 415 } 416 417 public boolean isTopLevel() 418 { 419 return parent == null; 420 } 421 422 public DeploymentContext getTopLevel() 423 { 424 DeploymentContext result = this; 425 DeploymentContext parent = getParent(); 426 while (parent != null) 427 { 428 result = parent; 429 parent = parent.getParent(); 430 } 431 return result; 432 } 433 434 public DeploymentContext getParent() 435 { 436 return parent; 437 } 438 439 public void setParent(DeploymentContext parent) 440 { 441 if (parent != null && this.parent != null) 442 throw new IllegalStateException ("Context already has a parent " + getName()); 443 this.parent = parent; 444 } 445 446 public Set <DeploymentContext> getChildren() 447 { 448 return Collections.unmodifiableSet(children); 449 } 450 451 public void addChild(DeploymentContext child) 452 { 453 if (child == null) 454 throw new IllegalArgumentException ("Null child"); 455 children.add(child); 456 } 457 458 public boolean removeChild(DeploymentContext child) 459 { 460 if (child == null) 461 throw new IllegalArgumentException ("Null child"); 462 return children.remove(child); 463 } 464 465 public boolean isComponent() 466 { 467 return false; 468 } 469 470 public Set <DeploymentContext> getComponents() 471 { 472 return Collections.unmodifiableSet(components); 473 } 474 475 public void addComponent(DeploymentContext component) 476 { 477 if (component == null) 478 throw new IllegalArgumentException ("Null component"); 479 deployed(); 480 components.add(component); 481 log.debug("Added component " + component.getName() + " to " + getName()); 482 } 483 484 public boolean removeComponent(DeploymentContext component) 485 { 486 if (component == null) 487 throw new IllegalArgumentException ("Null component"); 488 489 Set <DeploymentContext> componentComponents = component.getComponents(); 490 if (componentComponents.isEmpty() == false) 491 log.warn("Removing component " + name + " which still has components " + componentComponents); 492 boolean result = components.remove(component); 493 if (result) 494 log.debug("Removed component " + component.getName() + " from " + getName()); 495 return result; 496 } 497 498 public void visit(DeploymentContextVisitor visitor) throws DeploymentException 499 { 500 if (visitor == null) 501 throw new IllegalArgumentException ("Null visitor"); 502 503 visit(this, visitor); 504 } 505 506 513 private void visit(DeploymentContext context, DeploymentContextVisitor visitor) throws DeploymentException 514 { 515 visitor.visit(context); 516 try 517 { 518 Set <DeploymentContext> children = context.getChildren(); 519 if (children.isEmpty()) 520 return; 521 522 DeploymentContext[] childContexts = children.toArray(new DeploymentContext[children.size()]); 523 for (int i = 0; i < childContexts.length; ++i) 524 { 525 if (childContexts[i] == null) 526 throw new IllegalStateException ("Null child context for " + context.getName() + " children=" + children); 527 try 528 { 529 visit(childContexts[i], visitor); 530 } 531 catch (Throwable t) 532 { 533 for (int j = i-1; j >= 0; --j) 534 visitError(childContexts[j], visitor, true); 535 throw DeploymentException.rethrowAsDeploymentException("Error visiting: " + childContexts[i].getName(), t); 536 } 537 } 538 } 539 catch (Throwable t) 540 { 541 visitError(context, visitor, false); 542 throw DeploymentException.rethrowAsDeploymentException("Error visiting: " + context.getName(), t); 543 } 544 } 545 546 554 private void visitError(DeploymentContext context, DeploymentContextVisitor visitor, boolean visitChildren) throws DeploymentException 555 { 556 if (visitChildren) 557 { 558 Set <DeploymentContext> children = context.getChildren(); 559 if (children.isEmpty()) 560 return; 561 562 for (DeploymentContext child : children) 563 { 564 try 565 { 566 visitError(child, visitor, true); 567 } 568 catch (Throwable t) 569 { 570 log.warn("Error during visit error: " + child.getName(), t); 571 } 572 } 573 574 } 575 try 576 { 577 visitor.error(context); 578 } 579 catch (Throwable t) 580 { 581 log.warn("Error during visit error: " + context.getName(), t); 582 } 583 } 584 585 public Attachments getPredeterminedManagedObjects() 586 { 587 return predeterminedManagedObjects; 588 } 589 590 public Attachments getTransientManagedObjects() 591 { 592 return transientManagedObjects; 593 } 594 595 public Attachments getTransientAttachments() 596 { 597 return transientAttachments; 598 } 599 600 public Throwable getProblem() 601 { 602 return problem; 603 } 604 605 public void setProblem(Throwable problem) 606 { 607 this.problem = problem; 608 } 609 610 public VirtualFile getMetaDataFile(String name) 611 { 612 if (name == null) 613 throw new IllegalArgumentException ("Null name"); 614 try 615 { 616 if (metaDataLocation == null) 618 { 619 if (root != null && root.isLeaf()) 621 { 622 String fileName = root.getName(); 623 if (fileName.equals(name)) 624 return root; 625 } 626 627 return null; 629 } 630 VirtualFile result = metaDataLocation.findChild(name); 632 if (result != null) 633 deployed(); 634 return result; 635 } 636 catch (Exception e) 637 { 638 log.trace("Error retrieving meta data: " + name, e); 639 return null; 640 } 641 } 642 643 public List <VirtualFile> getMetaDataFiles(String name, String suffix) 644 { 645 if (name == null && suffix == null) 646 throw new IllegalArgumentException ("Null name and suffix"); 647 try 648 { 649 if (metaDataLocation == null) 652 { 653 if (root != null && root.isLeaf()) 655 { 656 String fileName = root.getName(); 657 if (name != null && fileName.equals(name)) 658 return Collections.singletonList(root); 659 if (suffix != null && fileName.endsWith(suffix)) 660 return Collections.singletonList(root); 661 } 662 663 return Collections.emptyList(); 665 } 666 List <VirtualFile> result = metaDataLocation.getChildren(new MetaDataMatchFilter(name, suffix)); 668 if (result != null && result.isEmpty() == false) 669 deployed(); 670 return result; 671 } 672 catch (Exception e) 673 { 674 log.debug("Error retrieving meta data: name=" + name + " suffix=" + suffix, e); 675 return Collections.emptyList(); 676 } 677 } 678 679 public boolean isDeployed() 680 { 681 return deployed; 682 } 683 684 public void deployed() 685 { 686 deployed = true; 687 } 688 689 public void reset() 690 { 691 if (structureDetermined != StructureDetermined.PREDETERMINED) 692 children.clear(); 693 else 694 { 695 for (DeploymentContext child : children) 696 child.reset(); 697 } 698 components.clear(); 699 deployed = false; 700 701 classLoader = null; 702 transientManagedObjects.clear(); 703 transientAttachments.clear(); 704 } 705 706 public String toString() 707 { 708 StringBuilder buffer = new StringBuilder (); 709 buffer.append(getClass().getSimpleName()); 710 buffer.append('@'); 711 buffer.append(System.identityHashCode(this)); 712 buffer.append('{').append(name).append('}'); 713 return buffer.toString(); 714 } 715 } 716 | Popular Tags |