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 java.net.URL; 020 import java.util.Vector; 021 022 import org.apache.commons.discovery.ResourceClass; 023 import org.apache.commons.discovery.ResourceClassDiscover; 024 import org.apache.commons.discovery.ResourceClassIterator; 025 import org.apache.commons.discovery.log.DiscoveryLogFactory; 026 import org.apache.commons.discovery.resource.ClassLoaders; 027 import org.apache.commons.logging.Log; 028 029 030 /** 031 * The findResources() method will check every loader. 032 * 033 * @author Richard A. Sitze 034 * @author Craig R. McClanahan 035 * @author Costin Manolache 036 * @author James Strachan 037 */ 038 public class DiscoverClasses 039 extends ResourceClassDiscoverImpl 040 implements ResourceClassDiscover 041 { 042 private static Log log = DiscoveryLogFactory.newLog(DiscoverClasses.class); 043 public static void setLog(Log _log) { 044 log = _log; 045 } 046 047 /** Construct a new resource discoverer 048 */ 049 public DiscoverClasses() { 050 super(); 051 } 052 053 /** Construct a new resource discoverer 054 */ 055 public DiscoverClasses(ClassLoaders classLoaders) { 056 super(classLoaders); 057 } 058 059 public ResourceClassIterator findResourceClasses(final String className) { 060 final String resourceName = className.replace('.','/') + ".class"; 061 062 if (log.isDebugEnabled()) 063 log.debug("find: className='" + className + "'"); 064 065 return new ResourceClassIterator() { 066 private Vector history = new Vector(); 067 private int idx = 0; 068 private ResourceClass resource = null; 069 070 public boolean hasNext() { 071 if (resource == null) { 072 resource = getNextClass(); 073 } 074 return resource != null; 075 } 076 077 public ResourceClass nextResourceClass() { 078 ResourceClass element = resource; 079 resource = null; 080 return element; 081 } 082 083 private ResourceClass getNextClass() { 084 while (idx < getClassLoaders().size()) { 085 ClassLoader loader = getClassLoaders().get(idx++); 086 URL url = loader.getResource(resourceName); 087 if (url != null) { 088 if (!history.contains(url)) { 089 history.addElement(url); 090 091 if (log.isDebugEnabled()) 092 log.debug("getNextClass: next URL='" + url + "'"); 093 094 return new ResourceClass(className, url, loader); 095 } 096 if (log.isDebugEnabled()) 097 log.debug("getNextClass: duplicate URL='" + url + "'"); 098 } else { 099 if (log.isDebugEnabled()) 100 log.debug("getNextClass: loader " + loader + ": '" + resourceName + "' not found"); 101 } 102 } 103 return null; 104 } 105 }; 106 } 107 }