https://issues.apache.org/bugzilla/show_bug.cgi?id=56771

            Bug ID: 56771
           Summary: Avoid throwing NameNotFoundException in
                    BaseDirContext#lookup()
           Product: Tomcat 7
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Catalina
          Assignee: dev@tomcat.apache.org
          Reporter: xs...@ebay.com

Created attachment 31848
  --> https://issues.apache.org/bugzilla/attachment.cgi?id=31848&action=edit
The issue

NameNotFoundException is thrown when the resource can't be found in this
DirContext.

In the method BaseDirContext, it also goes through all the alternate or backup
DirContexts to lookup the resource one by one until the resource is found.
A NameNotFoundException is thrown in the alternate DirContext when the given
resource name isn't in this DirContext.

  public final Object lookup(String name) throws NamingException {
        // First check for aliases
        if (!aliases.isEmpty()) {
            AliasResult result = findAlias(name);
            if (result.dirContext != null) {
                return result.dirContext.lookup(result.aliasName);
            }
        }

        // Next do a standard lookup
        Object obj = doLookup(name);

        if (obj != null)
            return obj;

        // Check the alternate locations
        for (DirContext altDirContext : altDirContexts) {
            try {
                obj = altDirContext.lookup("/META-INF/resources" + name);
                if (obj != null)
                    return obj;
            } catch ( NamingException ex) {
                // ignore
            }
        }

        // Really not found
        throw new NameNotFoundException(
                sm.getString("resources.notFound", name));
    }


It takes much CPU time.   It could be optimized by checking result is null or
not.

Here is the optimized code,
    public final Object lookup(String name) throws NamingException {
        // First check for aliases
        Object obj = doLookupWithoutNFE(name);
        if (obj != null) {
            return obj;
        }

        // Really not found
        throw new NameNotFoundException(
                sm.getString("resources.notFound", name));
    }

    protected Object doLookupWithoutNFE(String name) throws NamingException {
        if (!aliases.isEmpty()) {
            AliasResult result = findAlias(name);
            if (result.dirContext != null) {
                return result.dirContext.lookup(result.aliasName);
            }
        }

        // Next do a standard lookup
        Object obj = doLookup(name);

        if (obj != null)
            return obj;

        // Check the alternate locations
        String resourceName = "/META-INF/resources" + name;
        for (DirContext altDirContext : altDirContexts) {
            if (altDirContext instanceof BaseDirContext) {
                obj =
((BaseDirContext)altDirContext).doLookupWithoutNFE(resourceName);
            }
            else {
                try {
                    obj = altDirContext.lookup(resourceName);
                } catch ( NamingException ex) {
                    // ignore
                }
            }
            if (obj != null) {
                return obj;
            }
        }

        //Return null instead
        return null;
    }

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to