001 // Copyright 2006, 2007 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
015 package org.apache.tapestry5.internal.services;
016
017 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
018 import org.apache.tapestry5.services.AliasContribution;
019 import org.apache.tapestry5.services.AliasManager;
020 import org.slf4j.Logger;
021
022 import java.util.Collection;
023 import java.util.Map;
024
025 public class AliasManagerImpl implements AliasManager
026 {
027 private final Logger logger;
028
029 private final Collection<AliasContribution> contributions;
030
031 public AliasManagerImpl(Logger logger, Collection<AliasContribution> contributions)
032 {
033 this.logger = logger;
034 this.contributions = contributions;
035 }
036
037 public Map<Class, Object> getAliasesForMode(String mode)
038 {
039 Map<Class, Object> general = buildMapForMode("");
040 Map<Class, Object> specific = buildMapForMode(mode);
041
042 // Anything in specific overrides anything in general
043
044 general.putAll(specific);
045
046 return general;
047 }
048
049 private Map<Class, Object> buildMapForMode(String mode)
050 {
051 Map<Class, Object> result = CollectionFactory.newMap();
052
053 for (AliasContribution ic : contributions)
054 {
055 if (!ic.getMode().equalsIgnoreCase(mode)) continue;
056
057 Class contributionType = ic.getContributionType();
058
059 Object existing = result.get(contributionType);
060
061 if (existing != null)
062 {
063 logger.error(ServicesMessages.duplicateContribution(
064 ic.getObject(),
065 contributionType,
066 existing));
067 continue;
068 }
069
070 result.put(contributionType, ic.getObject());
071 }
072
073 return result;
074 }
075
076 }