You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2010/01/22 17:30:33 UTC

svn commit: r902151 - /tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/CommonResourcesInjectionProvider.java

Author: hlship
Date: Fri Jan 22 16:30:32 2010
New Revision: 902151

URL: http://svn.apache.org/viewvc?rev=902151&view=rev
Log:
Convert to use ComponentValueProvider to inject resources into components

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/CommonResourcesInjectionProvider.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/CommonResourcesInjectionProvider.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/CommonResourcesInjectionProvider.java?rev=902151&r1=902150&r2=902151&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/CommonResourcesInjectionProvider.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/CommonResourcesInjectionProvider.java Fri Jan 22 16:30:32 2010
@@ -1,10 +1,10 @@
-// Copyright 2006, 2007 The Apache Software Foundation
+// Copyright 2006, 2007, 2010 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
+// 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,
@@ -14,49 +14,83 @@
 
 package org.apache.tapestry5.internal.services;
 
+import java.util.Locale;
+import java.util.Map;
+
+import org.apache.tapestry5.ComponentResources;
 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.ComponentValueProvider;
 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();
+    private static ComponentValueProvider<Messages> messagesProvider = new ComponentValueProvider<Messages>()
+    {
 
+        @Override
+        public Messages get(ComponentResources resources)
+        {
+            return resources.getMessages();
+        }
+    };
+
+    private static ComponentValueProvider<Locale> localeProvider = new ComponentValueProvider<Locale>()
     {
-        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)
+        @Override
+        public Locale get(ComponentResources resources)
+        {
+            return resources.getLocale();
+        }
+    };
+
+    private static ComponentValueProvider<Logger> loggerProvider = new ComponentValueProvider<Logger>()
     {
-        String implementationMethodName = configuration.get(fieldType);
+        @Override
+        public Logger get(ComponentResources resources)
+        {
+            return resources.getLogger();
+        };
+    };
 
-        if (implementationMethodName == null) return false;
+    private static ComponentValueProvider<String> completeIdProvider = new ComponentValueProvider<String>()
+    {
 
-        String resourcesField = transformation.getResourcesFieldName();
+        @Override
+        public String get(ComponentResources resources)
+        {
+            return resources.getCompleteId();
+        }
+    };
 
-        String body = String.format(
-                "%s = %s.%s();",
-                fieldName,
-                resourcesField,
-                implementationMethodName);
+    private static final Map<Class, ComponentValueProvider> configuration = CollectionFactory
+            .newMap();
+
+    {
+        configuration.put(Messages.class, messagesProvider);
+        configuration.put(Locale.class, localeProvider);
+        configuration.put(Logger.class, loggerProvider);
+        configuration.put(String.class, completeIdProvider);
+    }
+
+    @SuppressWarnings("unchecked")
+    public boolean provideInjection(String fieldName, Class fieldType, ObjectLocator locator,
+            ClassTransformation transformation, MutableComponentModel componentModel)
+    {
+        ComponentValueProvider provider = configuration.get(fieldType);
 
-        transformation.makeReadOnly(fieldName);
+        if (provider == null)
+            return false;
 
-        transformation.extendConstructor(body);
+        transformation.injectFieldIndirect(fieldName, provider);
 
         return true;
     }