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.core;
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 = 24;
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 project,
056: String data) {
057: try {
058: File item = new File(getCacheDir(), getKey(project, data)
059: + ".image");
060: FileOutputStream fos = new FileOutputStream(item);
061: fos.write(image);
062: fos.close();
063: item = new File(cacheDir, getKey(project, data) + ".map");
064: fos = new FileOutputStream(item);
065: fos.write(map.getBytes());
066: fos.close();
067: } catch (Exception e) {
068: }
069: }
070:
071: public static byte[] getImage(URI project, String data) {
072: try {
073: File item = new File(getCacheDir(), getKey(project, data)
074: + ".image");
075:
076: if (!item.exists()) {
077: return null;
078: }
079:
080: if ((System.currentTimeMillis() - item.lastModified()) > (MAX_AGE * 3600 * 1000)) {
081: item.delete();
082:
083: return null;
084: }
085:
086: FileInputStream fis = new FileInputStream(item);
087: byte[] image = new byte[(int) item.length()];
088: fis.read(image);
089: fis.close();
090:
091: return image;
092: } catch (Exception e) {
093: return null;
094: }
095: }
096:
097: public static String getMap(URI project, String data) {
098: try {
099: File item = new File(getCacheDir(), getKey(project, data)
100: + ".map");
101:
102: if (!item.exists()) {
103: return null;
104: }
105:
106: if ((System.currentTimeMillis() - item.lastModified()) > (MAX_AGE * 3600 * 1000)) {
107: item.delete();
108:
109: return null;
110: }
111:
112: FileInputStream fis = new FileInputStream(item);
113: byte[] mapData = new byte[(int) item.length()];
114: fis.read(mapData);
115: fis.close();
116:
117: return new String(mapData);
118: } catch (Exception e) {
119: return null;
120: }
121: }
122:
123: private static String getKey(URI project, String data) {
124: return project.getPath().replaceAll("/", "_") + "$" + data;
125: }
126: }
|