Author: drobiazko
Date: Wed Jul  2 15:57:29 2008
New Revision: 673536

URL: http://svn.apache.org/viewvc?rev=673536&view=rev
Log:
TAPESTRY-1810: ServiceBinder.bind() with an interface should check to see if 
there's an Impl class and bind to that.

Added:
    
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionFailureModule.java
    
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionModule.java
    
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionModuleImplementationNotFound.java
    
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/PingableImpl.java
Modified:
    
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/IOCMessages.java
    
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java
    
tapestry/tapestry5/trunk/tapestry-ioc/src/main/resources/org/apache/tapestry5/ioc/internal/IOCStrings.properties
    tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/service.apt
    
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/IntegrationTest.java

Modified: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/IOCMessages.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/IOCMessages.java?rev=673536&r1=673535&r2=673536&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/IOCMessages.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/IOCMessages.java
 Wed Jul  2 15:57:29 2008
@@ -306,4 +306,9 @@
     {
         return MESSAGES.format("no-proxy-provider", serviceId);
     }
+
+    static String noConventionServiceImplementationFound(Class clazz)
+    {
+        return MESSAGES.format("no-convention-service-implementation-found", 
clazz.getName(), clazz.getName());
+    }
 }

Modified: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java?rev=673536&r1=673535&r2=673536&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ServiceBinderImpl.java
 Wed Jul  2 15:57:29 2008
@@ -112,9 +112,26 @@
         return result;
     }
 
-    public <T> ServiceBindingOptions bind(Class<T> implementationClass)
+    public <T> ServiceBindingOptions bind(Class<T> serviceClass)
     {
-        return bind(implementationClass, implementationClass);
+        if(serviceClass.isInterface())
+        {
+            try
+            {
+                Class<T> implementationClass = (Class<T>) 
Class.forName(serviceClass.getName()+"Impl");
+                
+                if(!implementationClass.isInterface() && 
serviceClass.isAssignableFrom(implementationClass))
+                {
+                    return bind(serviceClass, implementationClass);
+                }
+                throw new 
RuntimeException(IOCMessages.noServiceMatchesType(serviceClass));
+            }
+            catch (ClassNotFoundException ex)
+            {
+                throw new 
RuntimeException(IOCMessages.noConventionServiceImplementationFound(serviceClass));
+            }
+        }
+        return bind(serviceClass, serviceClass);
     }
 
     public <T> ServiceBindingOptions bind(Class<T> serviceInterface, Class<? 
extends T> serviceImplementation)

Modified: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/resources/org/apache/tapestry5/ioc/internal/IOCStrings.properties
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/resources/org/apache/tapestry5/ioc/internal/IOCStrings.properties?rev=673536&r1=673535&r2=673536&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/resources/org/apache/tapestry5/ioc/internal/IOCStrings.properties
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/resources/org/apache/tapestry5/ioc/internal/IOCStrings.properties
 Wed Jul  2 15:57:29 2008
@@ -87,3 +87,4 @@
 overlapping-service-proxy-providers=Setting a new service proxy provider when 
there's already an existing provider. This may indicate that you have multiple 
IoC Registries.
 unexpected-service-proxy-provider=Unexpected service proxy provider when 
clearing the provider. This may indicate that you have multiple IoC Registries.
 no-proxy-provider=Service token for service '%s' can not be converted back 
into a proxy because no proxy provider has been registered. This may indicate 
that an IoC Registry has not been started yet.
+no-convention-service-implementation-found=No service implements the interface 
%s. Please provide the implementation %sImpl or bind the service interface to a 
service implementation.

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/service.apt
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/service.apt?rev=673536&r1=673535&r2=673536&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/service.apt (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/site/apt/service.apt Wed Jul  2 
15:57:29 2008
@@ -107,6 +107,25 @@
 
   In terms of the evolution of the framework, service builder methods came 
first, and autobuilding was a later
   addition, inspired by the terseness of the 
{{{http://code.google.com/p/google-guice/}Guice}} IoC container.
+
+
+  Following the convention over configuration principle the autobuilding of 
services can be done even less verbose.
+  If a service interface is passed as a single argument to the bind() method 
Tapestry will try to find an implementation in the
+  same package whose name matches the name of the service interface followed 
by the suffix <Impl>.
+  
++------+
+package org.example.myapp.services;
+
+import org.apache.tapestry5.ioc.ServiceBinder;
+
+public class MyAppModule
+{
+  public static void bind(ServiceBinder binder)
+  {
+    binder.bind(Indexer.class);
+  }
+}
++----+
     
 Service Ids
 

Added: 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionFailureModule.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionFailureModule.java?rev=673536&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionFailureModule.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionFailureModule.java
 Wed Jul  2 15:57:29 2008
@@ -0,0 +1,9 @@
+package org.apache.tapestry5.ioc;
+
+public class ConventionFailureModule
+{
+    public static void bind(ServiceBinder binder)
+    {
+        binder.bind(Pingable.class);
+    }
+}

Added: 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionModule.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionModule.java?rev=673536&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionModule.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionModule.java
 Wed Jul  2 15:57:29 2008
@@ -0,0 +1,9 @@
+package org.apache.tapestry5.ioc;
+
+public class ConventionModule
+{
+    public static void bind(ServiceBinder binder)
+    {
+        binder.bind(StringHolder.class);
+    }
+}

Added: 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionModuleImplementationNotFound.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionModuleImplementationNotFound.java?rev=673536&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionModuleImplementationNotFound.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/ConventionModuleImplementationNotFound.java
 Wed Jul  2 15:57:29 2008
@@ -0,0 +1,9 @@
+package org.apache.tapestry5.ioc;
+
+public class ConventionModuleImplementationNotFound
+{
+    public static void bind(ServiceBinder binder)
+    {
+        binder.bind(StringTransformer.class);
+    }
+}

Modified: 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/IntegrationTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/IntegrationTest.java?rev=673536&r1=673535&r2=673536&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/IntegrationTest.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/IntegrationTest.java
 Wed Jul  2 15:57:29 2008
@@ -401,6 +401,47 @@
 
         r.shutdown();
     }
+    
+    @Test
+    public void convention_over_configuration_service()
+    {
+        Registry r = buildRegistry(ConventionModule.class);
+
+
+        StringHolder holder = r.getService(StringHolder.class);
+        
+        holder.setValue("Bar");
+
+        assertEquals(holder.getValue(), "Bar");
+
+        r.shutdown();
+    }
+    
+    @Test
+    public void convention_over_configuration_service_impl_not_found()
+    {
+        try
+        {
+            buildRegistry(ConventionModuleImplementationNotFound.class);
+            unreachable();
+        }catch (RuntimeException ex) {
+            assertMessageContains(ex,
+            "No service implements the interface 
"+StringTransformer.class.getName()+". Please provide");
+        }
+    }
+    
+    @Test
+    public void convention_over_configuration_service_wrong_impl_found()
+    {
+        try
+        {
+            buildRegistry(ConventionFailureModule.class);
+            unreachable();
+        }catch (RuntimeException ex) {
+            assertMessageContains(ex,
+            "No service implements the interface "+Pingable.class.getName());
+        }
+    }
 
     @Test
     public void service_builder_method_uses_autobuild()

Added: 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/PingableImpl.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/PingableImpl.java?rev=673536&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/PingableImpl.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/PingableImpl.java
 Wed Jul  2 15:57:29 2008
@@ -0,0 +1,6 @@
+package org.apache.tapestry5.ioc;
+
+public class PingableImpl
+{
+
+}


Reply via email to