001// Copyright 2007, 2008, 2010 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.util.Map; 018 019import org.apache.tapestry5.ValueEncoder; 020import org.apache.tapestry5.ioc.internal.util.CollectionFactory; 021import org.apache.tapestry5.ioc.util.StrategyRegistry; 022import org.apache.tapestry5.services.InvalidationListener; 023import org.apache.tapestry5.services.ValueEncoderFactory; 024import org.apache.tapestry5.services.ValueEncoderSource; 025 026@SuppressWarnings("all") 027public class ValueEncoderSourceImpl implements ValueEncoderSource, InvalidationListener 028{ 029 private final StrategyRegistry<ValueEncoderFactory> registry; 030 031 private final Map<Class, ValueEncoder> cache = CollectionFactory.newConcurrentMap(); 032 033 public ValueEncoderSourceImpl(Map<Class, ValueEncoderFactory> configuration) 034 { 035 registry = StrategyRegistry.newInstance(ValueEncoderFactory.class, configuration); 036 } 037 038 @SuppressWarnings({"unchecked"}) 039 public <T> ValueEncoder<T> getValueEncoder(Class<T> type) 040 { 041 assert type != null; 042 ValueEncoder<T> result = cache.get(type); 043 044 if (result == null) 045 { 046 ValueEncoderFactory<T> factory = registry.get(type); 047 048 result = factory.create(type); 049 050 cache.put(type, result); 051 } 052 053 return result; 054 } 055 056 public void objectWasInvalidated() 057 { 058 registry.clearCache(); 059 cache.clear(); 060 } 061}