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.LinkedList;
23  import java.util.List;
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  public class Resource {
33  
34      protected final String      name;
35  
36      protected final URL         resource;
37  
38      protected final ClassLoader loader;
39  
40      /**
41       * Create a new {@link Resource} instance.
42       *
43       * @param resourceName The resource name has to be located
44       * @param resource The resource URL has to be located
45       * @param loader The class loader used to locate the given resource
46       */
47      public Resource(String resourceName, URL resource, ClassLoader loader) {
48          this.name = resourceName;
49          this.resource = resource;
50          this.loader = loader;
51      }
52  
53      /**
54       * Get the value of resourceName.
55       * @return value of resourceName.
56       */
57      public String getName() {
58          return name;
59      }
60  
61      /**
62       * Get the value of URL.
63       * @return value of URL.
64       */
65      public URL getResource() {
66          return resource;
67      }
68  
69      /**
70       * Get the value of URL.
71       * @return value of URL.
72       */
73      public InputStream getResourceAsStream() {
74          try {
75              return resource.openStream();
76          } catch (IOException e) {
77              return null;  // ignore
78          }
79      }
80  
81      /**
82       * Get the value of loader.
83       * @return value of loader.
84       */
85      public ClassLoader getClassLoader() {
86          return loader ;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public String toString() {
94          return "Resource[" + getName() +  ", " + getResource() + ", " + getClassLoader() + "]";
95      }
96  
97      /**
98       * Returns an array containing all of the elements in this {@link ResourceIterator} in proper sequence.
99       *
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 }