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;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.net.URL;
022: import java.util.Vector;
023:
024: /**
025: * 'Resource' located by discovery.
026: * Naming of methods becomes a real pain ('getClass()')
027: * so I've patterned this after ClassLoader...
028: *
029: * I think it works well as it will give users a point-of-reference.
030: *
031: * @author Craig R. McClanahan
032: * @author Costin Manolache
033: * @author Richard A. Sitze
034: */
035: public class Resource {
036: protected final String name;
037: protected final URL resource;
038: protected final ClassLoader loader;
039:
040: public Resource(String resourceName, URL resource,
041: ClassLoader loader) {
042: this .name = resourceName;
043: this .resource = resource;
044: this .loader = loader;
045: }
046:
047: /**
048: * Get the value of resourceName.
049: * @return value of resourceName.
050: */
051: public String getName() {
052: return name;
053: }
054:
055: // /**
056: // * Set the value of URL.
057: // * @param v Value to assign to URL.
058: // */
059: // public void setResource(URL resource) {
060: // this.resource = resource;
061: // }
062:
063: /**
064: * Get the value of URL.
065: * @return value of URL.
066: */
067: public URL getResource() {
068: return resource;
069: }
070:
071: /**
072: * Get the value of URL.
073: * @return value of URL.
074: */
075: public InputStream getResourceAsStream() {
076: try {
077: return resource.openStream();
078: } catch (IOException e) {
079: return null; // ignore
080: }
081: }
082:
083: /**
084: * Get the value of loader.
085: * @return value of loader.
086: */
087: public ClassLoader getClassLoader() {
088: return loader;
089: }
090:
091: // /**
092: // * Set the value of loader.
093: // * @param v Value to assign to loader.
094: // */
095: // public void setClassLoader(ClassLoader loader) {
096: // this.loader = loader;
097: // }
098:
099: public String toString() {
100: return "Resource[" + getName() + ", " + getResource() + ", "
101: + getClassLoader() + "]";
102: }
103:
104: public static Resource[] toArray(ResourceIterator iterator) {
105: Vector vector = new Vector();
106: while (iterator.hasNext()) {
107: vector.add(iterator.nextResource());
108: }
109: Resource[] resources = new Resource[vector.size()];
110: vector.copyInto(resources);
111:
112: return resources;
113: }
114: }
|