Example #1
0
  /**
   * Create a context as a child of this one
   *
   * @param name a <code>Name</code> value
   * @return a <code>Context</code> value
   * @exception NamingException if an error occurs
   */
  public Context createSubcontext(Name name) throws NamingException {
    if (isLocked()) {
      NamingException ne = new NamingException("This context is immutable");
      ne.setRemainingName(name);
      throw ne;
    }

    Name cname = toCanonicalName(name);

    if (cname == null) throw new NamingException("Name is null");
    if (cname.size() == 0) throw new NamingException("Name is empty");

    if (cname.size() == 1) {
      // not permitted to bind if something already bound at that name
      Binding binding = getBinding(cname);
      if (binding != null) throw new NameAlreadyBoundException(cname.toString());

      Context ctx = new NamingContext((Hashtable) _env.clone(), cname.get(0), this, _parser);
      addBinding(cname, ctx);
      return ctx;
    }

    // If the name has multiple subcontexts, walk the hierarchy by
    // fetching the first one. All intermediate subcontexts in the
    // name must already exist.
    String firstComponent = cname.get(0);
    Object ctx = null;

    if (firstComponent.equals("")) ctx = this;
    else {
      Binding binding = getBinding(firstComponent);
      if (binding == null) throw new NameNotFoundException(firstComponent + " is not bound");

      ctx = binding.getObject();

      if (ctx instanceof Reference) {
        // deference the object
        if (__log.isDebugEnabled())
          __log.debug("Object bound at " + firstComponent + " is a Reference");
        try {
          ctx =
              NamingManager.getObjectInstance(
                  ctx, getNameParser("").parse(firstComponent), this, _env);
        } catch (NamingException e) {
          throw e;
        } catch (Exception e) {
          __log.warn("", e);
          throw new NamingException(e.getMessage());
        }
      }
    }

    if (ctx instanceof Context) {
      return ((Context) ctx).createSubcontext(cname.getSuffix(1));
    } else throw new NotContextException(firstComponent + " is not a Context");
  }
Example #2
0
 /**
  * Return a general naming exception with a root cause and a remaining name field.
  *
  * @param message the message
  * @param cause the exception cause, or {@code null} for none
  * @param remainingName the remaining name
  * @return the exception
  */
 public static NamingException namingException(
     final String message, final Throwable cause, final Name remainingName) {
   final NamingException exception = namingException(message, cause);
   exception.setRemainingName(remainingName);
   return exception;
 }