1 | /* NamingManager.java --
|
---|
2 | Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package javax.naming.spi;
|
---|
40 |
|
---|
41 | import java.util.*;
|
---|
42 | import javax.naming.*;
|
---|
43 |
|
---|
44 | public class NamingManager
|
---|
45 | {
|
---|
46 | public static final String CPE = "java.naming.spi.CannotProceedException";
|
---|
47 |
|
---|
48 | private static InitialContextFactoryBuilder icfb = null;
|
---|
49 |
|
---|
50 | // Package private so DirectoryManager can access it.
|
---|
51 | static ObjectFactoryBuilder ofb = null;
|
---|
52 |
|
---|
53 | // This class cannot be instantiated.
|
---|
54 | NamingManager ()
|
---|
55 | {
|
---|
56 | }
|
---|
57 |
|
---|
58 | public static boolean hasInitialContextFactoryBuilder ()
|
---|
59 | {
|
---|
60 | return icfb != null;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public static Context getInitialContext (Hashtable environment)
|
---|
64 | throws NamingException
|
---|
65 | {
|
---|
66 | InitialContextFactory icf = null;
|
---|
67 |
|
---|
68 | if (icfb != null)
|
---|
69 | icf = icfb.createInitialContextFactory(environment);
|
---|
70 | else
|
---|
71 | {
|
---|
72 | String java_naming_factory_initial = null;
|
---|
73 | if (environment != null)
|
---|
74 | java_naming_factory_initial
|
---|
75 | = (String) environment.get (Context.INITIAL_CONTEXT_FACTORY);
|
---|
76 | if (java_naming_factory_initial == null)
|
---|
77 | java_naming_factory_initial =
|
---|
78 | System.getProperty (Context.INITIAL_CONTEXT_FACTORY);
|
---|
79 | if (java_naming_factory_initial == null)
|
---|
80 | throw new
|
---|
81 | NoInitialContextException ("Can't find property: "
|
---|
82 | + Context.INITIAL_CONTEXT_FACTORY);
|
---|
83 |
|
---|
84 | try
|
---|
85 | {
|
---|
86 | icf = (InitialContextFactory) Class.forName (java_naming_factory_initial).newInstance ();
|
---|
87 | }
|
---|
88 | catch (Exception exception)
|
---|
89 | {
|
---|
90 | NoInitialContextException e
|
---|
91 | = new NoInitialContextException ("Can't load InitialContextFactory class: "
|
---|
92 | + java_naming_factory_initial);
|
---|
93 | e.setRootCause(exception);
|
---|
94 | throw e;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | return icf.getInitialContext (environment);
|
---|
99 | }
|
---|
100 |
|
---|
101 | static Context getURLContext (Object refInfo,
|
---|
102 | Name name,
|
---|
103 | Context nameCtx,
|
---|
104 | String scheme,
|
---|
105 | Hashtable environment)
|
---|
106 | throws NamingException
|
---|
107 | {
|
---|
108 | String prefixes = null;
|
---|
109 | if (environment != null)
|
---|
110 | prefixes = (String) environment.get (Context.URL_PKG_PREFIXES);
|
---|
111 | if (prefixes == null)
|
---|
112 | prefixes = System.getProperty (Context.URL_PKG_PREFIXES);
|
---|
113 | if (prefixes == null)
|
---|
114 | {
|
---|
115 | // Specified as the default in the docs. Unclear if this is
|
---|
116 | // right for us.
|
---|
117 | prefixes = "com.sun.jndi.url";
|
---|
118 | }
|
---|
119 |
|
---|
120 | scheme = scheme + "." + scheme + "URLContextFactory";
|
---|
121 |
|
---|
122 | StringTokenizer tokens = new StringTokenizer (prefixes, ":");
|
---|
123 | while (tokens.hasMoreTokens ())
|
---|
124 | {
|
---|
125 | String aTry = tokens.nextToken ();
|
---|
126 | try
|
---|
127 | {
|
---|
128 | Class factoryClass = Class.forName (aTry + "." + scheme);
|
---|
129 | ObjectFactory factory =
|
---|
130 | (ObjectFactory) factoryClass.newInstance ();
|
---|
131 | Object obj = factory.getObjectInstance (refInfo, name,
|
---|
132 | nameCtx, environment);
|
---|
133 | Context ctx = (Context) obj;
|
---|
134 | if (ctx != null)
|
---|
135 | return ctx;
|
---|
136 | }
|
---|
137 | catch (ClassNotFoundException _1)
|
---|
138 | {
|
---|
139 | // Ignore it.
|
---|
140 | }
|
---|
141 | catch (ClassCastException _2)
|
---|
142 | {
|
---|
143 | // This means that the class we found was not an
|
---|
144 | // ObjectFactory or that the factory returned something
|
---|
145 | // which was not a Context.
|
---|
146 | }
|
---|
147 | catch (InstantiationException _3)
|
---|
148 | {
|
---|
149 | // If we couldn't instantiate the factory we might get
|
---|
150 | // this.
|
---|
151 | }
|
---|
152 | catch (IllegalAccessException _4)
|
---|
153 | {
|
---|
154 | // Another possibility when instantiating.
|
---|
155 | }
|
---|
156 | catch (NamingException _5)
|
---|
157 | {
|
---|
158 | throw _5;
|
---|
159 | }
|
---|
160 | catch (Exception _6)
|
---|
161 | {
|
---|
162 | // Anything from getObjectInstance.
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | return null;
|
---|
167 | }
|
---|
168 |
|
---|
169 | public static Context getURLContext (String scheme,
|
---|
170 | Hashtable environment)
|
---|
171 | throws NamingException
|
---|
172 | {
|
---|
173 | return getURLContext (null, null, null, scheme, environment);
|
---|
174 | }
|
---|
175 |
|
---|
176 | public static void setObjectFactoryBuilder (ObjectFactoryBuilder builder)
|
---|
177 | throws NamingException
|
---|
178 | {
|
---|
179 | SecurityManager sm = System.getSecurityManager ();
|
---|
180 | if (sm != null)
|
---|
181 | sm.checkSetFactory ();
|
---|
182 | // Once the builder is installed it cannot be replaced.
|
---|
183 | if (ofb != null)
|
---|
184 | throw new IllegalStateException ("builder already installed");
|
---|
185 | if (builder != null)
|
---|
186 | ofb = builder;
|
---|
187 | }
|
---|
188 |
|
---|
189 | static StringTokenizer getPlusPath (String property, Hashtable env,
|
---|
190 | Context nameCtx)
|
---|
191 | throws NamingException
|
---|
192 | {
|
---|
193 | String path = (String) env.get (property);
|
---|
194 | if (nameCtx == null)
|
---|
195 | nameCtx = getInitialContext (env);
|
---|
196 | String path2 = (String) nameCtx.getEnvironment ().get (property);
|
---|
197 | if (path == null)
|
---|
198 | path = path2;
|
---|
199 | else if (path2 != null)
|
---|
200 | path += ":" + path2;
|
---|
201 | return new StringTokenizer (path != null ? path : "", ":");
|
---|
202 | }
|
---|
203 |
|
---|
204 | public static Object getObjectInstance (Object refInfo,
|
---|
205 | Name name,
|
---|
206 | Context nameCtx,
|
---|
207 | Hashtable environment)
|
---|
208 | throws Exception
|
---|
209 | {
|
---|
210 | ObjectFactory factory = null;
|
---|
211 |
|
---|
212 | if (ofb != null)
|
---|
213 | factory = ofb.createObjectFactory (refInfo, environment);
|
---|
214 | else
|
---|
215 | {
|
---|
216 | // First see if we have a Reference or a Referenceable. If so
|
---|
217 | // we do some special processing.
|
---|
218 | Object ref2 = refInfo;
|
---|
219 | if (refInfo instanceof Referenceable)
|
---|
220 | ref2 = ((Referenceable) refInfo).getReference ();
|
---|
221 | if (ref2 instanceof Reference)
|
---|
222 | {
|
---|
223 | Reference ref = (Reference) ref2;
|
---|
224 |
|
---|
225 | // If we have a factory class name then we use that.
|
---|
226 | String fClass = ref.getFactoryClassName ();
|
---|
227 | if (fClass != null)
|
---|
228 | {
|
---|
229 | // Exceptions here are passed to the caller.
|
---|
230 | Class k = Class.forName (fClass);
|
---|
231 | factory = (ObjectFactory) k.newInstance ();
|
---|
232 | }
|
---|
233 | else
|
---|
234 | {
|
---|
235 | // There's no factory class name. If the address is a
|
---|
236 | // StringRefAddr with address type `URL', then we try
|
---|
237 | // the URL's context factory.
|
---|
238 | Enumeration e = ref.getAll ();
|
---|
239 | while (e.hasMoreElements ())
|
---|
240 | {
|
---|
241 | RefAddr ra = (RefAddr) e.nextElement ();
|
---|
242 | if (ra instanceof StringRefAddr
|
---|
243 | && "URL".equals (ra.getType ()))
|
---|
244 | {
|
---|
245 | factory
|
---|
246 | = (ObjectFactory) getURLContext (refInfo,
|
---|
247 | name,
|
---|
248 | nameCtx,
|
---|
249 | (String) ra.getContent (),
|
---|
250 | environment);
|
---|
251 | Object obj = factory.getObjectInstance (refInfo,
|
---|
252 | name,
|
---|
253 | nameCtx,
|
---|
254 | environment);
|
---|
255 | if (obj != null)
|
---|
256 | return obj;
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | // Have to try the next step.
|
---|
261 | factory = null;
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | // Now look at OBJECT_FACTORIES to find the factory.
|
---|
266 | if (factory == null)
|
---|
267 | {
|
---|
268 | StringTokenizer tokens = getPlusPath (Context.OBJECT_FACTORIES,
|
---|
269 | environment, nameCtx);
|
---|
270 |
|
---|
271 | while (tokens.hasMoreTokens ())
|
---|
272 | {
|
---|
273 | String klassName = tokens.nextToken ();
|
---|
274 | Class k = Class.forName (klassName);
|
---|
275 | factory = (ObjectFactory) k.newInstance ();
|
---|
276 | Object obj = factory.getObjectInstance (refInfo, name,
|
---|
277 | nameCtx, environment);
|
---|
278 | if (obj != null)
|
---|
279 | return obj;
|
---|
280 | }
|
---|
281 |
|
---|
282 | // Failure.
|
---|
283 | return refInfo;
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | if (factory == null)
|
---|
288 | return refInfo;
|
---|
289 | Object obj = factory.getObjectInstance (refInfo, name,
|
---|
290 | nameCtx, environment);
|
---|
291 | return obj == null ? refInfo : obj;
|
---|
292 | }
|
---|
293 |
|
---|
294 | public static void setInitialContextFactoryBuilder (InitialContextFactoryBuilder builder)
|
---|
295 | throws NamingException
|
---|
296 | {
|
---|
297 | SecurityManager sm = System.getSecurityManager ();
|
---|
298 | if (sm != null)
|
---|
299 | sm.checkSetFactory ();
|
---|
300 | // Once the builder is installed it cannot be replaced.
|
---|
301 | if (icfb != null)
|
---|
302 | throw new IllegalStateException ("builder already installed");
|
---|
303 | if (builder != null)
|
---|
304 | icfb = builder;
|
---|
305 | }
|
---|
306 |
|
---|
307 | public static Context getContinuationContext (CannotProceedException cpe)
|
---|
308 | throws NamingException
|
---|
309 | {
|
---|
310 | Hashtable env = cpe.getEnvironment ();
|
---|
311 | if (env != null)
|
---|
312 | env.put (CPE, cpe);
|
---|
313 |
|
---|
314 | // It is really unclear to me if this is right.
|
---|
315 | try
|
---|
316 | {
|
---|
317 | Object obj = getObjectInstance (null, cpe.getAltName (),
|
---|
318 | cpe.getAltNameCtx (), env);
|
---|
319 | if (obj != null)
|
---|
320 | return (Context) obj;
|
---|
321 | }
|
---|
322 | catch (Exception _)
|
---|
323 | {
|
---|
324 | }
|
---|
325 |
|
---|
326 | throw cpe;
|
---|
327 | }
|
---|
328 |
|
---|
329 | public static Object getStateToBind (Object obj, Name name,
|
---|
330 | Context nameCtx, Hashtable environment)
|
---|
331 | throws NamingException
|
---|
332 | {
|
---|
333 | StringTokenizer tokens = getPlusPath (Context.STATE_FACTORIES,
|
---|
334 | environment, nameCtx);
|
---|
335 | while (tokens.hasMoreTokens ())
|
---|
336 | {
|
---|
337 | String klassName = tokens.nextToken ();
|
---|
338 | try
|
---|
339 | {
|
---|
340 | Class k = Class.forName (klassName);
|
---|
341 | StateFactory factory = (StateFactory) k.newInstance ();
|
---|
342 | Object o = factory.getStateToBind (obj, name, nameCtx,
|
---|
343 | environment);
|
---|
344 | if (o != null)
|
---|
345 | return o;
|
---|
346 | }
|
---|
347 | catch (ClassNotFoundException _1)
|
---|
348 | {
|
---|
349 | // Ignore it.
|
---|
350 | }
|
---|
351 | catch (ClassCastException _2)
|
---|
352 | {
|
---|
353 | // This means that the class we found was not an
|
---|
354 | // ObjectFactory or that the factory returned something
|
---|
355 | // which was not a Context.
|
---|
356 | }
|
---|
357 | catch (InstantiationException _3)
|
---|
358 | {
|
---|
359 | // If we couldn't instantiate the factory we might get
|
---|
360 | // this.
|
---|
361 | }
|
---|
362 | catch (IllegalAccessException _4)
|
---|
363 | {
|
---|
364 | // Another possibility when instantiating.
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | return obj;
|
---|
369 | }
|
---|
370 | }
|
---|