You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2009/01/08 21:29:57 UTC

svn commit: r732815 - in /tapestry/tapestry5/trunk: tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/ tapestry-core/src/main/java/org/apache/tapestry5/services/ tapestry-core/src/test/java/org/apache/tapestry5/integration/ tapestry-c...

Author: hlship
Date: Thu Jan  8 12:29:57 2009
New Revision: 732815

URL: http://svn.apache.org/viewvc?rev=732815&view=rev
Log:
TAP5-334: Component fields should allow @InjectService annotation, as well as @Inject

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectServiceWorker.java
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/InjectDemo.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/InjectDemo.tml
    tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/injection.apt

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectServiceWorker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectServiceWorker.java?rev=732815&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectServiceWorker.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/InjectServiceWorker.java Thu Jan  8 12:29:57 2009
@@ -0,0 +1,64 @@
+// Copyright 2009 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.transform;
+
+import org.apache.tapestry5.internal.services.ComponentClassCache;
+import org.apache.tapestry5.ioc.ObjectLocator;
+import org.apache.tapestry5.ioc.annotations.InjectService;
+import org.apache.tapestry5.model.MutableComponentModel;
+import org.apache.tapestry5.services.ClassTransformation;
+import org.apache.tapestry5.services.ComponentClassTransformWorker;
+
+import java.util.List;
+
+/**
+ * Processes the {@link org.apache.tapestry5.ioc.annotations.InjectService} annotation.
+ *
+ * @since 5.1.0.0
+ */
+public class InjectServiceWorker implements ComponentClassTransformWorker
+{
+    private final ObjectLocator locator;
+
+    private final ComponentClassCache cache;
+
+    public InjectServiceWorker(ObjectLocator locator, ComponentClassCache cache)
+    {
+        this.locator = locator;
+        this.cache = cache;
+    }
+
+    public void transform(ClassTransformation transformation, MutableComponentModel model)
+    {
+        List<String> names = transformation.findFieldsWithAnnotation(InjectService.class);
+
+        if (names.isEmpty()) return;
+
+        for (String name : names)
+        {
+            InjectService annotation = transformation.getFieldAnnotation(name, InjectService.class);
+
+            String typeName = transformation.getFieldType(name);
+
+            Class fieldType = cache.forName(typeName);
+
+            Object service = locator.getService(annotation.value(), fieldType);
+
+            transformation.injectField(name, service);
+
+            transformation.claimField(name, annotation);
+        }
+    }
+}

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java?rev=732815&r1=732814&r2=732815&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java Thu Jan  8 12:29:57 2009
@@ -300,7 +300,8 @@
      * based on the {@link org.apache.tapestry5.annotations.Component} annotation</dd> <dt>Mixin </dt> <dd>Adds a mixin
      * as part of a component's implementation</dd> <dt>Environment </dt> <dd>Allows fields to contain values extracted
      * from the {@link org.apache.tapestry5.services.Environment} service</dd> <dt>Inject </dt> <dd>Used with the {@link
-     * org.apache.tapestry5.ioc.annotations.Inject} annotation, when a value is supplied</dd> <dt>InjectPage</dt>
+     * org.apache.tapestry5.ioc.annotations.Inject} annotation, when a value is supplied</dd> <dt>InjectService</dt>
+     * <dd>Handles the {@link org.apache.tapestry5.ioc.annotations.InjectService} annotation</dd> <dt>InjectPage</dt>
      * <dd>Adds code to allow access to other pages via the {@link org.apache.tapestry5.annotations.InjectPage} field
      * annotation</dd> <dt>InjectBlock </dt> <dd>Allows a block from the template to be injected into a field</dd>
      * <dt>IncludeStylesheet </dt> <dd>Supports the {@link org.apache.tapestry5.annotations.IncludeStylesheet}
@@ -337,6 +338,7 @@
         configuration.add("Meta", new MetaWorker());
 
         configuration.add("Inject", new InjectWorker(locator, injectionProvider));
+        configuration.addInstance("InjectService", InjectServiceWorker.class);
 
         configuration.add("Secure", new SecureWorker());
 

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java?rev=732815&r1=732814&r2=732815&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java Thu Jan  8 12:29:57 2009
@@ -192,6 +192,8 @@
         // Prove that injection using a marker annotation (to match against a marked service) works.
 
         assertTextPresent("Injection via Marker: Bonjour!");
+
+        assertText("viaInjectService", "1722 tracks in music library");
     }
 
     @Test

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/InjectDemo.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/InjectDemo.java?rev=732815&r1=732814&r2=732815&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/InjectDemo.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/InjectDemo.java Thu Jan  8 12:29:57 2009
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007 The Apache Software Foundation
+// Copyright 2006, 2007, 2009 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.
@@ -17,9 +17,12 @@
 import org.apache.tapestry5.ComponentResources;
 import org.apache.tapestry5.annotations.InjectPage;
 import org.apache.tapestry5.annotations.OnEvent;
+import org.apache.tapestry5.annotations.Property;
 import org.apache.tapestry5.integration.app1.services.French;
 import org.apache.tapestry5.integration.app1.services.Greeter;
+import org.apache.tapestry5.integration.app1.services.MusicLibrary;
 import org.apache.tapestry5.ioc.annotations.Inject;
+import org.apache.tapestry5.ioc.annotations.InjectService;
 import org.apache.tapestry5.ioc.annotations.Symbol;
 import org.apache.tapestry5.services.BindingSource;
 import org.apache.tapestry5.services.Request;
@@ -55,6 +58,10 @@
     @French
     private Greeter greeter;
 
+    @Property
+    @InjectService("MusicLibrary")
+    private MusicLibrary musicLibrary;
+
     public String getGreeting()
     {
         return greeter.getGreeting();

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/InjectDemo.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/InjectDemo.tml?rev=732815&r1=732814&r2=732815&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/InjectDemo.tml (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/InjectDemo.tml Thu Jan  8 12:29:57 2009
@@ -1,17 +1,25 @@
-<html t:type="Border" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
-    <p> Demonstrates the use of the @Inject annotation. </p>
-    <p>WebRequest: ${request}</p>
-    <p>ComponentResources: ${resources}</p>
-    <p>BindingSource: ${bindingSource}</p>
-    <p>Injected Symbol: ${injectedSymbol}</p>
-    <p>Injection via Marker: ${greeting}</p>
-    
-    
-    <p>
-        ActionLinks used to demonstrate the use of @InjectPage:
-    </p>
-    
-    <p><a t:id="fred" t:type="ActionLink">Fred</a></p>
-    <p><a t:id="barney" t:type="ActionLink">Barney</a></p>
-    <p><a t:id="wilma" t:type="ActionLink">Wilma</a></p>
+<html t:type="Border" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+    <p>Demonstrates the use of the @Inject annotation.</p>
+    <p>WebRequest: ${request}</p>
+    <p>ComponentResources: ${resources}</p>
+    <p>BindingSource: ${bindingSource}</p>
+    <p>Injected Symbol: ${injectedSymbol}</p>
+    <p>Injection via Marker: ${greeting}</p>
+    <p>Injection via @InjectService:
+        <span id="viaInjectService">${musicLibrary.tracks.size()} tracks in music library</span>
+    </p>
+
+    <p>
+        ActionLinks used to demonstrate the use of @InjectPage:
+    </p>
+
+    <p>
+        <a t:id="fred" t:type="ActionLink">Fred</a>
+    </p>
+    <p>
+        <a t:id="barney" t:type="ActionLink">Barney</a>
+    </p>
+    <p>
+        <a t:id="wilma" t:type="ActionLink">Wilma</a>
+    </p>
 </html>

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/injection.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/injection.apt?rev=732815&r1=732814&r2=732815&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/injection.apt (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/injection.apt Thu Jan  8 12:29:57 2009
@@ -301,3 +301,9 @@
 * Service InjectionProvider
 
   Equivalent to the Service Lookup phase in an IoC layer injection.
+
+@InjectService in Components
+
+  This is now supported; you may use the
+  {{{../apidocs/org/apache/tapestry5/ioc/annotations/InjectService.html}InjectService}} annotation on component
+  fields.
\ No newline at end of file