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