01: // Copyright 2006, 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.CollectionFactory.newMap;
18:
19: import java.util.Map;
20:
21: import org.apache.tapestry.ioc.AnnotationProvider;
22: import org.apache.tapestry.ioc.ObjectLocator;
23: import org.apache.tapestry.ioc.ObjectProvider;
24: import org.apache.tapestry.services.Alias;
25: import org.apache.tapestry.services.AliasManager;
26:
27: public class AliasImpl implements Alias, ObjectProvider {
28: // Derived from the managers when first needed
29:
30: private final Map<Class, Object> _properties = newMap();
31:
32: private final String _mode;
33:
34: private boolean _initialized = false;
35:
36: private AliasManager _masterManager;
37:
38: private AliasManager _overridesManager;
39:
40: public AliasImpl(AliasManager masterManager, String mode,
41: AliasManager overridesManager) {
42: _masterManager = masterManager;
43: _mode = mode;
44: _overridesManager = overridesManager;
45: }
46:
47: public ObjectProvider getObjectProvider() {
48: return this ;
49: }
50:
51: private synchronized void initialize() {
52: if (_initialized)
53: return;
54:
55: _properties.putAll(_masterManager.getAliasesForMode(_mode));
56: _properties.putAll(_overridesManager.getAliasesForMode(_mode));
57:
58: _masterManager = null;
59: _overridesManager = null;
60:
61: _initialized = true;
62: }
63:
64: public <T> T provide(Class<T> objectType,
65: AnnotationProvider annotationProvider, ObjectLocator locator) {
66: initialize();
67:
68: Object object = _properties.get(objectType);
69:
70: // Let another provider handle this (probably the default object provider)
71: if (object == null)
72: return null;
73:
74: // A ClassCastException should never occur.
75:
76: return objectType.cast(object);
77: }
78: }
|