001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.bootstrap; 020 021import org.apache.logging.log4j.LogManager; 022import org.apache.logging.log4j.Logger; 023import org.apache.logging.log4j.core.LoggerContext; 024import org.apache.logging.log4j.core.config.Configuration; 025import org.apache.logging.log4j.core.config.ConfigurationSource; 026import org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory; 027import org.apache.wiki.api.spi.Wiki; 028import org.apache.wiki.util.TextUtil; 029 030import javax.servlet.ServletContextEvent; 031import javax.servlet.ServletContextListener; 032import java.io.ByteArrayInputStream; 033import java.io.ByteArrayOutputStream; 034import java.io.IOException; 035import java.io.InputStream; 036import java.util.Properties; 037 038 039public class WikiBootstrapServletContextListener implements ServletContextListener { 040 041 private static final Logger LOG = LogManager.getLogger( WikiBootstrapServletContextListener.class ); 042 043 /** {@inheritDoc} */ 044 @Override 045 public void contextInitialized( final ServletContextEvent sce ) { 046 final Properties properties = initWikiSPIs( sce ); 047 initWikiLoggingFramework( properties ); 048 } 049 050 /** 051 * Locate and init JSPWiki SPIs' implementations 052 * 053 * @param sce associated servlet context. 054 * @return JSPWiki configuration properties. 055 */ 056 Properties initWikiSPIs( final ServletContextEvent sce ) { 057 return Wiki.init( sce.getServletContext() ); 058 } 059 060 /** 061 * Initialize the logging framework(s). By default we try to load the log config statements from jspwiki.properties, 062 * unless the property jspwiki.use.external.logconfig=true, in that case we let the logging framework figure out the 063 * logging configuration. 064 * 065 * @param properties JSPWiki configuration properties. 066 * @return {@code true} if configuration was read from jspwiki.properties, {@code false} otherwise. 067 */ 068 boolean initWikiLoggingFramework( final Properties properties ) { 069 final String useExternalLogConfig = TextUtil.getStringProperty( properties, "jspwiki.use.external.logconfig", "false" ); 070 if ( useExternalLogConfig.equals( "false" ) ) { 071 final ConfigurationSource source = createConfigurationSource( properties ); 072 if( source != null ) { 073 final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory(); 074 final LoggerContext ctx = ( LoggerContext ) LogManager.getContext( this.getClass().getClassLoader(), false ); 075 final Configuration conf = factory.getConfiguration( ctx, source ); 076 conf.initialize(); 077 ctx.setConfiguration( conf ); 078 LOG.info( "Log configuration reloaded from Wiki properties" ); 079 } 080 } 081 return useExternalLogConfig.equals( "false" ); 082 } 083 084 ConfigurationSource createConfigurationSource( final Properties properties ) { 085 final ByteArrayOutputStream out = new ByteArrayOutputStream(); 086 try { 087 properties.store( out, null ); 088 final InputStream in = new ByteArrayInputStream( out.toByteArray() ); 089 return new ConfigurationSource( in ); 090 } catch( final IOException ioe ) { 091 LOG.error( "Unable to load the properties file into Log4j2, default Log4J2 configuration will be applied.", ioe ); 092 return null; 093 } 094 } 095 096 /** {@inheritDoc} */ 097 @Override 098 public void contextDestroyed( final ServletContextEvent sce ) { 099 } 100 101}