Search in sources :

Example 1 with PluginManager

use of org.apache.logging.log4j.plugins.util.PluginManager in project logging-log4j2 by apache.

the class AbstractConfiguration method initialize.

/**
 * Initialize the configuration.
 */
@Override
public void initialize() {
    LOGGER.debug(Version.getProductString() + " initializing configuration {}", this);
    runtimeStrSubstitutor.setConfiguration(this);
    configurationStrSubstitutor.setConfiguration(this);
    try {
        ServiceLoaderUtil.loadServices(ScriptManagerFactory.class, layer -> ServiceLoader.load(layer, ScriptManagerFactory.class), null).stream().findFirst().ifPresent(scriptManagerFactory -> scriptManager = scriptManagerFactory.createScriptManager(this, watchManager));
    } catch (final LinkageError | Exception e) {
        // LOG4J2-1920 ScriptEngineManager is not available in Android
        LOGGER.info("Cannot initialize scripting support because this JRE does not support it.", e);
    }
    pluginManager.collectPlugins(pluginPackages);
    final PluginManager levelPlugins = new PluginManager(Level.CATEGORY);
    levelPlugins.collectPlugins(pluginPackages);
    final Map<String, PluginType<?>> plugins = levelPlugins.getPlugins();
    if (plugins != null) {
        for (final PluginType<?> type : plugins.values()) {
            try {
                // Cause the class to be initialized if it isn't already.
                Loader.initializeClass(type.getPluginClass().getName(), type.getPluginClass().getClassLoader());
            } catch (final Exception e) {
                LOGGER.error("Unable to initialize {} due to {}", type.getPluginClass().getName(), e.getClass().getSimpleName(), e);
            }
        }
    }
    setup();
    setupAdvertisement();
    doConfigure();
    setState(State.INITIALIZED);
    LOGGER.debug("Configuration {} initialized", this);
}
Also used : Arrays(java.util.Arrays) Source(org.apache.logging.log4j.core.util.Source) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Arbiter(org.apache.logging.log4j.core.config.arbiters.Arbiter) Level(org.apache.logging.log4j.Level) AsyncLoggerConfig(org.apache.logging.log4j.core.async.AsyncLoggerConfig) LogEvent(org.apache.logging.log4j.core.LogEvent) PluginType(org.apache.logging.log4j.plugins.util.PluginType) AsyncLoggerConfigDisruptor(org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor) Map(java.util.Map) AsyncLoggerConfigDelegate(org.apache.logging.log4j.core.async.AsyncLoggerConfigDelegate) Loader(org.apache.logging.log4j.core.util.Loader) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) PluginManager(org.apache.logging.log4j.plugins.util.PluginManager) LifeCycle(org.apache.logging.log4j.core.LifeCycle) Filter(org.apache.logging.log4j.core.Filter) AsyncAppender(org.apache.logging.log4j.core.appender.AsyncAppender) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Node(org.apache.logging.log4j.plugins.Node) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) ScriptManagerFactory(org.apache.logging.log4j.core.script.ScriptManagerFactory) NanoClock(org.apache.logging.log4j.core.time.NanoClock) Serializable(java.io.Serializable) Objects(java.util.Objects) Advertiser(org.apache.logging.log4j.core.net.Advertiser) List(java.util.List) Interpolator(org.apache.logging.log4j.core.lookup.Interpolator) ConfigurationStrSubstitutor(org.apache.logging.log4j.core.lookup.ConfigurationStrSubstitutor) PropertiesLookup(org.apache.logging.log4j.core.lookup.PropertiesLookup) StrLookup(org.apache.logging.log4j.core.lookup.StrLookup) Layout(org.apache.logging.log4j.core.Layout) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) StrSubstitutor(org.apache.logging.log4j.core.lookup.StrSubstitutor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NameUtil(org.apache.logging.log4j.util.NameUtil) Appender(org.apache.logging.log4j.core.Appender) AbstractFilterable(org.apache.logging.log4j.core.filter.AbstractFilterable) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) WatcherFactory(org.apache.logging.log4j.core.util.WatcherFactory) ScriptManager(org.apache.logging.log4j.core.script.ScriptManager) Version(org.apache.logging.log4j.core.Version) SelectArbiter(org.apache.logging.log4j.core.config.arbiters.SelectArbiter) WeakReference(java.lang.ref.WeakReference) PluginBuilder(org.apache.logging.log4j.core.config.plugins.util.PluginBuilder) RuntimeStrSubstitutor(org.apache.logging.log4j.core.lookup.RuntimeStrSubstitutor) Constants(org.apache.logging.log4j.core.util.Constants) WatchManager(org.apache.logging.log4j.core.util.WatchManager) IOException(java.io.IOException) Watcher(org.apache.logging.log4j.core.util.Watcher) TimeUnit(java.util.concurrent.TimeUnit) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) ServiceLoaderUtil(org.apache.logging.log4j.util.ServiceLoaderUtil) Collections(java.util.Collections) InputStream(java.io.InputStream) PluginManager(org.apache.logging.log4j.plugins.util.PluginManager) ScriptManagerFactory(org.apache.logging.log4j.core.script.ScriptManagerFactory) PluginType(org.apache.logging.log4j.plugins.util.PluginType) IOException(java.io.IOException)

Example 2 with PluginManager

use of org.apache.logging.log4j.plugins.util.PluginManager in project logging-log4j2 by apache.

the class ConfigurationFactory method getInstance.

/**
 * Returns the ConfigurationFactory.
 * @return the ConfigurationFactory.
 */
public static ConfigurationFactory getInstance() {
    // noinspection DoubleCheckedLocking
    if (factories == null) {
        LOCK.lock();
        try {
            if (factories == null) {
                final List<ConfigurationFactory> list = new ArrayList<>();
                final PropertiesUtil props = PropertiesUtil.getProperties();
                final String factoryClass = props.getStringProperty(CONFIGURATION_FACTORY_PROPERTY);
                if (factoryClass != null) {
                    addFactory(list, factoryClass);
                }
                final PluginManager manager = new PluginManager(CATEGORY);
                manager.collectPlugins();
                final Map<String, PluginType<?>> plugins = manager.getPlugins();
                final List<Class<? extends ConfigurationFactory>> ordered = new ArrayList<>(plugins.size());
                for (final PluginType<?> type : plugins.values()) {
                    try {
                        ordered.add(type.getPluginClass().asSubclass(ConfigurationFactory.class));
                    } catch (final Exception ex) {
                        LOGGER.warn("Unable to add class {}", type.getPluginClass(), ex);
                    }
                }
                Collections.sort(ordered, OrderComparator.getInstance());
                for (final Class<? extends ConfigurationFactory> clazz : ordered) {
                    addFactory(list, clazz);
                }
                // see above comments about double-checked locking
                // noinspection NonThreadSafeLazyInitialization
                factories = Collections.unmodifiableList(list);
                authorizationProvider = authorizationProvider(props);
            }
        } finally {
            LOCK.unlock();
        }
    }
    LOGGER.debug("Using configurationFactory {}", configFactory);
    return configFactory;
}
Also used : ArrayList(java.util.ArrayList) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) PluginType(org.apache.logging.log4j.plugins.util.PluginType) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) PluginManager(org.apache.logging.log4j.plugins.util.PluginManager)

Example 3 with PluginManager

use of org.apache.logging.log4j.plugins.util.PluginManager in project logging-log4j2 by apache.

the class ValidPortValidatorTest method setUp.

@SuppressWarnings("unchecked")
@BeforeEach
public void setUp() throws Exception {
    final PluginManager manager = new PluginManager("Test");
    manager.collectPlugins();
    plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
    assertNotNull(plugin, "Rebuild this module to ensure annotation processing has been done.");
    node = new Node(null, "HostAndPort", plugin);
    node.getAttributes().put("host", "localhost");
}
Also used : PluginManager(org.apache.logging.log4j.plugins.util.PluginManager) HostAndPort(org.apache.logging.log4j.plugins.test.validation.HostAndPort) Node(org.apache.logging.log4j.plugins.Node) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with PluginManager

use of org.apache.logging.log4j.plugins.util.PluginManager in project logging-log4j2 by apache.

the class ValidatingPluginWithGenericSubclassFoo1BuilderTest method setUp.

@SuppressWarnings("unchecked")
@BeforeEach
public void setUp() throws Exception {
    final PluginManager manager = new PluginManager("Test");
    manager.collectPlugins();
    plugin = (PluginType<PluginWithGenericSubclassFoo1Builder>) manager.getPluginType("PluginWithGenericSubclassFoo1Builder");
    assertNotNull(plugin, "Rebuild this module to make sure annotation processing kicks in.");
    node = new Node(null, "Validator", plugin);
}
Also used : PluginManager(org.apache.logging.log4j.plugins.util.PluginManager) PluginWithGenericSubclassFoo1Builder(org.apache.logging.log4j.plugins.test.validation.PluginWithGenericSubclassFoo1Builder) Node(org.apache.logging.log4j.plugins.Node) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with PluginManager

use of org.apache.logging.log4j.plugins.util.PluginManager in project logging-log4j2 by apache.

the class ValidatingPluginWithFailoverTest method setUp.

@SuppressWarnings("unchecked")
@BeforeEach
public void setUp() throws Exception {
    final PluginManager manager = new PluginManager(Core.CATEGORY_NAME);
    manager.collectPlugins();
    plugin = (PluginType<FailoverAppender>) manager.getPluginType("failover");
    assertNotNull(plugin, "Rebuild this module to make sure annotation processing kicks in.");
    AppenderRef appenderRef = AppenderRef.createAppenderRef("List", Level.ALL, null);
    node = new Node(null, "failover", plugin);
    Node failoversNode = new Node(node, "Failovers", manager.getPluginType("Failovers"));
    Node appenderRefNode = new Node(failoversNode, "appenderRef", manager.getPluginType("appenderRef"));
    appenderRefNode.getAttributes().put("ref", "file");
    appenderRefNode.setObject(appenderRef);
    failoversNode.getChildren().add(appenderRefNode);
    failoversNode.setObject(FailoversPlugin.createFailovers(appenderRef));
    node.getAttributes().put("primary", "CONSOLE");
    node.getAttributes().put("name", "Failover");
    node.getChildren().add(failoversNode);
}
Also used : PluginManager(org.apache.logging.log4j.plugins.util.PluginManager) Node(org.apache.logging.log4j.plugins.Node) FailoverAppender(org.apache.logging.log4j.core.appender.FailoverAppender) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

PluginManager (org.apache.logging.log4j.plugins.util.PluginManager)9 Node (org.apache.logging.log4j.plugins.Node)8 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ArrayList (java.util.ArrayList)2 PluginType (org.apache.logging.log4j.plugins.util.PluginType)2 PropertiesUtil (org.apache.logging.log4j.util.PropertiesUtil)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Serializable (java.io.Serializable)1 WeakReference (java.lang.ref.WeakReference)1 URISyntaxException (java.net.URISyntaxException)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1