All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.cloudfarming.client.geoviewer.jxmap.map.cache.spi.CacheCleaner Maven / Gradle / Ivy

Go to download

AgroSense geoviewer JXMap implementation. Contains a map/geoviewer TopComponent based on the JXMap classes from swingx.

The newest version!
/**
 * Copyright (C) 2008-2012 AgroSense Foundation.
 *
 * AgroSense is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
 * this software, see the FLOSS License Exception
 * .
 *
 * AgroSense is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with AgroSense.  If not, see .
 */
package nl.cloudfarming.client.geoviewer.jxmap.map.cache.spi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.cloudfarming.client.geoviewer.jxmap.map.cache.options.OfflineCachePreferences;
import org.openide.filesystems.FileObject;

/**
 * 
 * @author Frantisek Post
 */
public class CacheCleaner {

    private static final Logger LOG = Logger.getLogger(CacheCleaner.class.getName());
    private static CacheCleaner instance = new CacheCleaner();
    
    public static final long DEFAULT_SIZE = 20 * 1024 * 1024; // 20 MB initial size
    public static final long UNLIMITED_SIZE = -1;
    
    private boolean running = false; //to avoid calling twice or more - tiles are loaded from different threads
    private Comparator comparator;
    protected long maximumCacheSize = OfflineCachePreferences.CACHE_LIMIT.getValue();
    
    protected CacheCleaner() {
        comparator = new Comparator() {
            
            @Override
            public int compare(FileObject file1, FileObject file2) {
                long lastModified1 = file1.lastModified().getTime();
                long lastModified2 = file2.lastModified().getTime();
                if (lastModified1 > lastModified2) {
                    return -1;
                } else if (lastModified1 < lastModified2) {
                    return 1;
                } else {
                    return 0;
                }
            }
            
        };
    }

    private List findCacheFiles(FileObject root) {
        List files = new ArrayList();
        findCacheFiles(root, files);
        return files;
    }
    
    private void findCacheFiles(FileObject file, List files) {
        if (file != null) {
            if (file.isFolder()) {
                FileObject[] children = file.getChildren(); 
                for (FileObject child : children) {
                    findCacheFiles(child, files);
                }
            } else {
                files.add(file);
            }
        }
    }
    
    private List getFilesToDelete(List files) {
        long sizeSummary = 0;
        List filesToDelete = new ArrayList();
        for (FileObject file : files) {
            sizeSummary += file.getSize();
            if (sizeSummary > maximumCacheSize) {
                filesToDelete.add(file);
            } 
        }
        return filesToDelete;
    }
    
    private void deleteFiles(List files) {
        List filesToDelete = getFilesToDelete(files);
        for (FileObject file : filesToDelete) {
            safelyDelete(file);
        }
    }
    
    
    private void safelyDelete(FileObject file) {
        try {
            file.delete();
        } catch (Throwable t) {
            LOG.log(Level.FINER, "unable to delete cache file {0}, exception {1}", new Object[] {file.getPath(), t.getMessage()});
        }
    }
    
    protected void runInternal() {
        running = true;
        try {
            List files = findCacheFiles(getRootDir());
            Collections.sort(files, comparator);
            deleteFiles(files);
        } finally {
            running = false;
        }
        
    }
    
    protected FileObject getRootDir() {
        return CacheManager.getInstance().getCacheFolder();
    }
    
    /// API ///
    
    public void run() {
        if (running || isUnlimited()) {
            return;
        }
        
        Thread thread = new Thread(new Runnable() {
            
            @Override
            public void run() {
                runInternal();
            }
            
        });
        thread.setName("Cache deleting thread");
        thread.start();
    }
    
    public static CacheCleaner getInstance() {
        return instance;
    }

    public void setMaximumCacheSize(long maximumCacheSize) {
        long oldSize = this.maximumCacheSize;
        this.maximumCacheSize = maximumCacheSize;
        //in case, the newly set maximum size is smaller than actual, check the files and delete necessary
        //or in case the cache was unlimited and I am setting limit
        if ((maximumCacheSize < oldSize && !isUnlimited()) || (oldSize == UNLIMITED_SIZE && !isUnlimited())) {
            run();
        }
    }

    public boolean isUnlimited() {
        return maximumCacheSize == UNLIMITED_SIZE;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy