KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > conf > BasicServletConfiguration


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.conf;
57
58 import javax.servlet.ServletContext JavaDoc;
59 import javax.servlet.http.HttpSession JavaDoc;
60
61 import org.apache.log4j.Logger;
62 import org.objectstyle.cayenne.access.DataContext;
63 import org.objectstyle.cayenne.util.ResourceLocator;
64 import org.objectstyle.cayenne.util.WebApplicationResourceLocator;
65
66 /**
67  * BasicServletConfiguration is a Configuration that uses ServletContext to locate
68  * resources. This class can only be used in a context of a servlet/jsp container. It
69  * resolves configuration file paths relative to the web application "WEB-INF" directory.
70  * <p>
71  * BasicServletConfiguration is compatible with Servlet Specification 2.2 and higher. Also
72  * look at ServletConfiguration for the information how to utilize listeners introduced in
73  * Servlet Specification 2.3.
74  * </p>
75  *
76  * @author Andrei Adamchik
77  * @author Scott Finnerty
78  * @deprecated Since 1.2 ServletUtil is used instead, as the actual file loading strategy
79  * is defined at the ResourceLocator elevel, and this class provides no value
80  * of its own.
81  */

82 public class BasicServletConfiguration extends DefaultConfiguration {
83     private static Logger logObj = Logger.getLogger(BasicServletConfiguration.class);
84
85     public static final String JavaDoc CONFIGURATION_PATH_KEY =
86         "cayenne.configuration.path";
87     public static final String JavaDoc DATA_CONTEXT_KEY = "cayenne.datacontext";
88
89     protected ServletContext JavaDoc servletContext;
90
91     public synchronized static BasicServletConfiguration initializeConfiguration(ServletContext JavaDoc ctxt) {
92         // check if this web application already has a servlet configuration
93
// sometimes multiple initializations are done by mistake...
94

95         // Andrus: are there any cases when reinitialization is absolutely required?
96

97         // don't use static getter, since it will do initialization on demand!!!
98
Configuration oldConfiguration = Configuration.sharedConfiguration;
99         if (oldConfiguration instanceof BasicServletConfiguration) {
100             BasicServletConfiguration basicConfiguration =
101                 (BasicServletConfiguration) oldConfiguration;
102             if (basicConfiguration.getServletContext() == ctxt) {
103                 logObj.info(
104                     "BasicServletConfiguration is already initialized, reusing.");
105                 return basicConfiguration;
106             }
107         }
108
109         BasicServletConfiguration conf = new BasicServletConfiguration(ctxt);
110         Configuration.initializeSharedConfiguration(conf);
111
112         return conf;
113     }
114
115     /**
116      * Returns default Cayenne DataContext associated with the HttpSession.
117      * If no DataContext exists in the session, it is created on the spot.
118      */

119     public static DataContext getDefaultContext(HttpSession JavaDoc session) {
120         synchronized (session) {
121             DataContext ctxt =
122                 (DataContext) session.getAttribute(DATA_CONTEXT_KEY);
123
124             if (ctxt == null) {
125                 ctxt = DataContext.createDataContext();
126                 session.setAttribute(
127                     BasicServletConfiguration.DATA_CONTEXT_KEY,
128                     ctxt);
129             }
130
131             return ctxt;
132         }
133     }
134
135     public BasicServletConfiguration() {
136         super();
137
138     }
139
140     public BasicServletConfiguration(ServletContext JavaDoc ctxt) {
141         super();
142         this.setServletContext(ctxt);
143
144         ResourceLocator l = new WebApplicationResourceLocator(servletContext);
145         l.setSkipAbsolutePath(true);
146         l.setSkipClasspath(false);
147         l.setSkipCurrentDirectory(true);
148         l.setSkipHomeDirectory(true);
149
150         // check for a configuration path in the context parameters
151
String JavaDoc configurationPath =
152             ctxt.getInitParameter(CONFIGURATION_PATH_KEY);
153         if (configurationPath != null
154             && configurationPath.trim().length() > 0) {
155             l.addFilesystemPath(configurationPath);
156         }
157
158         this.setResourceLocator(l);
159     }
160
161     /**
162      * Sets the servletContext.
163      * @param servletContext The servletContext to set
164      */

165     public void setServletContext(ServletContext JavaDoc servletContext) {
166         this.servletContext = servletContext;
167     }
168
169     /** Returns current application context object. */
170     public ServletContext JavaDoc getServletContext() {
171         return servletContext;
172     }
173
174     public boolean canInitialize() {
175         return (getServletContext() != null);
176     }
177 }
178
Popular Tags