KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.invicta.process;
2
3 import java.io.File JavaDoc;
4 import java.io.FilenameFilter JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 import net.sf.invicta.InvictaConstants;
11 import net.sf.invicta.InvictaException;
12 import net.sf.invicta.Logger;
13 import net.sf.invicta.api.InvictaProject;
14
15 /**
16  * The main flow of Invicta processing.
17  * Performs the following actions:
18  * - Reading configuration.
19  * - Checking running necessity.
20  * - Processing and resolving project and type definition files.
21  * - Dumping output files.
22  */

23 public class InvictaMainFlow {
24     protected List JavaDoc definitionFiles;
25     protected List JavaDoc typeFiles;
26     protected InvictaProject project;
27     protected ConfigurationManager confManager;
28     protected DumpersManager dumpersManager;
29     protected boolean wasProcessed = false;
30                             
31     /**
32      *
33      */

34     public InvictaMainFlow() throws InvictaException {
35         super();
36         this.confManager = new ConfigurationManager();
37         this.dumpersManager = new DumpersManager(this.confManager);
38     }
39
40     /**
41      * Peforms the main processing flow.
42      *
43      * @throws InvictaException
44      */

45     public void process() throws InvictaException {
46
47         // Read the required configuration parameters.
48
readConfiguration();
49                 
50         // Check whether processing is not required (no configuration changes
51
// and no required dumping.
52
if (!this.dumpersManager.forceRun() &&
53             !this.confManager.forceRun()) {
54             Logger.info("No settings were changed. No need to run Invicta.");
55             return;
56         }
57         
58         // Create a project resolver with the full lists of project
59
// definition files and type definition files.
60
ProjectResolver projectResolver = new ProjectResolver(this.definitionFiles, this.typeFiles);
61         
62         // Perform the actual project resolving.
63
this.project = projectResolver.resolveProject();
64                                 
65         // Perform dumping of the resolved Invicta project into output files.
66
this.dumpersManager.dump(this.project);
67         
68         // Write all settings (mainly configuration) of the current execution
69
// for checking running necessity in the next execution.
70
this.confManager.writeOutputProperties();
71         
72         this.wasProcessed = true;
73         Logger.info("Invicta finished running successfully !");
74     }
75
76     /**
77      * @return boolean
78      */

79     public boolean getWasProcessed() {
80         return wasProcessed;
81     }
82
83     /**
84      * Reads the configuration (from a property file), keep current execution
85      * settings and process the given list of file locations.
86      *
87      * @throws InvictaException
88      */

89     protected void readConfiguration() throws InvictaException {
90         
91         // If a JAR file name (Invicta.jar) was given, check its last
92
// modification time (for recognizing Invicta version change).
93
String JavaDoc jarFileName =
94             this.confManager.getProperty(InvictaConstants.PROPERTY_JAR_FILE);
95         if (jarFileName != null) {
96             File JavaDoc jarFile = new File JavaDoc(jarFileName);
97             this.confManager.setOutputTimestampProperty
98                 (InvictaConstants.OUTPUT_PROPERTY_JAR_FILE, jarFile);
99         }
100             
101         // Convert the comma-separated list of definition locations into
102
// a Java list.
103
List JavaDoc definitionLocations =
104             tokenizeList(this.confManager.getRequiredProperty(InvictaConstants.PROPERTY_DEFINITION_LIST));
105         List JavaDoc typeLocations =
106             tokenizeList(this.confManager.getRequiredProperty(InvictaConstants.PROPERTY_TYPE_LIST));
107         
108         // Process the lists of definition locations for getting full lists
109
// of definition files (instead of directories).
110
this.definitionFiles =
111             prepareFileList(definitionLocations, InvictaConstants.OUTPUT_PROPERTY_DEFINITION_FILES);
112         this.typeFiles =
113             prepareFileList(typeLocations, InvictaConstants.OUTPUT_PROPERTY_TYPE_FILES);
114
115         // Keep the lists of definitions files to be compared in the next
116
// execution.
117
this.confManager.setOutputProperty(
118             InvictaConstants.OUTPUT_PROPERTY_DEFINITION_FILES, this.definitionFiles.toString());
119         this.confManager.setOutputProperty(
120             InvictaConstants.OUTPUT_PROPERTY_TYPE_FILES, this.typeFiles.toString());
121         
122         Logger.info("Project definition list=" + this.definitionFiles);
123     }
124
125     /**
126      * Processes a list of locations for getting a full list of files
127      * (instead of directories).
128      *
129      * @param locations
130      * @param outputPropertyPrefix
131      * @return List of String objects with full paths of files.
132      * @throws InvictaException
133      */

134     protected List JavaDoc prepareFileList(List JavaDoc locations, String JavaDoc outputPropertyPrefix) throws InvictaException {
135         List JavaDoc fileList = new ArrayList JavaDoc();
136         
137         // Go over all locations.
138
for (Iterator JavaDoc iter = locations.iterator(); iter.hasNext();) {
139             String JavaDoc oneLocation = (String JavaDoc) iter.next();
140             File JavaDoc oneFile = new File JavaDoc(oneLocation);
141             
142             // Keep the last modification time of this location (file or dir).
143
this.confManager.setOutputTimestampProperty(outputPropertyPrefix, oneFile);
144             
145             if (!oneFile.exists())
146                 continue;
147             
148             // If the location is a directory, then add its *.xml content,
149
// otherwise simply add the file name.
150
if (oneFile.isDirectory()) {
151                 File JavaDoc[] files = oneFile.listFiles(new FilenameFilter JavaDoc() {
152                     public boolean accept(File JavaDoc dir, String JavaDoc name) {
153                         return name.endsWith(InvictaConstants.DEFINITION_FILE_EXTENSION);
154                     }
155                 });
156                 // Add all matching files while keeping their last modification
157
// time.
158
for (int index = 0; index < files.length; index++) {
159                     fileList.add(files[index].getAbsolutePath());
160                     this.confManager.setOutputTimestampProperty(outputPropertyPrefix, files[index]);
161                 }
162             } else {
163                 fileList.add(oneFile.getPath());
164             }
165         }
166                 
167         return fileList;
168     }
169
170     /**
171      * Converts a comma-separated list (String) of items into a Java list.
172      * @param listStr
173      * @return
174      */

175     protected List JavaDoc tokenizeList(String JavaDoc listStr) {
176         List JavaDoc list = new ArrayList JavaDoc();
177                     
178         StringTokenizer JavaDoc st =
179             new StringTokenizer JavaDoc(listStr, InvictaConstants.LIST_DELIMITER);
180         while (st.hasMoreTokens())
181             list.add(st.nextToken().trim());
182         return list;
183     }
184
185 }
186
Popular Tags