com.alefissak.web.AlefissakListener.java Source code

Java tutorial

Introduction

Here is the source code for com.alefissak.web.AlefissakListener.java

Source

/**
 * Copyright 2014 - Nabil Andriantomanga.
 *
 * 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.alefissak.web;

import java.net.URL;

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

import org.apache.commons.validator.routines.UrlValidator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alefissak.Alefissak;
import com.alefissak.parsers.AlefissakPlanningParser;

/**
 * @author N. Andriantomanga
 */
public class AlefissakListener implements ServletContextListener {

    //   Log
    private static final Logger LOG = LoggerFactory.getLogger(AlefissakListener.class);

    //   In your web.xml. You can add this snippet

    //    * <context-param>
    //    *       <param-name>{PLANNINGS_CONFIG_PARAM_NAME}</param-name>
    //    *       <param-value>{plannings config uri must be specified here}</param-value>
    //    * </context-param>
    private static final String PLANNINGS_CONFIG_PARAM_NAME = "alefissak-config";

    //   Ref. to the plannings config Uri value 
    private static String planningsConfigUri = null;

    /**
     * Verify if the parameter is a valid URL 
     * 
     * @param configUri
     * @return true if configUri is a valid URL
     */
    private boolean isURL(String configUri) {
        UrlValidator urlValidator = new UrlValidator(new String[] { "http", "https" });

        return urlValidator.isValid(configUri);
    }

    @Override
    public void contextDestroyed(ServletContextEvent contextEvent) {
        // do nothing ...
    }

    /**
     * Launching Alefissak in a web context
     * {@inheritDoc}
     */
    @Override
    public void contextInitialized(ServletContextEvent contextEvent) {

        LOG.debug("Starting Alefissak Configuration");

        try {

            ServletContext servletContext = contextEvent.getServletContext();

            if (servletContext != null) {

                planningsConfigUri = servletContext.getInitParameter(PLANNINGS_CONFIG_PARAM_NAME);

                if (null != planningsConfigUri) {

                    LOG.debug("Considering the configuration located here : {}", planningsConfigUri);

                    Alefissak alefissak;

                    if (isURL(planningsConfigUri)) {

                        alefissak = new Alefissak(new AlefissakPlanningParser(new URL(planningsConfigUri)));
                    } else {

                        alefissak = new Alefissak(new AlefissakPlanningParser(planningsConfigUri));
                    }

                    alefissak.run();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}