View Javadoc
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      static String CLASSIC_MODULE_NAME = "logback-classic";
34      static String CLASSIC_MODULE_VERSION_PROPERTIES_FILE = CLASSIC_MODULE_NAME + "-version.properties";
35      static String CLASSIC_MODULE_VERSION_PROPERTY_KEY = CLASSIC_MODULE_NAME + "-version";
36  
37  
38      // Code copied from VersionUtil. It must be located in the encompassing module and cannot be
39      // shared.
40      //
41      // Retrieving version information by self-declared properties solves the issue of collapsed
42      // MANIFEST.MF files as encountered in fat-jars.
43      //
44      // this code further assumes that the properties file is located in the same package as the aClass
45      // parameter.
46      static String getVersionBySelfDeclaredProperties(Class<?> aClass, String moduleName) {
47          Properties props = new Properties();
48          // example propertiesFileName: logback-core-version.properties
49          //
50          String propertiesFileName = moduleName + "-version.properties";
51          String propertyKey = moduleName+"-version";
52          try (InputStream is = aClass.getResourceAsStream(propertiesFileName)) {
53              if (is != null) {
54                  props.load(is);
55                  return props.getProperty(propertyKey);
56              } else {
57                  return null;
58              }
59          } catch (IOException e) {
60              return null;
61          }
62      }
63  
64      /**
65       * Retrieves the version information for the "logback-classic" module based on its self-declared properties.
66       * The method looks for a properties file named "logback-classic-version.properties" within the classpath,
67       * reads its contents, and fetches the value associated with the "logback-classic-version" key.
68       *
69       * <p>Note that the Class.getResourceAsStream() method checks that the resource is open to the
70       * caller. This entails that the caller must be in the same module as the properties file.
71       * </p>
72       *
73       * @return the version string of the "logback-classic" module if found, or null if the properties file or version
74       *         key is not present or an error occurs while reading the properties file.
75       *
76       * @since 1.5.26
77       */
78      static public String getVersionBySelfDeclaredProperties() {
79          Properties props = new Properties();
80  
81          try (InputStream is = ClassicConstants.class.getResourceAsStream(CLASSIC_MODULE_VERSION_PROPERTIES_FILE)) {
82              if (is != null) {
83                  props.load(is);
84                  return props.getProperty(CLASSIC_MODULE_VERSION_PROPERTY_KEY);
85              } else {
86                  return null;
87              }
88          } catch (IOException e) {
89              return null;
90          }
91      }
92  
93  }