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 */
017package org.apache.logging.log4j.core.config.arbiters;
018
019import javax.script.SimpleBindings;
020
021import org.apache.logging.log4j.Logger;
022import org.apache.logging.log4j.core.config.AbstractConfiguration;
023import org.apache.logging.log4j.core.config.Configuration;
024import org.apache.logging.log4j.core.config.Node;
025import org.apache.logging.log4j.core.config.plugins.Plugin;
026import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
027import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
028import org.apache.logging.log4j.core.config.plugins.PluginNode;
029import org.apache.logging.log4j.core.config.plugins.util.PluginType;
030import org.apache.logging.log4j.core.script.AbstractScript;
031import org.apache.logging.log4j.core.script.ScriptRef;
032import org.apache.logging.log4j.status.StatusLogger;
033
034/**
035 * Condition that evaluates a script.
036 */
037@Plugin(name = "ScriptArbiter", category = Node.CATEGORY, elementType = Arbiter.ELEMENT_TYPE,
038        deferChildren = true, printObject = true)
039public class ScriptArbiter implements Arbiter {
040
041    private final AbstractScript script;
042    private final Configuration configuration;
043
044    private ScriptArbiter(final Configuration configuration, final AbstractScript script) {
045        this.configuration = configuration;
046        this.script = script;
047        if (!(script instanceof ScriptRef)) {
048            configuration.getScriptManager().addScript(script);
049        }
050    }
051
052    /**
053     * Returns the boolean result of the Script.
054     */
055    @Override
056    public boolean isCondition() {
057        final SimpleBindings bindings = new SimpleBindings();
058        bindings.putAll(configuration.getProperties());
059        bindings.put("substitutor", configuration.getStrSubstitutor());
060        final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
061        return Boolean.parseBoolean(object.toString());
062    }
063
064    @PluginBuilderFactory
065    public static Builder newBuilder() {
066        return new Builder();
067    }
068
069    public static class Builder implements org.apache.logging.log4j.core.util.Builder<ScriptArbiter> {
070
071        private static final Logger LOGGER = StatusLogger.getLogger();
072
073        @PluginConfiguration
074        private AbstractConfiguration configuration;
075
076        @PluginNode
077        private Node node;
078
079        public Builder setConfiguration(final AbstractConfiguration configuration) {
080            this.configuration = configuration;
081            return asBuilder();
082        }
083
084        public Builder setNode(final Node node) {
085            this.node = node;
086            return asBuilder();
087        }
088
089        public Builder asBuilder() {
090            return this;
091        }
092
093        public ScriptArbiter build() {
094            AbstractScript script = null;
095            for (Node child : node.getChildren()) {
096                PluginType<?> type = child.getType();
097                if (type == null) {
098                    LOGGER.error("Node {} is missing a Plugintype", child.getName());
099                    continue;
100                }
101                if (AbstractScript.class.isAssignableFrom(type.getPluginClass())) {
102                    script = (AbstractScript) configuration.createPluginObject(type, child);
103                    node.getChildren().remove(child);
104                    break;
105                }
106            }
107
108            if (script == null) {
109                LOGGER.error("A Script, ScriptFile or ScriptRef element must be provided for this ScriptFilter");
110                return null;
111            }
112            if (script instanceof ScriptRef) {
113                if (configuration.getScriptManager().getScript(script.getName()) == null) {
114                    LOGGER.error("No script with name {} has been declared.", script.getName());
115                    return null;
116                }
117            }
118            return new ScriptArbiter(configuration, script);
119        }
120    }
121}