--- android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2011/04/19 16:19:45 1310 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2011/05/02 18:19:16 1426 @@ -17,7 +17,6 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.zip.GZIPInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -27,7 +26,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import dk.thoerup.traininfoservice.banedk.TimeoutMap; +import dk.thoerup.genericjavautils.TimeoutMap; @WebServlet(urlPatterns={"/RequestPlotter"}) public class RequestPlotter extends HttpServlet { @@ -61,37 +60,34 @@ protected File[] getFiles(int count) { File accessLogDir = new File("/home/app/domain1/logs/access/"); + //File accessLogDir = new File("/home/torben/inst/glassfishv3/glassfish/domains/domain1/logs/access/"); File logFiles[] = accessLogDir.listFiles( new FilenameFilter() { @Override public boolean accept(File dir, String name) { + //log.info("name:" + name); return name.startsWith("server_access_log"); } }); + Arrays.sort(logFiles); + if (logFiles == null) { File[] empty = {}; + log.info("file array was empty"); return empty; } - int from = logFiles.length - count; - int to = logFiles.length-1; + int from = (count>logFiles.length) ? 0 : logFiles.length - (count); + int to = logFiles.length; return Arrays.copyOfRange(logFiles, from, to); } - protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ + protected PositionContainer getRequestsFromFileWorker(int count) throws IOException{ PositionContainer positions = new PositionContainer(); - try { - - int count; - - if (multiple == false) { - count = 1; - } else { - count = 4; - } + try { File files[] = getFiles(count); @@ -271,8 +267,9 @@ for(RequestPosition current : list) { String id = color + count++; sb.append( " \n" ); + sb.append( " ").append(current.time).append("\n" ); sb.append( " #").append(color).append("\n" ); - sb.append( " Time=").append(current.time).append("]]>\n" ); + sb.append( " ]]>\n" ); sb.append( " ").append(current.lng).append(",").append(current.lat).append(",0\n" ); sb.append( " \n" ); } @@ -340,20 +337,16 @@ return sb.toString(); } - protected String getRequestsFromFile(boolean multiple) throws IOException { + protected String getRequestsFromFile(int count) throws IOException { String kmlData = null; String key; - if (multiple == false) { - key = "kmldata"; - } else { - key = "kmldata-multi"; - } + key = "kmldata-" + count; kmlData = cache.get(key); if (kmlData == null) { - kmlData = formatXml( getRequestsFromFileWorker(multiple) ); + kmlData = formatXml( getRequestsFromFileWorker(count) ); cache.put(key, kmlData); kmlData += ""; } else { @@ -376,12 +369,27 @@ return (p != 0); } + int getCount(String param) { + if (param == null || param.equals("")) { + return 1; + } + + return Integer.parseInt(param); + } + + @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - boolean multiple = enabled( req.getParameter("multi") ); - String kmlData = getRequestsFromFile(multiple); + int count = getCount( req.getParameter("count") ); + + if (count > 30) //limit to 30 days + count = 30; + if (count < 0) //negative count is not allowed + count = 0; + + String kmlData = getRequestsFromFile(count); if ( enabled(req.getParameter("zip")) ) {