KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > naming > Jndi


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.naming;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.Name JavaDoc;
37 import javax.naming.NameNotFoundException JavaDoc;
38 import javax.naming.NameParser JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Static utility functions.
45  */

46 public class Jndi {
47   private static Logger JavaDoc log = Log.open(Jndi.class);
48   private static L10N L = new L10N(Jndi.class);
49
50   /**
51    * Returns the full name.
52    */

53   public static String JavaDoc getFullName(String JavaDoc shortName)
54   {
55     if (shortName.startsWith("java:comp"))
56       return shortName;
57     else
58       return "java:comp/env/" + shortName;
59   }
60
61   public static void bindDeepShort(String JavaDoc name, Object JavaDoc obj)
62     throws NamingException JavaDoc
63   {
64     bindImpl(new InitialContext JavaDoc(), getFullName(name), obj, name);
65   }
66
67   public static void bindDeepShort(Context JavaDoc context, String JavaDoc name, Object JavaDoc obj)
68     throws NamingException JavaDoc
69   {
70     bindImpl(context, getFullName(name), obj, name);
71   }
72
73   public static void bindDeep(String JavaDoc name, Object JavaDoc obj)
74     throws NamingException JavaDoc
75   {
76     bindImpl(new InitialContext JavaDoc(), name, obj, name);
77   }
78
79   public static void bindDeep(Context JavaDoc context, String JavaDoc name, Object JavaDoc obj)
80     throws NamingException JavaDoc
81   {
82     bindImpl(context, name, obj, name);
83   }
84
85   private static void bindImpl(Context JavaDoc context, String JavaDoc name,
86                                Object JavaDoc obj, String JavaDoc fullName)
87     throws NamingException JavaDoc
88   {
89     NameParser JavaDoc parser = context.getNameParser("");
90     Name JavaDoc parsedName = parser.parse(name);
91
92     if (parsedName.size() == 1) {
93       Object JavaDoc value = null;
94
95       try {
96     value = context.lookup(name);
97       } catch (NameNotFoundException JavaDoc e) {
98       }
99       
100       context.rebind(name, obj);
101
102       // server/1620
103
if (value != null && value != obj)
104         log.warning(L.l("'{0}' overrides a previous JNDI resource. The old resource is '{1}' and the new one is '{2}'",
105                         fullName, value, obj));
106
107       return;
108     }
109
110     Object JavaDoc sub = null;
111
112     try {
113       sub = context.lookup(parsedName.get(0));
114     } catch (NameNotFoundException JavaDoc e) {
115     }
116
117     if (sub == null)
118       sub = context.createSubcontext(parsedName.get(0));
119       
120     if (sub instanceof Context JavaDoc)
121       bindImpl((Context JavaDoc) sub, parsedName.getSuffix(1).toString(), obj, fullName);
122
123     else
124       throw new NamingException JavaDoc(L.l("`{0}' is an invalid JNDI name because `{1} is not a Context. One of the subcontexts is not a Context as expected.",
125                                     fullName, sub));
126   }
127
128   /**
129    * Binds the object into JNDI without warnings if an old
130    * object exists. The name may be a full name or the short
131    * form.
132    */

133   public static void rebindDeepShort(String JavaDoc name, Object JavaDoc obj)
134     throws NamingException JavaDoc
135   {
136     rebindImpl(new InitialContext JavaDoc(), getFullName(name), obj, name);
137   }
138
139   /**
140    * Binds the object into JNDI without warnings if an old
141    * object exists. The name may be a full name or the short
142    * form.
143    */

144   public static void rebindDeepShort(Context JavaDoc context, String JavaDoc name, Object JavaDoc obj)
145     throws NamingException JavaDoc
146   {
147     rebindImpl(context, getFullName(name), obj, name);
148   }
149
150   /**
151    * Binds the object into JNDI without warnings if an old
152    * object exists, using the full JNDI name.
153    */

154   public static void rebindDeep(String JavaDoc name, Object JavaDoc obj)
155     throws NamingException JavaDoc
156   {
157     rebindImpl(new InitialContext JavaDoc(), 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 JavaDoc context, String JavaDoc name, Object JavaDoc obj)
165     throws NamingException JavaDoc
166   {
167     rebindImpl(context, name, obj, name);
168   }
169
170   /**
171    * Binds the object into JNDI without warnings if an old
172    * object exists.
173    */

174   private static void rebindImpl(Context JavaDoc context, String JavaDoc name,
175                                  Object JavaDoc obj, String JavaDoc fullName)
176     throws NamingException JavaDoc
177   {
178     NameParser JavaDoc parser = context.getNameParser("");
179     Name JavaDoc parsedName = parser.parse(name);
180
181     if (parsedName.size() == 1) {
182       context.rebind(name, obj);
183       return;
184     }
185
186     Object JavaDoc sub = null;
187
188     try {
189       sub = context.lookup(parsedName.get(0));
190     } catch (NameNotFoundException JavaDoc e) {
191       log.log(Level.FINEST, e.toString(), e);
192     }
193
194     if (sub == null)
195       sub = context.createSubcontext(parsedName.get(0));
196       
197     if (sub instanceof Context JavaDoc)
198       rebindImpl((Context JavaDoc) sub, parsedName.getSuffix(1).toString(), obj,
199                  fullName);
200
201     else
202       throw new NamingException JavaDoc(L.l("`{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 JavaDoc lookup(String JavaDoc name)
208   {
209     try {
210       Object JavaDoc value = new InitialContext JavaDoc().lookup(name);
211         
212       if (value != null)
213         return value;
214     } catch (NamingException JavaDoc e) {
215       if (log.isLoggable(Level.FINEST))
216         log.log(Level.FINEST, e.toString(), e);
217     }
218
219     if (! name.startsWith("java:comp/env")) {
220       try {
221         Object JavaDoc value = new InitialContext JavaDoc().lookup("java:comp/env/" + name);
222         
223         if (value != null)
224           return value;
225       } catch (NamingException JavaDoc e) {
226         if (log.isLoggable(Level.FINEST))
227           log.log(Level.FINEST, e.toString(), e);
228       }
229     }
230
231     return null;
232   }
233
234   private Jndi() {}
235 }
236
237
Popular Tags