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.Collection;
20: import java.util.Map;
21:
22: import org.apache.commons.logging.Log;
23: import org.apache.tapestry.services.AliasContribution;
24: import org.apache.tapestry.services.AliasManager;
25:
26: public class AliasManagerImpl implements AliasManager {
27: private final Log _log;
28:
29: private final Collection<AliasContribution> _contributions;
30:
31: public AliasManagerImpl(Log log,
32: Collection<AliasContribution> contributions) {
33: _log = log;
34: _contributions = contributions;
35: }
36:
37: public Map<Class, Object> getAliasesForMode(String mode) {
38: Map<Class, Object> general = buildMapForMode("");
39: Map<Class, Object> specific = buildMapForMode(mode);
40:
41: // Anything in specific overrides anything in general
42:
43: general.putAll(specific);
44:
45: return general;
46: }
47:
48: private Map<Class, Object> buildMapForMode(String mode) {
49: Map<Class, Object> result = newMap();
50:
51: for (AliasContribution ic : _contributions) {
52: if (!ic.getMode().equalsIgnoreCase(mode))
53: continue;
54:
55: Class contributionType = ic.getContributionType();
56:
57: Object existing = result.get(contributionType);
58:
59: if (existing != null) {
60: _log.error(ServicesMessages.duplicateContribution(ic
61: .getObject(), contributionType, existing));
62: continue;
63: }
64:
65: result.put(contributionType, ic.getObject());
66: }
67:
68: return result;
69: }
70:
71: }
|