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.classes;
018:
019: import org.apache.commons.discovery.ResourceClass;
020: import org.apache.commons.discovery.ResourceClassDiscover;
021: import org.apache.commons.discovery.ResourceClassIterator;
022: import org.apache.commons.discovery.ResourceIterator;
023: import org.apache.commons.discovery.ResourceNameIterator;
024: import org.apache.commons.discovery.resource.ClassLoaders;
025: import org.apache.commons.discovery.resource.ResourceDiscoverImpl;
026:
027: /**
028: * @author Richard A. Sitze
029: */
030: public abstract class ResourceClassDiscoverImpl extends
031: ResourceDiscoverImpl implements ResourceClassDiscover {
032: /**
033: * Construct a new resource discoverer
034: */
035: public ResourceClassDiscoverImpl() {
036: super ();
037: }
038:
039: /**
040: * Construct a new resource discoverer
041: */
042: public ResourceClassDiscoverImpl(ClassLoaders classLoaders) {
043: super (classLoaders);
044: }
045:
046: /**
047: * Locate names of resources that are bound to <code>resourceName</code>.
048: *
049: * @return ResourceNameIterator
050: */
051: public ResourceNameIterator findResourceNames(String resourceName) {
052: return findResourceClasses(resourceName);
053: }
054:
055: /**
056: * Locate names of resources that are bound to <code>resourceNames</code>.
057: *
058: * @return ResourceNameIterator
059: */
060: public ResourceNameIterator findResourceNames(
061: ResourceNameIterator resourceNames) {
062: return findResourceClasses(resourceNames);
063: }
064:
065: /**
066: * Locate resources that are bound to <code>resourceName</code>.
067: *
068: * @return ResourceIterator
069: */
070: public ResourceIterator findResources(String resourceName) {
071: return findResourceClasses(resourceName);
072: }
073:
074: /**
075: * Locate resources that are bound to <code>resourceNames</code>.
076: *
077: * @return ResourceIterator
078: */
079: public ResourceIterator findResources(
080: ResourceNameIterator resourceNames) {
081: return findResourceClasses(resourceNames);
082: }
083:
084: /**
085: * Locate class resources that are bound to <code>className</code>.
086: *
087: * @return ResourceClassIterator
088: */
089: public abstract ResourceClassIterator findResourceClasses(
090: String className);
091:
092: /**
093: * Locate class resources that are bound to <code>resourceNames</code>.
094: *
095: * @return ResourceIterator
096: */
097: public ResourceClassIterator findResourceClasses(
098: final ResourceNameIterator inputNames) {
099: return new ResourceClassIterator() {
100: private ResourceClassIterator classes = null;
101: private ResourceClass resource = null;
102:
103: public boolean hasNext() {
104: if (resource == null) {
105: resource = getNextResource();
106: }
107: return resource != null;
108: }
109:
110: public ResourceClass nextResourceClass() {
111: ResourceClass rsrc = resource;
112: resource = null;
113: return rsrc;
114: }
115:
116: private ResourceClass getNextResource() {
117: while (inputNames.hasNext()
118: && (classes == null || !classes.hasNext())) {
119: classes = findResourceClasses(inputNames
120: .nextResourceName());
121: }
122:
123: return (classes != null && classes.hasNext()) ? classes
124: .nextResourceClass() : null;
125: }
126: };
127: }
128: }
|