001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2015 ForgeRock AS.
015 */
016
017package org.forgerock.http.servlet;
018
019import javax.servlet.ServletContext;
020import javax.servlet.ServletContextEvent;
021import javax.servlet.ServletContextListener;
022
023import java.util.Map;
024
025import org.forgerock.http.HttpApplication;
026
027/**
028 * A {@code ServletContextListener} that sets {@code String} keyed
029 * {@code HttpApplication} instances as attributes on the
030 * {@code ServletContext}.
031 */
032public abstract class HttpFrameworkServletContextListener implements ServletContextListener {
033
034    /**
035     * Registers the {@link HttpApplication} instances in the
036     * {@link ServletContext} attributes.
037     *
038     * @param event {@inheritDoc}
039     */
040    @Override
041    public final void contextInitialized(ServletContextEvent event) {
042        for (Map.Entry<String, HttpApplication> applications : getHttpApplications().entrySet()) {
043            event.getServletContext().setAttribute(applications.getKey(), applications.getValue());
044        }
045        event.getServletContext().log("HTTP Application is ready.");
046    }
047
048    /**
049     * No action performed.
050     *
051     * @param event {@inheritDoc}
052     */
053    @Override
054    public final void contextDestroyed(ServletContextEvent event) {
055        //Nothing to do
056    }
057
058    /**
059     * Gets a {@code Map} of {@link HttpApplication} instances keyed by a
060     * {@code String} application key.
061     *
062     * @return A {@code Map} containing application key to
063     * {@code HttpApplication} instances.
064     */
065    protected abstract Map<String, HttpApplication> getHttpApplications();
066}