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 package ch.qos.logback.core.model.processor;
15
16 import ch.qos.logback.core.Context;
17 import ch.qos.logback.core.model.AppenderRefModel;
18 import ch.qos.logback.core.model.Model;
19
20 import java.util.List;
21
22 /**
23 * The AppenderRefDependencyAnalyser class is responsible for analyzing dependencies
24 * related to appender references within a logging model. This class extends
25 * ModelHandlerBase and operates during the dependency analysis phase of processing.
26 *
27 * The primary responsibilities of this class include:
28 * - Identifying instances of {@link AppenderRefModel} within a model hierarchy.
29 * - Substituting references to appender models using the context's interpretation logic.
30 * - Adding dependency definitions for the identified appender references to the interpretation context.
31 */
32 @PhaseIndicator(phase = ProcessingPhase.DEPENDENCY_ANALYSIS)
33 public class AppenderRefDependencyAnalyser extends ModelHandlerBase {
34
35 public AppenderRefDependencyAnalyser(Context context) {
36 super(context);
37 }
38
39 @Override
40 protected Class<Model> getSupportedModelClass() {
41 return Model.class;
42 }
43
44 @Override
45 public void handle(ModelInterpretationContext mic, Model parentModel) throws ModelHandlerException {
46 List<AppenderRefModel> appenderRefModelList = new java.util.ArrayList<>();
47 collectAllAppenderRefModels(appenderRefModelList, parentModel);
48
49 for (AppenderRefModel appenderRefModel : appenderRefModelList) {
50 // TODO: prevent substitution of references
51 String ref = appenderRefModel.getRef();
52 DependencyDefinition dd = new DependencyDefinition(parentModel, ref);
53 mic.addDependencyDefinition(dd);
54 }
55
56 }
57
58 /**
59 * Recursively processes the given Model object and its submodels, extracting instances
60 * of AppenderRefModel and adding them to the provided list.
61 *
62 * @param list the list to which AppenderRefModel instances are added
63 * @param model the root Model object from which to start the extraction
64 */
65 public void collectAllAppenderRefModels(List<AppenderRefModel> list, Model model) {
66 if(model == null)
67 return;
68 if(model instanceof AppenderRefModel) {
69 list.add((AppenderRefModel) model);
70 }
71 model.getSubModels().forEach(subModel -> collectAllAppenderRefModels(list, subModel));
72 }
73
74 }