001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.apps.logfile;
029
030import org.opencms.main.CmsLog;
031import org.opencms.main.OpenCms;
032
033import java.io.File;
034import java.util.List;
035import java.util.TreeSet;
036
037import org.apache.commons.logging.Log;
038
039/**
040 * Provides log files which should be available as options in the OpenCms log file viewer.<p>
041 *
042 */
043public final class CmsLogFileOptionProvider {
044
045    /** Environment variable used to configure additional log files. */
046    public static final String ENV_LOGFILES = "OCCO_ADDITIONAL_LOG_DIRS";
047
048    /** Logger for this class. */
049    private static final Log LOG = CmsLog.getLog(CmsLogFileOptionProvider.class);
050
051    /**
052     * Hidden default constructor.<p>
053     */
054    private CmsLogFileOptionProvider() {
055
056        // hidden default constructor
057    }
058
059    /**
060     * Gets the additional configured log folders.<p>
061     *
062     * @return the additional configured log folders
063     */
064    public static List<String> getAdditionalLogDirectories() {
065
066        return OpenCms.getWorkplaceManager().getAdditionalLogFolderConfiguration().getLogFolders();
067
068    }
069
070    /**
071     * Gets the log file options.<p>
072     *
073     * @return the log file options
074     */
075    public static TreeSet<File> getLogFiles() {
076
077        TreeSet<File> result = new TreeSet<>();
078        for (File file : new File(CmsLogFileApp.LOG_FOLDER).listFiles()) {
079            result.add(file);
080        }
081        for (String dir : getAdditionalLogDirectories()) {
082            File file = new File(dir);
083            if (file.exists()) {
084                if (file.isDirectory()) {
085                    for (File child : file.listFiles()) {
086                        if (child.canRead()) {
087                            result.add(child);
088                        } else {
089                            LOG.error("Can not read " + child.getAbsolutePath());
090                        }
091                    }
092                }
093            }
094        }
095        return result;
096    }
097
098}