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 */ 017package org.apache.xbean.finder.archive; 018 019import java.io.BufferedInputStream; 020import java.io.IOException; 021import java.io.InputStream; 022import java.net.URL; 023import java.util.Arrays; 024import java.util.Iterator; 025import java.util.LinkedHashMap; 026import java.util.LinkedHashSet; 027import java.util.Map; 028import java.util.Set; 029 030/** 031 * @version $Rev$ $Date$ 032 */ 033public class ClassesArchive implements Archive { 034 035 private final Set<ClassLoader> loaders = new LinkedHashSet<ClassLoader>(); 036 private final Map<String, Class<?>> classes = new LinkedHashMap<String, Class<?>>(); 037 038 public ClassesArchive(Class<?>... classes) { 039 this(Arrays.asList(classes)); 040 } 041 042 public ClassesArchive(Iterable<Class<?>> classes) { 043 assert classes != null; 044 045 for (Class<?> clazz : classes) { 046 if (clazz == null) continue; 047 if (clazz.getClassLoader() == null) continue; 048 this.classes.put(clazz.getName(), clazz); 049 loaders.add(clazz.getClassLoader()); 050 } 051 } 052 053 public Iterator<Entry> iterator() { 054 return new ArchiveIterator(this, classes.keySet().iterator()); 055 } 056 057 public InputStream getBytecode(String className) throws IOException, ClassNotFoundException { 058 assert className != null; 059 060 int pos = className.indexOf("<"); 061 if (pos > -1) { 062 className = className.substring(0, pos); 063 } 064 pos = className.indexOf(">"); 065 if (pos > -1) { 066 className = className.substring(0, pos); 067 } 068 if (!className.endsWith(".class")) { 069 className = className.replace('.', '/') + ".class"; 070 } 071 // TODO: support META-INF/versions/ 072 for (ClassLoader loader : loaders) { 073 URL resource = loader.getResource(className); 074 if (resource != null) return new BufferedInputStream(resource.openStream()); 075 } 076 077 throw new ClassNotFoundException(className); 078 } 079 080 public Class<?> loadClass(String className) throws ClassNotFoundException { 081 Class<?> clazz = classes.get(className); 082 if (clazz != null) return clazz; 083 084 for (ClassLoader loader : loaders) { 085 try { 086 return loader.loadClass(className); 087 } catch (ClassNotFoundException e) { 088 } 089 } 090 091 throw new ClassNotFoundException(className); 092 } 093 094}