001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 *
021 */
022package org.nuxeo.ecm.platform.pictures.tiles.service;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026
027/**
028 * Task for GC dedicated Thread
029 *
030 * @author tiry
031 */
032public class GCTask implements Runnable {
033
034    public static boolean GCEnabled = false;
035
036    private static String GCINTERVAL_KEY = "GCInterval";
037
038    private static long GCInterval = 0;
039
040    private static final Log log = LogFactory.getLog(GCTask.class);
041
042    public static long getGCIntervalInMinutes() {
043        if (GCInterval == 0) {
044            GCInterval = Long.parseLong(PictureTilingComponent.getEnvValue(GCINTERVAL_KEY, Long.toString(10)));
045            log.debug("GC interval set to " + GCInterval);
046        }
047        return GCInterval;
048    }
049
050    public static void setGCIntervalInMinutes(long interval) {
051        GCInterval = interval;
052    }
053
054    public void run() {
055        log.debug("starting GC thread");
056        while (GCEnabled) {
057            PictureTilingCacheGCManager.gcIfNeeded();
058            try {
059                long gcInterval = getGCIntervalInMinutes();
060
061                if (gcInterval < 0) {
062                    // for tests
063                    log.debug("GC sleeps for " + (-gcInterval));
064                    Thread.sleep(-gcInterval);
065                } else {
066                    log.debug("GC sleeps for " + (gcInterval * 60 * 1000));
067                    // GC Interval is stored in minutes
068                    Thread.sleep(gcInterval * 60 * 1000);
069                }
070            } catch (InterruptedException e) {
071                GCEnabled = false;
072                log.info("GCThread bruttaly interupted");
073            }
074        }
075    }
076
077}