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 v1.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
023public class ClassicVersionUtil {
024
025    // copied from VersionUtil
026    static String getVersionBySelfDeclaredProperties(Class<?> aClass, String moduleName) {
027        Properties props = new Properties();
028        // example propertiesFileName: logback-core-version.properties
029        //
030        String propertiesFileName = moduleName + "-version.properties";
031        String propertyKey = moduleName+"-version";
032        try (InputStream is = aClass.getResourceAsStream(propertiesFileName)) {
033            if (is != null) {
034                props.load(is);
035                return props.getProperty(propertyKey);
036            } else {
037                return null;
038            }
039        } catch (IOException e) {
040            return null;
041        }
042    }
043
044    /**
045     * Retrieves the version information for the "logback-classic" module based on its self-declared properties.
046     * The method looks for a properties file named "logback-classic-version.properties" within the classpath,
047     * reads its contents, and fetches the value associated with the "logback-classic-version" key.
048     *
049     * @return the version string of the "logback-classic" module if found, or null if the properties file or version
050     *         key is not present or an error occurs while reading the properties file.
051     *
052     * @since 1.5.26
053     */
054    static public String getVersionBySelfDeclaredProperties() {
055        return getVersionBySelfDeclaredProperties(ClassicConstants.class, "logback-classic");
056    }
057
058}