001// Copyright 2007, 2008, 2012 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 org.apache.tapestry5.ioc.services.PropertyAdapter; 018import org.apache.tapestry5.ioc.util.StrategyRegistry; 019import org.apache.tapestry5.services.DataTypeAnalyzer; 020import org.apache.tapestry5.services.InvalidationListener; 021 022import java.util.Map; 023 024/** 025 * The default data type analyzer, which is based entirely on the type of the property (and not on annotations or naming 026 * conventions). This is based on a configuration of property type class to string provided as an IoC service 027 * configuration. 028 */ 029public class DefaultDataTypeAnalyzer implements DataTypeAnalyzer, Runnable 030{ 031 private final StrategyRegistry<String> registry; 032 033 public DefaultDataTypeAnalyzer(Map<Class, String> configuration) 034 { 035 registry = StrategyRegistry.newInstance(String.class, configuration); 036 } 037 038 /** 039 * Clears the registry on an invalidation event (this is because the registry caches results, and the keys are 040 * classes that may be component classes from the invalidated component class loader). 041 */ 042 public void run() 043 { 044 registry.clearCache(); 045 } 046 047 public String identifyDataType(PropertyAdapter adapter) 048 { 049 Class propertyType = adapter.getType(); 050 051 String dataType = registry.get(propertyType); 052 053 // To avoid "no strategy" exceptions, we expect a contribution of Object.class to the empty 054 // string. We convert that back to a null. 055 056 if (dataType.equals("")) 057 return null; 058 059 return dataType; 060 } 061}