001: // ========================================================================
002: // $Id: Configuration.java 1448 2006-12-29 20:46:57Z janb $
003: // Copyright 2006 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.util.Random;
019:
020: import javax.naming.Context;
021: import javax.naming.InitialContext;
022:
023: import org.mortbay.jetty.plus.naming.EnvEntry;
024: import org.mortbay.jetty.plus.naming.NamingEntry;
025: import org.mortbay.jetty.plus.naming.Resource;
026: import org.mortbay.jetty.plus.naming.Transaction;
027: import org.mortbay.log.Log;
028: import org.mortbay.naming.NamingUtil;
029:
030: /**
031: * Configuration
032: *
033: *
034: */
035: public class Configuration extends AbstractConfiguration {
036:
037: private Integer _key;
038:
039: /**
040: * @see org.mortbay.jetty.plus.webapp.AbstractConfiguration#bindEnvEntry(java.lang.String, java.lang.String)
041: * @param name
042: * @param value
043: * @throws Exception
044: */
045: public void bindEnvEntry(String name, Object value)
046: throws Exception {
047: EnvEntry envEntry = EnvEntry.getEnvEntry(
048: NamingEntry.SCOPE_LOCAL, name);
049: if ((envEntry != null) && envEntry.isOverrideWebXml())
050: return; //already bound a locally scoped value which should override web.xml
051:
052: envEntry = EnvEntry.getEnvEntry(NamingEntry.SCOPE_GLOBAL, name);
053: if ((envEntry == null) || !envEntry.isOverrideWebXml()) {
054:
055: //if either there isn't an env-entry in jetty-env.xml or a global one,
056: //or there is one and it isn't set to override the web.xml one, then
057: //bind the web.xml one
058: InitialContext ic = new InitialContext();
059: Context envCtx = (Context) ic.lookup("java:comp/env");
060: NamingUtil.bind(envCtx, name, value);
061: Log.debug("Bound java:comp/env/" + name + "=" + value);
062: }
063: }
064:
065: /**
066: * @see org.mortbay.jetty.plus.webapp.AbstractConfiguration#bindResourceRef(java.lang.String)
067: * @param name
068: * @throws Exception
069: */
070: public void bindResourceRef(String name, Class typeClass)
071: throws Exception {
072:
073: Resource resource = Resource.getResource(
074: NamingEntry.SCOPE_LOCAL, name);
075:
076: if (resource != null)
077: return; //already bound the locally scoped resource of that name
078:
079: resource = Resource.getResource(NamingEntry.SCOPE_GLOBAL, name);
080: if (resource != null) {
081: //no locally scoped overrides, so bind the global one, if the
082: //TODO: check type is appropriate
083: resource.bindToEnv();
084: Log.debug("Bound resourceref java:comp/env/" + name);
085: } else {
086: Log.warn("No resource to bind matching name=" + name);
087: }
088: }
089:
090: /**
091: * @see org.mortbay.jetty.plus.webapp.AbstractConfiguration#bindResourceEnvRef(java.lang.String)
092: * @param name
093: * @throws Exception
094: */
095: public void bindResourceEnvRef(String name, Class typeClass)
096: throws Exception {
097:
098: Resource resource = Resource.getResource(
099: NamingEntry.SCOPE_LOCAL, name);
100: if (resource != null)
101: return; //already bound
102: resource = Resource.getResource(NamingEntry.SCOPE_GLOBAL, name);
103: if (resource != null) {
104: resource.bindToEnv();
105: Log.debug("Bound resource-env-ref java:comp/env/" + name);
106: } else
107: Log.warn("No resource to bind matching name=" + name);
108: }
109:
110: public void bindMessageDestinationRef(String name, Class typeClass)
111: throws Exception {
112: Resource resource = Resource.getResource(
113: NamingEntry.SCOPE_LOCAL, name);
114: if (resource != null)
115: return; //already bound
116: resource = Resource.getResource(NamingEntry.SCOPE_GLOBAL, name);
117: if (resource != null) {
118: resource.bindToEnv();
119: Log.debug("Bound message-destination-ref java:comp/env/"
120: + name);
121: } else
122: Log.warn("No resource to bind matching name=" + name);
123: }
124:
125: public void bindUserTransaction() throws Exception {
126: Transaction transaction = Transaction
127: .getTransaction(NamingEntry.SCOPE_LOCAL);
128: if (transaction != null) {
129: //special case, still need to bind it because it has to be bound to java:comp instead
130: //of java:comp/env
131: transaction.bindToEnv();
132: return;
133: }
134:
135: transaction = Transaction
136: .getTransaction(NamingEntry.SCOPE_GLOBAL);
137: if (transaction != null) {
138: transaction.bindToEnv();
139: Log.debug("Bound UserTransaction to java:comp/"
140: + transaction.getJndiName());
141: }
142: }
143:
144: public void configureClassLoader() throws Exception {
145: super .configureClassLoader();
146: }
147:
148: public void configureDefaults() throws Exception {
149: super .configureDefaults();
150: }
151:
152: public void configureWebApp() throws Exception {
153: super .configureWebApp();
154: //lock this webapp's java:comp namespace as per J2EE spec
155: ClassLoader oldLoader = Thread.currentThread()
156: .getContextClassLoader();
157: Thread.currentThread().setContextClassLoader(
158: getWebAppContext().getClassLoader());
159: lockCompEnv();
160: Thread.currentThread().setContextClassLoader(oldLoader);
161: }
162:
163: public void deconfigureWebApp() throws Exception {
164: ClassLoader oldLoader = Thread.currentThread()
165: .getContextClassLoader();
166: Thread.currentThread().setContextClassLoader(
167: getWebAppContext().getClassLoader());
168: unlockCompEnv();
169: Thread.currentThread().setContextClassLoader(oldLoader);
170: super .deconfigureWebApp();
171: }
172:
173: protected void lockCompEnv() throws Exception {
174: Random random = new Random();
175: _key = new Integer(random.nextInt());
176: Context context = new InitialContext();
177: Context compCtx = (Context) context.lookup("java:comp");
178: compCtx.addToEnvironment("org.mortbay.jndi.lock", _key);
179: }
180:
181: protected void unlockCompEnv() throws Exception {
182: Context context = new InitialContext();
183: Context compCtx = (Context) context.lookup("java:comp");
184: compCtx.addToEnvironment("org.mortbay.jndi.unlock", _key);
185: }
186: }
|