View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      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;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.net.URL;
22  import java.util.Vector;
23  
24  
25  /***
26   * 'Resource' located by discovery.
27   * Naming of methods becomes a real pain ('getClass()')
28   * so I've patterned this after ClassLoader...
29   * 
30   * I think it works well as it will give users a point-of-reference.
31   * 
32   * @author Craig R. McClanahan
33   * @author Costin Manolache
34   * @author Richard A. Sitze
35   */
36  public class Resource
37  {
38      protected final String      name;
39      protected final URL         resource;
40      protected final ClassLoader loader;
41  
42      public Resource(String resourceName, URL resource, ClassLoader loader) {
43          this.name = resourceName;
44          this.resource = resource;
45          this.loader = loader;
46      }
47  
48      /***
49       * Get the value of resourceName.
50       * @return value of resourceName.
51       */
52      public String getName() {
53          return name;
54      }
55  
56  //    /***
57  //     * Set the value of URL.
58  //     * @param v  Value to assign to URL.
59  //     */
60  //    public void setResource(URL  resource) {
61  //        this.resource = resource;
62  //    }
63      
64      /***
65       * Get the value of URL.
66       * @return value of URL.
67       */
68      public URL getResource() {
69          return resource;
70      }
71      
72      /***
73       * Get the value of URL.
74       * @return value of URL.
75       */
76      public InputStream getResourceAsStream() {
77          try {
78              return resource.openStream();
79          } catch (IOException e) {
80              return null;  // ignore
81          }
82      }
83      
84      /***
85       * Get the value of loader.
86       * @return value of loader.
87       */
88      public ClassLoader getClassLoader() {
89          return loader ;
90      }
91      
92  //    /***
93  //     * Set the value of loader.
94  //     * @param v  Value to assign to loader.
95  //     */
96  //    public void setClassLoader(ClassLoader  loader) {
97  //        this.loader = loader;
98  //    }
99      
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 }