KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > process > TypedComponent


1 package net.sf.invicta.process;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.apache.commons.collections.SequencedHashMap;
9
10 import net.sf.invicta.InvictaConstants;
11 import net.sf.invicta.InvictaException;
12 import net.sf.invicta.api.DefinedProperty;
13 import net.sf.invicta.api.InvictaComponent;
14 import net.sf.invicta.api.InvictaProject;
15 import net.sf.invicta.api.Product;
16 import net.sf.invicta.api.ProductContainer;
17 import net.sf.invicta.project.BasicProduct;
18 import net.sf.invicta.project.ComponentDefinition;
19 import net.sf.invicta.project.InvictaProjectException;
20 import net.sf.invicta.type.BasicDefinedProperty;
21 import net.sf.invicta.type.TypeDefinition;
22 import net.sf.invicta.type.TypeManager;
23
24 /**
25  * An implementation of the InvictaComponent interface of the API.
26  * Contains both a resolved definition of a component and a matching resolved
27  * type definition.
28  * This class acts as a delegator to ComponentDefinition and TypeDefinition
29  * classes.
30  */

31 public class TypedComponent implements InvictaComponent {
32     protected ComponentDefinition component;
33     protected TypeDefinition type;
34     protected InvictaProjectImpl project;
35     protected Map JavaDoc definedProperties = new SequencedHashMap();
36     
37     /**
38      * Creates a new TypedComponent by getting the type definition for the
39      * type of the given resolved component definition, validating that
40      * the component matches its type and formatting attributes of defined
41      * properties and products.
42      *
43      * @param typeManager
44      * @param component A resolved ComponentDefinition object.
45      * @param project The full project object.
46      * @throws InvictaException
47      */

48     public TypedComponent(TypeManager typeManager, ComponentDefinition component,
49                           InvictaProjectImpl project) throws InvictaException {
50         this.project = project;
51         this.component = component;
52         this.type = typeManager.getTypeDefinition(component.getType());
53         validateComponent();
54         createdTiedDefinedProperties();
55         formatProducts();
56     }
57
58     /**
59      * Returns a list of component names that this component depends on
60      * (directly).
61      *
62      * @return List of String objects.
63      */

64     public List JavaDoc getDependComponentNames() {
65         return component.getDependComponentNames();
66     }
67         
68
69     /**
70      * Returns a list of depend items of dependencies of this component.
71      *
72      * @return List of DependItem objects.
73      */

74     public List JavaDoc getDependItems() {
75         return component.getDependItems();
76     }
77
78     /**
79      * Returns the relative directory of this component.
80      *
81      * @return String. Relative directory.
82      */

83     public String JavaDoc getDir() {
84         return component.getDir();
85     }
86
87     /**
88      * Returns the products that this components depends on and are exported.
89      *
90      * @return ProductContainer
91      */

92     public ProductContainer getExportedProducts() {
93         return component.getExportedProducts();
94     }
95
96     /**
97      * Returns the full name of this component.
98      *
99      * @return
100      */

101     public String JavaDoc getName() {
102         return component.getName();
103     }
104
105
106     /**
107      * Returns the products that this components requires or defines
108      * (private, exported and self products).
109      *
110      * @return ProductContainer
111      */

112     public ProductContainer getProducts() {
113         return component.getProducts();
114     }
115
116     /**
117      * Returns the products that this component requires
118      * (for compilation. private and exported without self products).
119      *
120      * @return ProductContainer
121      */

122     public ProductContainer getProductsWithoutSelf() {
123         return component.getProductsWithoutSelf();
124     }
125
126     /**
127      * Returns the value of a property (component, project or general) with
128      * the given name.
129      * Returns null if the property is not defined.
130      *
131      * @param propertyName
132      * @return String
133      */

134     public String JavaDoc getPropertyValue(String JavaDoc propertyName) {
135         DefinedProperty definedProperty = getDefinedProperty(propertyName);
136         if (definedProperty == null)
137             return null;
138         
139         return definedProperty.getActualValue();
140     }
141
142     /**
143      * Returns a list of names of components that this component depends
144      * on directly or indirectly (recursively).
145      * @return A list of names (Strings).
146      */

147     public List JavaDoc getRecursiveDependComponentNames() {
148         return component.getRecursiveDependComponentNames();
149     }
150
151     /**
152      * Returns a map of components (key=name, value=component) that this
153      * component depends on directly or indirectly (recursively).
154      * @return A map of components (key=name(String), value=InvictaComponent)
155      */

156     public Map JavaDoc getRecursiveDependComponentsMap() {
157         return component.getRecursiveDependComponentNamesMap();
158     }
159
160     /**
161      * Returns the products that this components depends on recursively
162      * (directly and indirectly, exported and non-exported by dependent components,
163      * also includes self products).
164      *
165      * @return ProductContainer
166      */

167     public ProductContainer getRecursiveProducts() {
168         return component.getRecursiveProducts();
169     }
170
171     /**
172      * Returns the products that this components depends on recursively
173      * (directly and indirectly, exported and non-exported by dependent components,
174      * doesn't include self products).
175      *
176      * @return ProductContainer
177      */

178     public ProductContainer getRecursiveProductsWithoutSelf() {
179         return component.getRecursiveProductsWithoutSelf();
180     }
181
182     /**
183      * Returns the products that the product with the given defined name
184      * depends on recursively (directly and indirectly, exported and non-exported).
185      *
186      * @return
187      * @throws InvictaProjectException
188      */

189     public ProductContainer getRecursiveSelfProducts(List JavaDoc productNames)
190         throws InvictaProjectException {
191         return component.getRecursiveSelfProducts(productNames);
192     }
193
194     /**
195      * Returns the products with the given name that are defined in this component
196      *
197      * @param productName
198      * @return ProductContainer
199      */

200     public ProductContainer getSelfProducts(String JavaDoc productName) {
201         return component.getSelfProducts(productName);
202     }
203
204     /**
205      * Returns the products that are defined in this component.
206      *
207      * @return ProductContainer
208      */

209     public ProductContainer getSelfProducts() {
210         return component.getSelfProducts();
211     }
212
213     /**
214      * Returns the products that the product with the given defined name
215      * depends on recursively (directly and indirectly, exported and non-exported).
216      *
217      * @param productName
218      * @return
219      * @throws InvictaProjectException
220      */

221     public ProductContainer getRecursiveSelfProducts(String JavaDoc productName)
222         throws InvictaProjectException {
223         return component.getRecursiveSelfProducts(productName);
224     }
225
226     /**
227      * Returns the full ANT template code of this component.
228      *
229      * @return String. ANT template code.
230      */

231     public String JavaDoc getTemplate() {
232         return type.getAntTemplateString();
233     }
234
235     /**
236      * Returns the name of the type of this component ('JAR' for example).
237      *
238      * @return String. The type name.
239      */

240     public String JavaDoc getTypeName() {
241         return type.getName();
242     }
243
244     /**
245      * Returns a list of components that this component depends on (directly).
246      *
247      * @return List of InvictaComponent objects.
248      */

249     public List JavaDoc getDependComponents() {
250         return getComponentsForNames(component.getDependComponentNames());
251     }
252
253     /**
254      * Returns a product with the given type and name.
255      * Returns null if a matching product doesn't exist.
256      *
257      * @param type Optional. Ignored if null.
258      * @param name Optional. Ignored if null.
259      * @return Product
260      */

261     public Product getSelfProduct(String JavaDoc type, String JavaDoc name) {
262         return component.getSelfProduct(type, name);
263     }
264
265     /**
266      * Returns a list of components that this componen depends on recursively
267      * (directly of indirectly).
268      *
269      * @return List of InvictaComponent objects.
270      */

271     public List JavaDoc getRecursiveDependComponents() {
272         return component.getRecursiveDependComponents();
273     }
274
275     /**
276      *
277      * @see java.lang.Comparable#compareTo(Object)
278      */

279     public int compareTo(Object JavaDoc object) {
280         return this.component.getName().compareTo(((TypedComponent)object).getName());
281     }
282     
283     /**
284      * Returns the projec that this component is part of.
285      * @return InvictaProject
286      */

287     public InvictaProject getProject() {
288         return this.project;
289     }
290
291     /**
292      * Returns a map of the properties defined for this component (according
293      * to the type definition of this component).
294      * @return Map: Key=String (property name), Value=DefinedProperty
295      */

296     public Map JavaDoc getDefinedPropertiesMap() {
297         return this.definedProperties;
298     }
299
300     /**
301      * Returns a list of the properties defined for this component (according
302      * to the type definition of this component).
303      * @return List of DefinedProperty objects.
304      */

305     public List JavaDoc getDefinedProperties() {
306         return new ArrayList JavaDoc(this.definedProperties.values());
307     }
308
309     /**
310      * Returns a property with the given name that is defined for this
311      * component.
312      * @param propertyName
313      * @return DefinedProperty. null if no property with the given name is defined.
314      */

315     public DefinedProperty getDefinedProperty(String JavaDoc propertyName) {
316         return (DefinedProperty)this.definedProperties.get(propertyName);
317     }
318     /**
319      * Returns the a reference (ANT property reference) to the value of a
320      * property (component, project or general) with the given name.
321      * Returns null if the property is not defined.
322      *
323      * @param propertyName
324      * @return String. For example: ${sample.utils.src.dir}
325      */

326     public String JavaDoc getPropertyReferenceValue(String JavaDoc propertyName) {
327         DefinedProperty property = getDefinedProperty(propertyName);
328         if (property == null)
329             return null;
330         
331         return property.getReferenceValue();
332     }
333     
334
335     /**
336      * Returns the name of this component.
337      *
338      * @return String. Component name.
339      */

340     public boolean containsTarget(String JavaDoc targetName) {
341         return type.containsTarget(targetName);
342     }
343
344     /**
345      * Returns the name of the building target (target template) of
346      * this component. For example: 'build'.
347      * @return String. Building target.
348      */

349     public String JavaDoc getBuildTarget() {
350         return type.getBuildTarget();
351     }
352
353     /**
354      * Returns the name of the cleaning target (target template) of
355      * this component. For example: 'clean'.
356      * @return String. Initialization target.
357      */

358     public String JavaDoc getCleanTarget() {
359         return type.getCleanTarget();
360     }
361
362     /**
363      * Returns the name of the distribution target (target template) of
364      * this component. For example: 'dist'
365      * @return String. Distribution target.
366      */

367     public String JavaDoc getDistTarget() {
368         return type.getDistTarget();
369     }
370
371     public Map JavaDoc getTargetMap() {
372         return this.type.getTargetMap();
373     }
374     
375     
376     /**
377      *
378      */

379     public List JavaDoc getTargetList() {
380         return this.type.getTargetList();
381     }
382
383     /**
384      * Returns a value of a property specifically defined in this component's definition.
385      * @param string
386      * @return Sting
387      */

388     public String JavaDoc getComponentPropertyValue(String JavaDoc name) {
389         return this.component.getPropertyValue(name);
390     }
391
392     /**
393      *
394      */

395     protected void createdTiedDefinedProperties() throws InvictaException {
396         // Go over all basic defined properties.
397
for (Iterator JavaDoc iter = this.type.getDefinedProperties().values().iterator(); iter.hasNext();) {
398             BasicDefinedProperty property = (BasicDefinedProperty) iter.next();
399             TiedDefinedProperty tiedProperty = new TiedDefinedProperty(property, this, this.project);
400             this.definedProperties.put(tiedProperty.getName(), tiedProperty);
401         }
402     }
403     
404     
405     protected void validateComponent() throws InvictaException{
406         
407         // Make sure that all required properties are defined.
408
for (Iterator JavaDoc iter = this.type.getDefinedProperties().values().iterator(); iter.hasNext();) {
409             BasicDefinedProperty definedProperty = (BasicDefinedProperty) iter.next();
410                         
411             // Make sure that a component property was defined.
412
if (definedProperty.isComponentType()) {
413                                 
414                 if (definedProperty.getDefaultValue() != null) {
415                     this.component.setPropertyValue(definedProperty.getName(), definedProperty.getDefaultValue());
416                 } else if (this.component.getPropertyValue(definedProperty.getName()) == null) {
417                     throw InvictaProcessException.propertyUndefined(
418                         this.type.getName(), this.component.getName(), definedProperty.getName());
419                 }
420                                                                                             
421             // Make sure that a project property was defined.
422
} else if (definedProperty.isProjectType()) {
423                 
424                 if (definedProperty.getDefaultValue() != null) {
425                     this.project.setProjectProperty(definedProperty.getName(), definedProperty.getDefaultValue(), definedProperty.getDescription());
426                 } else if (this.project.getProjectPropertyValue(definedProperty.getName()) == null) {
427                     throw InvictaProcessException.projectPropertyUndefined(
428                             this.type.getName(), definedProperty.getName());
429                 }
430             }
431         }
432
433     }
434
435
436     protected void formatProducts() {
437         for (Iterator JavaDoc iter = this.component.getSelfProducts().iterator(); iter.hasNext();) {
438             BasicProduct product = (BasicProduct) iter.next();
439             
440             DefinedProperty property =
441                 getDefinedProperty(InvictaConstants.PRODUCT_DIR_PROPERTY);
442             
443             if (property != null) {
444                 product.setProductDir(property.getActualValue());
445             }
446         }
447     }
448
449     /**
450      *
451      * @param names
452      * @return List
453      */

454     protected List JavaDoc getComponentsForNames(List JavaDoc names) {
455         List JavaDoc components = new ArrayList JavaDoc();
456         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
457             String JavaDoc componentName = (String JavaDoc) iter.next();
458             components.add(this.project.getComponent(componentName));
459         }
460         return components;
461     }
462
463     /**
464      * Returns the name of the initialization target (target template) of
465      * this component. For example: 'init'.
466      * @return String. Initialization target.
467      */

468     public String JavaDoc getInitTarget() {
469         return this.type.getInitTarget();
470     }
471
472     
473 }
474
Popular Tags