001/*
002 *  Copyright 2018 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.odfpilotage.manager;
017
018import java.io.File;
019import java.io.IOException;
020
021import org.apache.avalon.framework.component.Component;
022import org.apache.commons.io.FileUtils;
023
024import org.ametys.core.ui.Callable;
025import org.ametys.runtime.plugin.component.AbstractLogEnabled;
026import org.ametys.runtime.util.AmetysHomeHelper;
027
028/**
029 * The manager for pilotage log files.
030 */
031public class PilotageLogFileManager extends AbstractLogEnabled implements Component
032{
033    /** The avalon role */
034    public static final String ROLE = PilotageLogFileManager.class.getName();
035    
036    /**
037     * Delete the given log file.
038     * @param logFileName The log file name
039     */
040    @Callable
041    public void deleteLog(String logFileName)
042    {
043        File logFile = new File(getLogsDirectory(), logFileName);  
044        if (logFile.exists())
045        {
046            try
047            {
048                FileUtils.forceDelete(logFile);
049            }
050            catch (IOException e)
051            {
052                getLogger().error("An error occurs during deletion of the file {}", logFile.getAbsolutePath(), e);
053            }
054        }
055    }
056    
057    /**
058     * Get the logs directory.
059     * @return The directory where the pilotage logs are stored
060     */
061    public File getLogsDirectory()
062    {
063        return new File(AmetysHomeHelper.getAmetysHome(), "logs/pilotage");
064    }
065}