开发者

Implementing ClassLoader.getResource(String) without a protocol handler

开发者 https://www.devze.com 2023-03-29 22:07 出处:网络
My class loader resolves resources to a source that does not have a url handler eg it gets resources from a database. This of course poses a problem for ClassLoader.getResource(String) which returns a

My class loader resolves resources to a source that does not have a url handler eg it gets resources from a database. This of course poses a problem for ClassLoader.getResource(String) which returns a URL. For the URL.openConnection() etc to work i need a protocol handler, which poses an additionoal problem. How cna i map the URL to my data provider without installing a custom Url proto开发者_JS百科col handler which requires me to set a system property at startup. The problem with url protocol handlers is they are static and must be set at startup while my class loaders are dynamic and can come from many places.

Is there are way to solve this elegantly without a protocol handler ?


Well, if you're implementing ClassLoader.getResource(String) yourself can you not use the constructor for URL that takes in the protocol handler as one of its arguments?

public URL(String protocol,
           String host,
           int port,
           String file,
           URLStreamHandler handler)
    throws MalformedURLException
0

精彩评论

暂无评论...
验证码 换一张
取 消