001: // ========================================================================
002: // $Id: EnvConfiguration.java 887 2006-09-05 13:46:42Z janb $
003: // Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.jetty.plus.webapp;
017:
018: import java.net.URL;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import javax.naming.Context;
023: import javax.naming.InitialContext;
024: import javax.naming.NamingException;
025:
026: import org.mortbay.jetty.plus.naming.EnvEntry;
027: import org.mortbay.jetty.plus.naming.NamingEntry;
028: import org.mortbay.jetty.plus.naming.Resource;
029: import org.mortbay.jetty.plus.naming.Transaction;
030: import org.mortbay.jetty.webapp.Configuration;
031: import org.mortbay.jetty.webapp.WebAppContext;
032: import org.mortbay.log.Log;
033:
034: import org.mortbay.xml.XmlConfiguration;
035:
036: /**
037: * EnvConfiguration
038: *
039: *
040: */
041: public class EnvConfiguration implements Configuration {
042: private WebAppContext webAppContext;
043: private Context compCtx;
044: private URL jettyEnvXmlUrl;
045:
046: protected void createEnvContext() throws NamingException {
047: Context context = new InitialContext();
048: compCtx = (Context) context.lookup("java:comp");
049: Context envCtx = compCtx.createSubcontext("env");
050: if (Log.isDebugEnabled())
051: Log.debug("Created java:comp/env for webapp "
052: + getWebAppContext().getContextPath());
053: }
054:
055: /**
056: * @see org.mortbay.jetty.webapp.Configuration#setWebAppContext(org.mortbay.jetty.webapp.WebAppContext)
057: * @param context
058: */
059: public void setWebAppContext(WebAppContext context) {
060: this .webAppContext = context;
061: }
062:
063: public void setJettyEnvXml(URL url) {
064: this .jettyEnvXmlUrl = url;
065: }
066:
067: /**
068: * @see org.mortbay.jetty.webapp.Configuration#getWebAppContext()
069: */
070: public WebAppContext getWebAppContext() {
071: return webAppContext;
072: }
073:
074: /**
075: * @see org.mortbay.jetty.webapp.Configuration#configureClassLoader()
076: * @throws Exception
077: */
078: public void configureClassLoader() throws Exception {
079: }
080:
081: /**
082: * @see org.mortbay.jetty.webapp.Configuration#configureDefaults()
083: * @throws Exception
084: */
085: public void configureDefaults() throws Exception {
086: }
087:
088: /**
089: * @see org.mortbay.jetty.webapp.Configuration#configureWebApp()
090: * @throws Exception
091: */
092: public void configureWebApp() throws Exception {
093: //create a java:comp/env
094: createEnvContext();
095:
096: //add java:comp/env entries for any globally defined EnvEntries
097: bindGlobalEnvEntries();
098:
099: //set up java:comp/env as the Context in which to bind directly
100: //the entries in jetty-env.xml
101: NamingEntry.setScope(NamingEntry.SCOPE_LOCAL);
102:
103: //check to see if an explicit file has been set, if not,
104: //look in WEB-INF/jetty-env.xml
105: if (jettyEnvXmlUrl == null) {
106:
107: //look for a file called WEB-INF/jetty-env.xml
108: //and process it if it exists
109: org.mortbay.resource.Resource web_inf = getWebAppContext()
110: .getWebInf();
111: if (web_inf != null && web_inf.isDirectory()) {
112: org.mortbay.resource.Resource jettyEnv = web_inf
113: .addPath("jetty-env.xml");
114: if (jettyEnv.exists()) {
115: jettyEnvXmlUrl = jettyEnv.getURL();
116: }
117: }
118: }
119: if (jettyEnvXmlUrl != null) {
120: XmlConfiguration configuration = new XmlConfiguration(
121: jettyEnvXmlUrl);
122: configuration.configure(getWebAppContext());
123: }
124:
125: }
126:
127: /**
128: * Remove all jndi setup
129: * @see org.mortbay.jetty.webapp.Configuration#deconfigureWebApp()
130: * @throws Exception
131: */
132: public void deconfigureWebApp() throws Exception {
133: //get rid of any bindings only defined for the webapp
134: ClassLoader oldLoader = Thread.currentThread()
135: .getContextClassLoader();
136: Thread.currentThread().setContextClassLoader(
137: webAppContext.getClassLoader());
138: NamingEntry.setScope(NamingEntry.SCOPE_LOCAL);
139: unbindLocalNamingEntries();
140: compCtx.destroySubcontext("env");
141: NamingEntry.setScope(NamingEntry.SCOPE_GLOBAL);
142: Thread.currentThread().setContextClassLoader(oldLoader);
143: }
144:
145: /**
146: * Bind all EnvEntries that have been globally declared.
147: * @throws NamingException
148: */
149: public void bindGlobalEnvEntries() throws NamingException {
150: Log.debug("Finding global env entries");
151: List list = NamingEntry.lookupNamingEntries(
152: NamingEntry.SCOPE_GLOBAL, EnvEntry.class);
153: Iterator itor = list.iterator();
154:
155: Log.debug("Finding env entries: size=" + list.size());
156: while (itor.hasNext()) {
157: EnvEntry ee = (EnvEntry) itor.next();
158: Log.debug("configuring env entry " + ee.getJndiName());
159: ee.bindToEnv();
160: }
161: }
162:
163: public void unbindLocalNamingEntries() throws NamingException {
164: List list = NamingEntry.lookupNamingEntries(
165: NamingEntry.SCOPE_LOCAL, EnvEntry.class);
166: list.addAll(NamingEntry.lookupNamingEntries(
167: NamingEntry.SCOPE_LOCAL, Resource.class));
168: list.addAll(NamingEntry.lookupNamingEntries(
169: NamingEntry.SCOPE_LOCAL, Transaction.class));
170:
171: Iterator itor = list.iterator();
172:
173: Log
174: .debug("Finding all naming entries for webapp local naming context: size="
175: + list.size());
176: while (itor.hasNext()) {
177: NamingEntry ne = (NamingEntry) itor.next();
178: Log.debug("Unbinding naming entry " + ne.getJndiName());
179: ne.unbindEnv();
180: ne.unbind();
181: }
182: }
183:
184: }
|