001/*
002 *  jDTAUS Core RI Servlet Container
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.core.container.ri.servlet;
022
023import javax.servlet.ServletContext;
024import org.jdtaus.core.container.Container;
025import org.jdtaus.core.container.ContainerFactory;
026import org.jdtaus.core.container.Context;
027import org.jdtaus.core.container.ContextFactory;
028import org.jdtaus.core.container.Model;
029import org.jdtaus.core.container.ModelFactory;
030
031/**
032 * Factories using {@code ServletContext}s for singleton instances.
033 * <p>This class provides implementations for the
034 * {@link ContainerFactory#getContainer()}, {@link ContextFactory#getContext()}
035 * and {@link ModelFactory#getModel()} methods. See the factories for how
036 * to configure this class to be used.</p>
037 *
038 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
039 * @version $JDTAUS: ServletContextFactories.java 8641 2012-09-27 06:45:17Z schulte $
040 *
041 * @see ServletFilter
042 */
043public abstract class ServletContextFactories
044{
045    //--ServletContextFactories-------------------------------------------------
046
047    /** @see ContainerFactory#getContainer() */
048    public static Container getContainer()
049    {
050        Container instance =
051            (Container) ServletContextFactories.getServletContext().
052            getAttribute( Container.class.getName() );
053
054        if ( instance == null )
055        {
056            instance = ContainerFactory.newContainer();
057            ServletContextFactories.getServletContext().
058                setAttribute( Container.class.getName(), instance );
059
060        }
061
062        return instance;
063    }
064
065    /** @see ContextFactory#getContext() */
066    public static Context getContext()
067    {
068        Context instance =
069            (Context) ServletContextFactories.getServletContext().
070            getAttribute( Context.class.getName() );
071
072        if ( instance == null )
073        {
074            instance = ContextFactory.newContext();
075            ServletContextFactories.getServletContext().
076                setAttribute( Context.class.getName(), instance );
077
078        }
079
080        return instance;
081    }
082
083    /** @see ModelFactory#getModel() */
084    public static Model getModel()
085    {
086        Model instance =
087            (Model) ServletContextFactories.getServletContext().
088            getAttribute( Model.class.getName() );
089
090        if ( instance == null )
091        {
092            instance = ModelFactory.newModel();
093            ServletContextFactories.getServletContext().
094                setAttribute( Model.class.getName(), instance );
095
096        }
097
098        return instance;
099    }
100
101    /** @see ServletFilter#getServletContext() */
102    public static ServletContext getServletContext()
103    {
104        return ServletFilter.getServletContext();
105    }
106
107    //-------------------------------------------------ServletContextFactories--
108}