private Object resolveLink(LinkRef result) throws NamingException {
   final Object linkResult;
   try {
     final LinkRef linkRef = (LinkRef) result;
     final String referenceName = linkRef.getLinkName();
     if (referenceName.startsWith("./")) {
       linkResult = lookup(referenceName.substring(2));
     } else {
       linkResult = new InitialContext().lookup(referenceName);
     }
   } catch (Throwable t) {
     throw namingException("failed to deref link", t);
   }
   return linkResult;
 }
Esempio n. 2
0
  /**
   * Recursively display the naming context information into the buffer.
   *
   * @param ctx
   * @param indent
   * @param buffer
   * @param verbose
   */
  public static void list(Context ctx, String indent, StringBuffer buffer, boolean verbose) {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    try {
      NamingEnumeration ne = ctx.list("");
      while (ne.hasMore()) {
        NameClassPair pair = (NameClassPair) ne.next();

        String name = pair.getName();
        String className = pair.getClassName();
        boolean recursive = false;
        boolean isLinkRef = false;
        boolean isProxy = false;
        Class c = null;
        try {
          c = loader.loadClass(className);

          if (Context.class.isAssignableFrom(c)) recursive = true;
          if (LinkRef.class.isAssignableFrom(c)) isLinkRef = true;

          isProxy = Proxy.isProxyClass(c);
        } catch (ClassNotFoundException cnfe) {
          // If this is a $Proxy* class its a proxy
          if (className.startsWith("$Proxy")) {
            isProxy = true;
            // We have to get the class from the binding
            try {
              Object p = ctx.lookup(name);
              c = p.getClass();
            } catch (NamingException e) {
              Throwable t = e.getRootCause();
              if (t instanceof ClassNotFoundException) {
                // Get the class name from the exception msg
                String msg = t.getMessage();
                if (msg != null) {
                  // Reset the class name to the CNFE class
                  className = msg;
                }
              }
            }
          }
        }

        buffer.append(indent + " +- " + name);

        // Display reference targets
        if (isLinkRef) {
          // Get the
          try {
            Object obj = ctx.lookupLink(name);

            LinkRef link = (LinkRef) obj;
            buffer.append("[link -> ");
            buffer.append(link.getLinkName());
            buffer.append(']');
          } catch (Throwable t) {
            buffer.append("invalid]");
          }
        }

        // Display proxy interfaces
        if (isProxy) {
          buffer.append(" (proxy: " + pair.getClassName());
          if (c != null) {
            Class[] ifaces = c.getInterfaces();
            buffer.append(" implements ");
            for (int i = 0; i < ifaces.length; i++) {
              buffer.append(ifaces[i]);
              buffer.append(',');
            }
            buffer.setCharAt(buffer.length() - 1, ')');
          } else {
            buffer.append(" implements " + className + ")");
          }
        } else if (verbose) {
          buffer.append(" (class: " + pair.getClassName() + ")");
        }

        buffer.append('\n');
        if (recursive) {
          try {
            Object value = ctx.lookup(name);
            if (value instanceof Context) {
              Context subctx = (Context) value;
              list(subctx, indent + " |  ", buffer, verbose);
            } else {
              buffer.append(indent + " |   NonContext: " + value);
              buffer.append('\n');
            }
          } catch (Throwable t) {
            buffer.append("Failed to lookup: " + name + ", errmsg=" + t.getMessage());
            buffer.append('\n');
          }
        }
      }
      ne.close();
    } catch (NamingException ne) {
      buffer.append("error while listing context " + ctx.toString() + ": " + ne.toString(true));
      formatException(buffer, ne);
    }
  }
Esempio n. 3
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;
  }