001    // Copyright 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.lang.reflect.Modifier;
018    import java.util.ArrayList;
019    import java.util.Iterator;
020    import java.util.List;
021    
022    import javassist.CtClass;
023    import javassist.CtMethod;
024    
025    import org.apache.hivemind.ApplicationRuntimeException;
026    import org.apache.hivemind.service.InterfaceFab;
027    import org.apache.hivemind.service.MethodSignature;
028    
029    /**
030     * @author Howard M. Lewis Ship
031     */
032    public class InterfaceFabImpl extends AbstractFab implements InterfaceFab
033    {
034        private List _methods = new ArrayList();
035    
036        public InterfaceFabImpl(CtClassSource source, CtClass ctClass)
037        {
038            super(source, ctClass);
039        }
040    
041        public String toString()
042        {
043            StringBuffer buffer = new StringBuffer("InterfaceFabImpl[\npublic interface ");
044    
045            CtClass ctClass = getCtClass();
046    
047            buffer.append(ctClass.getName());
048    
049            try
050            {
051                CtClass[] interfaces = ctClass.getInterfaces();
052    
053                for (int i = 0; i < interfaces.length; i++)
054                {
055                    buffer.append(i == 0 ? " extends " : ", ");
056                    buffer.append(interfaces[i].getName());
057                }
058    
059            }
060            catch (Exception ex)
061            {
062                buffer.append("<Exception: " + ex + ">");
063            }
064    
065            Iterator i = _methods.iterator();
066    
067            while (i.hasNext())
068            {
069                MethodSignature sig = (MethodSignature) i.next();
070    
071                buffer.append("\n\npublic ");
072                buffer.append(sig);
073                buffer.append(";");
074            }
075    
076            buffer.append("\n]");
077    
078            return buffer.toString();
079        }
080    
081        public void addMethod(MethodSignature ms)
082        {
083            CtClass ctReturnType = convertClass(ms.getReturnType());
084            CtClass[] ctParameters = convertClasses(ms.getParameterTypes());
085            CtClass[] ctExceptions = convertClasses(ms.getExceptionTypes());
086    
087            CtMethod method = new CtMethod(ctReturnType, ms.getName(), ctParameters, getCtClass());
088    
089            try
090            {
091                method.setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT);
092                method.setExceptionTypes(ctExceptions);
093    
094                getCtClass().addMethod(method);
095            }
096            catch (Exception ex)
097            {
098                throw new ApplicationRuntimeException(ServiceMessages.unableToAddMethod(
099                        ms,
100                        getCtClass(),
101                        ex), ex);
102            }
103    
104            _methods.add(ms);
105        }
106    
107        public Class createInterface()
108        {
109            return createClass();
110        }
111    
112    }