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)
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
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
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.
68 public static boolean hasInitialContextFactoryBuilder ()
73 public static Context
getInitialContext (Hashtable environment
)
74 throws NamingException
76 InitialContextFactory icf
= null;
79 icf
= icfb
.createInitialContextFactory(environment
);
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)
91 NoInitialContextException ("Can't find property: "
92 + Context
.INITIAL_CONTEXT_FACTORY
);
96 icf
= (InitialContextFactory
)Class
.forName
97 (java_naming_factory_initial
, true,
98 Thread
.currentThread().getContextClassLoader())
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
);
112 return icf
.getInitialContext (environment
);
115 static Context
getURLContext (Object refInfo
,
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
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
,
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
;
153 catch (ClassNotFoundException _1
)
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
168 catch (IllegalAccessException _4
)
170 // Another possibility when instantiating.
172 catch (NamingException _5
)
178 // Anything from getObjectInstance.
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 ();
197 sm
.checkSetFactory ();
198 // Once the builder is installed it cannot be replaced.
200 throw new IllegalStateException ("builder already installed");
205 static StringTokenizer
getPlusPath (String property
, Hashtable env
,
207 throws NamingException
209 String path
= (String
) env
.get (property
);
211 nameCtx
= getInitialContext (env
);
212 String path2
= (String
) nameCtx
.getEnvironment ().get (property
);
215 else if (path2
!= null)
217 return new StringTokenizer (path
!= null ? path
: "", ":");
220 public static Object
getObjectInstance (Object refInfo
,
223 Hashtable environment
)
226 ObjectFactory factory
= null;
229 factory
= ofb
.createObjectFactory (refInfo
, environment
);
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 ();
245 // Exceptions here are passed to the caller.
246 Class k
= Class
.forName (fClass
,
248 Thread
.currentThread().getContextClassLoader());
249 factory
= (ObjectFactory
) k
.newInstance ();
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 ()))
264 = (ObjectFactory
) getURLContext (refInfo
,
267 (String
) ra
.getContent (),
269 Object obj
= factory
.getObjectInstance (refInfo
,
278 // Have to try the next step.
283 // Now look at OBJECT_FACTORIES to find the factory.
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
,
294 Thread
.currentThread().getContextClassLoader());
295 factory
= (ObjectFactory
) k
.newInstance ();
296 Object obj
= factory
.getObjectInstance (refInfo
, name
,
297 nameCtx
, environment
);
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 ();
319 sm
.checkSetFactory ();
320 // Once the builder is installed it cannot be replaced.
322 throw new IllegalStateException ("builder already installed");
327 public static Context
getContinuationContext (CannotProceedException cpe
)
328 throws NamingException
330 Hashtable env
= cpe
.getEnvironment ();
334 // It is really unclear to me if this is right.
337 Object obj
= getObjectInstance (cpe
.getResolvedObj(),
339 cpe
.getAltNameCtx (),
342 return (Context
) obj
;
348 // fix stack trace for re-thrown exception (message confusing otherwise)
349 cpe
.fillInStackTrace();
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
,
367 Thread
.currentThread().getContextClassLoader());
368 StateFactory factory
= (StateFactory
) k
.newInstance ();
369 Object o
= factory
.getStateToBind (obj
, name
, nameCtx
,
374 catch (ClassNotFoundException _1
)
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
389 catch (IllegalAccessException _4
)
391 // Another possibility when instantiating.