| Summary: | I can not get resources in WEB-INF/classes | ||
|---|---|---|---|
| Product: | [RT] Jetty | Reporter: | weijx <weijx2003> |
| Component: | server | Assignee: | Greg Wilkins <gregw> |
| Status: | CLOSED WONTFIX | QA Contact: | |
| Severity: | enhancement | ||
| Priority: | P3 | CC: | janb, jetty-inbox |
| Version: | 7.2.2 | ||
| Target Milestone: | 7.2.x | ||
| Hardware: | PC | ||
| OS: | Windows 7 | ||
| Whiteboard: | |||
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 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?
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. |
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