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.access.common.util; 016 017import ch.qos.logback.access.common.AccessConstants; 018import ch.qos.logback.core.Context; 019import ch.qos.logback.core.util.VersionUtil; 020 021import java.io.IOException; 022import java.io.InputStream; 023import java.util.Properties; 024 025import static ch.qos.logback.access.common.AccessConstants.LOGBACK_ACCESS_COMMON_MODULE_NAME; 026 027/** 028 * Utility class for retrieving version information about the "logback-access-common" module 029 * based on the self-declared properties file. 030 * 031 * 032 * @since 2.0.9 033 */ 034public class AccessCommonVersionUtil extends VersionUtil { 035 036 037 static String LOGBACK_ACCESS_COMMON_MODULE_VERSION_PROPERTIES_FILE = LOGBACK_ACCESS_COMMON_MODULE_NAME + "-version.properties"; 038 static String LOGBACK_ACCESS_COMMON_MODULE_VERSION_PROPERTY_KEY = LOGBACK_ACCESS_COMMON_MODULE_NAME + "-version"; 039 040 041 public AccessCommonVersionUtil(Context context) { 042 super(context); 043 } 044 045 static public String getAccessCommonVersionBySelfDeclaredProperties() { 046 Properties props = new Properties(); 047 try (InputStream is = AccessConstants.class.getResourceAsStream(LOGBACK_ACCESS_COMMON_MODULE_VERSION_PROPERTIES_FILE)) { 048 if (is != null) { 049 props.load(is); 050 return props.getProperty(LOGBACK_ACCESS_COMMON_MODULE_VERSION_PROPERTY_KEY); 051 } else { 052 return null; 053 } 054 } catch (IOException e) { 055 return null; 056 } 057 } 058 059 /** 060 * Retrieves the expected version of a specific dependency from a properties file 061 * located in the classpath of the specified depender class. The dependency version 062 * is determined based on the key provided. 063 * 064 * @param dependerClass the class whose classloader is used to locate the properties file 065 * @param propertiesFileName the name of the properties file containing dependency information 066 * @param dependencyNameAsKey the key in the properties file representing the desired dependency 067 * @return the version of the dependency as specified in the properties file, or null if the file 068 * or key is not found, or an error occurs during processing 069 * 070 * @since 2.0.10 071 */ 072 @Override 073 public String getExpectedVersionOfDependencyByProperties(Class<?> dependerClass, String propertiesFileName, String dependencyNameAsKey) { 074 Properties props = new Properties(); 075 // propertiesFileName : logback-access-common-dependencies.properties 076 077 // Class.getResourceAsStream invokes Class.isOpenToCaller(name, Reflection.getCallerClass()) 078 // Thus, the caller class must be in the same module 079 try (InputStream is = dependerClass.getResourceAsStream(propertiesFileName)) { 080 if (is != null) { 081 props.load(is); 082 return props.getProperty(dependencyNameAsKey); 083 } else { 084 return null; 085 } 086 } catch (IOException e) { 087 return null; 088 } 089 } 090}