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 /**
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 *
032 * @author Craig R. McClanahan
033 * @author Costin Manolache
034 * @author Richard A. Sitze
035 */
036 public class Resource
037 {
038 protected final String name;
039 protected final URL resource;
040 protected final ClassLoader loader;
041
042 public Resource(String resourceName, URL resource, ClassLoader loader) {
043 this.name = resourceName;
044 this.resource = resource;
045 this.loader = loader;
046 }
047
048 /**
049 * Get the value of resourceName.
050 * @return value of resourceName.
051 */
052 public String getName() {
053 return name;
054 }
055
056 // /**
057 // * Set the value of URL.
058 // * @param v Value to assign to URL.
059 // */
060 // public void setResource(URL resource) {
061 // this.resource = resource;
062 // }
063
064 /**
065 * Get the value of URL.
066 * @return value of URL.
067 */
068 public URL getResource() {
069 return resource;
070 }
071
072 /**
073 * Get the value of URL.
074 * @return value of URL.
075 */
076 public InputStream getResourceAsStream() {
077 try {
078 return resource.openStream();
079 } catch (IOException e) {
080 return null; // ignore
081 }
082 }
083
084 /**
085 * Get the value of loader.
086 * @return value of loader.
087 */
088 public ClassLoader getClassLoader() {
089 return loader ;
090 }
091
092 // /**
093 // * Set the value of loader.
094 // * @param v Value to assign to loader.
095 // */
096 // public void setClassLoader(ClassLoader loader) {
097 // this.loader = loader;
098 // }
099
100 public String toString() {
101 return "Resource[" + getName() + ", " + getResource() + ", " + 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 }