1 package net.sf.invicta.project; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import java.util.StringTokenizer ; 7 8 import net.sf.invicta.InvictaConstants; 9 import net.sf.invicta.api.Product; 10 import net.sf.invicta.api.ProductContainer; 11 12 import org.apache.commons.lang.StringUtils; 13 14 17 public class BasicProduct implements Product { 18 protected boolean isResolved = false; 19 protected boolean export; 20 protected String projectDir = null; 21 protected String componentDir = null; 22 protected String file; 23 protected String name; 24 protected String fullName; 25 protected List dependNames = new ArrayList (); 26 protected String type; 27 protected String componentName; 28 protected ProductContainerImpl dependProducts = new ProductContainerImpl(); 29 protected String productDir = null; 30 protected String appName; 31 protected String fileSeparator = InvictaConstants.FILE_SEPARATOR; 32 33 36 public BasicProduct() { 37 super(); 38 } 39 40 45 public void resolve(ProjectDefinition projDef, ComponentDefinition component) throws InvictaProjectException { 46 resolve(projDef, component, 0); 47 } 48 49 57 public Product resolve(ProjectDefinition projDef, ComponentDefinition component, int productLevel) throws InvictaProjectException { 58 if (isResolved) 59 return this; 60 61 if (productLevel > InvictaConstants.MAX_RESOLVE_LEVEL) 63 throw InvictaProjectException.createProductDependencyCycle(component.getName(), this.name); 64 65 66 for (Iterator iterator = this.dependNames.iterator(); iterator.hasNext();) { 68 String dependentProductName = (String )iterator.next(); 69 70 ProductContainer dependentProducts = component.resolveProduct(projDef, dependentProductName, productLevel + 1); 72 73 for (Iterator productIter = dependentProducts.findAll().iterator(); productIter.hasNext();) { 76 Product dependentProduct = (Product) productIter.next(); 77 addProducts(dependentProduct.getProducts()); 78 } 79 } 80 81 this.componentName = component.getName(); 83 this.componentDir = component.getDir(); 84 this.fullName = component.getName(); 85 if (this.name != null) 86 this.fullName += "." + this.name; 87 88 this.dependProducts.insert(this); 90 91 this.isResolved = true; 92 return this; 93 } 94 95 96 101 public ProductContainer getProducts() { 102 return this.dependProducts; 103 } 104 105 110 public ProductContainer getPrivateProducts() { 111 if (this.export) 112 return new ProductContainerImpl(); 113 else 114 return this.dependProducts; 115 } 116 117 122 public ProductContainer getExportedProducts() { 123 if (!this.export) 124 return new ProductContainerImpl(); 125 else 126 return this.dependProducts; 127 } 128 129 133 public String getFile() { 134 return file; 135 } 136 137 141 public void setDepends(String dependsStr) { 142 StringTokenizer st = new StringTokenizer (dependsStr, ","); 143 while (st.hasMoreTokens()) { 144 String oneDepend = st.nextToken().trim(); 145 this.dependNames.add(oneDepend); 146 } 147 } 148 149 154 public String getKey() { 155 String key = this.componentName; 156 157 if (this.name != null) 158 key += "." + this.name; 159 160 key += "." + this.type; 161 162 return key; 163 } 164 165 171 public String getPath() { 172 String path = ""; 173 if (this.projectDir != null) 174 path += this.projectDir + this.fileSeparator; 175 176 if (this.componentDir != null) 177 path += this.componentDir + this.fileSeparator; 178 179 if (this.productDir != null) 180 path += this.productDir + this.fileSeparator; 181 182 path += this.file; 183 184 return cleanPath(path); 185 } 186 187 192 public String getRelativeDir() { 193 String full = this.file; 194 195 if (full == null) 196 return null; 197 198 int index = full.lastIndexOf(this.fileSeparator); 199 if (index == -1) 200 return "."; 201 202 return full.substring(0, index); 203 } 204 205 public String toString() { 206 return getPath(); 207 } 208 209 213 public String getName() { 214 return name; 215 } 216 217 221 public void setName(String name) { 222 this.name = name; 223 } 224 225 231 public List getDependNames() { 232 return dependNames; 233 } 234 235 239 public void setType(String typeStr) { 240 this.type = typeStr; 241 } 242 243 247 public String getType() { 248 return this.type; 249 } 250 251 256 public String getFullName() { 257 258 return this.fullName; 259 } 260 261 265 public String getComponentDir() { 266 return componentDir; 267 } 268 269 273 public String getComponentName() { 274 return componentName; 275 } 276 277 282 public String getProjectDir() { 283 return projectDir; 284 } 285 286 290 public void setComponentDir(String componentDir) { 291 this.componentDir = componentDir; 292 } 293 294 298 public void setComponentName(String componentName) { 299 this.componentName = componentName; 300 } 301 302 306 public void setProjectDir(String projectDir) { 307 this.projectDir = projectDir; 308 } 309 310 314 public void setFile(String file) { 315 this.file = file; 316 } 317 318 323 public void setFullName(String fullName) { 324 this.fullName = fullName; 325 } 326 327 332 public boolean getExport() { 333 return export; 334 } 335 336 340 public void setExport(boolean export) { 341 this.export = export; 342 } 343 344 347 public String getProductDir() { 348 return this.productDir; 349 } 350 351 354 public void setProductDir(String productDir) { 355 this.productDir = productDir; 356 } 357 358 362 public String getAppName() { 363 return this.appName; 364 } 365 366 369 public void setAppName(String string) { 370 appName = string; 371 } 372 373 378 protected String cleanPath(String path) { 379 380 String doubleSeparator = this.fileSeparator + this.fileSeparator; 381 String specialDoubleSeparator = this.fileSeparator + "." + this.fileSeparator; 382 383 String newPath = StringUtils.replace(path, doubleSeparator, this.fileSeparator); 384 newPath = StringUtils.replace(newPath, specialDoubleSeparator, this.fileSeparator); 385 386 return newPath; 387 } 388 389 393 protected void addProducts(ProductContainer moreProducts) { 394 this.dependProducts.insertAll(moreProducts); 395 } 396 397 } 398 399 | Popular Tags |