1 package net.sf.invicta.project; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import org.apache.commons.collections.SequencedHashMap; 10 import net.sf.invicta.InvictaConstants; 11 import net.sf.invicta.Logger; 12 import net.sf.invicta.api.Product; 13 import net.sf.invicta.api.ProductContainer; 14 15 16 23 public class ComponentDefinition { 24 private static final char COMPONENT_NAME_SEPARATOR = '.'; 25 private String name; 27 private String type; 28 private String dir; 29 private List depends = new ArrayList (); private ProductContainer selfProducts = new ProductContainerImpl(); 31 private Map properties = new SequencedHashMap(); private ProjectDefinition projDef; 33 34 private boolean isResolved = false; 36 private ProductContainer dependExportedProducts = new ProductContainerImpl(); 37 private ProductContainer dependPrivateProducts = new ProductContainerImpl(); 38 private ProductContainer recursiveProducts = new ProductContainerImpl(); 39 private Map dependComponents = new SequencedHashMap(); private Map dependComponentNames = new SequencedHashMap(); private Map recursiveDependComponentNames = new HashMap (); private Map recursiveDependComponents = new HashMap (); 44 45 53 public ComponentDefinition resolve(ProjectDefinition projDef, int level) throws InvictaProjectException { 54 if (isResolved) 55 return this; 56 57 this.projDef = projDef; 58 Logger.info(this.name + ", "); 59 if (level > InvictaConstants.MAX_RESOLVE_LEVEL) 61 throw InvictaProjectException.componentDependencyCycle(this.name); 62 63 resolveProducts(projDef); 65 66 resolveDependencies(projDef, level); 68 69 isResolved = true; 70 71 return this; 73 } 74 75 76 82 public ProductContainer resolveProduct(ProjectDefinition projDef, String productName, int productLevel) throws InvictaProjectException { 83 ProductContainer products = (ProductContainer)this.selfProducts.findByName(productName); 85 86 if (products.size() == 0) 88 throw InvictaProjectException.productUndefined(this.name, productName); 89 90 for (Iterator iter = products.findAll().iterator(); iter.hasNext();) { 92 BasicProduct product = (BasicProduct) iter.next(); 93 product.resolve(projDef, this, productLevel); 94 } 95 96 return products; 97 } 98 99 100 101 109 public ProductContainer getRecursiveSelfProducts(List productNames) throws InvictaProjectException { 110 ProductContainer newContainer = new ProductContainerImpl(); 111 112 for (Iterator iter = productNames.iterator(); iter.hasNext();) { 114 String productName = (String )iter.next(); 115 116 ProductContainer products = (ProductContainer)this.selfProducts.findByName(productName); 118 119 if (products.size() == 0) 121 throw InvictaProjectException.productUndefined(this.name, productName); 122 123 for (Iterator prodIter = products.findAll().iterator(); prodIter.hasNext();) { 125 Product product = (Product) prodIter.next(); 126 newContainer.insertAll(product.getProducts()); 127 } 128 } 129 130 return newContainer; 131 } 132 133 141 public ProductContainer getRecursiveSelfProducts(String productName) throws InvictaProjectException { 142 List productNames = new ArrayList (); 143 productNames.add(productName); 144 return getRecursiveSelfProducts(productNames); 145 } 146 147 153 public ProductContainer getSelfProducts(String productName) { 154 ProductContainer products = (ProductContainer)this.selfProducts.findByName(productName); 155 return products; 156 } 157 158 163 public ProductContainer getSelfProducts() { 164 return this.selfProducts; 165 } 166 167 public List getSelfProductsList() { 168 return this.selfProducts.findAll(); 169 } 170 171 177 public ProductContainer getProducts() { 178 ProductContainer allProducts = new ProductContainerImpl(); 179 180 allProducts.insertAll(this.dependExportedProducts); 182 183 allProducts.insertAll(this.dependPrivateProducts); 185 186 return allProducts; 187 } 188 189 195 public ProductContainer getProductsWithoutSelf() { 196 ProductContainer reqProducts = new ProductContainerImpl(); 197 198 reqProducts.insertAll(this.dependExportedProducts); 200 201 reqProducts.insertAll(this.dependPrivateProducts); 203 204 reqProducts.removeAll(this.selfProducts); 206 207 return reqProducts; 208 } 209 210 215 public ProductContainer getExportedProducts() { 216 217 return this.dependExportedProducts; 218 } 219 220 226 public ProductContainer getDependExportedProducts() { 227 return this.dependExportedProducts; 228 } 229 230 236 public ProductContainer getDependPrivateProducts() { 237 return this.dependPrivateProducts; 238 } 239 240 241 246 public List getDepends() { 247 return depends; 248 } 249 250 251 256 public String getName() { 257 return name; 258 } 259 260 261 266 public String getType() { 267 return type; 268 } 269 270 275 public void setName(String name) { 276 this.name = name; 277 } 278 279 284 public void addProduct(BasicProduct product) { 285 this.selfProducts.insert(product); 286 } 287 288 293 public void setType(String type) { 294 this.type = type; 295 } 296 297 301 public void addDepend(Depend depend) { 302 this.depends.add(depend); 303 } 304 305 309 public void addProperty(PropertyImpl property) throws InvictaProjectException { 310 if (this.properties.containsKey(property.getName())) 311 throw InvictaProjectException.duplicateProperty(this.name, property.getName()); 312 this.properties.put(property.getName(), property.getValue()); 313 } 314 315 320 public String getDir() { 321 if (this.dir != null) 322 return dir; 323 324 String dir = this.name; 325 326 int dotIndex = dir.indexOf(COMPONENT_NAME_SEPARATOR); 328 if (dotIndex != -1) { 329 dir = dir.substring(dotIndex + 1); 330 dir = dir.replace(COMPONENT_NAME_SEPARATOR, InvictaConstants.FILE_SEPARATOR_CHAR); 332 } else 333 dir = "."; 334 335 return dir; 336 } 337 338 342 public void setDir(String dir) { 343 this.dir = dir; 344 } 345 346 350 public String toString() { 351 return "[Component: name=" + getName() + 352 ", type=" + getType() + ", dir=" + getDir() + "]"; 353 } 354 355 361 public List getDependComponentNames() { 362 return (List )new ArrayList (this.dependComponentNames.values()); 363 } 364 365 370 public List getDependComponents() { 371 return (List )new ArrayList (this.dependComponents.values()); 372 } 373 374 375 383 public String getPropertyValue(String propertyName) { 384 return (String )this.properties.get(propertyName); 385 } 386 387 public void setPropertyValue(String name, String value) { 388 if (this.properties.containsKey(name)) 389 return; 390 391 this.properties.put(name, value); 392 } 393 394 402 public Product getSelfProduct(String type, String name) { 403 for (Iterator iter = this.selfProducts.iterator(); iter.hasNext();) { 404 Product product = (Product) iter.next(); 405 if (((type == null) || 410 ((product.getType() != null) && (product.getType().equals(type)))) 411 && 412 ((name == null) || 413 ((product.getName() != null) && (product.getName().equals(name)))) 414 ) 415 return product; 416 417 } 418 return null; 419 } 420 421 422 427 public List getDependItems() { 428 return this.depends; 429 } 430 431 438 public ProductContainer getRecursiveProducts() { 439 return this.recursiveProducts; 440 } 441 442 449 public ProductContainer getRecursiveProductsWithoutSelf() { 450 ProductContainer products = new ProductContainerImpl(); 451 452 products.insertAll(this.recursiveProducts); 454 455 products.removeAll(this.selfProducts); 457 458 return products; 459 } 460 461 462 public Map getRecursiveDependComponentNamesMap() { 463 return this.recursiveDependComponentNames; 464 } 465 466 public Map getRecursiveDependComponentsMap() { 467 return this.recursiveDependComponents; 468 } 469 470 public List getRecursiveDependComponentNames() { 471 return (List )new ArrayList (this.recursiveDependComponentNames.values()); 472 } 473 474 480 public List getRecursiveDependComponents() { 481 return (List )new ArrayList (this.recursiveDependComponents.values()); 482 } 483 484 492 private void resolveDependencies(ProjectDefinition projectDefinition, int level) throws InvictaProjectException { 493 494 if (!this.equals(projectDefinition.getGlobalComponent())) 496 this.depends.add( 497 new Depend(projectDefinition.getGlobalComponent().getName(), true)); 498 499 for (Iterator iterator = depends.iterator(); iterator.hasNext();) { 501 Depend depend = (Depend) iterator.next(); 502 503 depend.resolve(projectDefinition, level); 505 506 this.dependComponentNames.put(depend.getName(),depend.getName()); 508 this.dependComponents.put(depend.getName(),depend.getComponent()); 509 510 addDependPrivateProducts(depend.getPrivateProducts()); 512 addDependExportedProducts(depend.getExportedProducts()); 513 addRecursiveProducts(depend.getRecursiveProducts()); 514 addRecursiveDependComponentNames(depend.getRecursiveComponents()); 515 addRecursiveDependComponents(depend.getRecursiveComponents()); 516 } 517 } 518 519 524 private void resolveProducts(ProjectDefinition projDef) throws InvictaProjectException { 525 Product potentialPrimary = null; 526 for (Iterator iter = this.selfProducts.iterator(); iter.hasNext();) { 527 BasicProduct product = (BasicProduct) iter.next(); 528 product.resolve(projDef, this); 529 530 addDependPrivateProducts(product.getPrivateProducts()); 532 addDependExportedProducts(product.getExportedProducts()); 533 addRecursiveProducts(product.getProducts()); 534 } 535 } 536 537 543 private void addDependPrivateProducts(ProductContainer products) { 544 this.dependPrivateProducts.insertAll(products); 545 } 546 547 548 554 private void addDependExportedProducts(ProductContainer products) { 555 this.dependExportedProducts.insertAll(products); 556 } 557 558 559 private void addRecursiveProducts(ProductContainer products) { 560 561 this.recursiveProducts.insertAll(products); 562 } 563 564 private void addRecursiveDependComponentNames(Map moreDependComponentNames) { 565 this.recursiveDependComponentNames.putAll(moreDependComponentNames); 566 } 567 568 private void addRecursiveDependComponents(Map moreDependComponents) { 569 this.recursiveDependComponents.putAll(moreDependComponents); 570 } 571 572 573 } 574 | Popular Tags |