001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v2.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014
015package ch.qos.logback.classic.util;
016
017import ch.qos.logback.classic.ClassicConstants;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.util.Properties;
022
023/**
024 * Utility class for retrieving version information for the "logback-classic" module.
025 * This class provides functionality to read and parse self-declared properties files
026 * containing version metadata specific to the logback-classic module.
027 *
028 * It includes methods to locate the version properties file, extract the version string
029 * based on specified conventions, and return the retrieved information.
030 */
031public class ClassicVersionUtil {
032
033    // Code copied from VersionUtil. It must be located in the encompassing module and cannot be
034    // shared.
035    //
036    // Retrieving version information by self-declared properties solves the issue of collapsed
037    // MANIFEST.MF files as encountered in fat-jars.
038    //
039    // this code further assumes that the properties file is located in the same package as the aClass
040    // parameter.
041    static String getVersionBySelfDeclaredProperties(Class<?> aClass, String moduleName) {
042        Properties props = new Properties();
043        // example propertiesFileName: logback-core-version.properties
044        //
045        String propertiesFileName = moduleName + "-version.properties";
046        String propertyKey = moduleName+"-version";
047        try (InputStream is = aClass.getResourceAsStream(propertiesFileName)) {
048            if (is != null) {
049                props.load(is);
050                return props.getProperty(propertyKey);
051            } else {
052                return null;
053            }
054        } catch (IOException e) {
055            return null;
056        }
057    }
058
059    /**
060     * Retrieves the version information for the "logback-classic" module based on its self-declared properties.
061     * The method looks for a properties file named "logback-classic-version.properties" within the classpath,
062     * reads its contents, and fetches the value associated with the "logback-classic-version" key.
063     *
064     * @return the version string of the "logback-classic" module if found, or null if the properties file or version
065     *         key is not present or an error occurs while reading the properties file.
066     *
067     * @since 1.5.26
068     */
069    static public String getVersionBySelfDeclaredProperties() {
070        return getVersionBySelfDeclaredProperties(ClassicConstants.class, "logback-classic");
071    }
072
073}