<commit_before>/*
 * Copyright 2009 Kantega AS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package no.kantega.publishing.spring;

import org.simplericity.datadirlocator.DataDirectoryLocatorStrategy;
import org.simplericity.datadirlocator.DefaultDataDirectoryLocator;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.ServletContextResourceLoader;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.BeansException;

import javax.servlet.ServletContext;
import java.io.File;
import java.util.Properties;

import no.kantega.commons.configuration.Configuration;
import no.kantega.commons.configuration.DefaultConfigurationLoader;
import no.kantega.commons.configuration.ConfigurationLoader;
import no.kantega.publishing.common.Aksess;
import no.kantega.publishing.common.util.database.dbConnectionFactory;

/**
 * Custom ContextLoaderListener for Aksess.
 * Locates the application directory and exposes it as a ${appDir} property in the Spring Application Context.
 */
public class OpenAksessContextLoaderListener extends ContextLoaderListener {

    public static final String APPLICATION_DIRECTORY = OpenAksessContextLoaderListener.class.getName() +"_APPLICATION_DIRECTORY";
    @Override
    protected ContextLoader createContextLoader() {
        return new ContextLoader() {
            @Override
            protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext wac) {

                DefaultDataDirectoryLocator locator = new DefaultDataDirectoryLocator();

                locator.setContextParamName("kantega.appDir");
                locator.setSystemProperty("kantega.appDir");

                locator.addStrategy(new LegacyKantegaDirLocatorStrategy());
                locator.setServletContext(servletContext);
                File dataDir = locator.locateDataDirectory();

                Configuration.setApplicationDirectory(dataDir);

                // Make dataDir available as Servlet Context attribute
                servletContext.setAttribute(APPLICATION_DIRECTORY, dataDir);
                
                // Set up @Autowired support
                ApplicationContextUtils.addAutowiredSupport(wac);

                // Add ${appDir} property for the Spring Context
                ApplicationContextUtils.addAppDirPropertySupport(wac);

                ConfigurationLoader loader = createConfigurationLoader(servletContext);

                final Properties properties = loader.loadConfiguration();
                final Configuration configuration = new Configuration(properties);

                Configuration.setDefaultConfiguration(configuration);

                // Set and load configuration on these classes since they are not DI-based (hackish..)
                Aksess.setConfiguration(configuration);
                Aksess.loadConfiguration();

                dbConnectionFactory.setConfiguration(configuration);
                dbConnectionFactory.loadConfiguration();

                // Add the Configuration and the ConfigurationLoader as Spring beans
                addConfigurationInstance(wac, configuration, loader);

                // Replace ${} properties in Spring with config properties
                exposeConfigurationProperties(wac, properties);

            }
        };
    }

    private void exposeConfigurationProperties(ConfigurableWebApplicationContext wac, final Properties properties) {
        wac.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
            public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
                PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
                cfg.setProperties(properties);
                cfg.postProcessBeanFactory(beanFactory);
            }
        });
    }

    private void addConfigurationInstance(ConfigurableWebApplicationContext wac, final Configuration configuration, final ConfigurationLoader loader) {
        wac.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
            public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
                beanFactory.registerSingleton("aksessConfiguration", configuration);
                beanFactory.registerSingleton("aksessConfigurationLoader", loader);

            }
        });
    }

    private ConfigurationLoader createConfigurationLoader(ServletContext context) {
        DefaultConfigurationLoader loader = new DefaultConfigurationLoader(new ServletContextResourceLoader(context));

        File dataDir = (File) context.getAttribute(APPLICATION_DIRECTORY);

        // First, load Aksess defaults
        loader.addResource("/WEB-INF/config/aksess-defaults.conf");

        loader.addResource("classpath:no/kantega/publishing/configuration/aksess-organization.conf");

        // Override with project specific settings
        loader.addResource("/WEB-INF/config/aksess-project.conf");

        // Override with environment specific setting
        loader.addResource("file:" + dataDir.getAbsolutePath() +"/conf/aksess.conf");

        return loader;
    }

    private class LegacyKantegaDirLocatorStrategy implements DataDirectoryLocatorStrategy {<commit_msg>Refactoring. Extracted an anonymous class, improved method names<commit_after>/*
 * Copyright 2009 Kantega AS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package no.kantega.publishing.spring;

import org.simplericity.datadirlocator.DataDirectoryLocatorStrategy;
import org.simplericity.datadirlocator.DefaultDataDirectoryLocator;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.ServletContextResourceLoader;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.BeansException;

import javax.servlet.ServletContext;
import java.io.File;
import java.util.Properties;

import no.kantega.commons.configuration.Configuration;
import no.kantega.commons.configuration.DefaultConfigurationLoader;
import no.kantega.commons.configuration.ConfigurationLoader;
import no.kantega.publishing.common.Aksess;
import no.kantega.publishing.common.util.database.dbConnectionFactory;

/**
 * Custom ContextLoaderListener for Aksess.
 * Locates the application directory and exposes it as a ${appDir} property in the Spring Application Context.
 */
public class OpenAksessContextLoaderListener extends ContextLoaderListener {

    public static final String APPLICATION_DIRECTORY = OpenAksessContextLoaderListener.class.getName() +"_APPLICATION_DIRECTORY";
    @Override
    protected ContextLoader createContextLoader() {
        return new OpenAksessContextLoader();

    }

    class OpenAksessContextLoader extends ContextLoader {

        @Override
        protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext wac) {

            File dataDir = getDataDirectory(servletContext);

            Configuration.setApplicationDirectory(dataDir);

            // Make dataDir available as Servlet Context attribute
            servletContext.setAttribute(APPLICATION_DIRECTORY, dataDir);

            // Set up @Autowired support
            ApplicationContextUtils.addAutowiredSupport(wac);

            // Add ${appDir} property for the Spring Context
            ApplicationContextUtils.addAppDirPropertySupport(wac);

            ConfigurationLoader loader = createConfigurationLoader(servletContext);

            final Properties properties = loader.loadConfiguration();
            final Configuration configuration = new Configuration(properties);

            Configuration.setDefaultConfiguration(configuration);

            // Set and load configuration on these classes since they are not DI-based (hackish..)
            Aksess.setConfiguration(configuration);
            Aksess.loadConfiguration();

            dbConnectionFactory.setConfiguration(configuration);
            dbConnectionFactory.loadConfiguration();

            // Add the Configuration and the ConfigurationLoader as Spring beans
            addConfigurationAndLoaderAsSingletonsInContext(wac, configuration, loader);

            // Replace ${} properties in Spring with config properties
            addConfigurationPropertyReplacer(wac, properties);

        }

        private File getDataDirectory(ServletContext servletContext) {
            DefaultDataDirectoryLocator locator = new DefaultDataDirectoryLocator();

            locator.setContextParamName("kantega.appDir");
            locator.setSystemProperty("kantega.appDir");

            locator.addStrategy(new LegacyKantegaDirLocatorStrategy());
            locator.setServletContext(servletContext);
            File dataDir = locator.locateDataDirectory();
            return dataDir;
        }
    }

    private void addConfigurationPropertyReplacer(ConfigurableWebApplicationContext wac, final Properties properties) {
        wac.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
            public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
                PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
                cfg.setProperties(properties);
                cfg.postProcessBeanFactory(beanFactory);
            }
        });
    }

    private void addConfigurationAndLoaderAsSingletonsInContext(ConfigurableWebApplicationContext wac, final Configuration configuration, final ConfigurationLoader loader) {
        wac.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
            public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
                beanFactory.registerSingleton("aksessConfiguration", configuration);
                beanFactory.registerSingleton("aksessConfigurationLoader", loader);

            }
        });
    }

    private ConfigurationLoader createConfigurationLoader(ServletContext context) {
        DefaultConfigurationLoader loader = new DefaultConfigurationLoader(new ServletContextResourceLoader(context));

        File dataDir = (File) context.getAttribute(APPLICATION_DIRECTORY);

        // First, load Aksess defaults
        loader.addResource("/WEB-INF/config/aksess-defaults.conf");

        loader.addResource("classpath:no/kantega/publishing/configuration/aksess-organization.conf");

        // Override with project specific settings
        loader.addResource("/WEB-INF/config/aksess-project.conf");

        // Override with environment specific setting
        loader.addResource("file:" + dataDir.getAbsolutePath() +"/conf/aksess.conf");

        return loader;
    }


    private class LegacyKantegaDirLocatorStrategy implements DataDirectoryLocatorStrategy {
{'fraction_non_alphanumeric': 0.06946881480299888, 'fraction_numerical': 0.001276120593396076, 'mean_word_length': 3.65441722345954, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '683958', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2782, 'n_words': 692}