    001    // Copyright 2006, 2007, 2011 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.tapestry5.ioc.internal.services;
    016
    017    import java.util.Map;
    018
    019    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
    020    import org.apache.tapestry5.ioc.services.Builtin;
    021    import org.apache.tapestry5.ioc.services.DefaultImplementationBuilder;
    022    import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
    023    import org.apache.tapestry5.plastic.ClassInstantiator;
    024    import org.apache.tapestry5.plastic.PlasticClass;
    025    import org.apache.tapestry5.plastic.PlasticClassTransformer;
    026
    027    public class DefaultImplementationBuilderImpl implements DefaultImplementationBuilder
    028    {
    029        private final Map<Class, Object> cache = CollectionFactory.newConcurrentMap();
    030
    031        private final PlasticProxyFactory proxyFactory;
    032
    033        public DefaultImplementationBuilderImpl(@Builtin
    034        PlasticProxyFactory proxyFactory)
    035        {
    036            this.proxyFactory = proxyFactory;
    037        }
    038
    039        public <S> S createDefaultImplementation(Class<S> serviceInterface)
    040        {
    041            S instance = serviceInterface.cast(cache.get(serviceInterface));
    042
    043            if (instance == null)
    044            {
    045                instance = createInstance(serviceInterface);
    046                cache.put(serviceInterface, instance);
    047            }
    048
    049            return instance;
    050        }
    051
    052        /**
    053         * Creates a class and an instance of that class. Updates the cache and returns the instance.
    054         */
    055        private <S> S createInstance(final Class<S> serviceInterface)
    056        {
    057            ClassInstantiator instantiator = proxyFactory.createProxy(serviceInterface, new PlasticClassTransformer()
    058            {
    059                public void transform(PlasticClass plasticClass)
    060                {
    061                    plasticClass.addToString(String.format("<NoOp %s>", serviceInterface.getName()));
    062                }
    063            });
    064
    065            return serviceInterface.cast(instantiator.newInstance());
    066        }
    067    }