Source code: org/objectstyle/cayenne/conf/BasicServletConfiguration.java
1 /* ====================================================================
2 *
3 * The ObjectStyle Group Software License, Version 1.0
4 *
5 * Copyright (c) 2002-2003 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 package org.objectstyle.cayenne.conf;
57
58 import javax.servlet.ServletContext;
59 import javax.servlet.http.HttpSession;
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
68 * to locate resources.
69 * This class can only be used in a context of a servlet/jsp container.
70 * It resolves configuration file paths relative to the web application
71 * "WEB-INF" directory.
72 *
73 * <p>
74 * BasicServletConfiguration is compatible with Servlet Specification 2.2 and higher.
75 * Also look at ServletConfiguration for the information how to utilize listeners
76 * introduced in Servlet Specification 2.3.
77 * </p>
78 *
79 * @author Andrei Adamchik
80 * @author Scott Finnerty
81 */
82 public class BasicServletConfiguration extends DefaultConfiguration {
83 private static Logger logObj = Logger.getLogger(BasicServletConfiguration.class);
84
85 public static final String CONFIGURATION_PATH_KEY =
86 "cayenne.configuration.path";
87 public static final String DATA_CONTEXT_KEY = "cayenne.datacontext";
88
89 protected ServletContext servletContext;
90
91 public synchronized static BasicServletConfiguration initializeConfiguration(ServletContext 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 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 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 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 servletContext) {
166 this.servletContext = servletContext;
167 }
168
169 /** Returns current application context object. */
170 public ServletContext getServletContext() {
171 return servletContext;
172 }
173
174 public boolean canInitialize() {
175 return (getServletContext() != null);
176 }
177 }