1 | package simulator.workload.reader.archive.swf; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | |
---|
5 | import org.apache.commons.logging.Log; |
---|
6 | import org.apache.commons.logging.LogFactory; |
---|
7 | |
---|
8 | import simulator.workload.reader.archive.AbstractWAParser; |
---|
9 | |
---|
10 | |
---|
11 | /** |
---|
12 | * |
---|
13 | * @author Marcin Krystek |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | public class SWFParser extends AbstractWAParser { |
---|
18 | |
---|
19 | private Log log = LogFactory.getLog(SWFParser.class); |
---|
20 | |
---|
21 | public SWFParser(String fileName) throws IOException, NullPointerException{ |
---|
22 | super(fileName); |
---|
23 | this.fieldsNo = 18; |
---|
24 | this.COMMENT = ";"; |
---|
25 | } |
---|
26 | |
---|
27 | |
---|
28 | public String[] readTask(String id) throws IOException{ |
---|
29 | String fields[] = null; |
---|
30 | boolean found = false; |
---|
31 | |
---|
32 | |
---|
33 | Long jobPosition = this.jobIndex.get(id); |
---|
34 | |
---|
35 | // if index for job id is calculated, then use it! |
---|
36 | if(jobPosition != null){ |
---|
37 | super.seek(jobPosition); |
---|
38 | fields = readTask(); |
---|
39 | if(!id.equals(fields[0])) { |
---|
40 | log.error("Wrong index for task: " + id + ". Found task: " + fields[0]); |
---|
41 | return null; |
---|
42 | } |
---|
43 | } else { |
---|
44 | // do classic search, read file from current position to the end, move to the beginning |
---|
45 | // and continue to current position |
---|
46 | |
---|
47 | long filePointer = getFilePointer(); |
---|
48 | // reads from current position to the end of file |
---|
49 | while(!found && (fields = readTask()) != null){ |
---|
50 | if(fields[SWFFields.DATA_JOB_NUMBER].equals(id)) |
---|
51 | found = true; |
---|
52 | } |
---|
53 | |
---|
54 | if(!found){ |
---|
55 | // back to the beginning |
---|
56 | System.out.println("search from begin for " + id); |
---|
57 | super.seek(0); |
---|
58 | // reads from the beginning of file to current position |
---|
59 | while(!found && filePointer != getFilePointer() && (fields = readTask()) != null){ |
---|
60 | if(fields[SWFFields.DATA_JOB_NUMBER].equals(id)) |
---|
61 | found = true; |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | if(found == false) return null; |
---|
66 | } |
---|
67 | |
---|
68 | return fields; |
---|
69 | } |
---|
70 | |
---|
71 | protected void buildIdx(String line, long position){ |
---|
72 | if(line == null || line.length() == 0) |
---|
73 | return; |
---|
74 | |
---|
75 | String fields[] = line.split(FIELD_SEPARATOR+"+"); |
---|
76 | |
---|
77 | if(fields == null || fields.length != fieldsNo) { |
---|
78 | int i = (fields == null ? -1 : fields.length); |
---|
79 | log.error("Can not build job index for line: " + line + |
---|
80 | "\nSome data fields are missing. Expected fields length is " + fieldsNo + ", in file is " + i ); |
---|
81 | fields = null; |
---|
82 | return; |
---|
83 | } |
---|
84 | |
---|
85 | String jobId = fields[SWFFields.DATA_JOB_NUMBER]; |
---|
86 | this.jobIndex.put(jobId, position); |
---|
87 | } |
---|
88 | |
---|
89 | public int getType(){ |
---|
90 | return 0; |
---|
91 | } |
---|
92 | |
---|
93 | } |
---|