001// Licensed under the Apache License, Version 2.0 (the "License"); 002// you may not use this file except in compliance with the License. 003// You may obtain a copy of the License at 004// 005// http://www.apache.org/licenses/LICENSE-2.0 006// 007// Unless required by applicable law or agreed to in writing, software 008// distributed under the License is distributed on an "AS IS" BASIS, 009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 010// See the License for the specific language governing permissions and 011// limitations under the License. 012 013package org.apache.tapestry5.internal; 014 015import org.apache.tapestry5.services.ComponentOverride; 016import org.slf4j.Logger; 017 018import java.util.*; 019 020public class ComponentOverrideImpl implements ComponentOverride 021{ 022 023 private final Map<String, Class> nameToClass; 024 025 @SuppressWarnings("rawtypes") 026 public ComponentOverrideImpl(Map<Class, Class> contributions, Logger logger) 027 { 028 029 Map<Class, Class> replacements = Collections.unmodifiableMap(contributions); 030 Map<String, Class> nameToClass = new HashMap<String, Class>(); 031 032 int maxLength = 0; 033 034 for (Class<?> clazz : contributions.keySet()) 035 { 036 037 final String name = clazz.getName(); 038 if (name.length() > maxLength) 039 { 040 maxLength = name.length(); 041 } 042 nameToClass.put(name, contributions.get(clazz)); 043 044 } 045 046 this.nameToClass = Collections.unmodifiableMap(nameToClass); 047 048 if (replacements.size() > 0 && logger.isInfoEnabled()) 049 { 050 StringBuilder builder = new StringBuilder(1000); 051 final String format = "%" + maxLength + "s: %s\n"; 052 builder.append("Component replacements (including components, pages and mixins):\n"); 053 List<String> names = new ArrayList<String>(nameToClass.keySet()); 054 Collections.sort(names); 055 056 for (String name : names) 057 { 058 builder.append(String.format(format, name, nameToClass.get(name).getName())); 059 } 060 061 logger.info(builder.toString()); 062 } 063 064 } 065 066 @Override 067 public boolean hasReplacements() 068 { 069 return !nameToClass.isEmpty(); 070 } 071 072 @Override 073 public Class getReplacement(String className) 074 { 075 return nameToClass.get(className); 076 } 077 078}