de.romankreisel.faktotum.FaktotumContextListener.java Source code

Java tutorial

Introduction

Here is the source code for de.romankreisel.faktotum.FaktotumContextListener.java

Source

/**
 * This file is part of Faktotum.
 *
 *
 *  Faktotum is free software: you can redistribute it and/or 
 *  modify it under the terms of the GNU Lesser General Public License 
 *  as published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 *
 *  Faktotum is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with Faktotum.  
 *
 *  If not, see <http://www.gnu.org/licenses/>.
 */
package de.romankreisel.faktotum;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.logging.Level;

import javax.ejb.EJB;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.lang3.StringUtils;

import de.romankreisel.faktotum.beans.AuthenticationBean;

/**
 * @author Roman Kreisel <mail@romankreisel.de>
 * 
 *         This class is loaded on application-loadtime and initializes Faktotum
 *         It can be used internally to retrieve some information like build
 *         version and time and uptime
 * 
 */
public class FaktotumContextListener extends FaktotumObject implements ServletContextListener {

    private static Date buildTime = new Date();

    private static final Date startTime = new Date();

    private static String version = null;

    private static final String RELATIVE_MANIFEST_PATH = "/META-INF/MANIFEST.MF";

    private final static String DEVELOPMENT_VERSION = "DEVELOPMENT";

    /**
     * @return the buildDate
     */
    public static Date getBuildTime() {
        return FaktotumContextListener.buildTime;
    }

    /**
     * @return the startTime
     */
    public static Date getStartTime() {
        return FaktotumContextListener.startTime;
    }

    /**
     * @return the version
     */
    public static String getVersion() {
        String projectVersion = FaktotumContextListener.version;
        if (StringUtils.isBlank(projectVersion)) {
            projectVersion = FaktotumContextListener.DEVELOPMENT_VERSION;
        }
        return projectVersion;
    }

    @EJB
    private AuthenticationBean authenticationBean;

    /*
     * (non-Javadoc)
     * 
     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.
     * ServletContextEvent)
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        this.getLogger().info("Faktotum exited");
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * javax.servlet.ServletContextListener#contextInitialized(javax.servlet
     * .ServletContextEvent)
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try (InputStream is = sce.getServletContext()
                .getResourceAsStream(FaktotumContextListener.RELATIVE_MANIFEST_PATH)) {
            Manifest manifest = new Manifest(is);
            Attributes attributes = manifest.getMainAttributes();
            String buildTimeString = attributes.getValue("Build-Time");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isNotBlank(buildTimeString)) {
                FaktotumContextListener.buildTime = simpleDateFormat.parse(buildTimeString);
            }
            FaktotumContextListener.version = attributes.getValue("Implementation-Version");
            this.getLogger().info("Faktotum initialized");
            this.getLogger().info("Version: " + FaktotumContextListener.getVersion());
            this.getLogger().info("Build-Time: " + simpleDateFormat.format(FaktotumContextListener.getBuildTime()));

            this.getLogger().fine("Removing old sessions...");
            this.authenticationBean.removeOldSessions();
        } catch (IOException e) {
            this.getLogger().log(Level.WARNING, "Error reading build time from manifest", e);
        } catch (ParseException e) {
            this.getLogger().log(Level.WARNING, "Error parsing build time from manifest", e);
        } catch (Exception e) {
            this.getLogger().log(Level.SEVERE, "Error reading build time", e);
            throw e;
        }
    }

}