Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/objectstyle/cayenne/conf/ServletConfiguration.java


1   /* ====================================================================
2    * 
3    * The ObjectStyle Group Software License, Version 1.0 
4    *
5    * Copyright (c) 2002 The ObjectStyle Group 
6    * and individual authors of the software.  All rights reserved.
7    *
8    * Redistribution and use in source and binary forms, with or without
9    * modification, are permitted provided that the following conditions
10   * are met:
11   *
12   * 1. Redistributions of source code must retain the above copyright
13   *    notice, this list of conditions and the following disclaimer. 
14   *
15   * 2. Redistributions in binary form must reproduce the above copyright
16   *    notice, this list of conditions and the following disclaimer in
17   *    the documentation and/or other materials provided with the
18   *    distribution.
19   *
20   * 3. The end-user documentation included with the redistribution, if
21   *    any, must include the following acknowlegement:  
22   *       "This product includes software developed by the 
23   *        ObjectStyle Group (http://objectstyle.org/)."
24   *    Alternately, this acknowlegement may appear in the software itself,
25   *    if and wherever such third-party acknowlegements normally appear.
26   *
27   * 4. The names "ObjectStyle Group" and "Cayenne" 
28   *    must not be used to endorse or promote products derived
29   *    from this software without prior written permission. For written 
30   *    permission, please contact andrus@objectstyle.org.
31   *
32   * 5. Products derived from this software may not be called "ObjectStyle"
33   *    nor may "ObjectStyle" appear in their names without prior written
34   *    permission of the ObjectStyle Group.
35   *
36   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39   * DISCLAIMED.  IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
40   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47   * SUCH DAMAGE.
48   * ====================================================================
49   *
50   * This software consists of voluntary contributions made by many
51   * individuals on behalf of the ObjectStyle Group.  For more
52   * information on the ObjectStyle Group, please see
53   * <http://objectstyle.org/>.
54   *
55   */
56  
57  package org.objectstyle.cayenne.conf;
58  
59  import javax.servlet.ServletContext;
60  import javax.servlet.ServletContextEvent;
61  import javax.servlet.ServletContextListener;
62  import javax.servlet.http.HttpSession;
63  import javax.servlet.http.HttpSessionEvent;
64  import javax.servlet.http.HttpSessionListener;
65  
66  import org.objectstyle.cayenne.access.DataContext;
67  
68  /**
69    * ServletConfiguration is a Configuration that uses ServletContext to locate resources. 
70    * This class can only be used in a context of a servlet/jsp container.
71    * It resolves configuration file paths relative to the web application
72    * "WEB-INF" directory.
73    *
74    * <p>It performs the following tasks:
75    * <ul>
76    * <li>Loads Cayenne configuration when the application is started within container.</li>
77    * <li>Assigns new DataContext to every new session created within the application.</li>
78    * </ul>
79    * </p>
80    * 
81    * <p>ServletConfiguration must be configured in <code>web.xml</code> deployment
82    * descriptor as a listener of context and session events:</p>
83    *<pre>&lt;listener&gt;
84       &lt;listener-class&gt;org.objectstyle.cayenne.conf.ServletConfiguration&lt;/listener-class&gt;
85  &lt;/listener&gt;</pre>
86    *
87    * <p>Note that to set ServletConfiguration as a listener of web application events, 
88    *  you must use servlet containers 
89    * compatible with Servlet Specification 2.3 (such as Tomcat 4.0). Listeners were only added 
90    * to servlet specification in 2.3. If you are using an older container. You will need
91    * to configure Cayenne in you code.</p>
92    *
93    * @author Andrei Adamchik
94    */
95  public class ServletConfiguration
96      extends BasicServletConfiguration
97      implements HttpSessionListener, ServletContextListener {
98  
99      public static final String DATA_CONTEXT_KEY = "cayenne.datacontext";
100 
101     public ServletConfiguration() {}
102 
103     public ServletConfiguration(ServletContext context) {}
104 
105     /** Returns default Cayenne DataContext associated with session <code>s</code>. */
106     public static DataContext getDefaultContext(HttpSession s) {
107         return (DataContext) s.getAttribute(DATA_CONTEXT_KEY);
108     }
109 
110     /** Sets itself as a Cayenne shared Configuration object that can later
111       * be obtained by calling <code>Configuration.getSharedConfig()</code>.
112       * This method is a part of  ServletContextListener interface and is called
113       * on application startup. */
114     public void contextInitialized(ServletContextEvent sce) {
115         this.servletContext = sce.getServletContext();
116         servletContext.log("*************** app created");
117         Configuration.initSharedConfig(this);
118     }
119 
120     /** Currently does nothing. <i>In the future it should close down
121       * any database connections if they wheren't obtained via JNDI.</i>
122       * This method is a part of ServletContextListener interface and is called
123       * on application shutdown. */
124     public void contextDestroyed(ServletContextEvent sce) {}
125 
126     /** Creates and assigns a new data context based on default domain
127       * to the session object  associated with this event. This method
128       * is a part of HttpSessionListener interface and is called every time
129       * when a new session is created. */
130     public void sessionCreated(HttpSessionEvent se) {
131         servletContext.log("*************** session created");
132         se.getSession().setAttribute(DATA_CONTEXT_KEY, getDomain().createDataContext());
133     }
134 
135     /** Does nothing. This method
136       * is a part of HttpSessionListener interface and is called every time
137       * when a session is destroyed. */
138     public void sessionDestroyed(HttpSessionEvent se) {}
139 }