001/*
002 *  Copyright 2011 Anyware Services
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 */
016package org.ametys.plugins.skincommons;
017
018import java.io.File;
019import java.io.IOException;
020import java.nio.file.Files;
021import java.nio.file.Path;
022
023import org.apache.commons.io.FileUtils;
024
025import org.ametys.core.util.path.PathUtils;
026
027/**
028 * Thread for asynchronous file deletion
029 *
030 */
031public class AsynchronousPathDeletion implements Runnable
032{
033    Path _file;
034    
035    /**
036     * Constructor
037     * @param file The file to delete
038     */
039    public AsynchronousPathDeletion (Path file)
040    {
041        _file = file;
042    }
043    
044    @Override
045    public void run()
046    {
047        if (_file != null)
048        {
049            try
050            {
051                if (Files.isDirectory(_file))
052                {
053                    PathUtils.deleteDirectory(_file);
054                }
055                else 
056                {
057                    Files.deleteIfExists(_file);
058                }
059            }
060            catch (IOException e)
061            {
062                // Emtpy
063                try
064                {
065                    // There is no deleteOnExit for path... switching to File since we know we are working on default file system here
066                    File f = _file.toFile();
067                    FileUtils.forceDeleteOnExit(f);
068                }
069                catch (IOException uoe)
070                {
071                    // Nothing
072                }
073            }
074            
075        }
076
077    }
078
079}