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.resource;
018
019 import org.apache.commons.discovery.Resource;
020 import org.apache.commons.discovery.ResourceDiscover;
021 import org.apache.commons.discovery.ResourceIterator;
022 import org.apache.commons.discovery.ResourceNameIterator;
023 import org.apache.commons.discovery.resource.names.ResourceNameDiscoverImpl;
024
025
026 /**
027 * Helper class for methods implementing the ResourceDiscover interface.
028 *
029 * @author Richard A. Sitze
030 */
031 public abstract class ResourceDiscoverImpl
032 extends ResourceNameDiscoverImpl
033 implements ResourceDiscover
034 {
035 private ClassLoaders classLoaders;
036
037
038 /**
039 * Construct a new resource discoverer
040 */
041 public ResourceDiscoverImpl() {
042 }
043
044 /**
045 * Construct a new resource discoverer
046 */
047 public ResourceDiscoverImpl(ClassLoaders classLoaders) {
048 setClassLoaders(classLoaders);
049 }
050
051 /**
052 * Specify set of class loaders to be used in searching.
053 */
054 public void setClassLoaders(ClassLoaders loaders) {
055 classLoaders = loaders;
056 }
057
058 /**
059 * Specify a new class loader to be used in searching.
060 * The order of loaders determines the order of the result.
061 * It is recommended to add the most specific loaders first.
062 */
063 public void addClassLoader(ClassLoader loader) {
064 getClassLoaders().put(loader);
065 }
066
067 protected ClassLoaders getClassLoaders() {
068 if (classLoaders == null) {
069 classLoaders = ClassLoaders.getLibLoaders(this.getClass(), null, true);
070 }
071 return classLoaders;
072 }
073
074 /**
075 * Locate names of resources that are bound to <code>resourceName</code>.
076 *
077 * @return ResourceNameIterator
078 */
079 public ResourceNameIterator findResourceNames(String resourceName) {
080 return findResources(resourceName);
081 }
082
083 /**
084 * Locate names of resources that are bound to <code>resourceNames</code>.
085 *
086 * @return ResourceNameIterator
087 */
088 public ResourceNameIterator findResourceNames(ResourceNameIterator resourceNames) {
089 return findResources(resourceNames);
090 }
091
092 /**
093 * Locate resources that are bound to <code>resourceName</code>.
094 *
095 * @return ResourceIterator
096 */
097 public abstract ResourceIterator findResources(String resourceName);
098
099 /**
100 * Locate resources that are bound to <code>resourceNames</code>.
101 *
102 * @return ResourceIterator
103 */
104 public ResourceIterator findResources(final ResourceNameIterator inputNames) {
105 return new ResourceIterator() {
106 private ResourceIterator resources = null;
107 private Resource resource = null;
108
109 public boolean hasNext() {
110 if (resource == null) {
111 resource = getNextResource();
112 }
113 return resource != null;
114 }
115
116 public Resource nextResource() {
117 Resource rsrc = resource;
118 resource = null;
119 return rsrc;
120 }
121
122 private Resource getNextResource() {
123 while (inputNames.hasNext() &&
124 (resources == null || !resources.hasNext())) {
125 resources = findResources(inputNames.nextResourceName());
126 }
127
128 return (resources != null && resources.hasNext())
129 ? resources.nextResource()
130 : null;
131 }
132 };
133 }
134 }