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.core.model.processor; 016 017import ch.qos.logback.core.Context; 018import ch.qos.logback.core.FileAppender; 019import ch.qos.logback.core.model.AppenderModel; 020import ch.qos.logback.core.model.ImplicitModel; 021import ch.qos.logback.core.model.Model; 022import ch.qos.logback.core.rolling.RollingFileAppender; 023import ch.qos.logback.core.rolling.helper.FileNamePattern; 024 025import java.util.HashMap; 026import java.util.List; 027import java.util.Map; 028import java.util.Optional; 029import java.util.function.Supplier; 030import java.util.stream.Collectors; 031import java.util.stream.Stream; 032 033@PhaseIndicator(phase = ProcessingPhase.DEPENDENCY_ANALYSIS) 034public class FileCollisionAnalyser extends ModelHandlerBase { 035 036 public FileCollisionAnalyser(Context context) { 037 super(context); 038 } 039 040 @Override 041 protected Class<AppenderModel> getSupportedModelClass() { 042 return AppenderModel.class; 043 } 044 045 046 @Override 047 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 048 AppenderModel appenderModel = (AppenderModel) model; 049 050 String originalClassName = appenderModel.getClassName(); 051 String className = mic.getImport(originalClassName); 052 053 String appenderName = appenderModel.getName(); 054 055 if (!fileAppenderOrRollingFileAppender(className)) { 056 return; 057 } 058 059 String tagName0 = "file"; 060 checkForCollisions(mic, MapKey.FILE_COLLISION_MAP_KEY, appenderModel, appenderName, tagName0); 061 062 String tagName1 = "fileNamePattern"; 063 checkForCollisions(mic, MapKey.RFA_FILENAME_COLLISION_MAP, appenderModel, appenderName, tagName1); 064 } 065 066 private static boolean fileAppenderOrRollingFileAppender(String className) { 067 return FileAppender.class.getName().equals(className) || RollingFileAppender.class.getName().equals(className); 068 } 069 070 071 boolean tagPredicate(Model model, String tagName) { 072 return (model instanceof ImplicitModel) && tagName.equals(model.getTag()); 073 } 074 075 enum MapKey { 076 FILE_COLLISION_MAP_KEY, RFA_FILENAME_COLLISION_MAP 077 } 078 079 private void checkForCollisions(ModelInterpretationContext mic, MapKey mapKey, AppenderModel appenderModel, String appenderName, final String tagName) { 080 081 082 Stream<Model> streamLevel1 = appenderModel.getSubModels().stream(); 083 Stream<Model> streamLevel2 = appenderModel.getSubModels().stream().flatMap(child -> child.getSubModels().stream()); 084 085 List<Model> matchingModels = Stream.concat(streamLevel1, streamLevel2).filter(m -> tagPredicate(m, tagName)).collect(Collectors.toList()); 086 087 if(!matchingModels.isEmpty()) { 088 ImplicitModel implicitModel = (ImplicitModel) matchingModels.get(0); 089 String bodyValue = mic.subst(implicitModel.getBodyText()); 090 091 092 Map<String, String> collisionMap = getCollisionMapByKey(mic, mapKey); 093 094 Optional<Map.Entry<String, String>> collision = collisionMap.entrySet() 095 .stream() 096 .filter(entry -> bodyValue.equals(entry.getValue())) 097 .findFirst(); 098 099 if (collision.isPresent()) { 100 addErrorForCollision(tagName, appenderName, collision.get().getKey(), bodyValue); 101 appenderModel.markAsHandled(); 102 appenderModel.deepMarkAsSkipped(); 103 } else { 104 // add to collision map if and only if no collision detected 105 // reasoning: single entry is as effective as multiple entries for collision detection 106 collisionMap.put(appenderName, bodyValue); 107 } 108 } 109 } 110 111 private Map<String, String> getCollisionMapByKey(ModelInterpretationContext mic, MapKey mapKey) { 112 Map<String, String> map = (Map<String, String>) mic.getObjectMap().get(mapKey.name()); 113 if(map == null) { 114 map = new HashMap<>(); 115 mic.getObjectMap().put(mapKey.name(), map); 116 } 117 return map; 118 } 119 120 121 static public final String COLLISION_DETECTED = "Collision detected. Skipping initialization of appender named [%s]"; 122 static public final String COLLISION_MESSAGE = "In appender [%s] option '%s' has the same value '%s' as that set for appender [%s] defined earlier"; 123 private void addErrorForCollision(String optionName, String appenderName, String previousAppenderName, String optionValue) { 124 addError(String.format(COLLISION_DETECTED, appenderName)); 125 addError(String.format(COLLISION_MESSAGE, appenderName, optionName, optionValue, previousAppenderName)); 126 } 127}