    001// Copyright 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.internal.services;
    016
    017import java.lang.reflect.Method;
    018
    019import org.apache.tapestry5.ioc.services.Builtin;
    020import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
    021import org.apache.tapestry5.plastic.ClassInstantiator;
    022import org.apache.tapestry5.plastic.MethodAdvice;
    023import org.apache.tapestry5.plastic.MethodInvocation;
    024import org.apache.tapestry5.plastic.PlasticClass;
    025import org.apache.tapestry5.plastic.PlasticClassTransformer;
    026import org.apache.tapestry5.plastic.PlasticMethod;
    027import org.apache.tapestry5.plastic.PlasticUtils;
    028import org.apache.tapestry5.services.Environment;
    029import org.apache.tapestry5.services.EnvironmentalShadowBuilder;
    030
    031public class EnvironmentalShadowBuilderImpl implements EnvironmentalShadowBuilder
    032{
    033    private final PlasticProxyFactory proxyFactory;
    034
    035    private final Environment environment;
    036
    037    /**
    038     * Construct using the default builtin factory, not the component layer version.
    039     */
    040    public EnvironmentalShadowBuilderImpl(@Builtin
    041    PlasticProxyFactory proxyFactory,
    042
    043    Environment environment)
    044    {
    045        this.proxyFactory = proxyFactory;
    046        this.environment = environment;
    047    }
    048
    049    public <T> T build(final Class<T> serviceType)
    050    {
    051        ClassInstantiator<T> instantiator = proxyFactory.createProxy(serviceType, new PlasticClassTransformer()
    052        {
    053            public void transform(PlasticClass plasticClass)
    054            {
    055                PlasticMethod delegateMethod = plasticClass.introducePrivateMethod(
    056                        PlasticUtils.toTypeName(serviceType), "delegate", null, null);
    057
    058                delegateMethod.addAdvice(new MethodAdvice()
    059                {
    060                    public void advise(MethodInvocation invocation)
    061                    {
    062                        invocation.setReturnValue(environment.peekRequired(serviceType));
    063                    }
    064                });
    065
    066                for (Method method : serviceType.getMethods())
    067                {
    068                    plasticClass.introduceMethod(method).delegateTo(delegateMethod);
    069                }
    070
    071                plasticClass.addToString(String.format("<EnvironmentalProxy for %s>", serviceType.getName()));
    072            }
    073        });
    074
    075        return instantiator.newInstance();
    076    }
    077}