Remove test jobs from statistics
[jabaws.git] / webservices / compbio / stat / collector / InputFilter.java
1 package compbio.stat.collector;\r
2 \r
3 import java.io.BufferedReader;\r
4 import java.io.File;\r
5 import java.io.FileReader;\r
6 import java.io.IOException;\r
7 \r
8 import org.apache.log4j.Logger;\r
9 \r
10 import compbio.util.FileUtil;\r
11 import compbio.util.Util;\r
12 import compbio.ws.client.WSTester;\r
13 \r
14 public class InputFilter {\r
15 \r
16         private static final Logger log = Logger.getLogger(InputFilter.class);\r
17         /**\r
18          * Accepts input as valid unless it is a test input\r
19          * \r
20          * @param input\r
21          * @return\r
22          */\r
23         static boolean accept(File input) {\r
24                 if (input == null)\r
25                         return true;\r
26                 assert input.isFile() : "Input file is not a file! " + input;\r
27                 String[] fastainput = WSTester.fastaInput.split("\n");\r
28                 assert fastainput.length == 4;\r
29                 String[] aligninput = WSTester.fastaAlignment.split("\n");\r
30                 assert aligninput.length == 4;\r
31                 // We do now know the type of the input here so must compare with both\r
32                 // references\r
33                 boolean isReference = compareLines(input, fastainput);\r
34                 if (!isReference) {\r
35                         isReference = compareLines(input, aligninput);\r
36                 }\r
37                 // only accept genuine input\r
38                 return !isReference;\r
39         }\r
40 \r
41         private static boolean compareLines(File input, String[] reference) {\r
42                 BufferedReader reader = null;\r
43                 try {\r
44                         reader = new BufferedReader(new FileReader(input));\r
45                         // only compare first four lines of the file with reference\r
46                         // because the reference length is only 4 lines\r
47                         for (int i = 0; i < 4; i++) {\r
48                                 String line = reader.readLine();\r
49                                 if (Util.isEmpty(line)) {\r
50                                         return false;\r
51                                 }\r
52                                 line = line.trim();\r
53                                 if (!line.equals(reference[i].trim())) {\r
54                                         return false;\r
55                                 }\r
56                         }\r
57                         reader.close();\r
58                 } catch (IOException ioe) {\r
59                         log.warn(ioe, ioe.getCause());\r
60                 } finally {\r
61                         FileUtil.closeSilently(reader);\r
62                 }\r
63                 return true;\r
64         }\r
65 }\r