KickJava   Java API By Example, From Geeks To Geeks.

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


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.api.DefinedProperty;
11 import net.sf.invicta.api.InvictaComponent;
12 import net.sf.invicta.api.InvictaProject;
13 import net.sf.invicta.api.Property;
14 import net.sf.invicta.project.ProjectDefinition;
15 import net.sf.invicta.project.PropertyImpl;
16
17 /**
18  * Implementation of InvictaProject interface of the API.
19  * Contains global project settings and a map of resolved implementations
20  * of InvictaComponent (TypedComponent objectS).
21  */

22 public class InvictaProjectImpl implements InvictaProject {
23     protected Map JavaDoc generalProperties = new SequencedHashMap();
24     protected Map JavaDoc components = new SequencedHashMap();
25     protected ProjectDefinition projectDefinition;
26
27     /**
28      *
29      * @param projectDefinition
30      */

31     public InvictaProjectImpl(ProjectDefinition projectDefinition) {
32         this.projectDefinition = projectDefinition;
33     }
34     
35     /**
36      * Add a new resolved component to the map of components while adding
37      * its general defined properties to a global map.
38      *
39      * @param component
40      * @throws InvictaProcessException
41      */

42     public void addComponent(InvictaComponent component) throws InvictaProcessException {
43         this.components.put(component.getName(), component);
44         addGeneralProperties(component);
45     }
46         
47     /**
48      * Returns a list of all components that are defined in this project.
49      * @return List of InvictaComponent objects.
50      */

51     public List JavaDoc getComponents() {
52         return (List JavaDoc)new ArrayList JavaDoc(this.components.values());
53     }
54     
55     /**
56      * Returns a component with the given name or null if a component
57      * with the given name is not defined.
58      * @param componentName
59      * @return InvictaComponent
60      */

61     public InvictaComponent getComponent(String JavaDoc componentName) {
62         return (InvictaComponent) this.components.get(componentName);
63     }
64
65     /**
66      * Returns the relative directory of this project.
67      * @return String
68      */

69     public String JavaDoc getDir() {
70         return projectDefinition.getDir();
71     }
72
73     /**
74      * Returns the name of this project.
75      * @return String
76      */

77     public String JavaDoc getName() {
78         return projectDefinition.getName();
79     }
80
81     /**
82      * Returns a map of all defined project properties.
83      * @return Map. Key=String (property name), Value=Property
84      */

85     public Map JavaDoc getProjectPropertiesMap() {
86         return projectDefinition.getProjectPropertiesMap();
87     }
88
89     /**
90      * Returns a list of all defined project properties.
91      * @return List of Property objects.
92      */

93     public List JavaDoc getProjectProperties() {
94         return projectDefinition.getProjectProperties();
95     }
96
97     /**
98      * Returns the value of a property that is defined for this project.
99      * @param propertyName
100      * @return String. Property value or null if undefined.
101      */

102     public String JavaDoc getProjectPropertyValue(String JavaDoc propertyName) {
103         return projectDefinition.getProjectPropertyValue(propertyName);
104     }
105
106     /**
107      * Returns a property that is defined for this project.
108      * @param propertyName
109      * @return Property. null if a property with the given name is undefined.
110      */

111     public Property getProjectProperty(String JavaDoc propertyName) {
112         return projectDefinition.getProjectProperty(propertyName);
113     }
114     
115     /**
116      * Sets (overrides) the value of a project property with the given name.
117      * @param propertyName
118      * @param value
119      */

120     public void setProjectProperty(String JavaDoc propertyName, String JavaDoc value, String JavaDoc description) {
121         projectDefinition.setProjectProperty(propertyName, value, description);
122     }
123
124     /**
125      * Returns the version of this project.
126      * @return String
127      */

128     public String JavaDoc getVersion() {
129         return projectDefinition.getVersion();
130     }
131     
132     /**
133      * Returns a map of all defined general properties.
134      * @return Map. Key=String (property name), Value=Property
135      */

136     public Map JavaDoc getGeneralPropertiesMap() {
137         return this.generalProperties;
138     }
139
140     /**
141      * Returns a list of all defined general properties.
142      * @return List of Property objects.
143      */

144     public List JavaDoc getGeneralProperties() {
145         return (List JavaDoc)new ArrayList JavaDoc(this.generalProperties.values());
146     }
147     
148     /**
149      * Returns the global component of this project. This is the component
150      * that all components of the project automatically depends on, and is
151      * reposible for global initializations of the generated build script.
152      * @return InvictaComponent
153      */

154     public InvictaComponent getGlobalComponent() {
155         String JavaDoc componentName = this.projectDefinition.getGlobalComponentName();
156         return this.getComponent(componentName);
157     }
158
159     /**
160      * Adds the general properties of a component being added to the map of
161      * all general properties of the project.
162      */

163     protected void addGeneralProperties(InvictaComponent component) throws InvictaProcessException {
164         
165         // Go over all general properties of the added component.
166
for (Iterator JavaDoc iter = component.getDefinedProperties().iterator(); iter.hasNext();) {
167             DefinedProperty property = (DefinedProperty) iter.next();
168         
169             if (property.isGeneralType()) {
170                 String JavaDoc currentValue = null;
171                 Property currentProperty = (Property)this.generalProperties.get(property.getName());
172                 if (currentProperty != null)
173                     currentValue = currentProperty.getValue();
174                 
175                 // Check that there was no different previous value for this general property.
176
if ((currentValue != null) && !property.getDefaultValue().equals(currentValue))
177                     throw InvictaProcessException.conflictingGeneralProperties(component.getTypeName(), property.getName());
178                 
179                 // Add the new value of the general property.
180
this.generalProperties.put(property.getName(), new PropertyImpl(property.getName(), property.getDefaultValue(), property.getDescription()));
181             }
182         }
183     }
184 }
185
Popular Tags