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 */ 014package ch.qos.logback.core.joran.util; 015 016import ch.qos.logback.core.Context; 017import ch.qos.logback.core.CoreConstants; 018import ch.qos.logback.core.joran.spi.ConfigurationWatchList; 019import ch.qos.logback.core.status.ErrorStatus; 020import ch.qos.logback.core.status.InfoStatus; 021import ch.qos.logback.core.status.Status; 022import ch.qos.logback.core.status.StatusManager; 023import ch.qos.logback.core.status.WarnStatus; 024 025import java.net.URL; 026 027/** 028 * A thin layer on top of {@link ConfigurationWatchList}. 029 * 030 * @author Ceki Gülcü 031 */ 032public class ConfigurationWatchListUtil { 033 034 final static ConfigurationWatchListUtil ORIGIN = new ConfigurationWatchListUtil(); 035 036 private ConfigurationWatchListUtil() { 037 } 038 039 public static void registerConfigurationWatchList(Context context, ConfigurationWatchList cwl) { 040 context.putObject(CoreConstants.CONFIGURATION_WATCH_LIST, cwl); 041 } 042 043 /** 044 * Sets the main configuration watch URL in the given context's configuration watch list. 045 * If the provided URL is null, the method exits without making any changes. 046 * If the configuration watch list is not initialized, an error is added to the context's status. 047 * Otherwise, the configuration watch list is cleared and the given URL is set as the main URL. 048 * 049 * @param context the context in which the configuration watch list is managed 050 * @param url the main configuration watch URL to be set; if null, no action is taken 051 */ 052 public static void setMainWatchURL(Context context, URL url) { 053 if(url == null) { 054 return; 055 } 056 ConfigurationWatchList cwl = getConfigurationWatchList(context); 057 if (cwl == null) { 058 addError(context, "ConfigurationWatchList should have been initialized at this stage."); 059 return; 060 } else { 061 cwl.clear(); 062 } 063 cwl.setTopURL(url); 064 } 065 066 /** 067 * Returns true if there are watchable files, false otherwise. 068 * @return true if there are watchable files, false otherwise. 069 * @since 1.5.8 070 */ 071 public static boolean watchPredicateFulfilled(Context context) { 072 ConfigurationWatchList cwl = getConfigurationWatchList(context); 073 if (cwl == null) { 074 return false; 075 } 076 return cwl.watchPredicateFulfilled(); 077 } 078 079 public static URL getMainWatchURL(Context context) { 080 ConfigurationWatchList cwl = getConfigurationWatchList(context); 081 if (cwl == null) { 082 return null; 083 } else { 084 return cwl.getTopURL(); 085 } 086 } 087 088 public static void addToWatchList(Context context, URL url) { 089 addToWatchList(context, url, false); 090 } 091 092 public static void addToWatchList(Context context, URL url, boolean createCWL) { 093 ConfigurationWatchList cwl = getConfigurationWatchList(context); 094 if(cwl == null) { 095 if(createCWL && ConfigurationWatchList.isWatchableProtocol(url)) { 096 cwl = registerNewConfigurationWatchListWithContext(context); 097 } else { 098 addInfo(context, "ConfigurationWatchList not initialized due to absence of scan directive. Will not add " + url); 099 return; 100 } 101 } 102 103 String protocol = url.getProtocol(); 104 if(cwl.isWatchableProtocol(protocol)) { 105 addInfo(context, "Will add [" + url + "] to configuration watch list."); 106 cwl.addToWatchList(url); 107 } else { 108 addInfo(context, "Will not add configuration file ["+url + "] to watch list, because '"+protocol+"' protocol is not watchable."); 109 addInfo(context, "Only the protocols 'file', 'http' and 'https' are watchable."); 110 } 111 } 112 113 public static ConfigurationWatchList registerNewConfigurationWatchListWithContext(Context context) { 114 ConfigurationWatchList cwl = new ConfigurationWatchList(); 115 cwl.setContext(context); 116 context.putObject(CoreConstants.CONFIGURATION_WATCH_LIST, cwl); 117 return cwl; 118 } 119 120 public static ConfigurationWatchList getConfigurationWatchList(Context context) { 121 return (ConfigurationWatchList) context.getObject(CoreConstants.CONFIGURATION_WATCH_LIST); 122 } 123 124 static void addStatus(Context context, Status s) { 125 if (context == null) { 126 System.out.println("Null context in " + ConfigurationWatchList.class.getName()); 127 return; 128 } 129 StatusManager sm = context.getStatusManager(); 130 if (sm == null) 131 return; 132 sm.add(s); 133 } 134 135 static void addInfo(Context context, String msg) { 136 addStatus(context, new InfoStatus(msg, ORIGIN)); 137 } 138 139 static void addWarn(Context context, String msg) { 140 addStatus(context, new WarnStatus(msg, ORIGIN)); 141 } 142 static void addError(Context context, String msg) { 143 addStatus(context, new ErrorStatus(msg, ORIGIN)); 144 } 145}