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 static org.apache.tapestry.ioc.internal.util.Defense.notBlank;
18: import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
19:
20: import java.util.Map;
21:
22: import org.apache.tapestry.ComponentResources;
23: import org.apache.tapestry.ValueEncoder;
24: import org.apache.tapestry.internal.events.InvalidationListener;
25: import org.apache.tapestry.ioc.util.StrategyRegistry;
26: import org.apache.tapestry.services.ValueEncoderFactory;
27: import org.apache.tapestry.services.ValueEncoderSource;
28:
29: public class ValueEncoderSourceImpl implements ValueEncoderSource,
30: InvalidationListener {
31: private final StrategyRegistry<ValueEncoderFactory> _registry;
32:
33: public ValueEncoderSourceImpl(
34: Map<Class, ValueEncoderFactory> configuration) {
35: _registry = StrategyRegistry.newInstance(
36: ValueEncoderFactory.class, configuration);
37: }
38:
39: @SuppressWarnings("unchecked")
40: public ValueEncoder createEncoder(String parameterName,
41: ComponentResources resources) {
42: notBlank(parameterName, "parameterName");
43: notNull(resources, "resources");
44:
45: Class parameterType = resources.getBoundType(parameterName);
46:
47: if (parameterType == null)
48: return null;
49:
50: ValueEncoderFactory factory = _registry.get(parameterType);
51:
52: return factory.create(parameterType);
53: }
54:
55: public void objectWasInvalidated() {
56: _registry.clearCache();
57: }
58: }
|