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.appender;
018
019import java.io.Serializable;
020import java.util.Objects;
021
022import javax.script.Bindings;
023
024import org.apache.logging.log4j.core.Appender;
025import org.apache.logging.log4j.core.Core;
026import org.apache.logging.log4j.core.Filter;
027import org.apache.logging.log4j.core.Layout;
028import org.apache.logging.log4j.core.LogEvent;
029import org.apache.logging.log4j.core.config.Configuration;
030import org.apache.logging.log4j.core.config.plugins.Plugin;
031import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
032import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
033import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
034import org.apache.logging.log4j.core.config.plugins.PluginElement;
035import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
036import org.apache.logging.log4j.core.script.AbstractScript;
037import org.apache.logging.log4j.core.script.ScriptManager;
038
039@Plugin(name = "ScriptAppenderSelector", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE, printObject = true)
040public class ScriptAppenderSelector extends AbstractAppender {
041
042    /**
043     * Builds an appender.
044     */
045    public static final class Builder implements org.apache.logging.log4j.core.util.Builder<Appender> {
046
047        @PluginElement("AppenderSet")
048        @Required
049        private AppenderSet appenderSet;
050
051        @PluginConfiguration
052        @Required
053        private Configuration configuration;
054
055        @PluginBuilderAttribute
056        @Required
057        private String name;
058
059        @PluginElement("Script")
060        @Required
061        private AbstractScript script;
062
063        @Override
064        public Appender build() {
065            if (name == null) {
066                LOGGER.error("Name missing.");
067                return null;
068            }
069            if (script == null) {
070                LOGGER.error("Script missing for ScriptAppenderSelector appender {}", name);
071                return null;
072            }
073            if (appenderSet == null) {
074                LOGGER.error("AppenderSet missing for ScriptAppenderSelector appender {}", name);
075                return null;
076            }
077            if (configuration == null) {
078                LOGGER.error("Configuration missing for ScriptAppenderSelector appender {}", name);
079                return null;
080            }
081            final ScriptManager scriptManager = configuration.getScriptManager();
082            scriptManager.addScript(script);
083            final Bindings bindings = scriptManager.createBindings(script);
084            final Object object = scriptManager.execute(script.getName(), bindings);
085            final String appenderName = Objects.toString(object, null);
086            final Appender appender = appenderSet.createAppender(appenderName, name);
087            return appender;
088        }
089
090        public AppenderSet getAppenderSet() {
091            return appenderSet;
092        }
093
094        public Configuration getConfiguration() {
095            return configuration;
096        }
097
098        public String getName() {
099            return name;
100        }
101
102        public AbstractScript getScript() {
103            return script;
104        }
105
106        public Builder withAppenderNodeSet(@SuppressWarnings("hiding") final AppenderSet appenderSet) {
107            this.appenderSet = appenderSet;
108            return this;
109        }
110
111        public Builder withConfiguration(@SuppressWarnings("hiding") final Configuration configuration) {
112            this.configuration = configuration;
113            return this;
114        }
115
116        public Builder withName(@SuppressWarnings("hiding") final String name) {
117            this.name = name;
118            return this;
119        }
120
121        public Builder withScript(@SuppressWarnings("hiding") final AbstractScript script) {
122            this.script = script;
123            return this;
124        }
125
126    }
127
128    @PluginBuilderFactory
129    public static Builder newBuilder() {
130        return new Builder();
131    }
132
133    private ScriptAppenderSelector(final String name, final Filter filter, final Layout<? extends Serializable> layout) {
134        super(name, filter, layout);
135    }
136
137    @Override
138    public void append(final LogEvent event) {
139        // Do nothing: This appender is only used to discover and build another appender
140    }
141
142}