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