001    package jp.osdl.jbento2.analyzer;
002    
003    import java.io.IOException;
004    import java.io.InputStream;
005    import java.io.Reader;
006    import java.util.Iterator;
007    import java.util.List;
008    import java.util.Properties;
009    
010    import org.apache.commons.io.IOUtils;
011    
012    public class GohanSampleParser implements SampleParser {
013    
014        public SampleList parse(Reader reader) throws IOException {
015            Properties prop = loadProperties();
016            
017            int startTimePosition = Integer.parseInt(prop.getProperty("startTime"));
018            int endTimePosition = Integer.parseInt(prop.getProperty("endTime"));
019            int requestTypePosition = Integer.parseInt(prop.getProperty("requestType"));
020            int responseCodePosition = Integer.parseInt(prop.getProperty("responseCode"));
021            int clientIdPosition = Integer.parseInt(prop.getProperty("clientId"));
022            
023            List lines = IOUtils.readLines(reader);
024            SampleList result = new SampleList();
025            for (Iterator it = lines.iterator(); it.hasNext(); ) {
026                String line = (String)it.next();
027                Sample s = new Sample();
028                try {
029                    String[] items = line.split(",");
030                    s.setClientId(items[clientIdPosition]);
031                    s.setRequestType(items[requestTypePosition]);
032                    
033                    long startTime = Long.parseLong(items[startTimePosition]);
034                    s.addTime(new Time(startTime));
035                    long endTime = Long.parseLong(items[endTimePosition]);
036                    s.addTime(new Time(endTime));
037    
038                    s.setError(Integer.parseInt(items[responseCodePosition]) >= 400);
039                    
040                } catch (Exception ex) {
041                    // incomplete line - Gohan was killed.
042                    continue;
043                }
044                result.add(s);
045            }
046            return result;
047        }
048        
049        private Properties loadProperties() throws IOException {
050            Properties p = new Properties();
051            InputStream in = null;
052            try {
053                in = getClass().getResourceAsStream("gohan.properties");
054                p.load(in);
055            } catch (IOException ex) {
056                throw ex;
057            } finally {
058                IOUtils.closeQuietly(in);
059            }
060            return p;
061        }
062    }