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.log4j.PropertyConfigurator;
022import org.apache.wiki.api.spi.Wiki;
023import org.apache.wiki.util.ClassUtil;
024import org.apache.wiki.util.TextUtil;
025
026import javax.servlet.ServletContextEvent;
027import javax.servlet.ServletContextListener;
028import java.util.Properties;
029
030
031public class WikiBootstrapServletContextListener implements ServletContextListener {
032
033    /** {@inheritDoc} */
034    @Override
035    public void contextInitialized( final ServletContextEvent sce ) {
036        final Properties properties = initWikiSPIs( sce );
037        initWikiLoggingFramework( properties );
038    }
039
040    /**
041     * Locate and init JSPWiki SPIs' implementations
042     *
043     * @param sce associated servlet context.
044     * @return JSPWiki configuration properties.
045     */
046    Properties initWikiSPIs( final ServletContextEvent sce ) {
047        return Wiki.init( sce.getServletContext() );
048    }
049
050    /**
051     * Initialize the logging framework(s). By default we try to load the log config statements from jspwiki.properties,
052     * unless the property jspwiki.use.external.logconfig=true, in that case we let the logging framework figure out the
053     * logging configuration.
054     *
055     * <p>For Log4J, we only try to initialize it if its present on classpath</p>
056     *
057     * @param properties JSPWiki configuration properties.
058     * @return {@code true} if configuration was read from jspwiki.properties, {@code false} otherwise.
059     */
060    boolean initWikiLoggingFramework( final Properties properties ) {
061        final String useExternalLogConfig = TextUtil.getStringProperty( properties,"jspwiki.use.external.logconfig","false" );
062        if( useExternalLogConfig.equals( "false" ) ) {
063            if( ClassUtil.exists( "org.apache.log4j.Logger" ) ) {
064                PropertyConfigurator.configure( properties );
065            }
066        }
067        return useExternalLogConfig.equals( "false" );
068    }
069
070    /** {@inheritDoc} */
071    @Override
072    public void contextDestroyed( final ServletContextEvent sce ) {
073    }
074
075}