示例#1
0
  /**
   * Overwrite or create a binding
   *
   * @param name a <code>Name</code> value
   * @param obj an <code>Object</code> value
   * @exception NamingException if an error occurs
   */
  public void rebind(Name name, Object obj) throws NamingException {
    if (isLocked()) throw new NamingException("This context is immutable");

    Name cname = toCanonicalName(name);

    if (cname == null) throw new NamingException("Name is null");

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

    // if no subcontexts, just bind it
    if (cname.size() == 1) {
      // check if it is a Referenceable
      Object objToBind = NamingManager.getStateToBind(obj, name, this, _env);

      if (objToBind instanceof Referenceable) {
        objToBind = ((Referenceable) objToBind).getReference();
      }
      removeBinding(cname);
      addBinding(cname, objToBind);
    } else {
      // walk down the subcontext hierarchy
      if (__log.isDebugEnabled())
        __log.debug(
            "Checking for existing binding for name="
                + cname
                + " for first element of name="
                + cname.get(0));

      String firstComponent = cname.get(0);
      Object ctx = null;

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

        ctx = binding.getObject();

        if (ctx instanceof Reference) {
          // deference the object
          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) {
        ((Context) ctx).rebind(cname.getSuffix(1), obj);
      } else
        throw new NotContextException("Object bound at " + firstComponent + " is not a Context");
    }
  }
  @Before
  public void setUp() throws Exception {
    Locale.setDefault(Locale.US);
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    // enforce binary compatibility for the xml-files so that comparing them can be faster.

    ClassicEngineBoot.getInstance().start();
    if (NamingManager.hasInitialContextFactoryBuilder() == false) {
      NamingManager.setInitialContextFactoryBuilder(new DebugJndiContextFactoryBuilder());
    }

    localFontRegistry = new LocalFontRegistry();
    localFontRegistry.initialize();
  }
 /**
  * Gets the context in which to continue the operation. This method is called when this context is
  * asked to process a multicomponent Name in which the first component is a URL. Treat the first
  * component like a junction: resolve it and then use NamingManager.getContinuationContext() to
  * get the target context in which to operate on the remainder of the name (n.getSuffix(1)).
  */
 protected Context getContinuationContext(Name n) throws NamingException {
   Object obj = lookup(n.get(0));
   CannotProceedException cpe = new CannotProceedException();
   cpe.setResolvedObj(obj);
   cpe.setEnvironment(myEnv);
   return NamingManager.getContinuationContext(cpe);
 }
示例#4
0
  /**
   * Constructs a JNDI Binding object from the COS Naming binding object.
   *
   * @exception NameNotFound No objects under the name.
   * @exception CannotProceed Unable to obtain a continuation context
   * @exception InvalidName Name not understood.
   * @exception NamingException One of the above.
   */
  private javax.naming.Binding mapBinding(org.omg.CosNaming.Binding bndg) throws NamingException {
    java.lang.Object obj = _ctx.callResolve(bndg.binding_name);

    Name cname = CNNameParser.cosNameToName(bndg.binding_name);

    try {
      obj = NamingManager.getObjectInstance(obj, cname, _ctx, _env);
    } catch (NamingException e) {
      throw e;
    } catch (Exception e) {
      NamingException ne = new NamingException("problem generating object using object factory");
      ne.setRootCause(e);
      throw ne;
    }

    // Use cname.toString() instead of bindingName because the name
    // in the binding should be a composite name
    String cnameStr = cname.toString();
    javax.naming.Binding jbndg = new javax.naming.Binding(cnameStr, obj);

    NameComponent[] comps = _ctx.makeFullName(bndg.binding_name);
    String fullName = CNNameParser.cosNameToInsString(comps);
    jbndg.setNameInNamespace(fullName);
    return jbndg;
  }
  protected void setUp() throws Exception {
    super.setUp();

    if (!NamingManager.hasInitialContextFactoryBuilder()) {
      NamingManager.setInitialContextFactoryBuilder(
          new InitialContextFactoryBuilder() {
            public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment)
                throws NamingException {
              return new InitialContextFactory() {
                public Context getInitialContext(Hashtable<?, ?> environment)
                    throws NamingException {
                  return THREAD_INITIAL_CONTEXT.get();
                }
              };
            }
          });
    }
  }
  /**
   * Retrieves the named object.
   *
   * @param name the name of the object to look up
   * @param resolveLinks If true, the links will be resolved
   * @return the object bound to name
   * @exception NamingException if a naming exception is encountered
   */
  protected Object lookup(Name name, boolean resolveLinks) throws NamingException {

    // Removing empty parts
    while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1);
    if (name.isEmpty()) {
      // If name is empty, a newly allocated naming context is returned
      return new NamingContext(env, this.name, bindings);
    }

    NamingEntry entry = bindings.get(name.get(0));

    if (entry == null) {
      throw new NameNotFoundException(
          sm.getString("namingContext.nameNotBound", name, name.get(0)));
    }

    if (name.size() > 1) {
      // If the size of the name is greater that 1, then we go through a
      // number of subcontexts.
      if (entry.type != NamingEntry.CONTEXT) {
        throw new NamingException(sm.getString("namingContext.contextExpected"));
      }
      return ((Context) entry.value).lookup(name.getSuffix(1));
    } else {
      if ((resolveLinks) && (entry.type == NamingEntry.LINK_REF)) {
        String link = ((LinkRef) entry.value).getLinkName();
        if (link.startsWith(".")) {
          // Link relative to this context
          return lookup(link.substring(1));
        } else {
          return (new InitialContext(env)).lookup(link);
        }
      } else if (entry.type == NamingEntry.REFERENCE) {
        try {
          // TODO ÐèѧϰNamingManager
          Object obj = NamingManager.getObjectInstance(entry.value, name, this, env);
          if (entry.value instanceof ResourceRef) {
            boolean singleton =
                Boolean.parseBoolean(
                    (String) ((ResourceRef) entry.value).get("singleton").getContent());
            if (singleton) {
              entry.type = NamingEntry.ENTRY;
              entry.value = obj;
            }
          }
          return obj;
        } catch (NamingException e) {
          throw e;
        } catch (Exception e) {
          log.warn(sm.getString("namingContext.failResolvingReference"), e);
          throw new NamingException(e.getMessage());
        }
      } else {
        return entry.value;
      }
    }
  }
示例#7
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");
  }
  @Test
  public void testQueryFallback() throws Exception {
    PooledWithJndiDataSourceService service = new PooledWithJndiDataSourceService();
    NamingManager.setInitialContextFactoryBuilder(new DebugJndiContextFactoryBuilder());

    DataSource ds = service.queryFallback("SampleData");

    assertThat(ds, is(notNullValue()));
    assertThat(ds.getConnection(), is(notNullValue()));
  }
  public static Context getURLContext(String name, Hashtable<Object, Object> env)
      throws NamingException {
    String scheme = getURLScheme(name);
    if (scheme != null) {
      Context ctx = NamingManager.getURLContext(scheme, env);
      if (ctx != null) {
        return ctx;
      }
    }

    return null;
  }
  /**
   * Binds a name to an object. All intermediate contexts and the target context (that named by all
   * but terminal atomic component of the name) must already exist.
   *
   * @param name the name to bind; may not be empty
   * @param obj the object to bind; possibly null
   * @param rebind if true, then perform a rebind (ie, overwrite)
   * @exception NameAlreadyBoundException if name is already bound
   * @exception javax.naming.directory.InvalidAttributesException if object did not supply all
   *     mandatory attributes
   * @exception NamingException if a naming exception is encountered
   */
  protected void bind(Name name, Object obj, boolean rebind) throws NamingException {

    if (!checkWritable()) {
      return;
    }

    while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1);
    if (name.isEmpty()) throw new NamingException(sm.getString("namingContext.invalidName"));

    NamingEntry entry = bindings.get(name.get(0));

    if (name.size() > 1) {
      if (entry == null) {
        throw new NameNotFoundException(
            sm.getString("namingContext.nameNotBound", name, name.get(0)));
      }
      if (entry.type == NamingEntry.CONTEXT) {
        if (rebind) {
          ((Context) entry.value).rebind(name.getSuffix(1), obj);
        } else {
          ((Context) entry.value).bind(name.getSuffix(1), obj);
        }
      } else {
        throw new NamingException(sm.getString("namingContext.contextExpected"));
      }
    } else {
      if ((!rebind) && (entry != null)) {
        throw new NameAlreadyBoundException(
            sm.getString("namingContext.alreadyBound", name.get(0)));
      } else {
        // Getting the type of the object and wrapping it within a new
        // NamingEntry
        Object toBind = NamingManager.getStateToBind(obj, name, this, env);
        if (toBind instanceof Context) {
          entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT);
        } else if (toBind instanceof LinkRef) {
          entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF);
        } else if (toBind instanceof Reference) {
          entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
        } else if (toBind instanceof Referenceable) {
          toBind = ((Referenceable) toBind).getReference();
          entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
        } else {
          entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY);
        }
        bindings.put(name.get(0), entry);
      }
    }
  }
示例#11
0
 private Object dereference(Object value, Name tail, AbstractModel model) throws NamingException {
   try {
     if (value instanceof ObjectProxy) return ((ObjectProxy) value).createObject(_env);
     else if (value instanceof Reference) {
       Context context = create(model, _env);
       return NamingManager.getObjectInstance(value, null, context, _env);
     } else return value;
   } catch (RuntimeException e) {
     throw e;
   } catch (NamingException e) {
     throw e;
   } catch (Exception e) {
     throw new NamingExceptionWrapper(e);
   }
 }
  public static Context getURLContext(Name name, Hashtable<Object, Object> env)
      throws NamingException {
    if (name.size() > 0) {
      String first = name.get(0);
      String scheme = getURLScheme(first);
      if (scheme != null) {
        Context ctx = NamingManager.getURLContext(scheme, env);
        if (ctx != null) {
          return ctx;
        }
      }
    }

    return null;
  }
示例#13
0
  /**
   * List all Bindings present at Context named by Name
   *
   * @param name a <code>Name</code> value
   * @return a <code>NamingEnumeration</code> value
   * @exception NamingException if an error occurs
   */
  public NamingEnumeration listBindings(Name name) throws NamingException {
    Name cname = toCanonicalName(name);

    if (cname == null) {
      return new BindingEnumeration(__empty.iterator());
    }

    if (cname.size() == 0) {
      return new BindingEnumeration(_bindings.values().iterator());
    }

    // multipart name
    String firstComponent = cname.get(0);
    Object ctx = null;

    // if a name has a leading "/" it is parsed as "" so ignore it by staying
    // at this level in the tree
    if (firstComponent.equals("")) ctx = this;
    else {
      // it is a non-empty name component
      Binding binding = getBinding(firstComponent);
      if (binding == null) throw new NameNotFoundException();

      ctx = binding.getObject();

      if (ctx instanceof Reference) {
        // deference the object
        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)) throw new NotContextException();

    return ((Context) ctx).listBindings(cname.getSuffix(1));
  }
示例#14
0
  /**
   * List all names bound at Context named by Name
   *
   * @param name a <code>Name</code> value
   * @return a <code>NamingEnumeration</code> value
   * @exception NamingException if an error occurs
   */
  public NamingEnumeration list(Name name) throws NamingException {
    if (__log.isDebugEnabled()) __log.debug("list() on Context=" + getName() + " for name=" + name);
    Name cname = toCanonicalName(name);

    if (cname == null) {
      return new NameEnumeration(__empty.iterator());
    }

    if (cname.size() == 0) {
      return new NameEnumeration(_bindings.values().iterator());
    }

    // multipart name
    String firstComponent = cname.get(0);
    Object ctx = null;

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

      ctx = binding.getObject();

      if (ctx instanceof Reference) {
        // deference the object
        if (__log.isDebugEnabled()) __log.debug("Dereferencing Reference for " + name.get(0));
        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)) throw new NotContextException();

    return ((Context) ctx).list(cname.getSuffix(1));
  }
示例#15
0
  /**
   * Lookup a binding by name
   *
   * @param name name of bound object
   * @exception NamingException if an error occurs
   */
  public Object lookup(Name name) throws NamingException {
    if (__log.isDebugEnabled()) __log.debug("Looking up name=\"" + name + "\"");
    Name cname = toCanonicalName(name);

    if ((cname == null) || (cname.size() == 0)) {
      __log.debug("Null or empty name, returning copy of this context");
      NamingContext ctx = new NamingContext(_env, _name, _parent, _parser);
      ctx._bindings = _bindings;
      return ctx;
    }

    if (cname.size() == 1) {
      Binding binding = getBinding(cname);
      if (binding == null) {
        NameNotFoundException nnfe = new NameNotFoundException();
        nnfe.setRemainingName(cname);
        throw nnfe;
      }

      Object o = binding.getObject();

      // handle links by looking up the link
      if (o instanceof LinkRef) {
        // if link name starts with ./ it is relative to current context
        String linkName = ((LinkRef) o).getLinkName();
        if (linkName.startsWith("./")) return this.lookup(linkName.substring(2));
        else {
          // link name is absolute
          InitialContext ictx = new InitialContext();
          return ictx.lookup(linkName);
        }
      } else if (o instanceof Reference) {
        // deference the object
        try {
          return NamingManager.getObjectInstance(o, cname, this, _env);
        } catch (NamingException e) {
          throw e;
        } catch (Exception e) {
          __log.warn("", e);
          throw new NamingException(e.getMessage());
        }
      } else return o;
    }

    // it is a multipart name, recurse to the first subcontext

    String firstComponent = cname.get(0);
    Object ctx = null;

    if (firstComponent.equals("")) ctx = this;
    else {

      Binding binding = getBinding(firstComponent);
      if (binding == null) {
        NameNotFoundException nnfe = new NameNotFoundException();
        nnfe.setRemainingName(cname);
        throw nnfe;
      }

      // as we have bound a reference to an object factory
      // for the component specific contexts
      // at "comp" we need to resolve the reference
      ctx = binding.getObject();

      if (ctx instanceof Reference) {
        // deference the object
        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)) throw new NotContextException();

    return ((Context) ctx).lookup(cname.getSuffix(1));
  }
示例#16
0
  public Object lookup(String name) throws NamingException {
    if (name.length() == 0) {
      return this;
    }
    Object result = treeBindings.get(name);
    if (result == null) {
      result = bindings.get(name);
    }
    if (result == null) {
      int pos = name.indexOf(':');
      if (pos > 0) {
        String scheme = name.substring(0, pos);
        Context ctx = NamingManager.getURLContext(scheme, environment);
        if (ctx == null) {
          throw new NamingException("scheme " + scheme + " not recognized");
        }
        return ctx.lookup(name);
      } else {
        // Split out the first name of the path
        // and look for it in the bindings map.
        CompositeName path = new CompositeName(name);

        if (path.size() == 0) {
          return this;
        } else {
          String first = path.get(0);
          Object value = bindings.get(first);
          if (value == null) {
            throw new NameNotFoundException(name);
          } else if (value instanceof Context && path.size() > 1) {
            Context subContext = (Context) value;
            value = subContext.lookup(path.getSuffix(1));
          }
          return value;
        }
      }
    }
    if (result instanceof Provider) {
      Provider provider = (Provider) result;
      result = provider.get();
    }
    if (result instanceof LinkRef) {
      LinkRef ref = (LinkRef) result;
      result = lookup(ref.getLinkName());
    }
    if (result instanceof Reference) {
      try {
        result = NamingManager.getObjectInstance(result, null, null, this.environment);
      } catch (NamingException e) {
        throw e;
      } catch (Exception e) {
        throw (NamingException) new NamingException("could not look up : " + name).initCause(e);
      }
    }
    if (result instanceof JndiContext) {
      String prefix = getNameInNamespace();
      if (prefix.length() > 0) {
        prefix = prefix + SEPARATOR;
      }
      result = new JndiContext((JndiContext) result, environment, prefix + name);
    }
    return result;
  }
 public void setUp() throws Exception {
   ClassicEngineBoot.getInstance().start();
   if (NamingManager.hasInitialContextFactoryBuilder() == false) {
     NamingManager.setInitialContextFactoryBuilder(new DebugJndiContextFactoryBuilder());
   }
 }
示例#18
0
  /**
   * Bind a name to an object
   *
   * @param name Name of the object
   * @param obj object to bind
   * @exception NamingException if an error occurs
   */
  public void bind(Name name, Object obj) throws NamingException {
    if (isLocked()) throw new NamingException("This context is immutable");

    Name cname = toCanonicalName(name);

    if (cname == null) throw new NamingException("Name is null");

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

    // if no subcontexts, just bind it
    if (cname.size() == 1) {
      // get the object to be bound
      Object objToBind = NamingManager.getStateToBind(obj, name, this, _env);
      // Check for Referenceable
      if (objToBind instanceof Referenceable) {
        objToBind = ((Referenceable) objToBind).getReference();
      }

      // anything else we should be able to bind directly
      addBinding(cname, objToBind);
    } else {
      if (__log.isDebugEnabled())
        __log.debug(
            "Checking for existing binding for name="
                + cname
                + " for first element of name="
                + cname.get(0));

      // walk down the subcontext hierarchy
      // need to ignore trailing empty "" name components

      String firstComponent = cname.get(0);
      Object ctx = null;

      if (firstComponent.equals("")) ctx = this;
      else {

        Binding binding = getBinding(firstComponent);
        if (binding == null) {
          if (_supportDeepBinding) {
            Name subname = _parser.parse(firstComponent);
            Context subctx =
                new NamingContext((Hashtable) _env.clone(), firstComponent, this, _parser);
            addBinding(subname, subctx);
            binding = getBinding(subname);
          } else {
            throw new NameNotFoundException(firstComponent + " is not bound");
          }
        }

        ctx = binding.getObject();

        if (ctx instanceof Reference) {
          // deference the object
          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) {
        ((Context) ctx).bind(cname.getSuffix(1), obj);
      } else
        throw new NotContextException("Object bound at " + firstComponent + " is not a Context");
    }
  }
示例#19
0
  /**
   * Lookup link bound to name
   *
   * @param name name of link binding
   * @return LinkRef or plain object bound at name
   * @exception NamingException if an error occurs
   */
  public Object lookupLink(Name name) throws NamingException {
    Name cname = toCanonicalName(name);

    if (cname == null) {
      NamingContext ctx = new NamingContext(_env, _name, _parent, _parser);
      ctx._bindings = _bindings;
      return ctx;
    }
    if (cname.size() == 0) throw new NamingException("Name is empty");

    if (cname.size() == 1) {
      Binding binding = getBinding(cname);
      if (binding == null) throw new NameNotFoundException();

      Object o = binding.getObject();

      // handle links by looking up the link
      if (o instanceof Reference) {
        // deference the object
        try {
          return NamingManager.getObjectInstance(o, cname.getPrefix(1), this, _env);
        } catch (NamingException e) {
          throw e;
        } catch (Exception e) {
          __log.warn("", e);
          throw new NamingException(e.getMessage());
        }
      } else {
        // object is either a LinkRef which we don't dereference
        // or a plain object in which case spec says we return it
        return o;
      }
    }

    // it is a multipart name, recurse to the first subcontext
    String firstComponent = cname.get(0);
    Object ctx = null;

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

      ctx = binding.getObject();

      if (ctx instanceof Reference) {
        // deference the object
        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)) throw new NotContextException();

    return ((Context) ctx).lookup(cname.getSuffix(1));
  }