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
015 package org.apache.tapestry5.internal.services;
016
017 import java.lang.reflect.Method;
018
019 import org.apache.tapestry5.ioc.services.Builtin;
020 import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
021 import org.apache.tapestry5.plastic.ClassInstantiator;
022 import org.apache.tapestry5.plastic.MethodAdvice;
023 import org.apache.tapestry5.plastic.MethodInvocation;
024 import org.apache.tapestry5.plastic.PlasticClass;
025 import org.apache.tapestry5.plastic.PlasticClassTransformer;
026 import org.apache.tapestry5.plastic.PlasticMethod;
027 import org.apache.tapestry5.plastic.PlasticUtils;
028 import org.apache.tapestry5.services.Environment;
029 import org.apache.tapestry5.services.EnvironmentalShadowBuilder;
030
031 public 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 }