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