All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.logging.log4j.core.appender.rolling.action.ScriptCondition Maven / Gradle / Ivy

There is a newer version: 3.0.0-beta2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */

package org.apache.logging.log4j.core.appender.rolling.action;

import java.nio.file.Path;
import java.util.List;
import java.util.Objects;

import javax.script.SimpleBindings;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.script.AbstractScript;
import org.apache.logging.log4j.core.script.ScriptRef;
import org.apache.logging.log4j.status.StatusLogger;

/**
 * A condition of the {@link DeleteAction} where a user-provided script selects the files to delete from a provided
 * list. The specified script may be a {@link Script}, a {@link ScriptFile} or a {@link ScriptRef}.
 * 
 * @see #createCondition(AbstractScript, Configuration)
 */
@Plugin(name = "ScriptCondition", category = "Core", printObject = true)
public class ScriptCondition {
    private static Logger LOGGER = StatusLogger.getLogger();

    private final AbstractScript script;
    private final Configuration configuration;

    /**
     * Constructs a new ScriptCondition.
     * 
     * @param script the script that can select files to delete
     * @param configuration configuration containing the StrSubstitutor passed to the script
     */
    public ScriptCondition(final AbstractScript script, final Configuration configuration) {
        this.script = Objects.requireNonNull(script, "script");
        this.configuration = Objects.requireNonNull(configuration, "configuration");
        if (!(script instanceof ScriptRef)) {
            configuration.getScriptManager().addScript(script);
        }
    }

    /**
     * Executes the script
     * 
     * @param baseDir
     * @param candidates
     * @return
     */
    @SuppressWarnings("unchecked")
    public List selectFilesToDelete(final Path basePath, final List candidates) {
        final SimpleBindings bindings = new SimpleBindings();
        bindings.put("basePath", basePath);
        bindings.put("pathList", candidates);
        bindings.putAll(configuration.getProperties());
        bindings.put("configuration", configuration);
        bindings.put("substitutor", configuration.getStrSubstitutor());
        bindings.put("statusLogger", LOGGER);
        final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
        return (List) object;
    }

    /**
     * Creates the ScriptCondition.
     * 
     * @param script The script to run. This may be a {@link Script}, a {@link ScriptFile} or a {@link ScriptRef}. The
     *            script must return a {@code List}. When the script is executed, it is provided the
     *            following bindings:
     *            
    *
  • basePath - the directory from where the {@link DeleteAction Delete} action started scanning for * files to delete. Can be used to relativize the paths in the pathList.
  • *
  • pathList - a {@code java.util.List} containing {@link PathWithAttribute} objects. (The script is * free to modify and return this list.)
  • *
  • substitutor - a {@link StrSubstitutor} that can be used to look up variables embedded in the base * dir or other properties *
  • statusLogger - the {@link StatusLogger} that can be used to log events during script execution *
  • any properties declared in the configuration
  • *
* @param configuration the configuration * @return A ScriptCondition. */ @PluginFactory public static ScriptCondition createCondition(@PluginElement("Script") final AbstractScript script, @PluginConfiguration final Configuration configuration) { if (script == null) { LOGGER.error("A Script, ScriptFile or ScriptRef element must be provided for this ScriptCondition"); return null; } if (script instanceof ScriptRef) { if (configuration.getScriptManager().getScript(script.getName()) == null) { LOGGER.error("ScriptCondition: No script with name {} has been declared.", script.getName()); return null; } } return new ScriptCondition(script, configuration); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy