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 * Default {@link ResourceClassDiscover} implementation. 029 * 030 * @param <T> The SPI type 031 */ 032 public abstract class ResourceClassDiscoverImpl<T> extends ResourceDiscoverImpl implements ResourceClassDiscover<T> { 033 034 /** 035 * Construct a new resource discoverer. 036 */ 037 public ResourceClassDiscoverImpl() { 038 super(); 039 } 040 041 /** 042 * Construct a new resource discoverer. 043 * 044 * @param classLoaders The class loaders holder 045 */ 046 public ResourceClassDiscoverImpl(ClassLoaders classLoaders) { 047 super(classLoaders); 048 } 049 050 /** 051 * {@inheritDoc} 052 */ 053 @Override 054 public ResourceNameIterator findResourceNames(String resourceName) { 055 return findResourceClasses(resourceName); 056 } 057 058 /** 059 * {@inheritDoc} 060 */ 061 @Override 062 public ResourceNameIterator findResourceNames(ResourceNameIterator resourceNames) { 063 return findResourceClasses(resourceNames); 064 } 065 066 /** 067 * {@inheritDoc} 068 */ 069 @Override 070 public ResourceIterator findResources(String resourceName) { 071 return findResourceClasses(resourceName); 072 } 073 074 /** 075 * {@inheritDoc} 076 */ 077 @Override 078 public ResourceIterator findResources(ResourceNameIterator resourceNames) { 079 return findResourceClasses(resourceNames); 080 } 081 082 /** 083 * Locate class resources that are bound to <code>className</code>. 084 * 085 * @param className The class name has to be located 086 * @return The located resources iterator 087 */ 088 public abstract ResourceClassIterator<T> findResourceClasses(String className); 089 090 /** 091 * Locate class resources that are bound to {@code resourceNames}. 092 * 093 * @param inputNames The resource name iterator 094 * @return a new {@link ResourceClassIterator} over the given resource name iterator 095 */ 096 public ResourceClassIterator<T> findResourceClasses(final ResourceNameIterator inputNames) { 097 return new ResourceClassIterator<T>() { 098 private ResourceClassIterator<T> classes = null; 099 private ResourceClass<T> resource = null; 100 101 public boolean hasNext() { 102 if (resource == null) { 103 resource = getNextResource(); 104 } 105 return resource != null; 106 } 107 108 @Override 109 public ResourceClass<T> nextResourceClass() { 110 ResourceClass<T> rsrc = resource; 111 resource = null; 112 return rsrc; 113 } 114 115 private ResourceClass<T> getNextResource() { 116 while (inputNames.hasNext() && 117 (classes == null || !classes.hasNext())) { 118 classes = 119 findResourceClasses(inputNames.nextResourceName()); 120 } 121 122 return (classes != null && classes.hasNext()) 123 ? classes.nextResourceClass() 124 : null; 125 } 126 }; 127 } 128 129 }