1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.discovery;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.URL;
22 import java.util.Vector;
23
24
25 /***
26 * 'Resource' located by discovery.
27 * Naming of methods becomes a real pain ('getClass()')
28 * so I've patterned this after ClassLoader...
29 *
30 * I think it works well as it will give users a point-of-reference.
31 *
32 * @author Craig R. McClanahan
33 * @author Costin Manolache
34 * @author Richard A. Sitze
35 */
36 public class Resource
37 {
38 protected final String name;
39 protected final URL resource;
40 protected final ClassLoader loader;
41
42 public Resource(String resourceName, URL resource, ClassLoader loader) {
43 this.name = resourceName;
44 this.resource = resource;
45 this.loader = loader;
46 }
47
48 /***
49 * Get the value of resourceName.
50 * @return value of resourceName.
51 */
52 public String getName() {
53 return name;
54 }
55
56 /***
57 // * Set the value of URL.
58 // * @param v Value to assign to URL.
59 // */
60
61
62
63
64 /***
65 * Get the value of URL.
66 * @return value of URL.
67 */
68 public URL getResource() {
69 return resource;
70 }
71
72 /***
73 * Get the value of URL.
74 * @return value of URL.
75 */
76 public InputStream getResourceAsStream() {
77 try {
78 return resource.openStream();
79 } catch (IOException e) {
80 return null;
81 }
82 }
83
84 /***
85 * Get the value of loader.
86 * @return value of loader.
87 */
88 public ClassLoader getClassLoader() {
89 return loader ;
90 }
91
92 /***
93 // * Set the value of loader.
94 // * @param v Value to assign to loader.
95 // */
96
97
98
99
100 public String toString() {
101 return "Resource[" + getName() + ", " + getResource() + ", " + getClassLoader() + "]";
102 }
103
104 public static Resource[] toArray(ResourceIterator iterator) {
105 Vector vector = new Vector();
106 while (iterator.hasNext()) {
107 vector.add(iterator.nextResource());
108 }
109 Resource[] resources = new Resource[vector.size()];
110 vector.copyInto(resources);
111
112 return resources;
113 }
114 }