2003-12-06 Michael Koch <konqueror@gmx.de>
[official-gcc.git] / libjava / javax / naming / spi / NamingManager.java
blob4051f2288a9bb6379315226ba6013a3e98780db8
1 /* NamingManager.java --
2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package javax.naming.spi;
41 import java.util.Enumeration;
42 import java.util.Hashtable;
43 import java.util.StringTokenizer;
44 import javax.naming.CannotProceedException;
45 import javax.naming.Context;
46 import javax.naming.Name;
47 import javax.naming.NamingException;
48 import javax.naming.NoInitialContextException;
49 import javax.naming.RefAddr;
50 import javax.naming.Reference;
51 import javax.naming.Referenceable;
52 import javax.naming.StringRefAddr;
54 public class NamingManager
56 public static final String CPE = "java.naming.spi.CannotProceedException";
58 private static InitialContextFactoryBuilder icfb = null;
60 // Package private so DirectoryManager can access it.
61 static ObjectFactoryBuilder ofb = null;
63 // This class cannot be instantiated.
64 NamingManager ()
68 public static boolean hasInitialContextFactoryBuilder ()
70 return icfb != null;
73 public static Context getInitialContext (Hashtable environment)
74 throws NamingException
76 InitialContextFactory icf = null;
78 if (icfb != null)
79 icf = icfb.createInitialContextFactory(environment);
80 else
82 String java_naming_factory_initial = null;
83 if (environment != null)
84 java_naming_factory_initial
85 = (String) environment.get (Context.INITIAL_CONTEXT_FACTORY);
86 if (java_naming_factory_initial == null)
87 java_naming_factory_initial =
88 System.getProperty (Context.INITIAL_CONTEXT_FACTORY);
89 if (java_naming_factory_initial == null)
90 throw new
91 NoInitialContextException ("Can't find property: "
92 + Context.INITIAL_CONTEXT_FACTORY);
94 try
96 icf = (InitialContextFactory)Class.forName
97 (java_naming_factory_initial, true,
98 Thread.currentThread().getContextClassLoader())
99 .newInstance ();
101 catch (Exception exception)
103 NoInitialContextException e
104 = new NoInitialContextException
105 ("Can't load InitialContextFactory class: "
106 + java_naming_factory_initial);
107 e.setRootCause(exception);
108 throw e;
112 return icf.getInitialContext (environment);
115 static Context getURLContext (Object refInfo,
116 Name name,
117 Context nameCtx,
118 String scheme,
119 Hashtable environment)
120 throws NamingException
122 String prefixes = null;
123 if (environment != null)
124 prefixes = (String) environment.get (Context.URL_PKG_PREFIXES);
125 if (prefixes == null)
126 prefixes = System.getProperty (Context.URL_PKG_PREFIXES);
127 if (prefixes == null)
129 // Specified as the default in the docs. Unclear if this is
130 // right for us.
131 prefixes = "com.sun.jndi.url";
134 scheme = scheme + "." + scheme + "URLContextFactory";
136 StringTokenizer tokens = new StringTokenizer (prefixes, ":");
137 while (tokens.hasMoreTokens ())
139 String aTry = tokens.nextToken ();
142 Class factoryClass = Class.forName (aTry + "." + scheme,
143 true,
144 Thread.currentThread().getContextClassLoader());
145 ObjectFactory factory =
146 (ObjectFactory) factoryClass.newInstance ();
147 Object obj = factory.getObjectInstance (refInfo, name,
148 nameCtx, environment);
149 Context ctx = (Context) obj;
150 if (ctx != null)
151 return ctx;
153 catch (ClassNotFoundException _1)
155 // Ignore it.
157 catch (ClassCastException _2)
159 // This means that the class we found was not an
160 // ObjectFactory or that the factory returned something
161 // which was not a Context.
163 catch (InstantiationException _3)
165 // If we couldn't instantiate the factory we might get
166 // this.
168 catch (IllegalAccessException _4)
170 // Another possibility when instantiating.
172 catch (NamingException _5)
174 throw _5;
176 catch (Exception _6)
178 // Anything from getObjectInstance.
182 return null;
185 public static Context getURLContext (String scheme,
186 Hashtable environment)
187 throws NamingException
189 return getURLContext (null, null, null, scheme, environment);
192 public static void setObjectFactoryBuilder (ObjectFactoryBuilder builder)
193 throws NamingException
195 SecurityManager sm = System.getSecurityManager ();
196 if (sm != null)
197 sm.checkSetFactory ();
198 // Once the builder is installed it cannot be replaced.
199 if (ofb != null)
200 throw new IllegalStateException ("builder already installed");
201 if (builder != null)
202 ofb = builder;
205 static StringTokenizer getPlusPath (String property, Hashtable env,
206 Context nameCtx)
207 throws NamingException
209 String path = (String) env.get (property);
210 if (nameCtx == null)
211 nameCtx = getInitialContext (env);
212 String path2 = (String) nameCtx.getEnvironment ().get (property);
213 if (path == null)
214 path = path2;
215 else if (path2 != null)
216 path += ":" + path2;
217 return new StringTokenizer (path != null ? path : "", ":");
220 public static Object getObjectInstance (Object refInfo,
221 Name name,
222 Context nameCtx,
223 Hashtable environment)
224 throws Exception
226 ObjectFactory factory = null;
228 if (ofb != null)
229 factory = ofb.createObjectFactory (refInfo, environment);
230 else
232 // First see if we have a Reference or a Referenceable. If so
233 // we do some special processing.
234 Object ref2 = refInfo;
235 if (refInfo instanceof Referenceable)
236 ref2 = ((Referenceable) refInfo).getReference ();
237 if (ref2 instanceof Reference)
239 Reference ref = (Reference) ref2;
241 // If we have a factory class name then we use that.
242 String fClass = ref.getFactoryClassName ();
243 if (fClass != null)
245 // Exceptions here are passed to the caller.
246 Class k = Class.forName (fClass,
247 true,
248 Thread.currentThread().getContextClassLoader());
249 factory = (ObjectFactory) k.newInstance ();
251 else
253 // There's no factory class name. If the address is a
254 // StringRefAddr with address type `URL', then we try
255 // the URL's context factory.
256 Enumeration e = ref.getAll ();
257 while (e.hasMoreElements ())
259 RefAddr ra = (RefAddr) e.nextElement ();
260 if (ra instanceof StringRefAddr
261 && "URL".equals (ra.getType ()))
263 factory
264 = (ObjectFactory) getURLContext (refInfo,
265 name,
266 nameCtx,
267 (String) ra.getContent (),
268 environment);
269 Object obj = factory.getObjectInstance (refInfo,
270 name,
271 nameCtx,
272 environment);
273 if (obj != null)
274 return obj;
278 // Have to try the next step.
279 factory = null;
283 // Now look at OBJECT_FACTORIES to find the factory.
284 if (factory == null)
286 StringTokenizer tokens = getPlusPath (Context.OBJECT_FACTORIES,
287 environment, nameCtx);
289 while (tokens.hasMoreTokens ())
291 String klassName = tokens.nextToken ();
292 Class k = Class.forName (klassName,
293 true,
294 Thread.currentThread().getContextClassLoader());
295 factory = (ObjectFactory) k.newInstance ();
296 Object obj = factory.getObjectInstance (refInfo, name,
297 nameCtx, environment);
298 if (obj != null)
299 return obj;
302 // Failure.
303 return refInfo;
307 if (factory == null)
308 return refInfo;
309 Object obj = factory.getObjectInstance (refInfo, name,
310 nameCtx, environment);
311 return obj == null ? refInfo : obj;
314 public static void setInitialContextFactoryBuilder (InitialContextFactoryBuilder builder)
315 throws NamingException
317 SecurityManager sm = System.getSecurityManager ();
318 if (sm != null)
319 sm.checkSetFactory ();
320 // Once the builder is installed it cannot be replaced.
321 if (icfb != null)
322 throw new IllegalStateException ("builder already installed");
323 if (builder != null)
324 icfb = builder;
327 public static Context getContinuationContext (CannotProceedException cpe)
328 throws NamingException
330 Hashtable env = cpe.getEnvironment ();
331 if (env != null)
332 env.put (CPE, cpe);
334 // It is really unclear to me if this is right.
337 Object obj = getObjectInstance (cpe.getResolvedObj(),
338 cpe.getAltName (),
339 cpe.getAltNameCtx (),
340 env);
341 if (obj != null)
342 return (Context) obj;
344 catch (Exception _)
348 // fix stack trace for re-thrown exception (message confusing otherwise)
349 cpe.fillInStackTrace();
351 throw cpe;
354 public static Object getStateToBind (Object obj, Name name,
355 Context nameCtx, Hashtable environment)
356 throws NamingException
358 StringTokenizer tokens = getPlusPath (Context.STATE_FACTORIES,
359 environment, nameCtx);
360 while (tokens.hasMoreTokens ())
362 String klassName = tokens.nextToken ();
365 Class k = Class.forName (klassName,
366 true,
367 Thread.currentThread().getContextClassLoader());
368 StateFactory factory = (StateFactory) k.newInstance ();
369 Object o = factory.getStateToBind (obj, name, nameCtx,
370 environment);
371 if (o != null)
372 return o;
374 catch (ClassNotFoundException _1)
376 // Ignore it.
378 catch (ClassCastException _2)
380 // This means that the class we found was not an
381 // ObjectFactory or that the factory returned something
382 // which was not a Context.
384 catch (InstantiationException _3)
386 // If we couldn't instantiate the factory we might get
387 // this.
389 catch (IllegalAccessException _4)
391 // Another possibility when instantiating.
395 return obj;