Author: hlship
Date: Sun May 13 11:30:04 2007
New Revision: 537633

URL: http://svn.apache.org/viewvc?view=rev&rev=537633
Log:
TAPESTRY-1443: org.apache.tapestry.annotations.Service annotation is ignored

Added:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServiceAnnotationObjectProvider.java
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ServiceAnnotationObjectProviderTest.java
Modified:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/Service.java
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/Service.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/Service.java?view=diff&rev=537633&r1=537632&r2=537633
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/Service.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/annotations/Service.java
 Sun May 13 11:30:04 2007
@@ -23,10 +23,9 @@
 import java.lang.annotation.Target;
 
 /**
- * Used in conjunction with the [EMAIL PROTECTED] Inject} annotation (or the 
IoC container's
- * [EMAIL PROTECTED] org.apache.tapestry.ioc.annotations.Inject} annotation) 
to identify a service
- * <em>by name</em> and not by type (which is more typical). This is most 
useful when there are
- * multiple services with the same service interface and a particular one 
needs to be selected.
+ * Used in conjunction with the [EMAIL PROTECTED] Inject} annotation to 
identify a service <em>by name</em>
+ * and not by type. This is most useful when there are multiple services with 
the same service
+ * interface and a particular one needs to be selected.
  */
 @Target(
 { FIELD, PARAMETER })

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServiceAnnotationObjectProvider.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServiceAnnotationObjectProvider.java?view=auto&rev=537633
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServiceAnnotationObjectProvider.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServiceAnnotationObjectProvider.java
 Sun May 13 11:30:04 2007
@@ -0,0 +1,39 @@
+// Copyright 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.tapestry.internal.services;
+
+import org.apache.tapestry.annotations.Service;
+import org.apache.tapestry.ioc.AnnotationProvider;
+import org.apache.tapestry.ioc.ObjectLocator;
+import org.apache.tapestry.ioc.ObjectProvider;
+
+/**
+ * Adds support for the [EMAIL PROTECTED] Service} annotation (which can be 
applied to fields or parameters),
+ * which is used to disambiguate injection when multiple services implement 
the same service
+ * interface.
+ */
+public class ServiceAnnotationObjectProvider implements ObjectProvider
+{
+    public <T> T provide(Class<T> objectType, AnnotationProvider 
annotationProvider,
+            ObjectLocator locator)
+    {
+        Service annotation = annotationProvider.getAnnotation(Service.class);
+
+        if (annotation == null) return null;
+
+        return locator.getService(annotation.value(), objectType);
+    }
+
+}

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java?view=diff&rev=537633&r1=537632&r2=537633
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/services/TapestryModule.java
 Sun May 13 11:30:04 2007
@@ -48,6 +48,7 @@
 import org.apache.tapestry.annotations.PageDetached;
 import org.apache.tapestry.annotations.PageLoaded;
 import org.apache.tapestry.annotations.Path;
+import org.apache.tapestry.annotations.Service;
 import org.apache.tapestry.annotations.SetupRender;
 import org.apache.tapestry.beaneditor.Validate;
 import org.apache.tapestry.corelib.data.GridPagerPosition;
@@ -138,6 +139,7 @@
 import org.apache.tapestry.internal.services.ResponseImpl;
 import org.apache.tapestry.internal.services.RetainWorker;
 import org.apache.tapestry.internal.services.RootPathDispatcher;
+import org.apache.tapestry.internal.services.ServiceAnnotationObjectProvider;
 import 
org.apache.tapestry.internal.services.SessionApplicationStatePersistenceStrategy;
 import org.apache.tapestry.internal.services.SessionHolder;
 import org.apache.tapestry.internal.services.SessionPersistentFieldStrategy;
@@ -571,6 +573,7 @@
      * <li>Alias: Searches by type among [EMAIL PROTECTED] AliasContribution 
contributions} to the
      * [EMAIL PROTECTED] Alias} service</li>
      * <li>Asset: Checks for the [EMAIL PROTECTED] Path} annotation, and 
injects an [EMAIL PROTECTED] Asset}</li>
+     * <li>Service: Injects based on the [EMAIL PROTECTED] Service} 
annotation, if present</li>
      * </ul>
      */
     public static void contributeMasterObjectProvider(
@@ -598,6 +601,8 @@
         configuration.add("Alias", wrapper, "after:Value");
 
         configuration.add("Asset", assetObjectProvider, "before:Alias");
+
+        configuration.add("Service", new ServiceAnnotationObjectProvider(), 
"before:Alias");
     }
 
     /**

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ServiceAnnotationObjectProviderTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ServiceAnnotationObjectProviderTest.java?view=auto&rev=537633
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ServiceAnnotationObjectProviderTest.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/ServiceAnnotationObjectProviderTest.java
 Sun May 13 11:30:04 2007
@@ -0,0 +1,70 @@
+// Copyright 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.tapestry.internal.services;
+
+import org.apache.tapestry.annotations.Service;
+import org.apache.tapestry.ioc.AnnotationProvider;
+import org.apache.tapestry.ioc.ObjectLocator;
+import org.apache.tapestry.ioc.ObjectProvider;
+import org.apache.tapestry.test.TapestryTestCase;
+import org.testng.annotations.Test;
+
+public class ServiceAnnotationObjectProviderTest extends TapestryTestCase
+{
+    @SuppressWarnings("unchecked")
+    @Test
+    public void no_annotation()
+    {
+        Class objectType = Runnable.class;
+        AnnotationProvider provider = mockAnnotationProvider();
+        ObjectLocator locator = mockObjectLocator();
+
+        train_getAnnotation(provider, Service.class, null);
+
+        replay();
+
+        ObjectProvider objectProvider = new ServiceAnnotationObjectProvider();
+
+        assertNull(objectProvider.provide(objectType, provider, locator));
+
+        verify();
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void annotation_present()
+    {
+        Class objectType = Runnable.class;
+        AnnotationProvider provider = mockAnnotationProvider();
+        ObjectLocator locator = mockObjectLocator();
+        Service service = newMock(Service.class);
+        String serviceId = "JiffyPop";
+        Runnable instance = mockRunnable();
+
+        train_getAnnotation(provider, Service.class, service);
+
+        expect(service.value()).andReturn(serviceId);
+
+        train_getService(locator, serviceId, objectType, instance);
+
+        replay();
+
+        ObjectProvider objectProvider = new ServiceAnnotationObjectProvider();
+
+        assertSame(objectProvider.provide(objectType, provider, locator), 
instance);
+
+        verify();
+    }
+}


Reply via email to