01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import java.util.Map;
18:
19: import org.apache.tapestry.internal.events.InvalidationListener;
20: import org.apache.tapestry.ioc.services.PropertyAdapter;
21: import org.apache.tapestry.ioc.util.StrategyRegistry;
22: import org.apache.tapestry.services.DataTypeAnalyzer;
23:
24: /**
25: * The default data type analyzer, which is based entirely on the type of the property (and not on
26: * annotations or naming conventions). This is based on a configuration of property type class to
27: * string provided as an IoC service configuration.
28: */
29: public class DefaultDataTypeAnalyzer implements DataTypeAnalyzer,
30: InvalidationListener {
31: private final StrategyRegistry<String> _registry;
32:
33: public DefaultDataTypeAnalyzer(Map<Class, String> configuration) {
34: _registry = StrategyRegistry.newInstance(String.class,
35: configuration);
36: }
37:
38: /**
39: * Clears the registry on an invalidation event (this is because the registry caches results,
40: * and the keys are classes that may be component classes from the invalidated component class
41: * loader).
42: */
43: public void objectWasInvalidated() {
44: _registry.clearCache();
45: }
46:
47: public String identifyDataType(PropertyAdapter adapter) {
48: Class propertyType = adapter.getType();
49:
50: String dataType = _registry.get(propertyType);
51:
52: // To avoid "no strategy" exceptions, we expect a contribution of Object.class to the empty
53: // string. We convert that back to a null.
54:
55: if (dataType.equals(""))
56: return null;
57:
58: return dataType;
59: }
60:
61: }
|