01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.commons.discovery.ant;
18:
19: import java.util.Vector;
20:
21: import org.apache.commons.discovery.ResourceNameIterator;
22: import org.apache.commons.discovery.jdk.JDKHooks;
23: import org.apache.commons.discovery.resource.DiscoverResources;
24:
25: /**
26: * Small ant task that will use discovery to locate a particular impl.
27: * and display all values.
28: *
29: * You can execute this and save it with an id, then other classes can use it.
30: *
31: * @author Costin Manolache
32: */
33: public class ServiceDiscoveryTask {
34: String name;
35: int debug = 0;
36: String[] drivers = null;
37:
38: public void setServiceName(String name) {
39: this .name = name;
40: }
41:
42: public void setDebug(int i) {
43: this .debug = i;
44: }
45:
46: public String[] getServiceInfo() {
47: return drivers;
48: }
49:
50: public void execute() throws Exception {
51: System.out.println("XXX ");
52:
53: DiscoverResources disc = new DiscoverResources();
54: disc.addClassLoader(JDKHooks.getJDKHooks()
55: .getThreadContextClassLoader());
56: disc.addClassLoader(this .getClass().getClassLoader());
57:
58: ResourceNameIterator iterator = disc.findResources(name);
59:
60: Vector vector = new Vector();
61: while (iterator.hasNext()) {
62: String resourceInfo = iterator.nextResourceName();
63: vector.add(resourceInfo);
64: if (debug > 0) {
65: System.out.println("Found " + resourceInfo);
66: }
67: }
68:
69: drivers = new String[vector.size()];
70: vector.copyInto(drivers);
71: }
72:
73: }
|