| 1 | /* Copyright (C) 2000 Free Software Foundation
|
|---|
| 2 |
|
|---|
| 3 | This file is part of libgcj.
|
|---|
| 4 |
|
|---|
| 5 | This software is copyrighted work licensed under the terms of the
|
|---|
| 6 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 7 | details. */
|
|---|
| 8 |
|
|---|
| 9 | package javax.naming;
|
|---|
| 10 |
|
|---|
| 11 | import java.io.File;
|
|---|
| 12 | import java.io.FileInputStream;
|
|---|
| 13 | import java.io.IOException;
|
|---|
| 14 | import java.io.InputStream;
|
|---|
| 15 | import java.net.URL;
|
|---|
| 16 | import java.util.Enumeration;
|
|---|
| 17 | import java.util.Hashtable;
|
|---|
| 18 | import java.util.Properties;
|
|---|
| 19 | import java.applet.Applet;
|
|---|
| 20 | import java.util.Hashtable;
|
|---|
| 21 | import javax.naming.spi.NamingManager;
|
|---|
| 22 |
|
|---|
| 23 | public class InitialContext implements Context
|
|---|
| 24 | {
|
|---|
| 25 | protected Context defaultInitCtx;
|
|---|
| 26 | protected boolean gotDefault = false;
|
|---|
| 27 | protected Hashtable myProps;
|
|---|
| 28 |
|
|---|
| 29 | public InitialContext (Hashtable environment)
|
|---|
| 30 | {
|
|---|
| 31 | init (environment);
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | protected InitialContext (boolean lazy)
|
|---|
| 35 | {
|
|---|
| 36 | if (! lazy)
|
|---|
| 37 | init (null);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | public InitialContext ()
|
|---|
| 41 | {
|
|---|
| 42 | init (null);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | protected void init (Hashtable environment)
|
|---|
| 46 | {
|
|---|
| 47 | // FIXME: Is this enough?
|
|---|
| 48 | final String[] properties = {
|
|---|
| 49 | Context.DNS_URL,
|
|---|
| 50 | Context.INITIAL_CONTEXT_FACTORY,
|
|---|
| 51 | Context.OBJECT_FACTORIES,
|
|---|
| 52 | Context.PROVIDER_URL,
|
|---|
| 53 | Context.STATE_FACTORIES,
|
|---|
| 54 | Context.URL_PKG_PREFIXES,
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | // Create myProps, cloning environment if needed.
|
|---|
| 58 | if (environment != null)
|
|---|
| 59 | myProps = (Hashtable) environment.clone ();
|
|---|
| 60 | else
|
|---|
| 61 | myProps = new Hashtable ();
|
|---|
| 62 |
|
|---|
| 63 | Applet napplet = (Applet) myProps.get (Context.APPLET);
|
|---|
| 64 |
|
|---|
| 65 | for (int i = properties.length - 1; i >= 0; i--)
|
|---|
| 66 | {
|
|---|
| 67 | Object o = myProps.get (properties[i]);
|
|---|
| 68 |
|
|---|
| 69 | if (o == null)
|
|---|
| 70 | {
|
|---|
| 71 | if (napplet != null)
|
|---|
| 72 | o = napplet.getParameter (properties[i]);
|
|---|
| 73 | if (o == null)
|
|---|
| 74 | o = System.getProperty (properties[i]);
|
|---|
| 75 | if (o != null)
|
|---|
| 76 | myProps.put (properties[i], o);
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | try
|
|---|
| 81 | {
|
|---|
| 82 | Enumeration ep = Thread.currentThread().getContextClassLoader().getResources("jndi.naming");
|
|---|
| 83 | while (ep.hasMoreElements ())
|
|---|
| 84 | {
|
|---|
| 85 | URL url = (URL) ep.nextElement ();
|
|---|
| 86 | Properties p = new Properties ();
|
|---|
| 87 |
|
|---|
| 88 | try {
|
|---|
| 89 | InputStream is = url.openStream ();
|
|---|
| 90 | p.load (is);
|
|---|
| 91 | is.close ();
|
|---|
| 92 | } catch (IOException e) {}
|
|---|
| 93 |
|
|---|
| 94 | merge (myProps, p);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | catch (IOException e) {}
|
|---|
| 98 |
|
|---|
| 99 | String home = System.getProperty("java.home");
|
|---|
| 100 | if (home != null)
|
|---|
| 101 | {
|
|---|
| 102 | String fileName = home + File.separator
|
|---|
| 103 | + "lib" + File.separator + "jndi.properties";
|
|---|
| 104 | Properties p = new Properties ();
|
|---|
| 105 |
|
|---|
| 106 | try {
|
|---|
| 107 | InputStream is = new FileInputStream (fileName);
|
|---|
| 108 | p.load (is);
|
|---|
| 109 | is.close ();
|
|---|
| 110 | } catch (IOException e) {}
|
|---|
| 111 |
|
|---|
| 112 | merge (myProps, p);
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | // FIXME: Is this enough?
|
|---|
| 117 | private static final String[] colon_list =
|
|---|
| 118 | {
|
|---|
| 119 | Context.OBJECT_FACTORIES,
|
|---|
| 120 | Context.URL_PKG_PREFIXES,
|
|---|
| 121 | Context.STATE_FACTORIES
|
|---|
| 122 | };
|
|---|
| 123 |
|
|---|
| 124 | private static void merge (Hashtable h1, Hashtable h2)
|
|---|
| 125 | {
|
|---|
| 126 | Enumeration e2 = h2.keys();
|
|---|
| 127 |
|
|---|
| 128 | while (e2.hasMoreElements())
|
|---|
| 129 | {
|
|---|
| 130 | String key2 = (String) e2.nextElement();
|
|---|
| 131 | Object value1 = h1.get(key2);
|
|---|
| 132 | if (value1 == null)
|
|---|
| 133 | h1.put(key2, h2.get(key2));
|
|---|
| 134 | else if (key2.compareTo(colon_list[0]) == 0
|
|---|
| 135 | || key2.compareTo(colon_list[1]) == 0
|
|---|
| 136 | || key2.compareTo(colon_list[2]) == 0
|
|---|
| 137 | || key2.compareTo(colon_list[3]) == 0)
|
|---|
| 138 | {
|
|---|
| 139 | String value2 = (String) h2.get(key2);
|
|---|
| 140 | h1.put(key2, (String) value1 + ":" + value2);
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | protected Context getDefaultInitCtx () throws NamingException
|
|---|
| 146 | {
|
|---|
| 147 | if (! gotDefault)
|
|---|
| 148 | {
|
|---|
| 149 | defaultInitCtx = NamingManager.getInitialContext (myProps);
|
|---|
| 150 | gotDefault = true;
|
|---|
| 151 | }
|
|---|
| 152 | return defaultInitCtx;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 | protected Context getURLOrDefaultInitCtx (Name name)
|
|---|
| 157 | throws NamingException
|
|---|
| 158 | {
|
|---|
| 159 | if (name.size () > 0)
|
|---|
| 160 | return getURLOrDefaultInitCtx (name.get (0));
|
|---|
| 161 | else
|
|---|
| 162 | return getDefaultInitCtx ();
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | protected Context getURLOrDefaultInitCtx (String name)
|
|---|
| 166 | throws NamingException
|
|---|
| 167 | {
|
|---|
| 168 | String scheme = null;
|
|---|
| 169 |
|
|---|
| 170 | if (NamingManager.hasInitialContextFactoryBuilder())
|
|---|
| 171 | return getDefaultInitCtx();
|
|---|
| 172 | int colon = name.indexOf(':');
|
|---|
| 173 | int slash = name.indexOf('/');
|
|---|
| 174 | if (colon > 0 && (slash == -1 || colon < slash))
|
|---|
| 175 | scheme = name.substring(0, colon);
|
|---|
| 176 | if (scheme != null)
|
|---|
| 177 | {
|
|---|
| 178 | Context context =
|
|---|
| 179 | NamingManager.getURLContext(scheme, myProps);
|
|---|
| 180 | if (context != null)
|
|---|
| 181 | return context;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | return getDefaultInitCtx();
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | public void bind (Name name, Object obj) throws NamingException
|
|---|
| 188 | {
|
|---|
| 189 | getURLOrDefaultInitCtx (name).bind (name, obj);
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | public void bind (String name, Object obj) throws NamingException
|
|---|
| 193 | {
|
|---|
| 194 | getURLOrDefaultInitCtx (name).bind (name, obj);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | public Object lookup (Name name) throws NamingException
|
|---|
| 198 | {
|
|---|
| 199 | return getURLOrDefaultInitCtx (name).lookup (name);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | public Object lookup (String name) throws NamingException
|
|---|
| 203 | {
|
|---|
| 204 | return getURLOrDefaultInitCtx (name).lookup (name);
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | public void rebind (Name name, Object obj) throws NamingException
|
|---|
| 208 | {
|
|---|
| 209 | getURLOrDefaultInitCtx (name).rebind (name, obj);
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | public void rebind (String name, Object obj) throws NamingException
|
|---|
| 213 | {
|
|---|
| 214 | getURLOrDefaultInitCtx (name).rebind (name, obj);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | public void unbind (Name name) throws NamingException
|
|---|
| 218 | {
|
|---|
| 219 | getURLOrDefaultInitCtx (name).unbind (name);
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | public void unbind (String name) throws NamingException
|
|---|
| 223 | {
|
|---|
| 224 | getURLOrDefaultInitCtx (name).unbind (name);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | public void rename (Name oldName, Name newName) throws NamingException
|
|---|
| 228 | {
|
|---|
| 229 | getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | public void rename (String oldName, String newName) throws NamingException
|
|---|
| 233 | {
|
|---|
| 234 | getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | public NamingEnumeration list (Name name) throws NamingException
|
|---|
| 238 | {
|
|---|
| 239 | return getURLOrDefaultInitCtx (name).list (name);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | public NamingEnumeration list (String name) throws NamingException
|
|---|
| 243 | {
|
|---|
| 244 | return getURLOrDefaultInitCtx (name).list (name);
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | public NamingEnumeration listBindings (Name name) throws NamingException
|
|---|
| 248 | {
|
|---|
| 249 | return getURLOrDefaultInitCtx (name).listBindings (name);
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | public NamingEnumeration listBindings (String name) throws NamingException
|
|---|
| 253 | {
|
|---|
| 254 | return getURLOrDefaultInitCtx (name).listBindings (name);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | public void destroySubcontext (Name name) throws NamingException
|
|---|
| 258 | {
|
|---|
| 259 | getURLOrDefaultInitCtx (name).destroySubcontext (name);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | public void destroySubcontext (String name) throws NamingException
|
|---|
| 263 | {
|
|---|
| 264 | getURLOrDefaultInitCtx (name).destroySubcontext (name);
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | public Context createSubcontext (Name name) throws NamingException
|
|---|
| 268 | {
|
|---|
| 269 | return getURLOrDefaultInitCtx (name).createSubcontext (name);
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | public Context createSubcontext (String name) throws NamingException
|
|---|
| 273 | {
|
|---|
| 274 | return getURLOrDefaultInitCtx (name).createSubcontext (name);
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | public Object lookupLink (Name name) throws NamingException
|
|---|
| 278 | {
|
|---|
| 279 | return getURLOrDefaultInitCtx (name).lookupLink (name);
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | public Object lookupLink (String name) throws NamingException
|
|---|
| 283 | {
|
|---|
| 284 | return getURLOrDefaultInitCtx (name).lookupLink (name);
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | public NameParser getNameParser (Name name) throws NamingException
|
|---|
| 288 | {
|
|---|
| 289 | return getURLOrDefaultInitCtx (name).getNameParser (name);
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | public NameParser getNameParser (String name) throws NamingException
|
|---|
| 293 | {
|
|---|
| 294 | return getURLOrDefaultInitCtx (name).getNameParser (name);
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | public Name composeName (Name name, Name prefix) throws NamingException
|
|---|
| 298 | {
|
|---|
| 299 | return getURLOrDefaultInitCtx (name).composeName (name, prefix);
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | public String composeName (String name,
|
|---|
| 303 | String prefix) throws NamingException
|
|---|
| 304 | {
|
|---|
| 305 | return getURLOrDefaultInitCtx (name).composeName (name, prefix);
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | public Object addToEnvironment (String propName,
|
|---|
| 309 | Object propVal) throws NamingException
|
|---|
| 310 | {
|
|---|
| 311 | return myProps.put (propName, propVal);
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | public Object removeFromEnvironment (String propName) throws NamingException
|
|---|
| 315 | {
|
|---|
| 316 | return myProps.remove (propName);
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | public Hashtable getEnvironment () throws NamingException
|
|---|
| 320 | {
|
|---|
| 321 | return myProps;
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | public void close () throws NamingException
|
|---|
| 325 | {
|
|---|
| 326 | throw new OperationNotSupportedException ();
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | public String getNameInNamespace () throws NamingException
|
|---|
| 330 | {
|
|---|
| 331 | throw new OperationNotSupportedException ();
|
|---|
| 332 | }
|
|---|
| 333 | }
|
|---|