1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.classic.util;
16  
17  import ch.qos.logback.classic.ClassicConstants;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.Properties;
22  
23  /**
24   * Utility class for retrieving version information for the "logback-classic" module.
25   * This class provides functionality to read and parse self-declared properties files
26   * containing version metadata specific to the logback-classic module.
27   *
28   * It includes methods to locate the version properties file, extract the version string
29   * based on specified conventions, and return the retrieved information.
30   */
31  public class ClassicVersionUtil {
32  
33      // Code copied from VersionUtil. It must be located in the encompassing module and cannot be
34      // shared.
35      //
36      // Retrieving version information by self-declared properties solves the issue of collapsed
37      // MANIFEST.MF files as encountered in fat-jars.
38      //
39      // this code further assumes that the properties file is located in the same package as the aClass
40      // parameter.
41      static String getVersionBySelfDeclaredProperties(Class<?> aClass, String moduleName) {
42          Properties props = new Properties();
43          // example propertiesFileName: logback-core-version.properties
44          //
45          String propertiesFileName = moduleName + "-version.properties";
46          String propertyKey = moduleName+"-version";
47          try (InputStream is = aClass.getResourceAsStream(propertiesFileName)) {
48              if (is != null) {
49                  props.load(is);
50                  return props.getProperty(propertyKey);
51              } else {
52                  return null;
53              }
54          } catch (IOException e) {
55              return null;
56          }
57      }
58  
59      /**
60       * Retrieves the version information for the "logback-classic" module based on its self-declared properties.
61       * The method looks for a properties file named "logback-classic-version.properties" within the classpath,
62       * reads its contents, and fetches the value associated with the "logback-classic-version" key.
63       *
64       * @return the version string of the "logback-classic" module if found, or null if the properties file or version
65       *         key is not present or an error occurs while reading the properties file.
66       *
67       * @since 1.5.26
68       */
69      static public String getVersionBySelfDeclaredProperties() {
70          return getVersionBySelfDeclaredProperties(ClassicConstants.class, "logback-classic");
71      }
72  
73  }