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 */
017package org.apache.commons.discovery;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.net.URL;
022import java.util.LinkedList;
023import java.util.List;
024
025/**
026 * 'Resource' located by discovery.
027 * Naming of methods becomes a real pain ('getClass()')
028 * so I've patterned this after ClassLoader...
029 *
030 * I think it works well as it will give users a point-of-reference.
031 */
032public class Resource {
033
034    protected final String      name;
035
036    protected final URL         resource;
037
038    protected final ClassLoader loader;
039
040    /**
041     * Create a new {@link Resource} instance.
042     *
043     * @param resourceName The resource name has to be located
044     * @param resource The resource URL has to be located
045     * @param loader The class loader used to locate the given resource
046     */
047    public Resource(String resourceName, URL resource, ClassLoader loader) {
048        this.name = resourceName;
049        this.resource = resource;
050        this.loader = loader;
051    }
052
053    /**
054     * Get the value of resourceName.
055     * @return value of resourceName.
056     */
057    public String getName() {
058        return name;
059    }
060
061    /**
062     * Get the value of URL.
063     * @return value of URL.
064     */
065    public URL getResource() {
066        return resource;
067    }
068
069    /**
070     * Get the value of URL.
071     * @return value of URL.
072     */
073    public InputStream getResourceAsStream() {
074        try {
075            return resource.openStream();
076        } catch (IOException e) {
077            return null;  // ignore
078        }
079    }
080
081    /**
082     * Get the value of loader.
083     * @return value of loader.
084     */
085    public ClassLoader getClassLoader() {
086        return loader ;
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    @Override
093    public String toString() {
094        return "Resource[" + getName() +  ", " + getResource() + ", " + getClassLoader() + "]";
095    }
096
097    /**
098     * Returns an array containing all of the elements in this {@link ResourceIterator} in proper sequence.
099     *
100     * @param iterator The {@link ResourceIterator} containing the 
101     * @return An array containing the elements of the given {@link ResourceIterator}
102     */
103    public static Resource[] toArray(ResourceIterator iterator) {
104        List<Resource> resourceList = new LinkedList<Resource>();
105        while (iterator.hasNext()) {
106            resourceList.add(iterator.nextResource());
107        }
108        Resource[] resources = new Resource[resourceList.size()];
109        resourceList.toArray(resources);
110
111        return resources;
112    }
113
114}