Source for javax.naming.InitialContext

   1: /* InitialContext.java --
   2:    Copyright (C) 2000, 2002, 2003, 2004 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., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 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;
  40: 
  41: import java.applet.Applet;
  42: import java.io.IOException;
  43: import java.io.InputStream;
  44: import java.net.URL;
  45: import java.util.Enumeration;
  46: import java.util.Hashtable;
  47: import java.util.Properties;
  48: 
  49: import javax.naming.spi.NamingManager;
  50: 
  51: public class InitialContext implements Context
  52: {
  53:   protected Context defaultInitCtx;
  54:   protected boolean gotDefault = false;
  55:   protected Hashtable myProps;
  56:   
  57:   public InitialContext (Hashtable environment)
  58:     throws NamingException
  59:   {
  60:     init (environment);
  61:   }
  62:   
  63:   protected InitialContext (boolean lazy)
  64:     throws NamingException
  65:   {
  66:     if (! lazy)
  67:       init (null);
  68:   }
  69:   
  70:   public InitialContext ()
  71:     throws NamingException
  72:   {
  73:     init (null);
  74:   }
  75:  
  76:   /** @since 1.3 */
  77:   protected void init (Hashtable environment)
  78:     throws NamingException
  79:   {
  80:     // FIXME: Is this enough?
  81:     final String[] properties = {
  82:       Context.DNS_URL,
  83:       Context.INITIAL_CONTEXT_FACTORY,
  84:       Context.OBJECT_FACTORIES,
  85:       Context.PROVIDER_URL,
  86:       Context.STATE_FACTORIES,
  87:       Context.URL_PKG_PREFIXES,
  88:     };
  89:       
  90:     // Create myProps, cloning environment if needed.
  91:     if (environment != null)
  92:       myProps = (Hashtable) environment.clone ();
  93:     else
  94:       myProps = new Hashtable ();
  95:       
  96:     Applet napplet = (Applet) myProps.get (Context.APPLET);
  97:       
  98:     for (int i = properties.length - 1; i >= 0; i--)
  99:       {
 100:     Object o = myProps.get (properties[i]);
 101:       
 102:     if (o == null)
 103:       {
 104:         if (napplet != null)
 105:           o = napplet.getParameter (properties[i]);
 106:         if (o == null)
 107:           o = System.getProperty (properties[i]);
 108:         if (o != null)
 109:           myProps.put (properties[i], o);
 110:       }
 111:       }
 112: 
 113:     try
 114:       {
 115:     Enumeration ep = Thread.currentThread().getContextClassLoader().getResources("jndi.naming");
 116:     while (ep.hasMoreElements ())
 117:       {
 118:         URL url = (URL) ep.nextElement ();
 119:         Properties p = new Properties ();
 120:             
 121:         try
 122:           {
 123:         InputStream is = url.openStream ();
 124:         p.load (is);
 125:         is.close ();
 126:           }
 127:         catch (IOException e)
 128:           {
 129:           }
 130: 
 131:         merge (myProps, p);
 132:       }
 133:       }
 134:     catch (IOException e)
 135:       {
 136:       }
 137: 
 138:     String home = System.getProperty("gnu.classpath.home.url");
 139:     if (home != null)
 140:       {
 141:     String url = home + "/jndi.properties";
 142:     Properties p = new Properties ();
 143:     
 144:     try
 145:       {
 146:         InputStream is = new URL(url).openStream();
 147:         p.load (is);
 148:         is.close ();
 149:       }
 150:     catch (IOException e)
 151:       {
 152:         // Ignore.
 153:       }
 154: 
 155:     merge (myProps, p);
 156:       }
 157:   }
 158: 
 159:   // FIXME: Is this enough?
 160:   private static final String[] colon_list = 
 161:     {
 162:       Context.OBJECT_FACTORIES,
 163:       Context.URL_PKG_PREFIXES,
 164:       Context.STATE_FACTORIES
 165:     };
 166: 
 167:   private static void merge (Hashtable h1, Hashtable h2)
 168:   {
 169:     Enumeration e2 = h2.keys();
 170:     
 171:     while (e2.hasMoreElements())
 172:       {
 173:     String key2 = (String) e2.nextElement();
 174:     Object value1 = h1.get(key2);
 175:     if (value1 == null)
 176:       h1.put(key2, h2.get(key2));
 177:     else if (key2.compareTo(colon_list[0]) == 0
 178:          || key2.compareTo(colon_list[1]) == 0
 179:          || key2.compareTo(colon_list[2]) == 0
 180:          || key2.compareTo(colon_list[3]) == 0)
 181:       {
 182:         String value2 = (String) h2.get(key2);
 183:         h1.put(key2, (String) value1 + ":" + value2);
 184:       }
 185:       }
 186:   }
 187: 
 188:   protected Context getDefaultInitCtx () throws NamingException
 189:   {
 190:     if (! gotDefault)
 191:       {
 192:     defaultInitCtx = NamingManager.getInitialContext (myProps);
 193:     gotDefault = true;
 194:       }
 195:     return defaultInitCtx;
 196:   }
 197: 
 198: 
 199:   protected Context getURLOrDefaultInitCtx (Name name) 
 200:     throws NamingException
 201:   {
 202:     if (name.size () > 0)
 203:       return getURLOrDefaultInitCtx (name.get (0));
 204:     else
 205:       return getDefaultInitCtx ();
 206:   }
 207: 
 208:   protected Context getURLOrDefaultInitCtx (String name) 
 209:     throws NamingException
 210:   {
 211:     String scheme = null;
 212: 
 213:     if (NamingManager.hasInitialContextFactoryBuilder())
 214:       return getDefaultInitCtx();
 215:     int colon = name.indexOf(':');
 216:     int slash = name.indexOf('/');
 217:     if (colon > 0 && (slash == -1 || colon < slash))
 218:       scheme = name.substring(0, colon);
 219:     if (scheme != null) 
 220:       {
 221:     Context context = 
 222:       NamingManager.getURLContext(scheme, myProps);
 223:     if (context != null)
 224:       return context;
 225:       }
 226:     
 227:     return getDefaultInitCtx();
 228:   }
 229: 
 230:   public void bind (Name name, Object obj) throws NamingException
 231:   {
 232:     getURLOrDefaultInitCtx (name).bind (name, obj);
 233:   }
 234: 
 235:   public void bind (String name, Object obj) throws NamingException
 236:   {
 237:     getURLOrDefaultInitCtx (name).bind (name, obj);
 238:   }
 239: 
 240:   public Object lookup (Name name) throws NamingException
 241:   {
 242:     try
 243:       {
 244:     return getURLOrDefaultInitCtx (name).lookup (name);
 245:       }
 246:     catch (CannotProceedException cpe)
 247:       {
 248:     Context ctx = NamingManager.getContinuationContext (cpe);
 249:     return ctx.lookup (cpe.getRemainingName());
 250:       }
 251:   }
 252: 
 253:   public Object lookup (String name) throws NamingException
 254:   {
 255:       try
 256:     {
 257:       return getURLOrDefaultInitCtx (name).lookup (name);
 258:     }
 259:       catch (CannotProceedException cpe)
 260:     {
 261:       Context ctx = NamingManager.getContinuationContext (cpe);
 262:       return ctx.lookup (cpe.getRemainingName());
 263:     }
 264:   }
 265: 
 266:   public void rebind (Name name, Object obj) throws NamingException
 267:   {
 268:     getURLOrDefaultInitCtx (name).rebind (name, obj);
 269:   }
 270: 
 271:   public void rebind (String name, Object obj) throws NamingException
 272:   {
 273:     getURLOrDefaultInitCtx (name).rebind (name, obj);
 274:   }
 275: 
 276:   public void unbind (Name name) throws NamingException
 277:   {
 278:     getURLOrDefaultInitCtx (name).unbind (name);
 279:   }
 280: 
 281:   public void unbind (String name) throws NamingException
 282:   {
 283:     getURLOrDefaultInitCtx (name).unbind (name);
 284:   }
 285: 
 286:   public void rename (Name oldName, Name newName) throws NamingException
 287:   {
 288:     getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
 289:   }
 290: 
 291:   public void rename (String oldName, String newName) throws NamingException
 292:   {
 293:     getURLOrDefaultInitCtx (oldName).rename (oldName, newName);
 294:   }
 295: 
 296:   public NamingEnumeration list (Name name) throws NamingException
 297:   {
 298:     return getURLOrDefaultInitCtx (name).list (name);
 299:   }
 300: 
 301:   public NamingEnumeration list (String name) throws NamingException
 302:   {
 303:     return getURLOrDefaultInitCtx (name).list (name);
 304:   }
 305: 
 306:   public NamingEnumeration listBindings (Name name) throws NamingException
 307:   {
 308:     return getURLOrDefaultInitCtx (name).listBindings (name);
 309:   }
 310: 
 311:   public NamingEnumeration listBindings (String name) throws NamingException
 312:   {
 313:     return getURLOrDefaultInitCtx (name).listBindings (name);
 314:   }
 315: 
 316:   public void destroySubcontext (Name name) throws NamingException
 317:   {
 318:     getURLOrDefaultInitCtx (name).destroySubcontext (name);
 319:   }
 320: 
 321:   public void destroySubcontext (String name) throws NamingException
 322:   {
 323:     getURLOrDefaultInitCtx (name).destroySubcontext (name);
 324:   }
 325: 
 326:   public Context createSubcontext (Name name) throws NamingException
 327:   {
 328:     return getURLOrDefaultInitCtx (name).createSubcontext (name);
 329:   }
 330: 
 331:   public Context createSubcontext (String name) throws NamingException
 332:   {
 333:     return getURLOrDefaultInitCtx (name).createSubcontext (name);
 334:   }
 335: 
 336:   public Object lookupLink (Name name) throws NamingException
 337:   {
 338:     return getURLOrDefaultInitCtx (name).lookupLink (name);
 339:   }
 340: 
 341:   public Object lookupLink (String name) throws NamingException
 342:   {
 343:     return getURLOrDefaultInitCtx (name).lookupLink (name);
 344:   }
 345: 
 346:   public NameParser getNameParser (Name name) throws NamingException
 347:   {
 348:     return getURLOrDefaultInitCtx (name).getNameParser (name);
 349:   }
 350: 
 351:   public NameParser getNameParser (String name) throws NamingException
 352:   {
 353:     return getURLOrDefaultInitCtx (name).getNameParser (name);
 354:   }
 355: 
 356:   public Name composeName (Name name, Name prefix) throws NamingException
 357:   {
 358:     return getURLOrDefaultInitCtx (name).composeName (name, prefix);
 359:   }
 360: 
 361:   public String composeName (String name, 
 362:                  String prefix) throws NamingException
 363:   {
 364:     return getURLOrDefaultInitCtx (name).composeName (name, prefix);
 365:   }
 366: 
 367:   public Object addToEnvironment (String propName, 
 368:                   Object propVal) throws NamingException
 369:   {
 370:     return myProps.put (propName, propVal);
 371:   }
 372: 
 373:   public Object removeFromEnvironment (String propName) throws NamingException
 374:   {
 375:     return myProps.remove (propName);
 376:   }
 377: 
 378:   public Hashtable getEnvironment () throws NamingException
 379:   {
 380:     return myProps;
 381:   }
 382: 
 383:   public void close () throws NamingException
 384:   {
 385:     myProps = null;
 386:     defaultInitCtx = null;
 387:   }
 388: 
 389:   public String getNameInNamespace () throws NamingException
 390:   {
 391:     throw new OperationNotSupportedException ();
 392:   }
 393: }