001// Copyright 2007, 2008, 2010, 2011, 2012 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
015package org.apache.tapestry5.internal.services;
016
017import org.apache.tapestry5.Translator;
018import org.apache.tapestry5.ioc.annotations.PostInjection;
019import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
020import org.apache.tapestry5.ioc.internal.util.InternalUtils;
021import org.apache.tapestry5.ioc.util.AvailableValues;
022import org.apache.tapestry5.ioc.util.StrategyRegistry;
023import org.apache.tapestry5.ioc.util.UnknownValueException;
024import org.apache.tapestry5.plastic.PlasticUtils;
025import org.apache.tapestry5.services.ComponentClasses;
026import org.apache.tapestry5.services.InvalidationEventHub;
027import org.apache.tapestry5.services.TranslatorSource;
028
029import java.util.Collections;
030import java.util.List;
031import java.util.Map;
032
033@SuppressWarnings("unchecked")
034public class TranslatorSourceImpl implements TranslatorSource, Runnable
035{
036    private final Map<String, Translator> nameToTranslator = CollectionFactory.newCaseInsensitiveMap();
037
038    private final StrategyRegistry<Translator> registry;
039
040    private static final Map<String, Translator> EMPTY = Collections.emptyMap();
041
042    public TranslatorSourceImpl(Map<Class, Translator> configuration)
043    {
044        this(configuration, EMPTY);
045    }
046
047    public TranslatorSourceImpl(Map<Class, Translator> configuration, Map<String, Translator> alternates)
048    {
049        for (Map.Entry<Class, Translator> me : configuration.entrySet())
050        {
051            Class type = me.getKey();
052            Translator translator = me.getValue();
053
054            if (!type.equals(translator.getType()))
055                throw new RuntimeException(
056                        String
057                                .format(
058                                        "Contributed translator for type %s reports its type as %s. Please change the contribution so that the key matches that translator type.",
059                                        type.getName(), translator.getType().getName()));
060
061            String name = translator.getName();
062
063            if (nameToTranslator.containsKey(name))
064                throw new RuntimeException(
065                        String
066                                .format(
067                                        "Two different Translators contributed to the TranslatorSource service use the same translator name: '%s'.  Translator names must be unique.",
068                                        name));
069
070            nameToTranslator.put(name, translator);
071        }
072
073        for (String name : alternates.keySet())
074        {
075            if (nameToTranslator.containsKey(name))
076                throw new RuntimeException(
077                        String
078                                .format(
079                                        "Translator '%s' contributed to the TranslatorAlternatesSource service has the same name as a standard Translator contributed to the TranslatorSource service.",
080                                        name));
081
082            nameToTranslator.put(name, alternates.get(name));
083        }
084
085        registry = StrategyRegistry.newInstance(Translator.class, configuration, true);
086    }
087
088public Translator get(String name)
089    {
090        Translator result = nameToTranslator.get(name);
091
092        if (result == null)
093            throw new UnknownValueException(String.format("Unknown translator type '%s'.", name), new AvailableValues(
094                    "Configured translators", nameToTranslator));
095
096        return result;
097    }
098
099    public Translator getByType(Class valueType)
100    {
101        Translator result = registry.get(valueType);
102
103        if (result == null)
104        {
105            List<String> names = CollectionFactory.newList();
106
107            for (Class type : registry.getTypes())
108            {
109                names.add(type.getName());
110            }
111
112            throw new IllegalArgumentException(String.format("No translator is defined for type %s.  Registered types: %s.", PlasticUtils.toTypeName(valueType), InternalUtils
113                    .joinSorted(names)));
114        }
115
116        return result;
117    }
118
119    public Translator findByType(Class valueType)
120    {
121        return registry.get(valueType);
122    }
123
124    /**
125     * Invoked by InvalidationEventHub
126     */
127    public void run()
128    {
129        registry.clearCache();
130    }
131}