001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 *
019 */
020package org.nuxeo.ecm.platform.pictures.tiles.service;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024
025/**
026 * Task for GC dedicated Thread
027 *
028 * @author tiry
029 */
030public class GCTask implements Runnable {
031
032    public static boolean GCEnabled = false;
033
034    private static String GCINTERVAL_KEY = "GCInterval";
035
036    private static long GCInterval = 0;
037
038    private static final Log log = LogFactory.getLog(GCTask.class);
039
040    public static long getGCIntervalInMinutes() {
041        if (GCInterval == 0) {
042            GCInterval = Long.parseLong(PictureTilingComponent.getEnvValue(GCINTERVAL_KEY, Long.toString(10)));
043            log.debug("GC interval set to " + GCInterval);
044        }
045        return GCInterval;
046    }
047
048    public static void setGCIntervalInMinutes(long interval) {
049        GCInterval = interval;
050    }
051
052    public void run() {
053        log.debug("starting GC thread");
054        while (GCEnabled) {
055            PictureTilingCacheGCManager.gcIfNeeded();
056            try {
057                long gcInterval = getGCIntervalInMinutes();
058
059                if (gcInterval < 0) {
060                    // for tests
061                    log.debug("GC sleeps for " + (-gcInterval));
062                    Thread.sleep(-gcInterval);
063                } else {
064                    log.debug("GC sleeps for " + (gcInterval * 60 * 1000));
065                    // GC Interval is stored in minutes
066                    Thread.sleep(gcInterval * 60 * 1000);
067                }
068            } catch (InterruptedException e) {
069                GCEnabled = false;
070                log.info("GCThread bruttaly interupted");
071            }
072        }
073    }
074
075}