[英]how to implement java.lang.Classloader getResources()?
I have searched quite a lot, but didnt find any satisfactory answer. 我搜索了很多,但没有找到任何满意的答案。 so please forgive me if this is too obvious to you.
如果这对你来说太明显了,请原谅我。
I have written a classloader which is getting a callback for getResources(), and the resource is a folder name. 我编写了一个类加载器,它正在获取getResources()的回调,而资源是一个文件夹名称。 In the classloader i have the root path from which the resource is being asked for.
在类加载器中,我有从中获取资源的根路径。
now the getResources()
requires me to return an 'Enumeration'
of URL. 现在
getResources()
要求我返回URL的'Enumeration'
。
I am not getting any idea how to create an Enumeration
, how to implement its hasMoreElements()
and nextElement()
inside getResources()
. 我不知道如何创建
Enumeration
,如何在getResources()
实现其hasMoreElements()
和nextElement()
getResources()
。 I am not able to see the connection between the two. 我无法看到两者之间的联系。
Cant i simple search for the subpath from the root and return the absolute path of the resource as a URL? 我可以从根目录中简单地搜索子路径并将资源的绝对路径作为URL返回吗? why need to create this complicated
Enumeration
? 为什么需要创建这个复杂的
Enumeration
?
Thanks, VImal 谢谢,VImal
Enumeration
is a very old Java class that's been superceded by the newer Collections library. Enumeration
是一个非常古老的Java类,已被新的Collections库取代。 You can get one by creating a Collection
(of a single element) and then calling Collections.enumeration()
on it: 您可以通过创建一个
Collection
(单个元素)然后在其上调用Collections.enumeration()
来获取一个:
Enumeration<String> enumInstance = Collections.enumeration(Arrays.asList("Bla"));
Usually, the two most important methods, that you have to override in your own classloader, are public Class findClass(String name)
and public InputStream getResourceAsStream(String name)
. 通常,您必须在自己的类加载器中重写的两个最重要的方法是
public Class findClass(String name)
和public InputStream getResourceAsStream(String name)
。 Others can be delegated to the parent classloader in most cases. 在大多数情况下,可以将其他人委托给父类加载器。 That means that you must have a very special purpose in overriding
getResources()
. 这意味着你必须有一个非常特殊的目的来覆盖
getResources()
。 What is it? 它是什么?
In either case you can easily add put a logger inside, delegate method to parent classloader and see what is requested and returned by parent classloader. 在任何一种情况下,您都可以轻松添加记录器,将方法委托给父类加载器,并查看父类加载器请求和返回的内容。
UPDATE UPDATE
If, as per your comment, you want to load classes/resources from a path which is generated at runtime , you should do the following: when path is passed to classloader (say /home/user1/
), it should recursively list it's content storing files in two different collections - class files and other files. 根据您的注释,如果要从运行时生成的路径加载类/资源 ,则应执行以下操作:当path传递给类加载器(例如
/home/user1/
)时,它应递归列出其内容将文件存储在两个不同的集合中 - 类文件和其他文件。 The first collection will be used for classloading, the second - for resources. 第一个集合将用于类加载,第二个集合将用于资源。
For each file in resource collection you define it's resource path according to http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource%28java.lang.String%29 and get the URL from file: URL url = file.toURI().toURL();
对于资源集合中的每个文件,根据http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource%28java.lang.String%29定义它的资源路径并获取来自文件的URL:
URL url = file.toURI().toURL();
These path and URL you store as key->value somewhere in a map and use it in method in question. 这些路径和URL作为key-> value存储在地图中的某个位置,并在相关方法中使用它。
As for resource path, I believe it should be somewhat relative to the path, which was passed to your classloader: /home/user1/img/logo.gif
=> /img/logo.gif
至于资源路径,我认为它应该有点相对于路径,它被传递给你的类加载器:
/home/user1/img/logo.gif
=> /img/logo.gif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.