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    
019    import org.apache.hivemind.ApplicationRuntimeException;
020    import org.apache.hivemind.service.ClassFab;
021    import org.apache.hivemind.service.ClassFactory;
022    import org.apache.hivemind.service.InterfaceFab;
023    
024    /**
025     * Implementation of {@link org.apache.hivemind.service.ClassFactory}.
026     * 
027     * @author Howard Lewis Ship
028     */
029    public class ClassFactoryImpl implements ClassFactory
030    {
031        /**
032         * ClassPool shared by all modules (all CtClassSource instances).
033         */
034        private HiveMindClassPool _pool = new HiveMindClassPool();
035    
036        private CtClassSource _classSource = new CtClassSource(_pool);
037    
038        public ClassFab newClass(String name, Class superClass)
039        {
040            try
041            {
042                CtClass ctNewClass = _classSource.newClass(name, superClass);
043    
044                return new ClassFabImpl(_classSource, ctNewClass);
045            }
046            catch (Exception ex)
047            {
048                throw new ApplicationRuntimeException(ServiceMessages.unableToCreateClass(
049                        name,
050                        superClass,
051                        ex), ex);
052            }
053        }
054    
055        /** @since 1.1 */
056    
057        public InterfaceFab newInterface(String name)
058        {
059            try
060            {
061                CtClass ctNewClass = _classSource.newInterface(name);
062    
063                return new InterfaceFabImpl(_classSource, ctNewClass);
064            }
065            catch (Exception ex)
066            {
067                throw new ApplicationRuntimeException(
068                        ServiceMessages.unableToCreateInterface(name, ex), ex);
069            }
070    
071        }
072    
073        public int getCreatedClassCount()
074        {
075            return _classSource.getCreatedClassCount();
076        }
077    
078    }