001: /*
002: * Milin Radivoj
003: * rmilin@gmail.com
004: */
005: package org.enhydra.snapper.business;
006:
007: /**
008: * @author Milin Radivoj
009: */
010:
011: import java.io.BufferedOutputStream;
012: import java.io.BufferedReader;
013: import java.io.File;
014: import java.io.FileInputStream;
015: import java.io.FileOutputStream;
016: import java.io.InputStreamReader;
017: import java.util.Date;
018: import java.util.Enumeration;
019: import java.util.Properties;
020:
021: import org.enhydra.snapper.Snapper;
022:
023: import com.lutris.logging.Logger;
024:
025: public class IncrementQueryNumber {
026:
027: static synchronized void increment(Properties siteIds) {
028:
029: if (Snapper.getStatisticActive()) {
030: for (Enumeration e = siteIds.propertyNames(); e
031: .hasMoreElements();) {
032: String siteId = (String) e.nextElement();
033:
034: String statisticFilePath = Snapper
035: .getStatisticDirectory()
036: + siteId;
037: File statisticFile = new File(statisticFilePath);
038:
039: int newValue = 1;
040: String time = (new Date(System.currentTimeMillis()))
041: .toString();
042:
043: if (!statisticFile.exists()) {
044: Snapper.log.write(Logger.WARNING, " For Site :"
045: + siteId + " statistic file "
046: + statisticFilePath
047: + " does not exist , create new one ");
048: try {
049: statisticFile.createNewFile();
050: } catch (Exception ex) {
051: Snapper.log
052: .write(
053: Logger.WARNING,
054: " For Site :"
055: + siteId
056: + " problem ocured while trying to create statistic file "
057: + ex.getMessage());
058: }
059: } else {
060: BufferedReader d = null;
061: FileInputStream fis = null;
062:
063: try {
064:
065: fis = new FileInputStream(statisticFile);
066: d = new BufferedReader(new InputStreamReader(
067: fis));
068:
069: String curentObject = d.readLine();
070: int num = (new Integer(curentObject))
071: .intValue();
072: newValue = ++num;
073:
074: } catch (Exception ex) {
075: Snapper.log
076: .write(
077: Logger.WARNING,
078: " For Site :"
079: + siteId
080: + " problem ocured while reading statistic file "
081: + statisticFilePath);
082: } finally {
083:
084: if (fis != null) {
085: try {
086: fis.close();
087: } catch (Exception e1) {
088: }
089:
090: fis = null;
091: }
092:
093: if (d != null) {
094: try {
095: d.close();
096: } catch (Exception e1) {
097: }
098:
099: d = null;
100: }
101: }
102:
103: }
104:
105: //upisujemo u fajl
106:
107: BufferedOutputStream resultOS = null;
108:
109: try {
110:
111: resultOS = new BufferedOutputStream(
112: new FileOutputStream(statisticFile, false));
113: String number = (new Integer(newValue)).toString()
114: + "\n";
115: resultOS.write(number.getBytes());
116:
117: resultOS.write(time.getBytes());
118:
119: resultOS.flush();
120:
121: } catch (Exception exx) {
122: Snapper.log.write(Logger.WARNING,
123: "Problem ocured while trying to write into statistic file : "
124: + exx.getMessage());
125: } finally {
126:
127: if (resultOS != null) {
128: try {
129: resultOS.close();
130: } catch (Exception e1) {
131: }
132:
133: resultOS = null;
134: }
135:
136: }
137: }
138: }
139: }
140: }
|