/* * Copyright 2013, The Sporting Exchange Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.betfair.testing.utils.cougar.manager; import java.io.File; import java.io.IOException; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.Map; /** */ public class ServiceLogTailer extends LogTailer<ServiceLogRequirement> { public static final String MESSAGE = "_MESSAGE"; private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); public ServiceLogTailer(File toRead) throws IOException { super(toRead, 60000L); } @Override protected Map<String, String> getFieldsForLine(String s) { int comma = s.indexOf(": "); if (comma > 0) { Map<String, String> ret = new HashMap<String, String>(); ret.put(LogTailer.DATE_FIELD, s.substring(0,comma)); ret.put(MESSAGE, s.substring(comma+2)); return ret; } else { return null; } } @Override protected Timestamp toDate(String dateFieldValue) throws ParseException { return new Timestamp(dateFormat.parse(dateFieldValue).getTime()); } @Override protected boolean matches(LogLine line, ServiceLogRequirement requirement) { if (requirement.message != null) { if (requirement.containsCheck) { if (!line.getFields().get(MESSAGE).contains(requirement.message)) { return false; } } else { if (!requirement.message.equals(line.getFields().get(MESSAGE))) { return false; } } } return true; } }