KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > conf > ServletUtil


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.conf;
21
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.http.HttpSession JavaDoc;
24
25 import org.apache.cayenne.access.DataContext;
26 import org.apache.cayenne.util.ResourceLocator;
27 import org.apache.cayenne.util.WebApplicationResourceLocator;
28
29 /**
30  * Configuration class that uses ServletContext to locate resources. This class is
31  * intended for use in J2EE servlet containers. It is compatible with containers following
32  * servlet specification version 2.2 and newer (e.g. Tomcat can be used starting from
33  * version 3).
34  * <p>
35  * ServletConfiguration resolves configuration file locations relative to the web
36  * application "WEB-INF" directory, and does not require them to be in the CLASSPATH
37  * (though CLASSPATH locations such as "/WEB-INF/classes" and "/WEB-INF/lib/some.jar" are
38  * supported as well). By default search for cayenne.xml is done in /WEB-INF/ folder. To
39  * specify an arbitrary context path in the web application (e.g. "/WEB-INF/cayenne"), use
40  * <code>cayenne.configuration.path</code> context parameters in <code>web.xml</code>.
41  * </p>
42  *
43  * @author Andrus Adamchik
44  * @author Scott Finnerty
45  * @since 1.2
46  */

47 public class ServletUtil {
48
49     /**
50      * A name of the web application initialization parameter used to specify extra paths
51      * where Cayenne XML files might be located. E.g. "/WEB-INF/cayenne".
52      */

53     public static final String JavaDoc CONFIGURATION_PATH_KEY = "cayenne.configuration.path";
54
55     /**
56      * Used by BasicServletConfiguration as a session attribute for DataContext.
57      */

58     public static final String JavaDoc DATA_CONTEXT_KEY = "cayenne.datacontext";
59
60     /**
61      * Creates a new ServletConfiguration and sets is as a Configuration signleton.
62      */

63     public synchronized static Configuration initializeSharedConfiguration(
64             ServletContext JavaDoc context) {
65
66         // check if this web application is already configured
67

68         // don't use static getter, since it will do initialization on demand!!!
69
Configuration oldConfig = Configuration.sharedConfiguration;
70         if (oldConfig instanceof DefaultConfiguration) {
71
72             ResourceLocator locator = ((DefaultConfiguration) oldConfig)
73                     .getResourceLocator();
74
75             if (locator instanceof WebApplicationResourceLocator) {
76                 if (((WebApplicationResourceLocator) locator).getServletContext() == context) {
77                     return oldConfig;
78                 }
79             }
80         }
81
82         // create new shared configuration
83
DefaultConfiguration conf = new DefaultConfiguration(
84                 Configuration.DEFAULT_DOMAIN_FILE,
85                 createLocator(context));
86         Configuration.initializeSharedConfiguration(conf);
87
88         return conf;
89     }
90
91     /**
92      * A helper method to create default ResourceLocator.
93      */

94     protected static ResourceLocator createLocator(ServletContext JavaDoc context) {
95         WebApplicationResourceLocator locator = new WebApplicationResourceLocator();
96         locator.setSkipAbsolutePath(true);
97         locator.setSkipClasspath(false);
98         locator.setSkipCurrentDirectory(true);
99         locator.setSkipHomeDirectory(true);
100
101         locator.setServletContext(context);
102         String JavaDoc configurationPath = context.getInitParameter(CONFIGURATION_PATH_KEY);
103         if (configurationPath != null && configurationPath.trim().length() > 0) {
104             locator.addFilesystemPath(configurationPath.trim());
105         }
106
107         return locator;
108     }
109
110     /**
111      * Returns default Cayenne DataContext associated with the HttpSession, creating it on
112      * the fly and storing in the session if needed.
113      */

114     public static DataContext getSessionContext(HttpSession JavaDoc session) {
115         synchronized (session) {
116             DataContext ctxt = (DataContext) session.getAttribute(DATA_CONTEXT_KEY);
117
118             if (ctxt == null) {
119                 ctxt = DataContext.createDataContext();
120                 session.setAttribute(ServletUtil.DATA_CONTEXT_KEY, ctxt);
121             }
122
123             return ctxt;
124         }
125     }
126 }
127
Popular Tags