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.lib.impl;
016    
017    import java.util.Collections;
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import org.apache.hivemind.ApplicationRuntimeException;
022    import org.apache.hivemind.impl.BaseLocatable;
023    import org.apache.hivemind.lib.DefaultImplementationBuilder;
024    import org.apache.hivemind.service.ClassFab;
025    import org.apache.hivemind.service.ClassFabUtils;
026    import org.apache.hivemind.service.ClassFactory;
027    import org.apache.hivemind.service.MethodIterator;
028    
029    /**
030     * Implemenation of {@link org.apache.hivemind.lib.DefaultImplementationBuilder}.
031     * 
032     * @author Howard Lewis Ship
033     */
034    public class DefaultImplementationBuilderImpl extends BaseLocatable implements
035            DefaultImplementationBuilder
036    {
037        private Map _instances = Collections.synchronizedMap(new HashMap());
038    
039        private ClassFactory _classFactory;
040    
041        public Object buildDefaultImplementation(Class interfaceType)
042        {
043            Object result = _instances.get(interfaceType);
044    
045            if (result == null)
046            {
047                result = create(interfaceType);
048    
049                _instances.put(interfaceType, result);
050            }
051    
052            return result;
053        }
054    
055        private Object create(Class interfaceType)
056        {
057            Class defaultClass = createClass(interfaceType);
058    
059            try
060            {
061                return defaultClass.newInstance();
062            }
063            catch (Exception ex)
064            {
065                throw new ApplicationRuntimeException(ImplMessages.unableToCreateDefaultImplementation(
066                        interfaceType,
067                        ex), ex);
068            }
069        }
070    
071        private Class createClass(Class interfaceType)
072        {
073            if (!interfaceType.isInterface())
074                throw new ApplicationRuntimeException(ImplMessages.notAnInterface(interfaceType));
075    
076            String name = ClassFabUtils.generateClassName(interfaceType);
077    
078            ClassFab cf = _classFactory.newClass(name, Object.class);
079    
080            cf.addInterface(interfaceType);
081    
082            MethodIterator mi = new MethodIterator(interfaceType);
083    
084            while (mi.hasNext())
085            {
086                ClassFabUtils.addNoOpMethod(cf, mi.next());
087            }
088    
089            if (!mi.getToString())
090                ClassFabUtils.addToStringMethod(cf, ImplMessages
091                        .defaultImplementationDescription(interfaceType));
092    
093            return cf.createClass();
094        }
095    
096        public void setClassFactory(ClassFactory factory)
097        {
098            _classFactory = factory;
099        }
100    
101    }