1 50 51 package org.openlaszlo.iv.flash.servlet; 52 53 import java.io.*; 54 import java.net.*; 55 import java.util.*; 56 57 import javax.servlet.*; 58 import javax.servlet.http.*; 59 60 import org.openlaszlo.iv.flash.util.*; 61 import org.openlaszlo.iv.flash.cache.*; 62 63 64 67 public class StatManager { 68 69 static class StatDaemon extends Thread { 70 public StatDaemon() {} 71 72 public void run() { 73 for(;;) { 74 Date date = new Date( System.currentTimeMillis() ); 75 int day = date.getDay(); 76 try { 77 Thread.sleep(1000*60); } catch( InterruptedException e ) { 79 continue; 80 } 81 long now = System.currentTimeMillis(); 82 today.setEndTime( now ); 83 save(); 84 date = new Date( now ); 85 if( day != date.getDay() ) { 86 synchronized( StatManager.class ) { 87 today = new StatUnit( now ); 88 statistic.addElement(today); 89 } 90 } 91 } 92 } 93 } 94 95 private static String statFileName; 96 private static StatUnit sinceStartup; 97 private static StatUnit today; 98 private static Vector statistic; 99 private static StatDaemon statDaemon; 100 101 private static void save() { 102 try { 103 FileOutputStream out = new FileOutputStream( statFileName ); 104 ObjectOutputStream p = new ObjectOutputStream( out ); 105 p.writeObject( statistic ); 106 p.flush(); 107 out.close(); 108 } catch( IOException e ) { 109 Log.logRB(e); 110 } 111 } 112 113 private synchronized static void load() { 114 try { 115 FileInputStream in = new FileInputStream( statFileName ); 116 ObjectInputStream p = new ObjectInputStream( in ); 117 statistic = (Vector) p.readObject(); 118 p.close(); 119 in.close(); 120 } catch( FileNotFoundException e ) { 121 statistic = new Vector(); 122 } catch( Exception e ) { 123 Log.logRB(e); 124 statistic = new Vector(); 125 } 126 today = null; 128 Date date = new Date( System.currentTimeMillis() ); 129 int tday = date.getDay(); 130 for( int i=0; i<statistic.size(); i++ ) { 131 StatUnit unit = (StatUnit) statistic.elementAt(i); 132 date = new Date( unit.getStartTime() ); 133 if( tday == date.getDay() ) { 134 today = unit; 135 break; 136 } 137 } 138 if( today == null ) { 139 today = new StatUnit( System.currentTimeMillis() ); 140 statistic.addElement( today ); 141 } 142 } 143 144 public static void init() { 145 sinceStartup = new StatUnit( System.currentTimeMillis() ); 146 statFileName = PropertyManager.getProperty("org.openlaszlo.iv.flash.statFileName","logs/stat"); 147 if( statFileName != null ) { 148 File statFile = Util.getSysFile( statFileName ); 149 statFileName = statFile.getAbsolutePath(); 150 } 151 load(); 152 statDaemon = new StatDaemon(); 153 statDaemon.start(); 154 } 155 156 public static StatUnit getSinceStartup() { 157 return sinceStartup; 158 } 159 160 public static Vector getStatistic() { 161 return statistic; 162 } 163 164 167 public static void addRequest( String fileName, int size, long processTime, long totalTime ) { 168 sinceStartup.addRequest(fileName, size, processTime, totalTime); 169 today.addRequest(fileName, size, processTime, totalTime); 170 } 171 } 172 173 | Popular Tags |