001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 018package org.apache.logging.log4j.core.appender.rolling.action; 019 020import java.nio.file.Path; 021import java.util.List; 022import java.util.Objects; 023 024import javax.script.SimpleBindings; 025 026import org.apache.logging.log4j.Logger; 027import org.apache.logging.log4j.core.Core; 028import org.apache.logging.log4j.core.config.Configuration; 029import org.apache.logging.log4j.core.config.plugins.Plugin; 030import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; 031import org.apache.logging.log4j.core.config.plugins.PluginElement; 032import org.apache.logging.log4j.core.config.plugins.PluginFactory; 033import org.apache.logging.log4j.core.script.AbstractScript; 034import org.apache.logging.log4j.core.script.ScriptFile; 035import org.apache.logging.log4j.core.script.ScriptRef; 036import org.apache.logging.log4j.status.StatusLogger; 037 038/** 039 * A condition of the {@link DeleteAction} where a user-provided script selects the files to delete from a provided 040 * list. The specified script may be a {@link Script}, a {@link ScriptFile} or a {@link ScriptRef}. 041 * 042 * @see #createCondition(AbstractScript, Configuration) 043 */ 044@Plugin(name = "ScriptCondition", category = Core.CATEGORY_NAME, printObject = true) 045public class ScriptCondition { 046 private static Logger LOGGER = StatusLogger.getLogger(); 047 048 private final AbstractScript script; 049 private final Configuration configuration; 050 051 /** 052 * Constructs a new ScriptCondition. 053 * 054 * @param script the script that can select files to delete 055 * @param configuration configuration containing the StrSubstitutor passed to the script 056 */ 057 public ScriptCondition(final AbstractScript script, final Configuration configuration) { 058 this.script = Objects.requireNonNull(script, "script"); 059 this.configuration = Objects.requireNonNull(configuration, "configuration"); 060 if (!(script instanceof ScriptRef)) { 061 configuration.getScriptManager().addScript(script); 062 } 063 } 064 065 /** 066 * Executes the script 067 * 068 * @param baseDir 069 * @param candidates 070 * @return 071 */ 072 @SuppressWarnings("unchecked") 073 public List<PathWithAttributes> selectFilesToDelete(final Path basePath, final List<PathWithAttributes> candidates) { 074 final SimpleBindings bindings = new SimpleBindings(); 075 bindings.put("basePath", basePath); 076 bindings.put("pathList", candidates); 077 bindings.putAll(configuration.getProperties()); 078 bindings.put("configuration", configuration); 079 bindings.put("substitutor", configuration.getStrSubstitutor()); 080 bindings.put("statusLogger", LOGGER); 081 final Object object = configuration.getScriptManager().execute(script.getName(), bindings); 082 return (List<PathWithAttributes>) object; 083 } 084 085 /** 086 * Creates the ScriptCondition. 087 * 088 * @param script The script to run. This may be a {@link Script}, a {@link ScriptFile} or a {@link ScriptRef}. The 089 * script must return a {@code List<PathWithAttributes>}. When the script is executed, it is provided the 090 * following bindings: 091 * <ul> 092 * <li>basePath - the directory from where the {@link DeleteAction Delete} action started scanning for 093 * files to delete. Can be used to relativize the paths in the pathList.</li> 094 * <li>pathList - a {@code java.util.List} containing {@link PathWithAttribute} objects. (The script is 095 * free to modify and return this list.)</li> 096 * <li>substitutor - a {@link StrSubstitutor} that can be used to look up variables embedded in the base 097 * dir or other properties 098 * <li>statusLogger - the {@link StatusLogger} that can be used to log events during script execution 099 * <li>any properties declared in the configuration</li> 100 * </ul> 101 * @param configuration the configuration 102 * @return A ScriptCondition. 103 */ 104 @PluginFactory 105 public static ScriptCondition createCondition(@PluginElement("Script") final AbstractScript script, 106 @PluginConfiguration final Configuration configuration) { 107 108 if (script == null) { 109 LOGGER.error("A Script, ScriptFile or ScriptRef element must be provided for this ScriptCondition"); 110 return null; 111 } 112 if (script instanceof ScriptRef) { 113 if (configuration.getScriptManager().getScript(script.getName()) == null) { 114 LOGGER.error("ScriptCondition: No script with name {} has been declared.", script.getName()); 115 return null; 116 } 117 } 118 return new ScriptCondition(script, configuration); 119 } 120}