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 javassist.CtClass;
018    import javassist.NotFoundException;
019    
020    import org.apache.hivemind.ApplicationRuntimeException;
021    import org.apache.hivemind.service.ClassFabUtils;
022    
023    /**
024     * Wrapper around Javassist's {@link javassist.ClassPool} and our own
025     * {@link org.apache.hivemind.service.impl.ClassFactoryClassLoader} that manages the creation of new
026     * instance of {@link javassist.CtClass} and converts finished CtClass's into instantiable Classes.
027     * 
028     * @author Howard Lewis Ship
029     */
030    public class CtClassSource
031    {
032        private HiveMindClassPool _pool;
033    
034        public CtClassSource(HiveMindClassPool pool)
035        {
036            _pool = pool;
037        }
038    
039        public CtClass getCtClass(Class searchClass)
040        {
041            ClassLoader loader = searchClass.getClassLoader();
042    
043            // Add the class loader for the searchClass to the class pool and
044            // delegating class loader if needed.
045    
046            _pool.appendClassLoader(loader);
047    
048            String name = ClassFabUtils.getJavaClassName(searchClass);
049    
050            try
051            {
052                return _pool.get(name);
053            }
054            catch (NotFoundException ex)
055            {
056                throw new ApplicationRuntimeException(ServiceMessages.unableToLookupClass(name, ex), ex);
057            }
058        }
059    
060        public CtClass newClass(String name, Class superClass)
061        {
062            CtClass ctSuperClass = getCtClass(superClass);
063    
064            return _pool.makeClass(name, ctSuperClass);
065        }
066    
067        /**
068         * Creates a new, empty interace, with the given name.
069         * 
070         * @since 1.1
071         */
072    
073        public CtClass newInterface(String name)
074        {
075            return _pool.makeInterface(name);
076        }
077    
078        public Class createClass(CtClass ctClass)
079        {
080            // String className = ctClass.getName();
081    
082            try
083            {
084                return _pool.toClass(ctClass);
085            }
086            catch (Throwable ex)
087            {
088                throw new ApplicationRuntimeException(ServiceMessages.unableToWriteClass(ctClass, ex),
089                        ex);
090            }
091        }
092    }