com.freebox.engeneering.application.web.common.ApplicationContextListener.java Source code

Java tutorial

Introduction

Here is the source code for com.freebox.engeneering.application.web.common.ApplicationContextListener.java

Source

/*
 * Copyright 2012 Lexaden.com
 *
 *   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 com.freebox.engeneering.application.web.common;

import java.io.IOException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.freebox.engeneering.application.system.ApplicationContextLocator;
import com.lexaden.webflow.FlowConfigurationLoader;

/**
 * The class is used to find all web flow xml configs and load them into ApplicationContextLocator.
 */
public class ApplicationContextListener implements ServletContextListener {
    private static final Log LOGGER = LogFactory.getLog(ApplicationContextListener.class);

    public final String CONFIG_LOCATION_DELIMITERS = ",; \t\n";

    public static final String CONFIG_LOCATION_PARAM = "stateChartConfigLocation";

    public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/application.flows.xml";

    /**
     * Initializes the application context by web flow xml configs.
     * @param servletContextEvent the servlet context event.
     */
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        final ServletContext servletContext = servletContextEvent.getServletContext();
        final WebApplicationContext context = WebApplicationContextUtils
                .getRequiredWebApplicationContext(servletContext);

        String initParameter = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
        final String[] configLocations = getConfigLocations(context, initParameter);

        final Set<URL> xmlConfigs = new HashSet<URL>();
        for (String configLocation : configLocations) {
            try {
                final Resource[] locationResources = context.getResources(configLocation);
                for (Resource locationResource : locationResources) {
                    xmlConfigs.add(locationResource.getURL());
                }
            } catch (IOException e) {
                if (LOGGER.isErrorEnabled()) {
                    LOGGER.error("Could not find state chart configuration", e);
                }
            }
        }
        Assert.notEmpty(xmlConfigs, "Cannot find state chart configuration.");

        ApplicationContextLocator.setWebFlowConfiguration(xmlConfigs);
        ApplicationContextLocator.setApplicationContext(context);

        final FlowConfigurationLoader flowConfigurationLoader = (FlowConfigurationLoader) context
                .getBean("flowConfigurationLoader");
        flowConfigurationLoader.init(xmlConfigs);
    }

    /**
     * Reads config locations from initParameter.
     * @param context the web application context.
     * @param initParameter the init parameter.
     * @return the array of config locations.
     */
    private String[] getConfigLocations(WebApplicationContext context, String initParameter) {
        String[] configLocations = null;
        if (initParameter != null) {
            final String[] locations = StringUtils.tokenizeToStringArray(initParameter, CONFIG_LOCATION_DELIMITERS);
            if (locations != null) {
                Assert.noNullElements(locations, "Config locations must not be null");
                configLocations = new String[locations.length];
                for (int i = 0; i < locations.length; i++) {
                    configLocations[i] = context.getEnvironment().resolveRequiredPlaceholders(locations[i]).trim();
                }
            }
        }
        return configLocations != null ? configLocations : getDefaultConfigLocations();
    }

    /**
     * Returns the default config location.
     * @return the default config location.
     */
    protected String[] getDefaultConfigLocations() {
        return new String[] { DEFAULT_CONFIG_LOCATION };
    }

    /**
     * Sets the application context to null.
     * @param servletContextEvent the servlet context event.
     */
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        ApplicationContextLocator.setApplicationContext(null);
    }
}