KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > project > BasicProduct


1 package net.sf.invicta.project;
2  
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.StringTokenizer JavaDoc;
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 /**
15  * An implementation of a product definition in a component definition.
16  */

17 public class BasicProduct implements Product {
18     protected boolean isResolved = false;
19     protected boolean export;
20     protected String JavaDoc projectDir = null;
21     protected String JavaDoc componentDir = null;
22     protected String JavaDoc file;
23     protected String JavaDoc name;
24     protected String JavaDoc fullName;
25     protected List JavaDoc dependNames = new ArrayList JavaDoc();
26     protected String JavaDoc type;
27     protected String JavaDoc componentName;
28     protected ProductContainerImpl dependProducts = new ProductContainerImpl();
29     protected String JavaDoc productDir = null;
30     protected String JavaDoc appName;
31     protected String JavaDoc fileSeparator = InvictaConstants.FILE_SEPARATOR;
32         
33     /**
34      * Constructor for BasicProduct
35      */

36     public BasicProduct() {
37         super();
38     }
39         
40     /**
41      * Resolve this product by checking attributes and dependencies.
42      * @param projDef
43      * @param component
44      */

45     public void resolve(ProjectDefinition projDef, ComponentDefinition component) throws InvictaProjectException {
46         resolve(projDef, component, 0);
47     }
48
49     /**
50      * Resolve this product by checking attributes and dependencies.
51      *
52      * @param projDef
53      * @param component
54      * @param productLevel
55      * @return Product
56      */

57     public Product resolve(ProjectDefinition projDef, ComponentDefinition component, int productLevel) throws InvictaProjectException {
58         if (isResolved)
59             return this;
60         
61         // Check if we are in an endless loop of product dependencies.
62
if (productLevel > InvictaConstants.MAX_RESOLVE_LEVEL)
63             throw InvictaProjectException.createProductDependencyCycle(component.getName(), this.name);
64                                 
65                                                                             
66         // Go over the dependency list of the product.
67
for (Iterator JavaDoc iterator = this.dependNames.iterator(); iterator.hasNext();) {
68             String JavaDoc dependentProductName = (String JavaDoc)iterator.next();
69             
70             // Find the depenedent products.
71
ProductContainer dependentProducts = component.resolveProduct(projDef, dependentProductName, productLevel + 1);
72                                     
73             // Add the dependent products info to the depend list of the current
74
// processeed product.
75
for (Iterator JavaDoc productIter = dependentProducts.findAll().iterator(); productIter.hasNext();) {
76                 Product dependentProduct = (Product) productIter.next();
77                 addProducts(dependentProduct.getProducts());
78             }
79         }
80         
81         // Fill the product info with the full details
82
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         // Add also this product to the list of products it has
89
this.dependProducts.insert(this);
90                     
91         this.isResolved = true;
92         return this;
93     }
94
95
96     /**
97      * Returns all products that this product depends on, including this
98      * product itself.
99      * @return ProductContainer
100      */

101     public ProductContainer getProducts() {
102         return this.dependProducts;
103     }
104
105     /**
106      * Returns all products, which are not exported, that this product
107      * depends on, including this product itself.
108      * @return ProductContainer
109      */

110     public ProductContainer getPrivateProducts() {
111         if (this.export)
112             return new ProductContainerImpl();
113         else
114             return this.dependProducts;
115     }
116
117     /**
118      * Returns all products, which are exported, that this product
119      * depends on, including this product itself.
120      * @return ProductContainer
121      */

122     public ProductContainer getExportedProducts() {
123         if (!this.export)
124             return new ProductContainerImpl();
125         else
126             return this.dependProducts;
127     }
128         
129     /**
130      * Returns the file that was specified for this product (optional).
131      * @return String. File name or relative path. null if not specified.
132      */

133     public String JavaDoc getFile() {
134         return file;
135     }
136
137     /**
138      * Sets the depends.
139      * @param depends The depends to set
140      */

141     public void setDepends(String JavaDoc dependsStr) {
142         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(dependsStr, ",");
143         while (st.hasMoreTokens()) {
144             String JavaDoc oneDepend = st.nextToken().trim();
145             this.dependNames.add(oneDepend);
146         }
147     }
148     
149     /**
150      * Returns the unique key of this product. This keys allows to easy
151      * manage lists of products.
152      * @return String. Product key.
153      */

154     public String JavaDoc getKey() {
155         String JavaDoc 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     /**
166      * Returns the path of the file of this product. The path is a construction
167      * of the project directory, component directory, component's product
168      * directory and product's file.
169      * @return String. Product's file path. null if a file was not specified.
170      */

171     public String JavaDoc getPath() {
172         String JavaDoc 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     /**
188      * Returns the directory of this product relatively to the component
189      * directory.
190      * @return String. Product's file relative directory.
191      */

192     public String JavaDoc getRelativeDir() {
193         String JavaDoc 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 JavaDoc toString() {
206         return getPath();
207     }
208
209     /**
210      * Returns the name that was specified for this product.
211      * @return String. Product's name. null if a name was not specified.
212      */

213     public String JavaDoc getName() {
214         return name;
215     }
216
217     /**
218      * Sets the name.
219      * @param name The name to set
220      */

221     public void setName(String JavaDoc name) {
222         this.name = name;
223     }
224
225     /**
226      * Returns a list of names of products that this product depends on.
227      * These are products are defined within the same component of this
228      * product.
229      * @return List of String objects.
230      */

231     public List JavaDoc getDependNames() {
232         return dependNames;
233     }
234
235     /**
236      *
237      * @param typeStr
238      */

239     public void setType(String JavaDoc typeStr) {
240         this.type = typeStr;
241     }
242
243     /**
244      * Returns the type of this product. For example: 'jar'.
245      * @return String. Product's type.
246      */

247     public String JavaDoc getType() {
248         return this.type;
249     }
250     
251     /**
252      * Returns the full name of this product, which is a concatenation of the
253      * component name with the product name (if specified).
254      * @return String. Product's full name.
255      */

256     public String JavaDoc getFullName() {
257         
258         return this.fullName;
259     }
260
261     /**
262      * Returns the relative directory of the component of this product.
263      * @return String
264      */

265     public String JavaDoc getComponentDir() {
266         return componentDir;
267     }
268     
269     /**
270      * Returns the name of the component of this product.
271      * @return String
272      */

273     public String JavaDoc getComponentName() {
274         return componentName;
275     }
276
277     /**
278      * Returns the relative directory of the project of the component of
279      * this product.
280      * @return String
281      */

282     public String JavaDoc getProjectDir() {
283         return projectDir;
284     }
285
286     /**
287      * Sets the componentDir.
288      * @param componentDir The componentDir to set
289      */

290     public void setComponentDir(String JavaDoc componentDir) {
291         this.componentDir = componentDir;
292     }
293
294     /**
295      *
296      * @param componentName
297      */

298     public void setComponentName(String JavaDoc componentName) {
299         this.componentName = componentName;
300     }
301
302     /**
303      * Sets the projectDir.
304      * @param projectDir The projectDir to set
305      */

306     public void setProjectDir(String JavaDoc projectDir) {
307         this.projectDir = projectDir;
308     }
309
310     /**
311      * Sets the file.
312      * @param file The file to set
313      */

314     public void setFile(String JavaDoc file) {
315         this.file = file;
316     }
317
318     /**
319     /**
320      * Sets the fullName.
321      * @param fullName The fullName to set
322      */

323     public void setFullName(String JavaDoc fullName) {
324         this.fullName = fullName;
325     }
326
327     /**
328      * Returns whether this product is exported to components that depend
329      * on the component of this product.
330      * @return boolean
331      */

332     public boolean getExport() {
333         return export;
334     }
335
336     /**
337      * Sets the export.
338      * @param export The export to set
339      */

340     public void setExport(boolean export) {
341         this.export = export;
342     }
343     
344     /**
345      * @return
346      */

347     public String JavaDoc getProductDir() {
348         return this.productDir;
349     }
350
351     /**
352      * @param string
353      */

354     public void setProductDir(String JavaDoc productDir) {
355         this.productDir = productDir;
356     }
357
358     /**
359      * Returns the application name specified for this product.
360      * @return String. Application name or null if not specified.
361      */

362     public String JavaDoc getAppName() {
363         return this.appName;
364     }
365
366     /**
367      * @param string
368      */

369     public void setAppName(String JavaDoc string) {
370         appName = string;
371     }
372
373     /**
374      *
375      * @param path
376      * @return String
377      */

378     protected String JavaDoc cleanPath(String JavaDoc path) {
379
380         String JavaDoc doubleSeparator = this.fileSeparator + this.fileSeparator;
381         String JavaDoc specialDoubleSeparator = this.fileSeparator + "." + this.fileSeparator;
382
383         String JavaDoc newPath = StringUtils.replace(path, doubleSeparator, this.fileSeparator);
384         newPath = StringUtils.replace(newPath, specialDoubleSeparator, this.fileSeparator);
385                     
386         return newPath;
387     }
388
389     /**
390      *
391      * @param moreProducts
392      */

393     protected void addProducts(ProductContainer moreProducts) {
394         this.dependProducts.insertAll(moreProducts);
395     }
396
397 }
398
399
Popular Tags