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