source: DCWoRMS/branches/coolemall/src/simulator/workload/reader/archive/AbstractWAParser.java @ 1207

Revision 1207, 7.4 KB checked in by wojtekp, 11 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator.workload.reader.archive;
2
3import java.io.IOException;
4import java.io.RandomAccessFile;
5import java.util.HashMap;
6import java.util.Map;
7
8import org.apache.commons.logging.Log;
9import org.apache.commons.logging.LogFactory;
10
11import simulator.workload.exceptons.NoSuchCommentException;
12import simulator.workload.reader.archive.gwf.GWFParser;
13import simulator.workload.reader.archive.swf.SWFParser;
14
15/**
16 *
17 * @author Marcin Krystek
18 *
19 */
20
21public abstract class AbstractWAParser extends RandomAccessFile implements WAParser {
22
23        private Log log = LogFactory.getLog(AbstractWAParser.class);
24       
25        private static final long serialVersionUID = 1L;
26        private String fileName;
27       
28        protected String COMMENT = ";";
29        protected String FIELD_SEPARATOR = "\\s"; //white spaces
30
31        protected HashMap<String, String> comments;
32        protected HashMap<String, String[]> idMapping; // key - swf job id, value - value[0] xml job id, value[1] xml task id
33        protected HashMap<String, String> reverseIdMapping; // key - xmlJobId_xmlTaskId, value - swf job id
34        protected HashMap<String, Long> jobIndex;
35        protected HashMap<String, String> appProfilesLocation;
36        protected String fields[];
37        protected int fieldsNo;
38        protected boolean headerLoaded;
39        protected boolean buildIndex;
40       
41        public AbstractWAParser(String fileName) throws IOException, NullPointerException{
42                super(fileName, "r");
43                this.fileName = fileName;
44                this.comments = new HashMap<String, String>();
45                this.idMapping = new HashMap<String, String[]>();
46                this.reverseIdMapping = new HashMap<String, String>();
47                this.jobIndex = new HashMap<String, Long>();
48                this.appProfilesLocation = new HashMap<String, String>();
49                this.headerLoaded = false;
50                this.buildIndex = true;
51                this.fieldsNo = 18;
52               
53        }
54
55
56        /**
57         * Reads one task description and returns all fields in table.
58         * Skips comments lines, so you can mix task description with comments.
59         */
60        public String[] readTask() throws IOException {
61                String line = null;
62                fields = null;
63               
64                while((line = readLine()) != null){
65                        line = line.trim();
66                        if(line.length() == 0 || line.startsWith(COMMENT)){
67                                continue;
68                        } else{
69                                fields = line.split(FIELD_SEPARATOR+"+");
70
71                                if(fields == null || fields.length != fieldsNo)
72                                        throw new IOException("Some data fields are missing.");
73                                else
74                                        break;
75                        }
76                               
77                }
78                return fields;
79        }
80       
81               
82        public String[] readTask(String jobID, String taskId) throws IOException{
83                String key = jobID+"_"+taskId;
84                String swfJobId;
85               
86                if(reverseIdMapping.containsKey(key)){
87                        swfJobId = reverseIdMapping.get(key);
88                } else {
89                        swfJobId = jobID;
90                }
91               
92                return readTask(swfJobId);
93        }
94
95        public String getCommentValue(String label) throws NoSuchCommentException {
96                if(!comments.containsKey(label))
97                        throw new NoSuchCommentException("No comment value for "+label);
98               
99                return comments.get(label);
100        }
101
102        public String getDataField(int field) throws NoSuchFieldException {
103                if(field >= fields.length)
104                        throw new NoSuchFieldException("No such field.");
105               
106                return fields[field];
107        }
108
109        /**
110         * Reads all comments that have appropriate format and place them in HashMap.
111         * Builds job index - decision depends on value of buildIndex variable.
112         */
113        public HashMap<String, String> loadHeader() throws IOException {
114                String line = null;
115                String label = null;
116                String value = null;
117                int idx = -1;
118               
119               
120                // default speed for processing unit in millions of instructions.
121                comments.put(WAFields.COMMENT_PUSPEED, "1");
122               
123                long position = 0;
124                while((position = super.getFilePointer()) != -1 && (line = readLine()) != null){
125                        line = line.trim();
126                        if(!line.startsWith(COMMENT)){
127                                if(buildIndex){
128                                        buildIdx(line, position);
129                                }       
130                                continue;
131                        }
132                               
133                        idx = line.indexOf(":");
134                        if(idx == -1)
135                                continue;
136
137                        label = line.substring(1, idx);
138                        label = label.trim();
139
140                        if(idx + 1 > line.length())
141                                throw new IOException("No value for comment: "+label);
142                       
143                        value = line.substring(idx + 1, line.length());
144                        value = value.trim();
145                       
146                        if(label.equals(WAFields.COMMENT_NOTE) && comments.containsKey(label)){
147                                value = comments.get(label)+"; "+value;
148                        } else if(label.equals(WAFields.COMMENT_IDMAPPING)){
149                                loadIDMapping();
150                                continue;
151                        } else if(label.equals(WAFields.COMMENT_APPLICATION)){
152                                String [] valueData = value.split(" ");
153                                String appId = valueData[0];
154                                String pathToAppProfile = valueData[1];
155                                appProfilesLocation.put(appId, pathToAppProfile);
156                                continue;
157                        }
158
159                        comments.put(label, value);
160                }
161                reset();
162                headerLoaded = true;
163                return comments;
164        }
165
166        protected void loadIDMapping() throws IOException{
167                String line = null;
168               
169                while((line = readLine()) != null){
170                       
171                        line = line.trim();
172               
173                        if(!line.startsWith(COMMENT))
174                                continue;
175                       
176                        line = line.substring(1).trim(); // to cut beginning ';'
177                        if(line.startsWith(WAFields.COMMENT_IDMAPPING+": end"))
178                                break;
179                       
180                        String idsInLine[] = line.split(",");
181                        String third = null;
182                        String ids[] = null;
183                       
184                        for(int i = 0; i < idsInLine.length; i++){
185                                third = idsInLine[i];
186                                ids = third.split(":");
187                                if(ids.length < 3){
188                                        if(ids.length != 1)
189                                                if(log.isWarnEnabled())
190                                                        log.warn(third + " is not complete. Use swfJobId:xmlJobId:xmlTaskId");
191                                        continue;
192                                }
193                                String key = ids[0].trim();
194                                String value[] = new String[2];
195                                value[0] = ids[1].trim(); // job id
196                                value[1] = ids[2].trim(); // task id
197                                idMapping.put(key, value);
198                                reverseIdMapping.put(value[0]+"_"+value[1], key);
199                        }
200                       
201                }
202        }
203       
204        /**
205         * Sets comment sign. Default is ';'.
206         */
207        public String setCommentSign(String comment) {
208                String oldComment = this.COMMENT;
209                this.COMMENT = comment;
210                return oldComment;
211        }
212
213        /**
214         * Sets data fields separator. Default is tab (\t).
215         */
216        public String setFieldSeparator(String fieldSeparator) {
217                String oldSeparator = this.FIELD_SEPARATOR;
218                this.FIELD_SEPARATOR = fieldSeparator;
219                return oldSeparator;
220        }
221       
222        /**
223         * Decide if task position index should be build. It is very useful
224         * and significantly speedup
225         * @return previous value
226         */
227        public boolean buildIndex(boolean build){
228                boolean old = this.buildIndex;
229                this.buildIndex = build;
230                return old;
231        }
232
233        public void close() throws IOException {
234                super.close();
235                this.comments.clear();
236                this.comments = null;
237                this.idMapping.clear();
238                this.idMapping = null;
239                this.reverseIdMapping.clear();
240                this.reverseIdMapping = null;
241                this.jobIndex.clear();
242                this.jobIndex = null;
243        }
244
245        /**
246         * Puts file pointer to the beginning of file.
247         */
248        public void reset() throws IOException {
249                super.seek(0);
250        }
251       
252        public boolean isHeaderLoaded() {
253                return headerLoaded;
254        }
255       
256        public HashMap<String, String[]> getIDMapping(){
257                return this.idMapping;
258        }
259
260
261        public String[] getIDMapping(String waID) {
262               
263                if(idMapping.containsKey(waID)){
264                        return this.idMapping.get(waID);
265                }
266                else {  // if mapping for job is not provided, then we assume that xmlJobId, xmlTaskId, swfJobId are equal.
267                        String tab[] = {waID, waID};
268                        return tab;
269                }
270        }
271       
272       
273        public String getFileName(){
274                return this.fileName;
275        }
276       
277        protected abstract void buildIdx(String line, long position);
278       
279        public static WAParser getInstance(String fileName){
280                String name = fileName.trim().toLowerCase();
281               
282                try {
283                       
284                        if(name.endsWith("swf")){
285                                return new SWFParser(fileName);
286                               
287                        } else if(name.endsWith("gwf")){
288                                return new GWFParser(fileName);
289                        }
290                       
291                } catch(Exception e){
292                        e.printStackTrace();
293                }
294               
295                return null;
296        }
297       
298       
299        public Map<String, String> getAppProfilesLocation() {
300                return appProfilesLocation;
301        }
302       
303}
Note: See TracBrowser for help on using the repository browser.