// Copyright 2006, 2007 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry5.internal.services;

import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.ObjectLocator;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.model.MutableComponentModel;
import org.apache.tapestry5.services.ClassTransformation;
import org.apache.tapestry5.services.InjectionProvider;
import org.slf4j.Logger;

import java.util.Locale;
import java.util.Map;

/**
 * Allows for a number of anonymous injections based on the type of field that is to be injected.
 */
public class CommonResourcesInjectionProvider implements InjectionProvider
{
    private static final Map<Class, String> configuration = CollectionFactory.newMap();

    {
        configuration.put(Messages.class, "getMessages");
        configuration.put(Locale.class, "getLocale");
        configuration.put(Logger.class, "getLogger");
        configuration.put(String.class, "getCompleteId");
    }

    public boolean provideInjection(String fieldName, Class fieldType, ObjectLocator locator,
                                    ClassTransformation transformation, MutableComponentModel componentModel)
    {
        String implementationMethodName = configuration.get(fieldType);

        if (implementationMethodName == null) return false;

        String resourcesField = transformation.getResourcesFieldName();

        String body = String.format(
                "%s = %s.%s();",
                fieldName,
                resourcesField,
                implementationMethodName);

        transformation.makeReadOnly(fieldName);

        transformation.extendConstructor(body);

        return true;
    }
}

@@ -16,16 +16,22 @@
 06, 2007
+, 2010
  The Apa
@@ -240,20 +240,16 @@
 at%0A//%0A//
-    
  http://
@@ -658,16 +658,112 @@
 vices;%0A%0A
+import java.util.Locale;%0Aimport java.util.Map;%0A%0Aimport org.apache.tapestry5.ComponentResources;%0A
 import o
@@ -1023,16 +1023,77 @@
 mation;%0A
+import org.apache.tapestry5.services.ComponentValueProvider;%0A
 import o
@@ -1170,56 +1170,8 @@
 r;%0A%0A
-import java.util.Locale;%0Aimport java.util.Map;%0A%0A
 /**%0A
@@ -1372,31 +1372,1085 @@
 tic 
-final Map%3CClass, String
+ComponentValueProvider%3CMessages%3E messagesProvider = new ComponentValueProvider%3CMessages%3E()%0A    %7B%0A%0A        @Override%0A        public Messages get(ComponentResources resources)%0A        %7B%0A            return resources.getMessages();%0A        %7D%0A    %7D;%0A%0A    private static ComponentValueProvider%3CLocale%3E localeProvider = new ComponentValueProvider%3CLocale%3E()%0A    %7B%0A%0A        @Override%0A        public Locale get(ComponentResources resources)%0A        %7B%0A            return resources.getLocale();%0A        %7D%0A    %7D;%0A%0A    private static ComponentValueProvider%3CLogger%3E loggerProvider = new ComponentValueProvider%3CLogger%3E()%0A    %7B%0A        @Override%0A        public Logger get(ComponentResources resources)%0A        %7B%0A            return resources.getLogger();%0A        %7D;%0A    %7D;%0A%0A    private static ComponentValueProvider%3CString%3E completeIdProvider = new ComponentValueProvider%3CString%3E()%0A    %7B%0A%0A        @Override%0A        public String get(ComponentResources resources)%0A        %7B%0A            return resources.getCompleteId();%0A        %7D%0A    %7D;%0A%0A    private static final Map%3CClass, ComponentValueProvider
 %3E co
@@ -2480,16 +2480,29 @@
 nFactory
+%0A            
 .newMap(
@@ -2557,21 +2557,24 @@
 ss, 
-%22getM
+m
 essages
-%22
+Provider
 );%0A 
@@ -2616,19 +2616,22 @@
 ss, 
-%22getLocale%22
+localeProvider
 );%0A 
@@ -2673,19 +2673,22 @@
 ss, 
-%22getLogger%22
+loggerProvider
 );%0A 
@@ -2730,13 +2730,9 @@
 ss, 
-%22getC
+c
 ompl
@@ -2736,17 +2736,24 @@
 mpleteId
-%22
+Provider
 );%0A    %7D
@@ -2754,16 +2754,51 @@
 %0A    %7D%0A%0A
+    @SuppressWarnings(%22unchecked%22)%0A
     publ
@@ -2887,32 +2887,8 @@
 or,%0A
-                        
     
@@ -2987,39 +2987,39 @@
     
-String implementationMethodName
+ComponentValueProvider provider
  = c
@@ -3064,397 +3064,115 @@
 if (
-implementationMethodName == null) return false;%0A%0A        String resourcesField = transformation.getResourcesFieldName();%0A%0A        String body = String.format(%0A                %22%25s = %25s.%25s();%22,%0A                fieldName,%0A                resourcesField,%0A                implementationMethodName);%0A%0A        transformation.makeReadOnly(fieldName);%0A%0A        transformation.extendConstructor(body
+provider == null)%0A            return false;%0A%0A        transformation.injectFieldIndirect(fieldName, provider
 );%0A%0A

Convert to use ComponentValueProvider to inject resources into components