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