001// Copyright 2006, 2007, 2010, 2011 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.ComponentResources;
018import org.apache.tapestry5.commons.Messages;
019import org.apache.tapestry5.commons.ObjectLocator;
020import org.apache.tapestry5.commons.util.CollectionFactory;
021import org.apache.tapestry5.internal.transform.ReadOnlyComponentFieldConduit;
022import org.apache.tapestry5.model.MutableComponentModel;
023import org.apache.tapestry5.plastic.ComputedValue;
024import org.apache.tapestry5.plastic.InstanceContext;
025import org.apache.tapestry5.plastic.PlasticField;
026import org.apache.tapestry5.services.pageload.ComponentResourceSelector;
027import org.apache.tapestry5.services.transform.InjectionProvider2;
028import org.slf4j.Logger;
029
030import java.util.Locale;
031import java.util.Map;
032
033/**
034 * Allows for a number of anonymous injections based on the type of field that is to be injected.
035 */
036public class CommonResourcesInjectionProvider implements InjectionProvider2
037{
038    abstract static class ResourceProvider<T> implements ComputedValue<T>
039    {
040        public T get(InstanceContext context)
041        {
042            ComponentResources resources = context.get(ComponentResources.class);
043
044            return get(resources);
045        }
046
047        abstract T get(ComponentResources resources);
048    }
049
050    private static ResourceProvider<ComponentResourceSelector> selectorProvider = new ResourceProvider<ComponentResourceSelector>()
051    {
052        public ComponentResourceSelector get(ComponentResources resources)
053        {
054            return resources.getResourceSelector();
055        }
056    };
057
058    private static ResourceProvider<Messages> messagesProvider = new ResourceProvider<Messages>()
059    {
060
061        public Messages get(ComponentResources resources)
062        {
063            return resources.getMessages();
064        }
065    };
066
067    private static ResourceProvider<Locale> localeProvider = new ResourceProvider<Locale>()
068    {
069
070        public Locale get(ComponentResources resources)
071        {
072            return resources.getLocale();
073        }
074    };
075
076    private static ResourceProvider<Logger> loggerProvider = new ResourceProvider<Logger>()
077    {
078
079        public Logger get(ComponentResources resources)
080        {
081            return resources.getLogger();
082        }
083    };
084
085
086    private static ResourceProvider<ComponentResources> resourcesProvider = new ResourceProvider<ComponentResources>()
087    {
088        public ComponentResources get(ComponentResources resources)
089        {
090            return resources;
091        }
092    };
093
094    private static final Map<String, ResourceProvider> configuration = CollectionFactory.newMap();
095
096    {
097        add(ComponentResources.class, resourcesProvider);
098        add(ComponentResourceSelector.class, selectorProvider);
099        add(Messages.class, messagesProvider);
100        add(Locale.class, localeProvider);
101        add(Logger.class, loggerProvider);
102    }
103
104    private static void add(Class type, ResourceProvider provider)
105    {
106        configuration.put(type.getName(), provider);
107    }
108
109    public boolean provideInjection(PlasticField field, ObjectLocator locator, MutableComponentModel componentModel)
110    {
111        final ResourceProvider provider = configuration.get(field.getTypeName());
112
113        if (provider == null)
114        {
115            return false;
116        }
117
118        field.injectComputed(provider);
119
120        return true;
121    }
122}