Search in sources :

Example 6 with LoggerContext

use of org.apache.logging.log4j.spi.LoggerContext in project mule by mulesoft.

the class MessageProcessingFlowTraceManager method dispose.

@Override
public void dispose() {
    LoggerContext context = LogManager.getContext(false);
    if (context != null && context instanceof LogConfigChangeSubject) {
        ((LogConfigChangeSubject) context).unregisterLogConfigChangeListener(logConfigChangeListener);
    }
    removeNotificationListeners();
}
Also used : LogConfigChangeSubject(org.mule.runtime.core.internal.logging.LogConfigChangeSubject) LoggerContext(org.apache.logging.log4j.spi.LoggerContext)

Example 7 with LoggerContext

use of org.apache.logging.log4j.spi.LoggerContext in project Openfire by igniterealtime.

the class PluginManager method loadPlugin.

/**
 * Loads a plugin.
 *
 * @param pluginDir the plugin directory.
 */
synchronized boolean loadPlugin(String canonicalName, Path pluginDir) {
    final PluginMetadata metadata = PluginMetadata.getInstance(pluginDir);
    pluginMetadata.put(canonicalName, metadata);
    // Only load the admin plugin during setup mode.
    if (XMPPServer.getInstance().isSetupMode() && !(canonicalName.equals("admin"))) {
        return false;
    }
    if (failureToLoadCount.containsKey(canonicalName) && failureToLoadCount.get(canonicalName) > JiveGlobals.getIntProperty("plugins.loading.retries", 5)) {
        Log.debug("The unloaded file for plugin '{}' is silently ignored, as it has failed to load repeatedly.", canonicalName);
        return false;
    }
    Log.debug("Loading plugin '{}'...", canonicalName);
    try {
        final Path pluginConfig = pluginDir.resolve("plugin.xml");
        if (!Files.exists(pluginConfig)) {
            Log.warn("Plugin '{}' could not be loaded: no plugin.xml file found.", canonicalName);
            // Don't retry - this cannot be recovered from.
            failureToLoadCount.put(canonicalName, Integer.MAX_VALUE);
            return false;
        }
        final Version currentServerVersion = XMPPServer.getInstance().getServerInfo().getVersion();
        // See if the plugin specifies a minimum version of Openfire required to run.
        if (metadata.getMinServerVersion() != null) {
            // OF-1338: Ignore release status when comparing minimum server version requirement.
            if (metadata.getMinServerVersion().isNewerThan(currentServerVersion.ignoringReleaseStatus())) {
                Log.warn("Ignoring plugin '{}': requires server version {}. Current server version is {}.", canonicalName, metadata.getMinServerVersion(), currentServerVersion);
                // Don't retry - this cannot be recovered from.
                failureToLoadCount.put(canonicalName, Integer.MAX_VALUE);
                return false;
            }
        }
        // See if the plugin specifies a maximum version of Openfire required to run.
        if (metadata.getPriorToServerVersion() != null) {
            // OF-1338: Ignore release status when comparing maximum server version requirement.
            final Version compareVersion = new Version(currentServerVersion.getMajor(), currentServerVersion.getMinor(), currentServerVersion.getMicro(), null, -1);
            if (!metadata.getPriorToServerVersion().isNewerThan(compareVersion)) {
                Log.warn("Ignoring plugin '{}': compatible with server versions up to but excluding {}. Current server version is {}.", canonicalName, metadata.getPriorToServerVersion(), currentServerVersion);
                // Don't retry - this cannot be recovered from.
                failureToLoadCount.put(canonicalName, Integer.MAX_VALUE);
                return false;
            }
        }
        // See if the plugin specifies a minimum version of Java required to run.
        if (metadata.getMinJavaVersion() != null) {
            final JavaSpecVersion runtimeVersion = new JavaSpecVersion(System.getProperty("java.specification.version"));
            if (metadata.getMinJavaVersion().isNewerThan(runtimeVersion)) {
                Log.warn("Ignoring plugin '{}': requires Java specification version {}. Openfire is currently running in Java {}.", canonicalName, metadata.getMinJavaVersion(), System.getProperty("java.specification.version"));
                // Don't retry - this cannot be recovered from.
                failureToLoadCount.put(canonicalName, Integer.MAX_VALUE);
                return false;
            }
        }
        // Initialize the plugin class loader, which is either a new instance, or a the loader from a parent plugin.
        final PluginClassLoader pluginLoader;
        // Check to see if this is a child plugin of another plugin. If it is, we re-use the parent plugin's class
        // loader so that the plugins can interact.
        String parentPluginName = null;
        Plugin parentPlugin = null;
        final String parentCanonicalName = PluginMetadataHelper.getParentPlugin(pluginDir);
        if (parentCanonicalName != null) {
            // The name of the parent plugin as specified in plugin.xml might have incorrect casing. Lookup the correct name.
            for (final Map.Entry<String, Plugin> entry : pluginsLoaded.entrySet()) {
                if (entry.getKey().equalsIgnoreCase(parentCanonicalName)) {
                    parentPluginName = entry.getKey();
                    parentPlugin = entry.getValue();
                    break;
                }
            }
            // See if the parent is loaded.
            if (parentPlugin == null) {
                Log.info("Unable to load plugin '{}': parent plugin '{}' has not been loaded.", canonicalName, parentCanonicalName);
                Integer count = failureToLoadCount.get(canonicalName);
                if (count == null) {
                    count = 0;
                }
                failureToLoadCount.put(canonicalName, ++count);
                return false;
            }
            pluginLoader = classloaders.get(parentPlugin);
        } else {
            // This is not a child plugin, so create a new class loader.
            pluginLoader = new PluginClassLoader();
        }
        // Add the plugin sources to the classloaded.
        pluginLoader.addDirectory(pluginDir.toFile());
        // Initialise a logging context, if necessary
        final Path path = pluginDir.resolve("classes/log4j2.xml");
        if (Files.isRegularFile(path)) {
            synchronized (PluginManager.class) {
                final LoggerContext loggerContext = LogManager.getContext(pluginLoader, false, path.toUri());
                loggerContext.getLogger("To avoid LOG4J2-1094");
            }
        }
        // Instantiate the plugin!
        final Document pluginXML = SAXReaderUtil.readDocument(pluginConfig.toFile());
        final String className = pluginXML.selectSingleNode("/plugin/class").getText().trim();
        final Plugin plugin;
        final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(pluginLoader);
            plugin = (Plugin) pluginLoader.loadClass(className).newInstance();
        } finally {
            Thread.currentThread().setContextClassLoader(originalClassLoader);
        }
        // Bookkeeping!
        classloaders.put(plugin, pluginLoader);
        pluginsLoaded.put(canonicalName, plugin);
        pluginDirs.put(canonicalName, pluginDir);
        // If this is a child plugin, register it as such.
        if (parentPlugin != null) {
            List<String> childrenPlugins = parentPluginMap.get(parentPlugin);
            if (childrenPlugins == null) {
                childrenPlugins = new ArrayList<>();
                parentPluginMap.put(parentPlugin, childrenPlugins);
            }
            childrenPlugins.add(canonicalName);
            // Also register child to parent relationship.
            childPluginMap.put(plugin, parentPluginName);
        }
        // Check the plugin's database schema (if it requires one).
        if (!DbConnectionManager.getSchemaManager().checkPluginSchema(plugin)) {
            // The schema was not there and auto-upgrade failed.
            Log.error("Error while loading plugin '{}': {}", canonicalName, LocaleUtils.getLocalizedString("upgrade.database.failure"));
        }
        // Load any JSP's defined by the plugin.
        final Path webXML = pluginDir.resolve("web").resolve("WEB-INF").resolve("web.xml");
        if (Files.exists(webXML)) {
            PluginServlet.registerServlets(this, plugin, webXML.toFile());
        }
        // Load any custom-defined servlets.
        final Path customWebXML = pluginDir.resolve("web").resolve("WEB-INF").resolve("web-custom.xml");
        if (Files.exists(customWebXML)) {
            PluginServlet.registerServlets(this, plugin, customWebXML.toFile());
        }
        // Configure caches of the plugin
        configureCaches(pluginDir, canonicalName);
        // Initialze the plugin.
        final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(pluginLoader);
        plugin.initializePlugin(this, pluginDir.toFile());
        Log.debug("Initialized plugin '{}'.", canonicalName);
        Thread.currentThread().setContextClassLoader(oldLoader);
        // If there a <adminconsole> section defined, register it.
        final Element adminElement = (Element) pluginXML.selectSingleNode("/plugin/adminconsole");
        if (adminElement != null) {
            final Element appName = (Element) adminElement.selectSingleNode("/plugin/adminconsole/global/appname");
            if (appName != null) {
                // Set the plugin name so that the proper i18n String can be loaded.
                appName.addAttribute("plugin", canonicalName);
            }
            // If global images are specified, override their URL.
            Element imageEl = (Element) adminElement.selectSingleNode("/plugin/adminconsole/global/logo-image");
            if (imageEl != null) {
                imageEl.setText("plugins/" + canonicalName + "/" + imageEl.getText());
                // Set the plugin name so that the proper i18n String can be loaded.
                imageEl.addAttribute("plugin", canonicalName);
            }
            imageEl = (Element) adminElement.selectSingleNode("/plugin/adminconsole/global/login-image");
            if (imageEl != null) {
                imageEl.setText("plugins/" + canonicalName + "/" + imageEl.getText());
                // Set the plugin name so that the proper i18n String can be loaded.
                imageEl.addAttribute("plugin", canonicalName);
            }
            // Modify all the URL's in the XML so that they are passed through the plugin servlet correctly.
            final List urls = adminElement.selectNodes("//@url");
            for (final Object url : urls) {
                final Attribute attr = (Attribute) url;
                attr.setValue("plugins/" + canonicalName + "/" + attr.getValue());
            }
            // In order to internationalize the names and descriptions in the model, we add a "plugin" attribute to
            // each tab, sidebar, and item so that the the renderer knows where to load the i18n Strings from.
            final String[] elementNames = new String[] { "tab", "sidebar", "item" };
            for (final String elementName : elementNames) {
                final List values = adminElement.selectNodes("//" + elementName);
                for (final Object value : values) {
                    final Element element = (Element) value;
                    // Make sure there's a name or description. Otherwise, no need to i18n settings.
                    if (element.attribute("name") != null || element.attribute("value") != null) {
                        element.addAttribute("plugin", canonicalName);
                    }
                }
            }
            AdminConsole.addModel(canonicalName, adminElement);
        }
        firePluginCreatedEvent(canonicalName, plugin);
        Log.info("Successfully loaded plugin '{}'.", canonicalName);
        return true;
    } catch (Throwable e) {
        Log.error("An exception occurred while loading plugin '{}':", canonicalName, e);
        Integer count = failureToLoadCount.get(canonicalName);
        if (count == null) {
            count = 0;
        }
        failureToLoadCount.put(canonicalName, ++count);
        if (e instanceof InterruptedException) {
            Thread.currentThread().interrupt();
        }
        return false;
    }
}
Also used : Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) Document(org.dom4j.Document) LoggerContext(org.apache.logging.log4j.spi.LoggerContext)

Example 8 with LoggerContext

use of org.apache.logging.log4j.spi.LoggerContext in project pinpoint by pinpoint-apm.

the class AccessorInjectionTest method getTestClassLoader.

private TestClassLoader getTestClassLoader() {
    LoggerContext context = LogManager.getContext();
    PLoggerFactory.initialize(new Log4j2Binder(context));
    DefaultProfilerConfig profilerConfig = new DefaultProfilerConfig();
    MockApplicationContextFactory factory = new MockApplicationContextFactory();
    this.applicationContext = factory.build(profilerConfig);
    this.applicationContext.start();
    TestClassLoader testClassLoader = new TestClassLoader(applicationContext);
    testClassLoader.initialize();
    return testClassLoader;
}
Also used : MockApplicationContextFactory(com.navercorp.pinpoint.test.MockApplicationContextFactory) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) DefaultProfilerConfig(com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig) Log4j2Binder(com.navercorp.pinpoint.profiler.logging.Log4j2Binder) LoggerContext(org.apache.logging.log4j.spi.LoggerContext)

Example 9 with LoggerContext

use of org.apache.logging.log4j.spi.LoggerContext in project pinpoint by pinpoint-apm.

the class InvokeMethodInterceptorTest method before.

/**
 * Before.
 */
@BeforeClass
public static void before() {
    LoggerContext context = LogManager.getContext();
    PLoggerFactory.initialize(new Log4j2Binder(context));
}
Also used : Log4j2Binder(com.navercorp.pinpoint.profiler.logging.Log4j2Binder) LoggerContext(org.apache.logging.log4j.spi.LoggerContext) BeforeClass(org.junit.BeforeClass)

Example 10 with LoggerContext

use of org.apache.logging.log4j.spi.LoggerContext in project hazelcast by hazelcast.

the class TestLoggerFactory method changeConfigFile.

/**
 * Changes the configuration to be used.
 *
 * @param configName the Log4j XML configuration to be used,
 *                   {@code null} for default behavior
 */
public void changeConfigFile(String configName) {
    for (LoggerContext context : loggerContexts) {
        LogManager.getFactory().removeContext(context);
    }
    configFile = configName != null ? URI.create(configName) : null;
    clearLoadedLoggers();
    loggerContexts.clear();
}
Also used : LoggerContext(org.apache.logging.log4j.spi.LoggerContext)

Aggregations

LoggerContext (org.apache.logging.log4j.spi.LoggerContext)17 Test (org.junit.Test)5 Log4j2Binder (com.navercorp.pinpoint.profiler.logging.Log4j2Binder)4 DefaultProfilerConfig (com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig)2 MockApplicationContextFactory (com.navercorp.pinpoint.test.MockApplicationContextFactory)2 TestClassLoader (com.navercorp.pinpoint.test.classloader.TestClassLoader)2 ExtendedLogger (org.apache.logging.log4j.spi.ExtendedLogger)2 BeforeClass (org.junit.BeforeClass)2 LogConfigChangeSubject (org.mule.runtime.core.internal.logging.LogConfigChangeSubject)2 ILogger (com.hazelcast.logging.ILogger)1 Log4j2Factory (com.hazelcast.logging.Log4j2Factory)1 File (java.io.File)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 ListAppender (org.apache.log4j.ListAppender)1 Logger (org.apache.log4j.Logger)1 AppenderAdapter (org.apache.log4j.bridge.AppenderAdapter)1 LoggingEvent (org.apache.log4j.spi.LoggingEvent)1 Logger (org.apache.logging.log4j.Logger)1 Appender (org.apache.logging.log4j.core.Appender)1