001 // Copyright 2008, 2009, 2010, 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.structure; 016 017 import org.apache.tapestry5.ComponentResources; 018 import org.apache.tapestry5.Link; 019 import org.apache.tapestry5.internal.InternalConstants; 020 import org.apache.tapestry5.internal.services.ComponentClassCache; 021 import org.apache.tapestry5.internal.services.LinkSource; 022 import org.apache.tapestry5.internal.services.RequestPageCache; 023 import org.apache.tapestry5.ioc.Invokable; 024 import org.apache.tapestry5.ioc.LoggerSource; 025 import org.apache.tapestry5.ioc.Messages; 026 import org.apache.tapestry5.ioc.OperationTracker; 027 import org.apache.tapestry5.ioc.services.PerThreadValue; 028 import org.apache.tapestry5.ioc.services.PerthreadManager; 029 import org.apache.tapestry5.ioc.services.TypeCoercer; 030 import org.apache.tapestry5.model.ComponentModel; 031 import org.apache.tapestry5.services.ComponentClassResolver; 032 import org.apache.tapestry5.services.ContextValueEncoder; 033 import org.apache.tapestry5.services.messages.ComponentMessagesSource; 034 import org.apache.tapestry5.services.pageload.ComponentResourceSelector; 035 import org.slf4j.Logger; 036 037 public class ComponentPageElementResourcesImpl implements ComponentPageElementResources 038 { 039 private final ComponentResourceSelector selector; 040 041 private final ComponentMessagesSource componentMessagesSource; 042 043 private final TypeCoercer typeCoercer; 044 045 private final ComponentClassCache componentClassCache; 046 047 private final ContextValueEncoder contextValueEncoder; 048 049 private final LinkSource linkSource; 050 051 private final RequestPageCache requestPageCache; 052 053 private final ComponentClassResolver componentClassResolver; 054 055 private final LoggerSource loggerSource; 056 057 private final OperationTracker tracker; 058 059 private final PerthreadManager perThreadManager; 060 061 public ComponentPageElementResourcesImpl(ComponentResourceSelector selector, 062 ComponentMessagesSource componentMessagesSource, TypeCoercer typeCoercer, 063 ComponentClassCache componentClassCache, ContextValueEncoder contextValueEncoder, LinkSource linkSource, 064 RequestPageCache requestPageCache, ComponentClassResolver componentClassResolver, 065 LoggerSource loggerSource, OperationTracker tracker, PerthreadManager perThreadManager) 066 { 067 this.selector = selector; 068 this.componentMessagesSource = componentMessagesSource; 069 this.typeCoercer = typeCoercer; 070 this.componentClassCache = componentClassCache; 071 this.contextValueEncoder = contextValueEncoder; 072 this.linkSource = linkSource; 073 this.requestPageCache = requestPageCache; 074 this.componentClassResolver = componentClassResolver; 075 this.loggerSource = loggerSource; 076 this.tracker = tracker; 077 this.perThreadManager = perThreadManager; 078 } 079 080 public ComponentResourceSelector getSelector() 081 { 082 return selector; 083 } 084 085 public Messages getMessages(ComponentModel componentModel) 086 { 087 return componentMessagesSource.getMessages(componentModel, selector); 088 } 089 090 public <S, T> T coerce(S input, Class<T> targetType) 091 { 092 return typeCoercer.coerce(input, targetType); 093 } 094 095 public Class toClass(String className) 096 { 097 return componentClassCache.forName(className); 098 } 099 100 public Link createComponentEventLink(ComponentResources resources, String eventType, boolean forForm, 101 Object... context) 102 { 103 Page page = requestPageCache.get(resources.getPageName()); 104 105 return linkSource.createComponentEventLink(page, resources.getNestedId(), eventType, forForm, 106 defaulted(context)); 107 } 108 109 public Link createPageRenderLink(String pageName, boolean override, Object... context) 110 { 111 return linkSource.createPageRenderLink(pageName, override, defaulted(context)); 112 } 113 114 public Link createPageRenderLink(Class pageClass, boolean override, Object... context) 115 { 116 assert pageClass != null; 117 String pageName = componentClassResolver.resolvePageClassNameToPageName(pageClass.getName()); 118 119 return linkSource.createPageRenderLink(pageName, override, defaulted(context)); 120 } 121 122 public Logger getEventLogger(Logger componentLogger) 123 { 124 String name = "tapestry.events." + componentLogger.getName(); 125 126 return loggerSource.getLogger(name); 127 } 128 129 public String toClient(Object value) 130 { 131 return contextValueEncoder.toClient(value); 132 } 133 134 public <T> T toValue(Class<T> requiredType, String clientValue) 135 { 136 return contextValueEncoder.toValue(requiredType, clientValue); 137 } 138 139 private Object[] defaulted(Object[] context) 140 { 141 return context == null ? InternalConstants.EMPTY_STRING_ARRAY : context; 142 } 143 144 public <T> T invoke(String description, Invokable<T> operation) 145 { 146 return tracker.invoke(description, operation); 147 } 148 149 public void run(String description, Runnable operation) 150 { 151 tracker.run(description, operation); 152 } 153 154 public <T> PerThreadValue<T> createPerThreadValue() 155 { 156 return perThreadManager.createValue(); 157 } 158 159 }