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