001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.discovery.ant;
018    
019    import java.util.Vector;
020    
021    import org.apache.commons.discovery.ResourceNameIterator;
022    import org.apache.commons.discovery.jdk.JDKHooks;
023    import org.apache.commons.discovery.resource.DiscoverResources;
024    
025    
026    /**
027     * Small ant task that will use discovery to locate a particular impl.
028     * and display all values.
029     *
030     * You can execute this and save it with an id, then other classes can use it.
031     *
032     * @author Costin Manolache
033     */
034    public class ServiceDiscoveryTask
035    {
036        String name;
037        int debug=0;
038        String[] drivers = null;
039            
040        public void setServiceName(String name ) {
041            this.name=name;
042        }
043    
044        public void setDebug(int i) {
045            this.debug=i;
046        }
047    
048        public String[] getServiceInfo() {
049            return drivers;
050        }
051    
052        public void execute() throws Exception {
053            System.out.println("XXX ");
054            
055            DiscoverResources disc = new DiscoverResources();
056            disc.addClassLoader( JDKHooks.getJDKHooks().getThreadContextClassLoader() );
057            disc.addClassLoader( this.getClass().getClassLoader() );
058            
059            ResourceNameIterator iterator = disc.findResources(name);
060    
061            Vector vector = new Vector();
062            while (iterator.hasNext()) {
063                String resourceInfo = iterator.nextResourceName();
064                vector.add(resourceInfo);
065                if( debug > 0 ) {
066                    System.out.println("Found " + resourceInfo);
067                }
068            }
069            
070            drivers = new String[vector.size()];
071            vector.copyInto(drivers);
072        }
073            
074    }