1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.discovery.resource;
18
19 import org.apache.commons.discovery.Resource;
20 import org.apache.commons.discovery.ResourceDiscover;
21 import org.apache.commons.discovery.ResourceIterator;
22 import org.apache.commons.discovery.ResourceNameIterator;
23 import org.apache.commons.discovery.resource.names.ResourceNameDiscoverImpl;
24
25
26
27
28 public abstract class ResourceDiscoverImpl extends ResourceNameDiscoverImpl implements ResourceDiscover {
29
30 private ClassLoaders classLoaders;
31
32
33
34
35 public ResourceDiscoverImpl() {
36 }
37
38
39
40
41
42
43 public ResourceDiscoverImpl(ClassLoaders classLoaders) {
44 setClassLoaders(classLoaders);
45 }
46
47
48
49
50
51
52 public void setClassLoaders(ClassLoaders loaders) {
53 classLoaders = loaders;
54 }
55
56
57
58
59
60
61
62
63
64 public void addClassLoader(ClassLoader loader) {
65 getClassLoaders().put(loader);
66 }
67
68
69
70
71
72
73 protected ClassLoaders getClassLoaders() {
74 if (classLoaders == null) {
75 classLoaders = ClassLoaders.getLibLoaders(this.getClass(), null, true);
76 }
77 return classLoaders;
78 }
79
80
81
82
83 @Override
84 public ResourceNameIterator findResourceNames(String resourceName) {
85 return findResources(resourceName);
86 }
87
88
89
90
91 @Override
92 public ResourceNameIterator findResourceNames(ResourceNameIterator resourceNames) {
93 return findResources(resourceNames);
94 }
95
96
97
98
99
100
101
102 public abstract ResourceIterator findResources(String resourceName);
103
104
105
106
107
108
109
110 public ResourceIterator findResources(final ResourceNameIterator inputNames) {
111 return new ResourceIterator() {
112
113 private ResourceIterator resources = null;
114
115 private Resource resource = null;
116
117 public boolean hasNext() {
118 if (resource == null) {
119 resource = getNextResource();
120 }
121 return resource != null;
122 }
123
124 @Override
125 public Resource nextResource() {
126 Resource rsrc = resource;
127 resource = null;
128 return rsrc;
129 }
130
131 private Resource getNextResource() {
132 while (inputNames.hasNext() &&
133 (resources == null || !resources.hasNext())) {
134 resources = findResources(inputNames.nextResourceName());
135 }
136
137 return (resources != null && resources.hasNext())
138 ? resources.nextResource()
139 : null;
140 }
141 };
142 }
143
144 }