001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.naming;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.L10N;
033:
034: import javax.naming.Context;
035: import javax.naming.InitialContext;
036: import javax.naming.Name;
037: import javax.naming.NameNotFoundException;
038: import javax.naming.NameParser;
039: import javax.naming.NamingException;
040: import java.util.logging.Level;
041: import java.util.logging.Logger;
042:
043: /**
044: * Static utility functions.
045: */
046: public class Jndi {
047: private static Logger log = Log.open(Jndi.class);
048: private static L10N L = new L10N(Jndi.class);
049:
050: private Jndi() {
051: }
052:
053: /**
054: * Returns the full name.
055: */
056: public static String getFullName(String shortName) {
057: if (shortName.startsWith("java:"))
058: return shortName;
059: else
060: return "java:comp/env/" + shortName;
061: }
062:
063: public static void bindDeepShort(String name, Object obj)
064: throws NamingException {
065: bindImpl(new InitialContext(), getFullName(name), obj, name);
066: }
067:
068: public static void bindDeepShort(Context context, String name,
069: Object obj) throws NamingException {
070: bindImpl(context, getFullName(name), obj, name);
071: }
072:
073: public static void bindDeep(String name, Object obj)
074: throws NamingException {
075: bindImpl(new InitialContext(), name, obj, name);
076: }
077:
078: public static void bindDeep(Context context, String name, Object obj)
079: throws NamingException {
080: bindImpl(context, name, obj, name);
081: }
082:
083: private static void bindImpl(Context context, String name,
084: Object obj, String fullName) throws NamingException {
085: NameParser parser = context.getNameParser("");
086: Name parsedName = parser.parse(name);
087:
088: if (parsedName.size() == 1) {
089: Object value = null;
090:
091: try {
092: value = context.lookup(name);
093: } catch (NameNotFoundException e) {
094: }
095:
096: context.rebind(name, obj);
097:
098: // server/1620
099: if (value != null && value != obj)
100: log
101: .config(L
102: .l(
103: "'{0}' overrides a previous JNDI resource. The old resource is '{1}' and the new one is '{2}'",
104: fullName, value, obj));
105:
106: return;
107: }
108:
109: Object sub = null;
110:
111: try {
112: sub = context.lookup(parsedName.get(0));
113: } catch (NameNotFoundException e) {
114: }
115:
116: if (sub == null)
117: sub = context.createSubcontext(parsedName.get(0));
118:
119: if (sub instanceof Context)
120: bindImpl((Context) sub, parsedName.getSuffix(1).toString(),
121: obj, fullName);
122:
123: else
124: throw new NamingException(
125: L
126: .l(
127: "'{0}' is an invalid JNDI name because '{1} is not a Context. One of the subcontexts is not a Context as expected.",
128: fullName, sub));
129: }
130:
131: /**
132: * Binds the object into JNDI without warnings if an old
133: * object exists. The name may be a full name or the short
134: * form.
135: */
136: public static void rebindDeepShort(String name, Object obj)
137: throws NamingException {
138: rebindImpl(new InitialContext(), getFullName(name), obj, name);
139: }
140:
141: /**
142: * Binds the object into JNDI without warnings if an old
143: * object exists. The name may be a full name or the short
144: * form.
145: */
146: public static void rebindDeepShort(Context context, String name,
147: Object obj) throws NamingException {
148: rebindImpl(context, getFullName(name), obj, name);
149: }
150:
151: /**
152: * Binds the object into JNDI without warnings if an old
153: * object exists, using the full JNDI name.
154: */
155: public static void rebindDeep(String name, Object obj)
156: throws NamingException {
157: rebindImpl(new InitialContext(), name, obj, name);
158: }
159:
160: /**
161: * Binds the object into JNDI without warnings if an old
162: * object exists, using the full JNDI name.
163: */
164: public static void rebindDeep(Context context, String name,
165: Object obj) throws NamingException {
166: rebindImpl(context, name, obj, name);
167: }
168:
169: /**
170: * Binds the object into JNDI without warnings if an old
171: * object exists.
172: */
173: private static void rebindImpl(Context context, String name,
174: Object obj, String fullName) throws NamingException {
175: NameParser parser = context.getNameParser("");
176: Name parsedName = parser.parse(name);
177:
178: if (parsedName.size() == 1) {
179: context.rebind(name, obj);
180: return;
181: }
182:
183: Object sub = null;
184:
185: try {
186: sub = context.lookup(parsedName.get(0));
187: } catch (NameNotFoundException e) {
188: log.log(Level.FINEST, e.toString(), e);
189: }
190:
191: if (sub == null)
192: sub = context.createSubcontext(parsedName.get(0));
193:
194: if (sub instanceof Context)
195: rebindImpl((Context) sub, parsedName.getSuffix(1)
196: .toString(), obj, fullName);
197:
198: else
199: throw new NamingException(
200: L
201: .l(
202: "'{0}' is an invalid JNDI name because '{1} is not a Context. One of the subcontexts is not a Context as expected.",
203: fullName, sub));
204: }
205:
206: // For EL
207: public static Object lookup(String name) {
208: Exception ex = null;
209:
210: try {
211: Object value = new InitialContext().lookup(name);
212:
213: if (value != null)
214: return value;
215: } catch (NamingException e) {
216: ex = e;
217: }
218:
219: if (!name.startsWith("java:")) {
220: try {
221: Object value = new InitialContext()
222: .lookup("java:comp/env/" + name);
223:
224: if (value != null)
225: return value;
226: } catch (NamingException e) {
227: ex = e;
228: }
229: }
230:
231: if (ex != null && log.isLoggable(Level.FINEST))
232: log.log(Level.FINEST, ex.toString(), ex);
233:
234: return null;
235: }
236: }
|