001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.controllers.survey;
034:
035: import java.io.File;
036: import java.io.FileInputStream;
037: import java.io.FileOutputStream;
038:
039: import java.net.URI;
040:
041: public class StatsCache {
042: private static final int MAX_AGE = 1;
043: private static File cacheDir = null;
044:
045: private static File getCacheDir() {
046: if (cacheDir == null) {
047: cacheDir = new File(System.getProperty("java.io.tmpdir"),
048: "libresource_stats_cache");
049: cacheDir.mkdirs();
050: }
051:
052: return cacheDir;
053: }
054:
055: public static void putItem(byte[] image, String map, URI uri,
056: String data) {
057: try {
058: File item = new File(getCacheDir(), getKey(uri, data)
059: + ".image");
060: FileOutputStream fos = new FileOutputStream(item);
061: fos.write(image);
062: fos.close();
063: item = new File(cacheDir, getKey(uri, data) + ".map");
064: fos = new FileOutputStream(item);
065: fos.write(map.getBytes());
066: fos.close();
067: } catch (Exception e) {
068: //
069: }
070: }
071:
072: public static byte[] getImage(URI uri, String data) {
073: try {
074: File item = new File(getCacheDir(), getKey(uri, data)
075: + ".image");
076:
077: if (!item.exists()) {
078: return null;
079: }
080:
081: if (item.lastModified() > (System.currentTimeMillis() + MAX_AGE)) {
082: item.delete();
083:
084: return null;
085: }
086:
087: FileInputStream fis = new FileInputStream(item);
088: byte[] image = new byte[(int) item.length()];
089: fis.read(image);
090: fis.close();
091:
092: return image;
093: } catch (Exception e) {
094: return null;
095: }
096: }
097:
098: public static String getMap(URI uri, String data) {
099: try {
100: File item = new File(getCacheDir(), getKey(uri, data)
101: + ".map");
102:
103: if (!item.exists()) {
104: return null;
105: }
106:
107: if (item.lastModified() > (System.currentTimeMillis() + MAX_AGE)) {
108: item.delete();
109:
110: return null;
111: }
112:
113: FileInputStream fis = new FileInputStream(item);
114: byte[] mapData = new byte[(int) item.length()];
115: fis.read(mapData);
116: fis.close();
117:
118: return new String(mapData);
119: } catch (Exception e) {
120: return null;
121: }
122: }
123:
124: private static String getKey(URI project, String data) {
125: return project.getPath().replaceAll("/", "_") + "$" + data;
126: }
127: }
|