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