001// Copyright 2006, 2007, 2008, 2010, 2011, 2023 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.transform; 016 017import org.apache.tapestry5.annotations.InjectPage; 018import org.apache.tapestry5.commons.ObjectCreator; 019import org.apache.tapestry5.internal.services.ComponentDependencyRegistry; 020import org.apache.tapestry5.ioc.internal.util.InternalUtils; 021import org.apache.tapestry5.ioc.services.PerthreadManager; 022import org.apache.tapestry5.model.MutableComponentModel; 023import org.apache.tapestry5.plastic.InstanceContext; 024import org.apache.tapestry5.plastic.PlasticClass; 025import org.apache.tapestry5.plastic.PlasticField; 026import org.apache.tapestry5.services.ComponentClassResolver; 027import org.apache.tapestry5.services.ComponentSource; 028import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2; 029import org.apache.tapestry5.services.transform.TransformationSupport; 030 031/** 032 * Performs transformations that allow pages to be injected into components. 033 * 034 * @see org.apache.tapestry5.annotations.InjectPage 035 */ 036public class InjectPageWorker implements ComponentClassTransformWorker2 037{ 038 private final class InjectedPageConduit extends ReadOnlyComponentFieldConduit 039 { 040 private final String injectedPageName; 041 042 private final ObjectCreator<Object> pageValue = perThreadManager.createValue(new ObjectCreator<Object>() { 043 @Override 044 public Object createObject() { 045 return componentSource.getPage(injectedPageName); 046 } 047 }); 048 049 private InjectedPageConduit(String className, String fieldName, 050 String injectedPageName) 051 { 052 super(className, fieldName); 053 054 this.injectedPageName = injectedPageName; 055 } 056 057 public Object get(Object instance, InstanceContext context) 058 { 059 return pageValue.createObject(); 060 } 061 } 062 063 private final ComponentSource componentSource; 064 065 private final ComponentClassResolver resolver; 066 067 private final PerthreadManager perThreadManager; 068 069 private final ComponentDependencyRegistry componentDependencyRegistry; 070 071 public InjectPageWorker(ComponentSource componentSource, ComponentClassResolver resolver, PerthreadManager perThreadManager, 072 ComponentDependencyRegistry componentDependencyRegistry) 073 { 074 this.componentSource = componentSource; 075 this.resolver = resolver; 076 this.perThreadManager = perThreadManager; 077 this.componentDependencyRegistry = componentDependencyRegistry; 078 } 079 080 public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) 081 { 082 for (PlasticField field : plasticClass.getFieldsWithAnnotation(InjectPage.class)) 083 { 084 addInjectedPage(field, model); 085 } 086 } 087 088 private void addInjectedPage(PlasticField field, MutableComponentModel model) 089 { 090 InjectPage annotation = field.getAnnotation(InjectPage.class); 091 092 field.claim(annotation); 093 094 String pageName = annotation.value(); 095 096 String fieldName = field.getName(); 097 098 String injectedPageName = InternalUtils.isBlank(pageName) ? resolver 099 .resolvePageClassNameToPageName(field.getTypeName()) : pageName; 100 101 field.setConduit(new InjectedPageConduit(field.getPlasticClass().getClassName(), fieldName, injectedPageName)); 102 103 componentDependencyRegistry.register(field, model); 104 } 105}