    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 java.io.ByteArrayInputStream;
    022import java.io.ByteArrayOutputStream;
    023import java.io.IOException;
    024import java.io.InputStream;
    025import java.util.Properties;
    026
    027import org.apache.logging.log4j.LogManager;
    028import org.apache.logging.log4j.Logger;
    029import org.apache.logging.log4j.core.LoggerContext;
    030import org.apache.logging.log4j.core.config.Configuration;
    031import org.apache.logging.log4j.core.config.ConfigurationSource;
    032import org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory;
    033import org.apache.wiki.api.spi.Wiki;
    034import org.apache.wiki.util.TextUtil;
    035
    036
    037import jakarta.servlet.ServletContextEvent;
    038import jakarta.servlet.ServletContextListener;
    039
    040
    041public class WikiBootstrapServletContextListener implements ServletContextListener {
    042
    043    private static final Logger LOG = LogManager.getLogger( WikiBootstrapServletContextListener.class );
    044    private static final String[] LOG4J_CONF = new String[] { "appender", "logger", "rootLogger", "filter", "status", "dest", "name", "properties", "property", "log4j2" };
    045
    046    /** {@inheritDoc} */
    047    @Override
    048    public void contextInitialized( final ServletContextEvent sce ) {
    049        final Properties properties = initWikiSPIs( sce );
    050        initWikiLoggingFramework( properties );
    051    }
    052
    053    /**
    054     * Locate and init JSPWiki SPIs' implementations
    055     *
    056     * @param sce associated servlet context.
    057     * @return JSPWiki configuration properties.
    058     */
    059    Properties initWikiSPIs( final ServletContextEvent sce ) {
    060        return Wiki.init( sce.getServletContext() );
    061    }
    062
    063    /**
    064     * Initialize the logging framework(s). By default, we try to load the log config statements from jspwiki.properties,
    065     * unless the property jspwiki.use.external.logconfig=true, in that case we let the logging framework figure out the
    066     * logging configuration.
    067     *
    068     * @param properties JSPWiki configuration properties.
    069     * @return {@code true} if configuration was read from jspwiki.properties, {@code false} otherwise.
    070     */
    071    boolean initWikiLoggingFramework( final Properties properties ) {
    072        final String useExternalLogConfig = TextUtil.getStringProperty( properties, "jspwiki.use.external.logconfig", "false" );
    073        if ( useExternalLogConfig.equals( "false" ) ) {
    074            final ConfigurationSource source = createConfigurationSource( properties );
    075            if( source != null ) {
    076                final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
    077                final LoggerContext ctx = ( LoggerContext ) LogManager.getContext( this.getClass().getClassLoader(), false );
    078                final Configuration conf = factory.getConfiguration( ctx, source );
    079                conf.initialize();
    080                ctx.setConfiguration( conf );
    081                LOG.info( "Log configuration reloaded from Wiki properties" );
    082            }
    083        }
    084        return useExternalLogConfig.equals( "false" );
    085    }
    086
    087    ConfigurationSource createConfigurationSource( final Properties properties ) {
    088        final ByteArrayOutputStream out = new ByteArrayOutputStream();
    089        try {
    090            final Properties log4JProperties = new Properties();
    091            properties.forEach( ( k, v ) -> {
    092                for( final String log4JNsProp : LOG4J_CONF ) {
    093                    if( k.toString().startsWith( log4JNsProp ) ) {
    094                        log4JProperties.put( k, v );
    095                    }
    096                }
    097            } );
    098            log4JProperties.store( out, null );
    099            final InputStream in = new ByteArrayInputStream( out.toByteArray() );
    100            return new ConfigurationSource( in );
    101        } catch( final IOException ioe ) {
    102            LOG.error( "Unable to load the properties file into Log4j2, default Log4J2 configuration will be applied.", ioe );
    103            return null;
    104        }
    105    }
    106
    107    /** {@inheritDoc} */
    108    @Override
    109    public void contextDestroyed( final ServletContextEvent sce ) {
    110    }
    111}