1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17
18 package org.apache.logging.log4j.core.appender.rolling.action;
19
20 import java.nio.file.Path;
21 import java.util.List;
22 import java.util.Objects;
23
24 import javax.script.SimpleBindings;
25
26 import org.apache.logging.log4j.Logger;
27 import org.apache.logging.log4j.core.Core;
28 import org.apache.logging.log4j.core.config.Configuration;
29 import org.apache.logging.log4j.core.config.plugins.Plugin;
30 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
31 import org.apache.logging.log4j.core.config.plugins.PluginElement;
32 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
33 import org.apache.logging.log4j.core.script.AbstractScript;
34 import org.apache.logging.log4j.core.script.ScriptFile;
35 import org.apache.logging.log4j.core.script.ScriptRef;
36 import org.apache.logging.log4j.status.StatusLogger;
37
38 /**
39 * A condition of the {@link DeleteAction} where a user-provided script selects the files to delete from a provided
40 * list. The specified script may be a {@link Script}, a {@link ScriptFile} or a {@link ScriptRef}.
41 *
42 * @see #createCondition(AbstractScript, Configuration)
43 */
44 @Plugin(name = "ScriptCondition", category = Core.CATEGORY_NAME, printObject = true)
45 public class ScriptCondition {
46 private static Logger LOGGER = StatusLogger.getLogger();
47
48 private final AbstractScript script;
49 private final Configuration configuration;
50
51 /**
52 * Constructs a new ScriptCondition.
53 *
54 * @param script the script that can select files to delete
55 * @param configuration configuration containing the StrSubstitutor passed to the script
56 */
57 public ScriptCondition(final AbstractScript script, final Configuration configuration) {
58 this.script = Objects.requireNonNull(script, "script");
59 this.configuration = Objects.requireNonNull(configuration, "configuration");
60 if (!(script instanceof ScriptRef)) {
61 configuration.getScriptManager().addScript(script);
62 }
63 }
64
65 /**
66 * Executes the script
67 *
68 * @param baseDir
69 * @param candidates
70 * @return
71 */
72 @SuppressWarnings("unchecked")
73 public List<PathWithAttributes> selectFilesToDelete(final Path basePath, final List<PathWithAttributes> candidates) {
74 final SimpleBindings bindings = new SimpleBindings();
75 bindings.put("basePath", basePath);
76 bindings.put("pathList", candidates);
77 bindings.putAll(configuration.getProperties());
78 bindings.put("configuration", configuration);
79 bindings.put("substitutor", configuration.getStrSubstitutor());
80 bindings.put("statusLogger", LOGGER);
81 final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
82 return (List<PathWithAttributes>) object;
83 }
84
85 /**
86 * Creates the ScriptCondition.
87 *
88 * @param script The script to run. This may be a {@link Script}, a {@link ScriptFile} or a {@link ScriptRef}. The
89 * script must return a {@code List<PathWithAttributes>}. When the script is executed, it is provided the
90 * following bindings:
91 * <ul>
92 * <li>basePath - the directory from where the {@link DeleteAction Delete} action started scanning for
93 * files to delete. Can be used to relativize the paths in the pathList.</li>
94 * <li>pathList - a {@code java.util.List} containing {@link PathWithAttribute} objects. (The script is
95 * free to modify and return this list.)</li>
96 * <li>substitutor - a {@link StrSubstitutor} that can be used to look up variables embedded in the base
97 * dir or other properties
98 * <li>statusLogger - the {@link StatusLogger} that can be used to log events during script execution
99 * <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 }