Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 349005 - I can not get resources in WEB-INF/classes
Summary: I can not get resources in WEB-INF/classes
Status: CLOSED WONTFIX
Alias: None
Product: Jetty
Classification: RT
Component: server (show other bugs)
Version: 7.2.2   Edit
Hardware: PC Windows 7
: P3 enhancement (vote)
Target Milestone: 7.2.x   Edit
Assignee: Greg Wilkins CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-10 05:52 EDT by weijx CLA
Modified: 2011-07-19 17:24 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description weijx CLA 2011-06-10 05:52:49 EDT
Build Identifier: 

I have a file in WEB-INF/classes called config.properties, so I write a method to load the file. cls.getResources("/config.properties") always return a empty Enumeration. I must user cls.getResources("config.properties") to get the config.properties file.

I test the method in Tomcat5 and WebSphere5, cls.getResources( "/config.properties" ) can return the config.properties file.

the test method : 

	public static void testURL() throws Exception {
		ClassLoader cls = TestClass.class.getClassLoader();
		
		System.out.println("---TEST1");
		Enumeration<URL> iter = cls.getResources("/config.properties");
		if(iter.hasMoreElements()){
			System.out.println(iter.nextElement());
		}else{
			System.out.println("not exist: /config.properties");
		}
		
		System.out.println("---TEST2");
		Enumeration<URL> iter1 = cls.getResources("config.properties");
		if(iter1.hasMoreElements()){
			System.out.println(iter1.nextElement());
		}else{
			System.out.println("not exist: config.properties");
		}
		
		System.out.println("---TEST3");
		URL url = cls.getResource("/config.properties");
		System.out.println(url);
		
		System.out.println("---TEST4");
		URL url1 = cls.getResource("config.properties");
		System.out.println(url1);
	}

Reproducible: Always
Comment 1 Greg Wilkins CLA 2011-06-22 01:36:01 EDT
Jetty is just using a URLClassLoader and the getResources method is implemented by it.  So I can't say why it does not handle a leading /, but it appears standard JVM behaviour.

Can you find any references that says a classloader should ignore leading /

cheers
Comment 2 weijx CLA 2011-06-24 02:07:15 EDT
It is my mistake.The correct usage is :

classLoader.getResources("config.properties");
classLoader.getResource("config.properties");

but statement below can return the config.properties resource

classLoader.getResource("/config.properties")

I look jetty 's WebAppClassLoader.getResource method,and find code below:

    public URL getResource(String name)
    {
        URL url= null;
        boolean tried_parent= false;
        boolean system_class=_context.isSystemClass(name);
        boolean server_class=_context.isServerClass(name);
        
        if (system_class && server_class)
            return null;
        
        if (_parent!=null &&(_context.isParentLoaderPriority() || system_class ) && !server_class)
        {
            tried_parent= true;
            
            if (_parent!=null)
                url= _parent.getResource(name);
        }

        if (url == null)
        {
            url= this.findResource(name);

            if (url == null && name.startsWith("/"))
            {
                if (Log.isDebugEnabled())
                    Log.debug("HACK leading / off " + name);
                url= this.findResource(name.substring(1));
            }
        }

        if (url == null && !tried_parent && !server_class )
        {
            if (_parent!=null)
                url= _parent.getResource(name);
        }

        if (url != null)
            if (Log.isDebugEnabled())
                Log.debug("getResource("+name+")=" + url);

        return url;
    }

It deal the "/" as HACK, why not deal "/" in getResources mehtod?
Comment 3 Jesse McConnell CLA 2011-07-19 17:24:32 EDT
I am closing this and marking it wont fix.  I waffled back and forth a bit on this and even coded in the hack into getResources but in hindsight we probably ought not have put the hack in the first method so it defies my reason to further propagate, it encourages bad decisions.  Class and ClassLoader have slightly different conventions and its probably not a good idea to make it hazier then it already is.