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.script;
018
019import org.apache.logging.log4j.core.config.Configuration;
020import org.apache.logging.log4j.core.config.Node;
021import org.apache.logging.log4j.core.config.plugins.Plugin;
022import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
023import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
024import org.apache.logging.log4j.core.config.plugins.PluginFactory;
025
026/**
027 * Contains a reference to a script defined elsewhere in the configuration.
028 */
029@Plugin(name = "ScriptRef", category = Node.CATEGORY, printObject = true)
030public class ScriptRef extends AbstractScript {
031
032    private final ScriptManager scriptManager;
033
034    public ScriptRef(final String name, final ScriptManager scriptManager) {
035        super(name, null, null);
036        this.scriptManager = scriptManager;
037    }
038
039    @Override
040    public String getLanguage() {
041        final AbstractScript script = this.scriptManager.getScript(getName());
042        return script != null ? script.getLanguage() : null;
043    }
044
045
046    @Override
047    public String getScriptText() {
048        final AbstractScript script = this.scriptManager.getScript(getName());
049        return script != null ? script.getScriptText() : null;
050    }
051
052    @PluginFactory
053    public static ScriptRef createReference(
054            // @formatter:off
055            @PluginAttribute("ref") final String name,
056            @PluginConfiguration final Configuration configuration) {
057            // @formatter:on
058        if (name == null) {
059            LOGGER.error("No script name provided");
060            return null;
061        }
062        return new ScriptRef(name, configuration.getScriptManager());
063
064    }
065
066    @Override
067    public String toString() {
068        return "ref=" + getName();
069    }
070}