private void initializeJndi(final String location) { final URI configLocation = getConfigURI(location); if (this.name == null) { throw new IllegalStateException("A log4jContextName context parameter is required"); } LoggerContext context; final LoggerContextFactory factory = LogManager.getFactory(); if (factory instanceof Log4jContextFactory) { final ContextSelector selector = ((Log4jContextFactory) factory).getSelector(); if (selector instanceof NamedContextSelector) { this.namedContextSelector = (NamedContextSelector) selector; context = this.namedContextSelector.locateContext(this.name, this.servletContext, configLocation); ContextAnchor.THREAD_CONTEXT.set(context); if (context.isInitialized()) { context.start(); } ContextAnchor.THREAD_CONTEXT.remove(); } else { LOGGER.warn("Potential problem: Selector is not an instance of NamedContextSelector."); return; } } else { LOGGER.warn("Potential problem: LoggerContextFactory is not an instance of Log4jContextFactory."); return; } this.loggerContext = context; LOGGER.debug("Created logger context for [{}] using [{}].", this.name, context.getClass().getClassLoader()); }
@Override public synchronized boolean stop(final long timeout, final TimeUnit timeUnit) { if (!this.isStarted() && !this.isStopped()) { throw new IllegalStateException("Cannot stop this Log4jWebInitializer because it has not started."); } // only do this once if (this.isStarted()) { this.setStopping(); if (this.loggerContext != null) { LOGGER.debug("Removing LoggerContext for [{}].", this.name); this.servletContext.removeAttribute(CONTEXT_ATTRIBUTE); if (this.namedContextSelector != null) { this.namedContextSelector.removeContext(this.name); } this.loggerContext.stop(timeout, timeUnit); this.loggerContext.setExternalContext(null); this.loggerContext = null; } this.setStopped(); } return super.stop(timeout, timeUnit); }
public void destroy() { final LoggerContext ctx = (LoggerContext) context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE); if (ctx != null && created) { context.log("Removing context for " + name); context.removeAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE); if (selector != null) { selector.removeContext(name); } ctx.stop(); } } }
public void init(final FilterConfig filterConfig) throws ServletException { context = filterConfig.getServletContext(); name = filterConfig.getInitParameter(CONTEXT_NAME); final String configLocn = filterConfig.getInitParameter(CONFIG_LOCATION); if (name == null) { throw new UnavailableException("A context-name attribute is required"); } if (context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE) == null) { LoggerContext ctx; final LoggerContextFactory factory = LogManager.getFactory(); if (factory instanceof Log4jContextFactory) { final ContextSelector sel = ((Log4jContextFactory) factory).getSelector(); if (sel instanceof NamedContextSelector) { selector = (NamedContextSelector) sel; ctx = selector.locateContext(name, configLocn); } else { return; } } else { return; } context.setAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE, ctx); created = true; context.log("Created context for " + name + " using " + ctx.getClass().getClassLoader()); } }