Search in sources :

Example 1 with PluginType

use of org.apache.logging.log4j.plugins.util.PluginType 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 PluginType

use of org.apache.logging.log4j.plugins.util.PluginType 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 PluginType

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

the class TypeConverterRegistry method loadKnownTypeConverters.

private void loadKnownTypeConverters(final Collection<PluginType<?>> knownTypes) {
    for (final PluginType<?> knownType : knownTypes) {
        final Class<?> clazz = knownType.getPluginClass();
        if (TypeConverter.class.isAssignableFrom(clazz)) {
            @SuppressWarnings("rawtypes") final Class<? extends TypeConverter> pluginClass = clazz.asSubclass(TypeConverter.class);
            final Type conversionType = getTypeConverterSupportedType(pluginClass);
            final TypeConverter<?> converter = ReflectionUtil.instantiate(pluginClass);
            registerConverter(conversionType, converter);
        }
    }
}
Also used : PluginType(org.apache.logging.log4j.plugins.util.PluginType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 4 with PluginType

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

the class PluginProcessorTest method testNestedPlugin.

@Test
public void testNestedPlugin() throws Exception {
    final Plugin p = FakePlugin.Nested.class.getAnnotation(Plugin.class);
    final List<PluginType<?>> list = pluginService.getCategory(p.category());
    assertNotNull(list);
    final PluginEntry nested = getEntry(list, p.name());
    assertNotNull(nested);
    assertEquals(p.name().toLowerCase(), nested.getKey());
    assertEquals(FakePlugin.Nested.class.getName(), nested.getClassName());
    assertEquals(p.name(), nested.getName());
    assertEquals(Plugin.EMPTY, p.elementType());
    assertEquals(p.printObject(), nested.isPrintable());
    assertEquals(p.deferChildren(), nested.isDefer());
}
Also used : PluginEntry(org.apache.logging.log4j.plugins.processor.PluginEntry) FakePlugin(org.apache.logging.log4j.plugins.test.validation.FakePlugin) PluginType(org.apache.logging.log4j.plugins.util.PluginType) FakePlugin(org.apache.logging.log4j.plugins.test.validation.FakePlugin) Plugin(org.apache.logging.log4j.plugins.Plugin) Test(org.junit.Test)

Example 5 with PluginType

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

the class DefaultMergeStrategy method updateFilterNode.

private void updateFilterNode(final Node target, final Node targetChildNode, final Node sourceChildNode, final PluginManager pluginManager) {
    if (CompositeFilter.class.isAssignableFrom(targetChildNode.getType().getPluginClass())) {
        final Node node = new Node(targetChildNode, sourceChildNode.getName(), sourceChildNode.getType());
        node.getChildren().addAll(sourceChildNode.getChildren());
        node.getAttributes().putAll(sourceChildNode.getAttributes());
        targetChildNode.getChildren().add(node);
    } else {
        final PluginType pluginType = pluginManager.getPluginType(FILTERS);
        final Node filtersNode = new Node(targetChildNode, FILTERS, pluginType);
        final Node node = new Node(filtersNode, sourceChildNode.getName(), sourceChildNode.getType());
        node.getAttributes().putAll(sourceChildNode.getAttributes());
        final List<Node> children = filtersNode.getChildren();
        children.add(targetChildNode);
        children.add(node);
        final List<Node> nodes = target.getChildren();
        nodes.remove(targetChildNode);
        nodes.add(filtersNode);
    }
}
Also used : Node(org.apache.logging.log4j.plugins.Node) PluginType(org.apache.logging.log4j.plugins.util.PluginType)

Aggregations

PluginType (org.apache.logging.log4j.plugins.util.PluginType)7 ArrayList (java.util.ArrayList)2 PluginEntry (org.apache.logging.log4j.plugins.processor.PluginEntry)2 PluginManager (org.apache.logging.log4j.plugins.util.PluginManager)2 PropertiesUtil (org.apache.logging.log4j.util.PropertiesUtil)2 Test (org.junit.Test)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 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)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