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    
018    package org.apache.commons.beanutils.converters;
019    
020    import java.io.InputStream;
021    import java.io.ByteArrayOutputStream;
022    import java.io.FileNotFoundException;
023    import java.io.IOException;
024    
025    /**
026     * A special classloader useful for testing j2ee-like scenarios.
027     * 
028     * <p>In some tests we want to be able to emulate "container" frameworks,
029     * where code runs in a hierarchy of classloaders, and certain classes may
030     * be loaded by various classloaders in the hierarchy.</p>
031     *
032     * <p>Normally this is done by having certain jars or class-file-directories
033     * in the classpath of some classloaders but not others. This is quite 
034     * difficult difficult to integrate with the build process for the unit
035     * tests though; compiling certain classes and having the output go into
036     * places that is not in the default classpath for the unit tests would be
037     * a major pain.</p>
038     *
039     * <p>So this class takes a sneaky alternative approach: it can grab any class
040     * already loaded by a parent classloader and <i>reload</i> that class via this
041     * classloader. The effect is exactly as if a class (or jar file) had been
042     * present in the classpath for a container's "shared" classloader <i>and</i>
043     * been present in the component-specific classpath too, without any messing
044     * about with the way unit test code is compiled or executed.
045     */
046    
047    public class ClassReloader extends ClassLoader {
048        public ClassReloader(ClassLoader parent) {
049            super(parent);
050        }
051        
052        /**
053         * Given a class already in the classpath of a parent classloader,
054         * reload that class via this classloader.
055         */
056        public Class reload(Class clazz) throws FileNotFoundException, IOException {
057            String className = clazz.getName();
058            String classFile = className.replace('.', '/') + ".class";
059            InputStream classStream = getParent().getResourceAsStream(classFile);
060            
061            if (classStream == null) {
062                throw new FileNotFoundException(classFile);
063            }
064            
065            byte[] buf = new byte[1024];
066            ByteArrayOutputStream baos = new ByteArrayOutputStream();
067            for(;;) {
068                int bytesRead = classStream.read(buf);
069                if (bytesRead == -1)
070                    break;
071                baos.write(buf, 0, bytesRead);
072            }
073            classStream.close();
074            
075            byte[] classData = baos.toByteArray();
076            
077            // now we have the raw class data, let's turn it into a class
078            Class newClass = defineClass(className, classData, 0, classData.length);
079            resolveClass(newClass);
080            return newClass;
081        }
082    }