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