01: // ========================================================================
02: // $Id: EnvEntry.java 887 2006-09-05 13:46:42Z janb $
03: // Copyright 2006 Mort Bay Consulting Pty. Ltd.
04: // ------------------------------------------------------------------------
05: // Licensed under the Apache License, Version 2.0 (the "License");
06: // you may not use this file except in compliance with the License.
07: // You may obtain a copy of the License at
08: // http://www.apache.org/licenses/LICENSE-2.0
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14: // ========================================================================
15:
16: package org.mortbay.jetty.plus.naming;
17:
18: import javax.naming.Context;
19: import javax.naming.InitialContext;
20: import javax.naming.NameNotFoundException;
21: import javax.naming.NamingException;
22:
23: import org.mortbay.log.Log;
24:
25: /**
26: * EnvEntry
27: *
28: *
29: */
30: public class EnvEntry extends NamingEntry {
31: private boolean overrideWebXml;
32:
33: public static EnvEntry getEnvEntry(int scopeType, String jndiName)
34: throws NamingException {
35: return (EnvEntry) lookupNamingEntry(scopeType, EnvEntry.class,
36: jndiName);
37: }
38:
39: public EnvEntry(String jndiName, Object objToBind)
40: throws NamingException {
41: this (jndiName, objToBind, false);
42: }
43:
44: public EnvEntry(String jndiName, Object objToBind,
45: boolean overrideWebXml) throws NamingException {
46: super (jndiName, objToBind);
47: this .overrideWebXml = overrideWebXml;
48: }
49:
50: public boolean isOverrideWebXml() {
51: return this .overrideWebXml;
52: }
53:
54: /** Bind the object wrapped in this EnvEntry into java:comp/env.
55: * If, however, it is set to NOT override the web.xml entry,
56: * then don't bind it. This method works in conjunction with
57: * org.mortbay.jetty.plus.webapp.Configuration.bindEnvEntry().
58: * TODO clean this up
59: * @see org.mortbay.jetty.plus.naming.NamingEntry#bindToEnv()
60: * @throws NamingException
61: */
62: public void bindToEnv() throws NamingException {
63: InitialContext iContext = new InitialContext();
64: Context env = (Context) iContext.lookup("java:comp/env");
65:
66: boolean doBind = false;
67: try {
68: env.lookup(getJndiName());
69: if (isOverrideWebXml())
70: doBind = true;
71: } catch (NameNotFoundException e) {
72: doBind = true;
73: }
74:
75: if (doBind)
76: super.bindToEnv();
77: }
78:
79: }
|