1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.discovery.ant;
18
19 import java.util.LinkedList;
20 import java.util.List;
21
22 import org.apache.commons.discovery.ResourceNameIterator;
23 import org.apache.commons.discovery.jdk.JDKHooks;
24 import org.apache.commons.discovery.resource.DiscoverResources;
25
26
27
28
29
30
31
32 public class ServiceDiscoveryTask {
33
34 String name;
35
36 int debug=0;
37
38 String[] drivers = null;
39
40
41
42
43
44
45 public void setServiceName(String name) {
46 this.name=name;
47 }
48
49
50
51
52
53
54 public void setDebug(int i) {
55 this.debug=i;
56 }
57
58
59
60
61
62
63 public String[] getServiceInfo() {
64 return drivers;
65 }
66
67
68
69
70
71
72 public void execute() throws Exception {
73 System.out.printf("Discovering service '%s'...%n", name);
74
75 DiscoverResources disc = new DiscoverResources();
76 disc.addClassLoader( JDKHooks.getJDKHooks().getThreadContextClassLoader() );
77 disc.addClassLoader( this.getClass().getClassLoader() );
78
79 ResourceNameIterator iterator = disc.findResources(name);
80
81 List<String> resources = new LinkedList<String>();
82 while (iterator.hasNext()) {
83 String resourceInfo = iterator.nextResourceName();
84 resources.add(resourceInfo);
85 if (debug > 0) {
86 System.out.printf("Found '%s'%n", resourceInfo);
87 }
88 }
89
90 drivers = new String[resources.size()];
91 resources.toArray(drivers);
92 }
93
94 }