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 static String CLASSIC_MODULE_NAME = "logback-classic"; 034 static String CLASSIC_MODULE_VERSION_PROPERTIES_FILE = CLASSIC_MODULE_NAME + "-version.properties"; 035 static String CLASSIC_MODULE_VERSION_PROPERTY_KEY = CLASSIC_MODULE_NAME + "-version"; 036 037 038 // Code copied from VersionUtil. It must be located in the encompassing module and cannot be 039 // shared. 040 // 041 // Retrieving version information by self-declared properties solves the issue of collapsed 042 // MANIFEST.MF files as encountered in fat-jars. 043 // 044 // this code further assumes that the properties file is located in the same package as the aClass 045 // parameter. 046 static String getVersionBySelfDeclaredProperties(Class<?> aClass, String moduleName) { 047 Properties props = new Properties(); 048 // example propertiesFileName: logback-core-version.properties 049 // 050 String propertiesFileName = moduleName + "-version.properties"; 051 String propertyKey = moduleName+"-version"; 052 try (InputStream is = aClass.getResourceAsStream(propertiesFileName)) { 053 if (is != null) { 054 props.load(is); 055 return props.getProperty(propertyKey); 056 } else { 057 return null; 058 } 059 } catch (IOException e) { 060 return null; 061 } 062 } 063 064 /** 065 * Retrieves the version information for the "logback-classic" module based on its self-declared properties. 066 * The method looks for a properties file named "logback-classic-version.properties" within the classpath, 067 * reads its contents, and fetches the value associated with the "logback-classic-version" key. 068 * 069 * <p>Note that the Class.getResourceAsStream() method checks that the resource is open to the 070 * caller. This entails that the caller must be in the same module as the properties file. 071 * </p> 072 * 073 * @return the version string of the "logback-classic" module if found, or null if the properties file or version 074 * key is not present or an error occurs while reading the properties file. 075 * 076 * @since 1.5.26 077 */ 078 static public String getVersionBySelfDeclaredProperties() { 079 Properties props = new Properties(); 080 081 try (InputStream is = ClassicConstants.class.getResourceAsStream(CLASSIC_MODULE_VERSION_PROPERTIES_FILE)) { 082 if (is != null) { 083 props.load(is); 084 return props.getProperty(CLASSIC_MODULE_VERSION_PROPERTY_KEY); 085 } else { 086 return null; 087 } 088 } catch (IOException e) { 089 return null; 090 } 091 } 092 093}