001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.hivemind.service.impl;
016    
017    import java.util.HashSet;
018    import java.util.Set;
019    
020    import javassist.CannotCompileException;
021    import javassist.ClassPath;
022    import javassist.ClassPool;
023    import javassist.CtClass;
024    import javassist.LoaderClassPath;
025    
026    /**
027     * Used to ensure that {@link javassist.ClassPool#appendClassPath(javassist.ClassPath)} is invoked
028     * with a synchronized lock. Additionally, wraps around a shared
029     * {@link org.apache.hivemind.service.impl.ClassFactoryClassLoader}.
030     * 
031     * @author Howard Lewis Ship
032     */
033    public class HiveMindClassPool extends ClassPool
034    {
035        private ClassFactoryClassLoader _loader = new ClassFactoryClassLoader(HiveMindClassPool.class.getClassLoader());
036        
037        /**
038         * Used to identify which class loaders have already been integrated into the pool.
039         */
040        private Set _loaders = new HashSet();
041    
042        public HiveMindClassPool() 
043        {
044            super(null);
045    
046            appendClassLoader(Thread.currentThread().getContextClassLoader());
047        }
048    
049        /**
050         * Convienience method for adding to the ClassPath for a particular class loader.
051         */
052        public synchronized void appendClassLoader(ClassLoader loader)
053        {
054            if (loader == null || loader == _loader || _loaders.contains(loader))
055                return;
056    
057            _loader.addDelegateLoader(loader);
058    
059            ClassPath path = new LoaderClassPath(loader);
060    
061            appendClassPath(path);
062    
063            _loaders.add(loader);
064        }
065    
066        /**
067         * Invoked to convert an fabricated class into a real class. The new classes' class loader will
068         * be the delegating ClassFactoryClassLoader, which has visibility to all class loaders for all
069         * modules.
070         * 
071         * @since 1.1
072         */
073        public Class toClass(CtClass ctClass) throws CannotCompileException
074        {
075            return ctClass.toClass(_loader, this.getClass().getProtectionDomain());
076        }
077    }