Bug 56771 - Avoid throwing NameNotFoundException in BaseDirContext#lookup()
Summary: Avoid throwing NameNotFoundException in BaseDirContext#lookup()
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 7
Classification: Unclassified
Component: Catalina (show other bugs)
Version: trunk
Hardware: All All
: P2 normal (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-07-25 07:40 UTC by Sheldon Shao
Modified: 2014-07-29 04:53 UTC (History)
0 users



Attachments
The issue (278.96 KB, image/png)
2014-07-25 07:40 UTC, Sheldon Shao
Details
Patch for BaseDirContext (4.90 KB, patch)
2014-07-25 07:42 UTC, Sheldon Shao
Details | Diff
JProfiler snapshot after optimized (190.54 KB, image/png)
2014-07-25 07:45 UTC, Sheldon Shao
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sheldon Shao 2014-07-25 07:40:20 UTC
Created attachment 31848 [details]
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;
    }
Comment 1 Sheldon Shao 2014-07-25 07:42:05 UTC
Created attachment 31849 [details]
Patch for BaseDirContext

Patch for BaseDirContext
Comment 2 Sheldon Shao 2014-07-25 07:45:36 UTC
Created attachment 31850 [details]
JProfiler snapshot after optimized
Comment 3 Violeta Georgieva 2014-07-29 04:53:09 UTC
Hi,

Thanks for the report and the patch.
This has been fixed in 7.0.x for 7.0.56 onwards.

Regards,
Violeta