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.joran.GenericXMLConfigurator; 019import ch.qos.logback.core.joran.event.SaxEvent; 020import ch.qos.logback.core.joran.event.SaxEventRecorder; 021import ch.qos.logback.core.joran.spi.JoranException; 022import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil; 023import ch.qos.logback.core.model.IncludeModel; 024import ch.qos.logback.core.model.Model; 025import ch.qos.logback.core.spi.ContextAwarePropertyContainer; 026import ch.qos.logback.core.spi.ErrorCodes; 027import ch.qos.logback.core.util.Loader; 028import ch.qos.logback.core.util.OptionHelper; 029 030import java.io.File; 031import java.io.IOException; 032import java.io.InputStream; 033import java.net.MalformedURLException; 034import java.net.URI; 035import java.net.URL; 036import java.util.List; 037import java.util.function.Supplier; 038 039import static ch.qos.logback.core.joran.JoranConstants.CONFIGURATION_TAG; 040import static ch.qos.logback.core.joran.JoranConstants.INCLUDED_TAG; 041 042/** 043 * @since 1.5.5 044 */ 045public class IncludeModelHandler extends ResourceHandlerBase { 046 boolean inError = false; 047 048 public IncludeModelHandler(Context context) { 049 super(context); 050 } 051 052 static public IncludeModelHandler makeInstance(Context context, ModelInterpretationContext mic) { 053 return new IncludeModelHandler(context); 054 } 055 056 @Override 057 protected Class<IncludeModel> getSupportedModelClass() { 058 return IncludeModel.class; 059 } 060 061 @Override 062 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 063 IncludeModel includeModel = (IncludeModel) model; 064 065 URL topURL = mic.getTopURL(); 066 Boolean topScan = mic.getTopScanBoolean(); 067 Model modelFromIncludedFile = buildModelFromIncludedFile(mic, topURL, topScan, includeModel); 068 if (modelFromIncludedFile == null) { 069 warnIfRequired("Failed to build include model from included file"); 070 return; 071 } 072 processModelFromIncludedFile(includeModel, modelFromIncludedFile); 073 } 074 075 /** 076 * This method is called by logback-tyler at TylerConfigurator run-time. 077 * 078 * @param capc 079 * @param includeModel 080 * @throws ModelHandlerException 081 * @since 1.5.11 082 */ 083 public Model buildModelFromIncludedFile(ContextAwarePropertyContainer capc, URL topURL, Boolean topScan, IncludeModel includeModel) throws ModelHandlerException { 084 085 this.optional = OptionHelper.toBoolean(includeModel.getOptional(), false); 086 087 if (!checkAttributes(includeModel)) { 088 inError = true; 089 return null; 090 } 091 092 093 URL inputURL = getInputURL(capc, includeModel); 094 if (inputURL == null) { 095 inError = true; 096 return null; 097 } 098 099 InputStream in = openURL(inputURL); 100 if (in == null) { 101 inError = true; 102 return null; 103 } 104 105 updateConfigurationWatchList(inputURL, topURL, topScan); 106 107 108 SaxEventRecorder recorder = null; 109 110 try { 111 recorder = populateSaxEventRecorder(in); 112 113 List<SaxEvent> saxEvents = recorder.getSaxEventList(); 114 if (saxEvents.isEmpty()) { 115 addWarn("Empty sax event list"); 116 return null; 117 } 118 119 Supplier<? extends GenericXMLConfigurator> jcSupplier = capc.getConfiguratorSupplier(); 120 if (jcSupplier == null) { 121 addError("null configurator supplier. Abandoning inclusion of [" + attributeInUse + "]"); 122 inError = true; 123 return null; 124 } 125 126 GenericXMLConfigurator genericXMLConfigurator = jcSupplier.get(); 127 genericXMLConfigurator.getRuleStore().addPathPathMapping(INCLUDED_TAG, CONFIGURATION_TAG); 128 129 Model modelFromIncludedFile = genericXMLConfigurator.buildModelFromSaxEventList(recorder.getSaxEventList()); 130 return modelFromIncludedFile; 131 } catch (JoranException e) { 132 inError = true; 133 addError("Error processing XML data in [" + attributeInUse + "]", e); 134 return null; 135 } 136 } 137 138 private void processModelFromIncludedFile(IncludeModel includeModel, Model modelFromIncludedFile) { 139 includeModel.getSubModels().addAll(modelFromIncludedFile.getSubModels()); 140 } 141 142 public SaxEventRecorder populateSaxEventRecorder(final InputStream inputStream) throws JoranException { 143 SaxEventRecorder recorder = new SaxEventRecorder(context); 144 recorder.recordEvents(inputStream); 145 return recorder; 146 } 147 148 private void updateConfigurationWatchList(URL inputURL, URL topURL, Boolean topScan) throws ModelHandlerException { 149 150 if(topScan == Boolean.TRUE) { 151 if(topURL != null) { 152 ConfigurationWatchListUtil.addToWatchList(context, inputURL); 153 } else { 154 addWarn("No top URL for XML configuration file. Will not add [" + inputURL + "] to watch list."); 155 } 156 } 157 158 } 159 160}