View Javadoc

1   /*
2    *  jDTAUS Core RI Servlet Container
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.container.ri.servlet;
22  
23  import javax.servlet.ServletContext;
24  import org.jdtaus.core.container.Container;
25  import org.jdtaus.core.container.ContainerFactory;
26  import org.jdtaus.core.container.Context;
27  import org.jdtaus.core.container.ContextFactory;
28  import org.jdtaus.core.container.Model;
29  import org.jdtaus.core.container.ModelFactory;
30  
31  /**
32   * Factories using {@code ServletContext}s for singleton instances.
33   * <p>This class provides implementations for the
34   * {@link ContainerFactory#getContainer()}, {@link ContextFactory#getContext()}
35   * and {@link ModelFactory#getModel()} methods. See the factories for how
36   * to configure this class to be used.</p>
37   *
38   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
39   * @version $JDTAUS: ServletContextFactories.java 8641 2012-09-27 06:45:17Z schulte $
40   *
41   * @see ServletFilter
42   */
43  public abstract class ServletContextFactories
44  {
45      //--ServletContextFactories-------------------------------------------------
46  
47      /** @see ContainerFactory#getContainer() */
48      public static Container getContainer()
49      {
50          Container instance =
51              (Container) ServletContextFactories.getServletContext().
52              getAttribute( Container.class.getName() );
53  
54          if ( instance == null )
55          {
56              instance = ContainerFactory.newContainer();
57              ServletContextFactories.getServletContext().
58                  setAttribute( Container.class.getName(), instance );
59  
60          }
61  
62          return instance;
63      }
64  
65      /** @see ContextFactory#getContext() */
66      public static Context getContext()
67      {
68          Context instance =
69              (Context) ServletContextFactories.getServletContext().
70              getAttribute( Context.class.getName() );
71  
72          if ( instance == null )
73          {
74              instance = ContextFactory.newContext();
75              ServletContextFactories.getServletContext().
76                  setAttribute( Context.class.getName(), instance );
77  
78          }
79  
80          return instance;
81      }
82  
83      /** @see ModelFactory#getModel() */
84      public static Model getModel()
85      {
86          Model instance =
87              (Model) ServletContextFactories.getServletContext().
88              getAttribute( Model.class.getName() );
89  
90          if ( instance == null )
91          {
92              instance = ModelFactory.newModel();
93              ServletContextFactories.getServletContext().
94                  setAttribute( Model.class.getName(), instance );
95  
96          }
97  
98          return instance;
99      }
100 
101     /** @see ServletFilter#getServletContext() */
102     public static ServletContext getServletContext()
103     {
104         return ServletFilter.getServletContext();
105     }
106 
107     //-------------------------------------------------ServletContextFactories--
108 }