hivemind-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ahue...@apache.org
Subject svn commit: r464933 - in /hivemind/branches/branch-2-0-annot: annotations/src/java/org/apache/hivemind/annotations/ annotations/src/java/org/apache/hivemind/annotations/internal/ annotations/src/test/org/apache/hivemind/annotations/ framework/src/java/...
Date Tue, 17 Oct 2006 14:33:29 GMT
Author: ahuegen
Date: Tue Oct 17 07:33:28 2006
New Revision: 464933

URL: http://svn.apache.org/viewvc?view=rev&rev=464933
Log:
Introduced autowiring service

Added:
    hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AbstractAnnotatedModule.java
    hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Module.java
    hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AutowiringModule.java
    hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/ModuleWithExplicitId.java
    hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/TestAnnotatedModuleReader.java
      - copied, changed from r453372, hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AnnotatedModuleReaderTest.java
    hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/Autowiring.java
    hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/AutowiringStrategy.java
    hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringByTypeStrategy.java
    hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringImpl.java
    hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringStrategyContribution.java
    hivemind/branches/branch-2-0-annot/framework/src/test/org/apache/hivemind/service/impl/SkippedPropertiesAutowireTarget.java
    hivemind/branches/branch-2-0-annot/framework/src/test/org/apache/hivemind/service/impl/TestAutowiring.java
Removed:
    hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AnnotatedModuleReaderTest.java
Modified:
    hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Registry.java
    hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/AnnotatedModuleProcessor.java
    hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/RegistryImpl.java
    hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/SimpleAnnotatedModule.java
    hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/HiveMind.java
    hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/impl/CoreServicesProvider.java
    hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/ServiceMessages.java
    hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/ServiceStrings.properties
    hivemind/branches/branch-2-0-annot/xml/src/java/org/apache/hivemind/service/impl/BuilderFactoryLogic.java
    hivemind/branches/branch-2-0-annot/xml/src/java/org/apache/hivemind/service/impl/BuilderPropertyFacet.java
    hivemind/branches/branch-2-0-annot/xml/src/test/org/apache/hivemind/service/TestBuilderFactory.java
    hivemind/branches/branch-2-0-annot/xml/src/test/org/apache/hivemind/service/impl/TestBuilderPropertyFacet.java

Added: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AbstractAnnotatedModule.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AbstractAnnotatedModule.java?view=auto&rev=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AbstractAnnotatedModule.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/AbstractAnnotatedModule.java Tue Oct 17 07:33:28 2006
@@ -0,0 +1,29 @@
+package org.apache.hivemind.annotations;
+
+import org.apache.hivemind.service.Autowiring;
+
+public class AbstractAnnotatedModule
+{
+    private Registry _registry;
+
+    public Registry getRegistry()
+    {
+        return _registry;
+    }
+
+    public void setRegistry(Registry registry)
+    {
+        _registry = registry;
+    }
+    
+    public Autowiring getAutowiring()
+    {
+        return _registry.getAutowiring();
+    }
+    
+    public <T> T autowireProperties(T target) 
+    {
+        return (T) getAutowiring().autowireProperties(target);
+    }
+
+}

Added: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Module.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Module.java?view=auto&rev=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Module.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Module.java Tue Oct 17 07:33:28 2006
@@ -0,0 +1,20 @@
+package org.apache.hivemind.annotations;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Marks a class as an annotated module and assigns an id 
+ * to it. 
+ * 
+ * @author Achim Huegen
+ */
+@Documented
+@Retention(value = RetentionPolicy.RUNTIME)
+@Target(value = ElementType.TYPE)
+public @interface Module {
+    String id();
+}

Modified: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Registry.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Registry.java?view=diff&rev=464933&r1=464932&r2=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Registry.java (original)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/Registry.java Tue Oct 17 07:33:28 2006
@@ -1,5 +1,7 @@
 package org.apache.hivemind.annotations;
 
+import org.apache.hivemind.service.Autowiring;
+
 /**
  * Specialized interface for the registry access from annotated modules.
  * Implements typed access to services and configurations by use of Generics. 
@@ -13,5 +15,5 @@
     public <T> T getConfiguration(String configurationId, Class<T> configurationType);
     public <T> T getConfiguration(Class<T> configurationType);
     public Object getConfiguration(String configurationId);
-    public void autowire(Object target);
+    public Autowiring getAutowiring();
 }

Modified: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/AnnotatedModuleProcessor.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/AnnotatedModuleProcessor.java?view=diff&rev=464933&r1=464932&r2=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/AnnotatedModuleProcessor.java (original)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/AnnotatedModuleProcessor.java Tue Oct 17 07:33:28 2006
@@ -12,6 +12,7 @@
 import org.apache.hivemind.Resource;
 import org.apache.hivemind.annotations.Configuration;
 import org.apache.hivemind.annotations.Contribution;
+import org.apache.hivemind.annotations.Module;
 import org.apache.hivemind.annotations.Service;
 import org.apache.hivemind.definition.ConfigurationPointDefinition;
 import org.apache.hivemind.definition.ContributionDefinition;
@@ -212,6 +213,8 @@
 
     /**
      * Determines the module id of the module defined by the annotated class.
+     * First priority has a {@link Module} annotation. If none is defined the
+     * id is determined from class and package name.
      * 
      * @param moduleClass
      *            the module class
@@ -219,7 +222,16 @@
      */
     private String determineModuleId(Class moduleClass)
     {
-        return moduleClass.getName();
+        Module moduleAnnotation = (Module) moduleClass.getAnnotation(Module.class);
+        if (moduleAnnotation != null) {
+            return moduleAnnotation.id();
+        } else {
+            return getDefaultModuleId(moduleClass);
+        }
     }
 
+    private String getDefaultModuleId(Class moduleClass)
+    {
+        return moduleClass.getName();
+    }
 }

Modified: hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/RegistryImpl.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/RegistryImpl.java?view=diff&rev=464933&r1=464932&r2=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/RegistryImpl.java (original)
+++ hivemind/branches/branch-2-0-annot/annotations/src/java/org/apache/hivemind/annotations/internal/RegistryImpl.java Tue Oct 17 07:33:28 2006
@@ -3,6 +3,7 @@
 import org.apache.hivemind.annotations.Registry;
 import org.apache.hivemind.internal.Module;
 import org.apache.hivemind.internal.RegistryInfrastructure;
+import org.apache.hivemind.service.Autowiring;
 import org.apache.hivemind.util.IdUtils;
 
 public class RegistryImpl implements Registry
@@ -17,11 +18,6 @@
         _delegate = delegate;
     }
 
-    public void autowire(Object target)
-    {
-        throw new UnsupportedOperationException();
-    }
-
     public <T> T getConfiguration(String configurationId, Class<T> configurationType)
     {
         String fullyQualifiedConfigurationId = IdUtils.qualify(
@@ -61,6 +57,11 @@
     {
         Object service = _delegate.getService(serviceInterface, _callingModule);
         return (T) service;
+    }
+
+    public Autowiring getAutowiring()
+    {
+        return getService(Autowiring.class);
     }
 
 }

Added: hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AutowiringModule.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AutowiringModule.java?view=auto&rev=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AutowiringModule.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AutowiringModule.java Tue Oct 17 07:33:28 2006
@@ -0,0 +1,21 @@
+package org.apache.hivemind.annotations;
+
+import hivemind.test.services.ServiceAutowireTarget;
+import hivemind.test.services.StringHolder;
+import hivemind.test.services.impl.StringHolderImpl;
+
+public class AutowiringModule extends AbstractAnnotatedModule
+{
+    @Service(id = "AutowireTarget")
+    public ServiceAutowireTarget getAutowireTarget()
+    {
+        ServiceAutowireTarget target = new ServiceAutowireTarget();
+        return autowireProperties(target);
+    }
+
+    @Service(id = "StringHolder")
+    public StringHolder getStringHolder()
+    {
+        return new StringHolderImpl();
+    }
+}

Added: hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/ModuleWithExplicitId.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/ModuleWithExplicitId.java?view=auto&rev=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/ModuleWithExplicitId.java (added)
+++ hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/ModuleWithExplicitId.java Tue Oct 17 07:33:28 2006
@@ -0,0 +1,6 @@
+package org.apache.hivemind.annotations;
+
+@Module(id = "Test")
+public class ModuleWithExplicitId
+{
+}

Modified: hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/SimpleAnnotatedModule.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/SimpleAnnotatedModule.java?view=diff&rev=464933&r1=464932&r2=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/SimpleAnnotatedModule.java (original)
+++ hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/SimpleAnnotatedModule.java Tue Oct 17 07:33:28 2006
@@ -8,31 +8,33 @@
 public class SimpleAnnotatedModule
 {
     private Registry _registry;
-    
-    @Service(id="Test")
+
+    @Service(id = "Test")
     public Runnable getRunnable()
     {
-        return new Runnable() {
+        return new Runnable()
+        {
 
             public void run()
             {
-                List<ServiceModelContribution> models = (List<ServiceModelContribution>) getRegistry().getConfiguration("hivemind.ServiceModels");
+                List<ServiceModelContribution> models = (List<ServiceModelContribution>) getRegistry()
+                        .getConfiguration("hivemind.ServiceModels");
                 for (ServiceModelContribution model : models)
                 {
                     System.out.println(model.getName());
                 }
-                
+
                 List<String> demoList = (List<String>) getRegistry().getConfiguration("Demo", List.class);
                 for (String entry : demoList)
                 {
                     System.out.println(entry);
                 }
-                
+
             }
         };
     }
-    
-    @Configuration(id="Demo")
+
+    @Configuration(id = "Demo")
     public List<String> getDemo()
     {
         List<String> result = new ArrayList<String>();
@@ -40,7 +42,7 @@
         return result;
     }
 
-    @Contribution(configurationId="Demo")
+    @Contribution(configurationId = "Demo")
     public void contributeData(List<String> container)
     {
         container.add("contributed-data");

Copied: hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/TestAnnotatedModuleReader.java (from r453372, hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AnnotatedModuleReaderTest.java)
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/TestAnnotatedModuleReader.java?view=diff&rev=464933&p1=hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AnnotatedModuleReaderTest.java&r1=453372&p2=hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/TestAnnotatedModuleReader.java&r2=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/AnnotatedModuleReaderTest.java (original)
+++ hivemind/branches/branch-2-0-annot/annotations/src/test/org/apache/hivemind/annotations/TestAnnotatedModuleReader.java Tue Oct 17 07:33:28 2006
@@ -1,29 +1,46 @@
 package org.apache.hivemind.annotations;
 
+import hivemind.test.services.ServiceAutowireTarget;
+
 import java.util.Locale;
 
 import org.apache.hivemind.ClassResolver;
+import org.apache.hivemind.ErrorHandler;
 import org.apache.hivemind.Registry;
+import org.apache.hivemind.definition.RegistryDefinition;
+import org.apache.hivemind.impl.DefaultErrorHandler;
 import org.apache.hivemind.impl.RegistryBuilder;
 import org.apache.hivemind.test.HiveMindTestCase;
 
-public class AnnotatedModuleReaderTest extends HiveMindTestCase
+public class TestAnnotatedModuleReader extends HiveMindTestCase
 {
     protected Registry buildRegistry(String[] moduleClassNames)
     {
+        RegistryDefinition definition = buildRegistryDefinition(moduleClassNames);
+        
+        RegistryBuilder builder = new RegistryBuilder(definition);
+//        builder.
+//        builder.autoDetectModules();
+
+        return builder.constructRegistry(Locale.getDefault());
+    }
+    
+    protected RegistryDefinition buildRegistryDefinition(String[] moduleClassNames)
+    {
         ClassResolver resolver = getClassResolver();
         
-        RegistryBuilder builder = new RegistryBuilder();
-        builder.autoDetectModules();
+        ErrorHandler errorHandler = new DefaultErrorHandler();
+        
+        RegistryDefinition definition = new RegistryDefinition();
 
         for (int i = 0; i < moduleClassNames.length; i++)
         {
-            AnnotatedModuleReader reader = new AnnotatedModuleReader(builder.getRegistryDefinition(),
-                    resolver, builder.getErrorHandler());
+            AnnotatedModuleReader reader = new AnnotatedModuleReader(definition,
+                    resolver, errorHandler);
             reader.readModule(moduleClassNames[i]);
         }
 
-        return builder.constructRegistry(Locale.getDefault());
+        return definition;
     }
     
     public void testSimpleModule()
@@ -33,4 +50,17 @@
         service.run();
     }
 
+    public void testModuleId()
+    {
+        RegistryDefinition registry = buildRegistryDefinition((new String[] {ModuleWithExplicitId.class.getName()}));
+        assertNotNull(registry.getModule("Test"));
+    }
+    
+    public void testAutowiring()
+    {
+        Registry registry = buildRegistry(new String[] {AutowiringModule.class.getName()});
+        ServiceAutowireTarget service = (ServiceAutowireTarget) registry.getService(ServiceAutowireTarget.class);
+        assertNotNull(service.getStringHolder());
+    }
+    
 }

Modified: hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/HiveMind.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/HiveMind.java?view=diff&rev=464933&r1=464932&r2=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/HiveMind.java (original)
+++ hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/HiveMind.java Tue Oct 17 07:33:28 2006
@@ -46,6 +46,14 @@
      */
 
     public static final String INTERFACE_SYNTHESIZER_SERVICE = "hivemind.InterfaceSynthesizer";
+    
+    /**
+     * The full id of the {@link org.apache.hivemind.service.Autowiring} service.
+     * 
+     * @since 2.0
+     */
+
+    public static final String AUTOWIRING_SERVICE = "hivemind.Autowiring";
 
     /**
      * An object used to synchronize access to {@link java.beans.Introspector} (which is not fully

Modified: hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/impl/CoreServicesProvider.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/impl/CoreServicesProvider.java?view=diff&rev=464933&r1=464932&r2=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/impl/CoreServicesProvider.java (original)
+++ hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/impl/CoreServicesProvider.java Tue Oct 17 07:33:28 2006
@@ -28,11 +28,16 @@
 import org.apache.hivemind.impl.servicemodel.ThreadedServiceModelFactory;
 import org.apache.hivemind.internal.AbstractServiceImplementationConstructor;
 import org.apache.hivemind.internal.ServiceModel;
+import org.apache.hivemind.service.Autowiring;
+import org.apache.hivemind.service.AutowiringStrategy;
 import org.apache.hivemind.service.ClassFactory;
 import org.apache.hivemind.service.InterfaceSynthesizer;
 import org.apache.hivemind.service.ThreadEventNotifier;
 import org.apache.hivemind.service.ThreadLocalStorage;
 import org.apache.hivemind.service.ThreadLocale;
+import org.apache.hivemind.service.impl.AutowiringByTypeStrategy;
+import org.apache.hivemind.service.impl.AutowiringImpl;
+import org.apache.hivemind.service.impl.AutowiringStrategyContribution;
 import org.apache.hivemind.service.impl.ClassFactoryImpl;
 import org.apache.hivemind.service.impl.DefaultsSymbolSource;
 import org.apache.hivemind.service.impl.EagerLoader;
@@ -95,6 +100,10 @@
         addInterfaceSynthesizer(md);
         
         addServiceModelConfiguration();
+        
+        addAutowiring(md);
+        
+        addAutowiringStrategiesConfiguration();
     }
 
     /**
@@ -358,4 +367,55 @@
 
         helper.addServiceImplementation(spd, constructor, ServiceModel.SINGLETON);
     }
+    
+    /**
+      * Service that wires properties of object with services defined in the registry.  
+      */
+    private void addAutowiring(ModuleDefinition md)
+    {
+        ServicePointDefinition spd = helper.addServicePoint("Autowiring", Autowiring.class.getName());
+
+        // Define inline implementation constructor, that wires the AutowiringStrategies configuration
+        ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(md.getLocation())
+        {
+            public Object constructCoreServiceImplementation(ImplementationConstructionContext context)
+            {
+                List strategies = (List) context.getConfiguration("AutowiringStrategies");
+                Autowiring result = new AutowiringImpl(context.getRegistry(), strategies, context.getDefiningModule().getErrorHandler());
+                return result;
+            }
+        };
+        helper.addServiceImplementation(spd, constructor, ServiceModel.PRIMITIVE);
+
+    }
+    
+    /**
+     * Defines service models, providing a name and a class for each.
+     */
+    private void addAutowiringStrategiesConfiguration()
+    {
+
+        ConfigurationPointDefinition cpd = helper.addConfigurationPoint("AutowiringStrategies", 
+                new CreateClassConfigurationConstructor(helper.getModule().getLocation(), ArrayList.class.getName()), 
+                List.class.getName());
+        
+        final List serviceModels = getDefaultAutowiringStrategies();
+        helper.addContributionDefinition(cpd, new ContributionConstructor() {
+
+            public void contribute(ContributionConstructionContext context, Object container)
+            {
+                ((List) container).addAll(serviceModels);
+            }});
+    }
+
+    /**
+     * Returns default service Models as instances of {@link AutowiringStrategyContribution}.
+     */
+    private List getDefaultAutowiringStrategies()
+    {
+        List result = new ArrayList();
+        result.add(new AutowiringStrategyContribution(new AutowiringByTypeStrategy(), AutowiringStrategy.BY_TYPE, null, null));
+        return result;
+    }
+
  }

Added: hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/Autowiring.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/Autowiring.java?view=auto&rev=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/Autowiring.java (added)
+++ hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/Autowiring.java Tue Oct 17 07:33:28 2006
@@ -0,0 +1,49 @@
+package org.apache.hivemind.service;
+
+/**
+ * Service that wires properties of object with services defined in the registry.
+ * Different strategies are available. The standard strategies are defined
+ * in {@link AutowiringStrategy}.
+ * 
+ * @author Achim Huegen
+ */
+public interface Autowiring
+{
+    /**
+     * Autowires the properties of <code>target</code> defined in <code>propertyNames</code>.
+     * All available strategies are tried until one strategy succeeds. 
+     * @param target  the target object whose properties should be wired
+     * @param propertyNames  the properties to wire
+     * @return the wired target object
+     */
+    public Object autowireProperties(Object target, String[] propertyNames);
+    
+    /**
+     * Autowires all writable properties of <code>target</code>.
+     * All available strategies are tried until one strategy succeeds. 
+     * @param target  the target object whose properties should be wired
+     * @return the wired target object
+     */
+    public Object autowireProperties(Object target);
+    
+    /**
+     * Autowires the properties of <code>target</code> defined in <code>propertyNames</code>
+     * using a certain strategy.
+     * @param strategy  name of the strategy to be used. Standard strategies are defined
+     *                  in {@link AutowiringStrategy}
+     * @param target  the target object whose properties should be wired
+     * @param propertyNames  the properties to wire
+     * @return the wired target object
+     */
+    public Object autowireProperties(String strategy, Object target, String[] propertyNames);
+    
+    /**
+     * Autowires all writable properties of <code>target</code> using a certain strategy.
+     * @param strategy  name of the strategy to be used. Standard strategies are defined
+     *                  in {@link AutowiringStrategy}
+     * @param target  the target object whose properties should be wired
+     * @return the wired target object
+     */
+    public Object autowireProperties(String strategy, Object target);
+
+}

Added: hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/AutowiringStrategy.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/AutowiringStrategy.java?view=auto&rev=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/AutowiringStrategy.java (added)
+++ hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/AutowiringStrategy.java Tue Oct 17 07:33:28 2006
@@ -0,0 +1,27 @@
+package org.apache.hivemind.service;
+
+import org.apache.hivemind.internal.RegistryInfrastructure;
+
+/**
+ * Wires a single property of an object with a service from the hivemind registry. 
+ * The implementations of this interface can use different strategies for 
+ * finding a matching service. 
+ * 
+ * @author Achim Huegen
+ */
+public interface AutowiringStrategy
+{
+    /**
+     * Strategy that searches for services by the type of a property.
+     */
+    public final static String BY_TYPE = "ByType";
+
+    /**
+     * Autowire a single property. 
+     * @param registry  registry for lookup of services
+     * @param target  the target object
+     * @param propertyName  name of the property
+     * @return  true if the wiring has succeeded
+     */
+    public boolean autowireProperty(RegistryInfrastructure registry, Object target, String propertyName);
+}

Added: hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringByTypeStrategy.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringByTypeStrategy.java?view=auto&rev=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringByTypeStrategy.java (added)
+++ hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringByTypeStrategy.java Tue Oct 17 07:33:28 2006
@@ -0,0 +1,39 @@
+package org.apache.hivemind.service.impl;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hivemind.internal.RegistryInfrastructure;
+import org.apache.hivemind.service.AutowiringStrategy;
+import org.apache.hivemind.util.PropertyUtils;
+
+/**
+ * Implementation of {@link AutowiringStrategy} that searches for
+ * a matching service by the type of a property. 
+ * 
+ * @author Achim Huegen
+ */
+public class AutowiringByTypeStrategy implements AutowiringStrategy
+{
+    private static final Log LOG = LogFactory.getLog(AutowiringByTypeStrategy.class);
+
+    public boolean autowireProperty(RegistryInfrastructure registry, Object target, String propertyName)
+    {
+        Class propertyType = PropertyUtils.getPropertyType(target, propertyName);
+
+        // search for a property with an interface that matches the property type
+        if (registry.containsService(propertyType, null))
+        {
+            Object collaboratingService = registry.getService(propertyType, null);
+            PropertyUtils.write(target, propertyName, collaboratingService);
+
+            if (LOG.isDebugEnabled())
+            {
+                LOG.debug("Autowired property " + propertyName + " by type to " + collaboratingService);
+            }
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+}

Added: hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringImpl.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringImpl.java?view=auto&rev=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringImpl.java (added)
+++ hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringImpl.java Tue Oct 17 07:33:28 2006
@@ -0,0 +1,193 @@
+package org.apache.hivemind.service.impl;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hivemind.ApplicationRuntimeException;
+import org.apache.hivemind.ErrorHandler;
+import org.apache.hivemind.internal.RegistryInfrastructure;
+import org.apache.hivemind.service.Autowiring;
+import org.apache.hivemind.service.AutowiringStrategy;
+import org.apache.hivemind.util.PropertyUtils;
+
+/**
+ * Implementation of {@link Autowiring}.
+ * Skips properties of primitive type and the standard java data types (String, Double etc.).
+ * Properties that are already assigned are skipped too.
+ * Delegates the autowiring to implementations of {@link AutowiringStrategy}.
+ * If errors occur they are passed to an {@link ErrorHandler}. Depending on its
+ * implementation the wiring either continues with the next property or an exception
+ * is thrown. 
+ * 
+ * @author Achim Huegen
+ */
+public class AutowiringImpl implements Autowiring
+{
+    private static final Log LOG = LogFactory.getLog(AutowiringImpl.class);
+
+    private Map _strategies = new TreeMap();
+
+    private RegistryInfrastructure _registry;
+    
+    private ErrorHandler _errorHandler;
+    
+    private static Set SKIPPED_PROPERTY_TYPES = new HashSet();
+    
+    static {
+        SKIPPED_PROPERTY_TYPES.add(String.class);
+        SKIPPED_PROPERTY_TYPES.add(Double.class);
+        SKIPPED_PROPERTY_TYPES.add(Integer.class);
+        SKIPPED_PROPERTY_TYPES.add(Float.class);
+        SKIPPED_PROPERTY_TYPES.add(Byte.class);
+        SKIPPED_PROPERTY_TYPES.add(Short.class);
+        SKIPPED_PROPERTY_TYPES.add(Character.class);
+    }
+    
+    /**
+     * @param context
+     * @param strategyContributions  list with instances of {@link AutowiringStrategyContribution}
+     */
+    public AutowiringImpl(RegistryInfrastructure registry, List strategyContributions, ErrorHandler errorHandler)
+    {
+        _registry = registry;
+        _errorHandler = errorHandler;
+        // Add the strategies in default order to map
+        for (Iterator iter = strategyContributions.iterator(); iter.hasNext();)
+        {
+            AutowiringStrategyContribution c = (AutowiringStrategyContribution) iter.next();
+            _strategies.put(c.getName(), c.getStrategy());
+        }
+    }
+
+    /**
+     * @see org.apache.hivemind.service.Autowiring#autowireProperties(java.lang.Object)
+     */
+    public Object autowireProperties(Object target)
+    {
+        Set writeablePropertiesSet = new HashSet(PropertyUtils.getWriteableProperties(target));
+        String[] writableProperties = (String[]) writeablePropertiesSet.toArray(new String[writeablePropertiesSet.size()]);
+        return autowireProperties(target, writableProperties);
+    }
+    
+    /**
+     * @see org.apache.hivemind.service.Autowiring#autowireProperties(java.lang.Object, java.lang.String[])
+     */
+    public Object autowireProperties(Object target, String[] propertyNames)
+    {
+        for (int i = 0; i < propertyNames.length; i++)
+        {
+            String propertyName = propertyNames[i];
+            if (isPropertyWirable(target, propertyName)) {
+                autowirePropertyAllStrategies(target, propertyName);
+            }
+        }
+        return target;
+    }
+
+    /**
+     * Wires a single property by calling all available strategies in the configured order
+     * until a strategy signals that the property has been wired. 
+     * 
+     * @param target
+     * @param propertyName
+     */
+    private void autowirePropertyAllStrategies(Object target, String propertyName)
+    {
+        try
+        {
+            for (Iterator iter = _strategies.values().iterator(); iter.hasNext();)
+            {
+                AutowiringStrategy strategy = (AutowiringStrategy) iter.next();
+                boolean isWired = strategy.autowireProperty(_registry, target, propertyName);
+                // Stop if strategy has wired the property 
+                if (isWired)
+                    break;
+            }
+        }
+        catch (Exception ex)
+        {
+            _errorHandler.error(
+                    LOG,
+                    ServiceMessages.autowirePropertyFailure(propertyName, target.getClass(), ex),
+                    null,
+                    ex);
+        }
+    }
+    
+    /**
+     * @see org.apache.hivemind.service.Autowiring#autowireProperties(java.lang.String, java.lang.Object)
+     */
+    public Object autowireProperties(String strategy, Object target)
+    {
+        Set writeablePropertiesSet = new HashSet(PropertyUtils.getWriteableProperties(target));
+        String[] writableProperties = (String[]) writeablePropertiesSet.toArray(new String[writeablePropertiesSet.size()]);
+        return autowireProperties(strategy, target, writableProperties);
+    }
+
+    /**
+     * @see org.apache.hivemind.service.Autowiring#autowireProperties(java.lang.String, java.lang.Object, java.lang.String[])
+     */
+    public Object autowireProperties(String strategyName, Object target, String[] propertyNames)
+    {
+        for (int i = 0; i < propertyNames.length; i++)
+        {
+            String propertyName = propertyNames[i];
+            if (isPropertyWirable(target, propertyName)) {
+                autowireProperty(strategyName, target, propertyName);
+            }
+        }
+        return target;
+    }
+    
+    /**
+     * @return  true if the property is wirable. Primitive types and the types in
+     *  SKIPPED_PROPERTY_TYPES are ignored. If the property is already assigned it is
+     *  ignored too.
+     */
+    private boolean isPropertyWirable(Object target, String propertyName)
+    {
+        Class propertyType = PropertyUtils.getPropertyType(target, propertyName);
+        if (propertyType.isPrimitive() || SKIPPED_PROPERTY_TYPES.contains(propertyType)) {
+            return false;
+        } else {
+            Object value = PropertyUtils.read(target, propertyName);
+            // Don't wire if value is already assigned
+            return value == null;
+        }
+    }
+
+    /**
+     * Wires a single property by using the strategy <code>strategyName</code> 
+     * @param strategyName
+     * @param target
+     * @param propertyName
+     * @return true if wiring succeeded
+     */
+    private boolean autowireProperty(String strategyName, Object target, String propertyName)
+    {
+        try
+        {
+           AutowiringStrategy strategy = (AutowiringStrategy) _strategies.get(strategyName);
+           if (strategy == null) {
+               throw new ApplicationRuntimeException(ServiceMessages.unknownStrategy(strategyName));
+           }
+           return strategy.autowireProperty(_registry, target, propertyName);
+        }
+        catch (Exception ex)
+        {
+            _errorHandler.error(
+                    LOG,
+                    ServiceMessages.autowirePropertyFailure(propertyName, target.getClass(), ex),
+                    null,
+                    ex);
+        }
+        return false;
+    }
+
+}

Added: hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringStrategyContribution.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringStrategyContribution.java?view=auto&rev=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringStrategyContribution.java (added)
+++ hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/AutowiringStrategyContribution.java Tue Oct 17 07:33:28 2006
@@ -0,0 +1,88 @@
+// Copyright 2004, 2005 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.hivemind.service.impl;
+
+import org.apache.hivemind.Orderable;
+import org.apache.hivemind.service.AutowiringStrategy;
+
+/**
+ * Contribution to the <code>org.apache.hivemind.service.Autowiring</code>
+ * configuration extension point; used to provide
+ * a {@link org.apache.hivemind.service.AutowiringStrategy} implementation
+ * and advice on ordering the service.
+ *
+ * @author Achim Huegen
+ */
+public class AutowiringStrategyContribution implements Orderable
+{
+    private String _name;
+    private String _precedingNames;
+    private String _followingNames;
+
+    private AutowiringStrategy _strategy;
+
+    public AutowiringStrategyContribution()
+    {
+    }
+    
+    public AutowiringStrategyContribution(AutowiringStrategy strategy, String name, String precedingNames, String followingNames)
+    {
+        _strategy = strategy;
+        _name = name;
+        _precedingNames = precedingNames;
+        _followingNames = followingNames;
+    }
+
+    public AutowiringStrategy getStrategy()
+    {
+        return _strategy;
+    }
+
+    public void setStrategy(AutowiringStrategy strategy)
+    {
+        _strategy = strategy;
+    }
+
+    public String getFollowingNames()
+    {
+        return _followingNames;
+    }
+
+    public String getName()
+    {
+        return _name;
+    }
+
+    public String getPrecedingNames()
+    {
+        return _precedingNames;
+    }
+
+    public void setFollowingNames(String string)
+    {
+        _followingNames = string;
+    }
+
+    public void setName(String string)
+    {
+        _name = string;
+    }
+
+    public void setPrecedingNames(String string)
+    {
+        _precedingNames = string;
+    }
+
+}

Modified: hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/ServiceMessages.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/ServiceMessages.java?view=diff&rev=464933&r1=464932&r2=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/ServiceMessages.java (original)
+++ hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/ServiceMessages.java Tue Oct 17 07:33:28 2006
@@ -148,9 +148,9 @@
         return _formatter.format("failure-building-service", serviceId, cause);
     }
 
-    static String autowirePropertyFailure(String propertyName, String serviceId, Throwable cause)
+    static String autowirePropertyFailure(String propertyName, Class targetClass, Throwable cause)
     {
-        return _formatter.format("autowire-property-failure", propertyName, serviceId, cause);
+        return _formatter.format("autowire-property-failure", propertyName, targetClass.getName(), cause);
     }
 
     static String unableToFindAutowireConstructor()
@@ -169,5 +169,10 @@
     static String threadCleanupException(Throwable cause)
     {
         return _formatter.format("thread-cleanup-exception", cause);
+    }
+
+    static String unknownStrategy(String strategyName)
+    {
+        return _formatter.format("unknown-strategy", strategyName);
     }
 }

Modified: hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/ServiceStrings.properties
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/ServiceStrings.properties?view=diff&rev=464933&r1=464932&r2=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/ServiceStrings.properties (original)
+++ hivemind/branches/branch-2-0-annot/framework/src/java/org/apache/hivemind/service/impl/ServiceStrings.properties Tue Oct 17 07:33:28 2006
@@ -34,6 +34,7 @@
 duplicate-provider-prefix=Object provider prefix ''{0}'' duplicates a previous prefix at {1}.
 invalid-service-property-locator=''{0}'' is not a valid locator for use with the service-property object provider. It should be the id of a service, a comma, and the name of a property provided by that service.
 failure-building-service=Error building service {0}: {1}
-autowire-property-failure=Unable to autowire property {0} of service {1}: {2}
+autowire-property-failure=Unable to autowire property {0} of class {1}: {2}
 unable-to-find-autowire-constructor=Unable to find constructor applicable for autowiring. Use explicit constructor parameters.
-thread-cleanup-exception=Thread cleanup exception: {0}
\ No newline at end of file
+thread-cleanup-exception=Thread cleanup exception: {0}
+unknown-strategy=Unknown strategy {0}
\ No newline at end of file

Added: hivemind/branches/branch-2-0-annot/framework/src/test/org/apache/hivemind/service/impl/SkippedPropertiesAutowireTarget.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/framework/src/test/org/apache/hivemind/service/impl/SkippedPropertiesAutowireTarget.java?view=auto&rev=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/framework/src/test/org/apache/hivemind/service/impl/SkippedPropertiesAutowireTarget.java (added)
+++ hivemind/branches/branch-2-0-annot/framework/src/test/org/apache/hivemind/service/impl/SkippedPropertiesAutowireTarget.java Tue Oct 17 07:33:28 2006
@@ -0,0 +1,172 @@
+package org.apache.hivemind.service.impl;
+
+public class SkippedPropertiesAutowireTarget
+{
+    private int _int;
+
+    private double _double;
+
+    private float _float;
+
+    private boolean _boolean;
+
+    private byte _byte;
+
+    private char _char;
+
+    private short _short;
+
+    private String _string;
+
+    private Integer _integerObject;
+
+    private Double _doubleObject;
+
+    private Float _floatObject;
+
+    private Byte _byteObject;
+
+    private Short _shortObject;
+
+    private Character _charObject;
+
+    public boolean isBoolean()
+    {
+        return _boolean;
+    }
+
+    public void setBoolean(boolean b)
+    {
+        _boolean = b;
+    }
+
+    public byte getByte()
+    {
+        return _byte;
+    }
+
+    public void setByte(byte b)
+    {
+        _byte = b;
+    }
+
+    public Byte getByteObject()
+    {
+        return _byteObject;
+    }
+
+    public void setByteObject(Byte byteObject)
+    {
+        _byteObject = byteObject;
+    }
+
+    public char getChar()
+    {
+        return _char;
+    }
+
+    public void setChar(char c)
+    {
+        _char = c;
+    }
+
+    public Character getCharObject()
+    {
+        return _charObject;
+    }
+
+    public void setCharObject(Character charObject)
+    {
+        _charObject = charObject;
+    }
+
+    public double getDouble()
+    {
+        return _double;
+    }
+
+    public void setDouble(double d)
+    {
+        _double = d;
+    }
+
+    public Double getDoubleObject()
+    {
+        return _doubleObject;
+    }
+
+    public void setDoubleObject(Double doubleObject)
+    {
+        _doubleObject = doubleObject;
+    }
+
+    public float getFloat()
+    {
+        return _float;
+    }
+
+    public void setFloat(float f)
+    {
+        _float = f;
+    }
+
+    public Float getFloatObject()
+    {
+        return _floatObject;
+    }
+
+    public void setFloatObject(Float floatObject)
+    {
+        _floatObject = floatObject;
+    }
+
+    public int getInt()
+    {
+        return _int;
+    }
+
+    public void setInt(int i)
+    {
+        _int = i;
+    }
+
+    public Integer getIntegerObject()
+    {
+        return _integerObject;
+    }
+
+    public void setIntegerObject(Integer integerObject)
+    {
+        _integerObject = integerObject;
+    }
+
+    public short getShort()
+    {
+        return _short;
+    }
+
+    public void setShort(short s)
+    {
+        _short = s;
+    }
+
+    public Short getShortObject()
+    {
+        return _shortObject;
+    }
+
+    public void setShortObject(Short shortObject)
+    {
+        _shortObject = shortObject;
+    }
+
+    public String getString()
+    {
+        return _string;
+    }
+
+    public void setString(String string)
+    {
+        _string = string;
+    }
+}

Added: hivemind/branches/branch-2-0-annot/framework/src/test/org/apache/hivemind/service/impl/TestAutowiring.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/framework/src/test/org/apache/hivemind/service/impl/TestAutowiring.java?view=auto&rev=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/framework/src/test/org/apache/hivemind/service/impl/TestAutowiring.java (added)
+++ hivemind/branches/branch-2-0-annot/framework/src/test/org/apache/hivemind/service/impl/TestAutowiring.java Tue Oct 17 07:33:28 2006
@@ -0,0 +1,170 @@
+package org.apache.hivemind.service.impl;
+
+import hivemind.test.services.ServiceAutowireTarget;
+import hivemind.test.services.StringHolder;
+import hivemind.test.services.impl.StringHolderImpl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.hivemind.impl.DefaultErrorHandler;
+import org.apache.hivemind.internal.RegistryInfrastructure;
+import org.apache.hivemind.service.Autowiring;
+import org.apache.hivemind.service.AutowiringStrategy;
+import org.apache.hivemind.test.HiveMindTestCase;
+import org.easymock.MockControl;
+
+/**
+ * Tests related to {@link Autowiring} and {@link AutowiringStrategy}.
+ * 
+ * @author Achim Huegen
+ */
+public class TestAutowiring extends HiveMindTestCase
+{
+    /**
+     * Ensures that all configured strategies are called in the configured order
+     * until one signals successful wiring.
+     */
+    public void testStrategies()
+    {
+        // Configure an instance of AutowiringImpl with three strategies 
+        MockControl strategy1Control = newControl(AutowiringStrategy.class);
+        AutowiringStrategy strategy1 = (AutowiringStrategy) strategy1Control.getMock();
+        
+        // This strategy is expected to be called and wire successfully
+        AutowiringStrategy strategy2 = new AutowiringByTypeStrategy();
+
+        // This strategy is not expected to be called since the previous one is successful
+        MockControl strategy3Control = newControl(AutowiringStrategy.class);
+        AutowiringStrategy strategy3 = (AutowiringStrategy) strategy3Control.getMock();
+        
+        List strategyContributions = new ArrayList();
+        AutowiringStrategyContribution contrib1 = new AutowiringStrategyContribution(strategy1,
+                "strategy1", null, null);
+        strategyContributions.add(contrib1);
+        AutowiringStrategyContribution contrib2 = new AutowiringStrategyContribution(strategy2,
+                "strategy2", null, null);
+        strategyContributions.add(contrib2);
+        AutowiringStrategyContribution contrib3 = new AutowiringStrategyContribution(strategy3,
+                "strategy3", null, null);
+        strategyContributions.add(contrib3);
+        
+        MockControl registryControl = newControl(RegistryInfrastructure.class);
+        RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock();
+        
+        ServiceAutowireTarget target = new ServiceAutowireTarget();
+        
+        // Training
+        strategy1.autowireProperty(registry, target, "stringHolder");
+        strategy1Control.setReturnValue(false);
+
+        registry.containsService(StringHolder.class, null);
+        registryControl.setReturnValue(true);
+
+        registry.getService(StringHolder.class, null);
+        registryControl.setReturnValue(new StringHolderImpl());
+
+        replayControls();
+        
+        Autowiring autowiring = new AutowiringImpl(registry, strategyContributions, new DefaultErrorHandler());
+        autowiring.autowireProperties(target, new String[] {"stringHolder"});
+
+        verifyControls();
+
+        assertNotNull(target.getStringHolder());
+    }
+    
+    /**
+     * Tests the wiring by specifying an explicit strategy 
+     */
+    public void testAutowireSingleStrategy()
+    {
+        // Configure an instance of AutowiringImpl with two strategies 
+        MockControl strategy1Control = newControl(AutowiringStrategy.class);
+        AutowiringStrategy strategy1 = (AutowiringStrategy) strategy1Control.getMock();
+        
+        MockControl strategy2Control = newControl(AutowiringStrategy.class);
+        AutowiringStrategy strategy2 = (AutowiringStrategy) strategy2Control.getMock();
+        
+        List strategyContributions = new ArrayList();
+        AutowiringStrategyContribution contrib1 = new AutowiringStrategyContribution(strategy1,
+                "strategy1", null, null);
+        strategyContributions.add(contrib1);
+        AutowiringStrategyContribution contrib2 = new AutowiringStrategyContribution(strategy2,
+                "strategy2", null, null);
+        strategyContributions.add(contrib2);
+        
+        ServiceAutowireTarget target = new ServiceAutowireTarget();
+        
+        MockControl registryControl = newControl(RegistryInfrastructure.class);
+        RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock();
+        
+        // Training
+        strategy1.autowireProperty(registry, target, "stringHolder");
+        strategy1Control.setReturnValue(false);
+        
+        replayControls();
+        
+        Autowiring autowiring = new AutowiringImpl(registry, strategyContributions, new DefaultErrorHandler());
+        autowiring.autowireProperties("strategy1", target);
+
+        verifyControls();
+
+    }
+    
+    /**
+     * Checks that it is not tried to wire primitives and strings
+     */
+    public void testSkipPrimitives()
+    {
+        MockControl strategy1Control = newControl(AutowiringStrategy.class);
+        AutowiringStrategy strategy1 = (AutowiringStrategy) strategy1Control.getMock();
+        
+        List strategyContributions = new ArrayList();
+        AutowiringStrategyContribution contrib1 = new AutowiringStrategyContribution(strategy1,
+                "strategy1", null, null);
+        strategyContributions.add(contrib1);
+
+        Object target = new SkippedPropertiesAutowireTarget();
+        
+        MockControl registryControl = newControl(RegistryInfrastructure.class);
+        RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock();
+        
+        replayControls();
+        
+        Autowiring autowiring = new AutowiringImpl(registry, strategyContributions, new DefaultErrorHandler());
+        autowiring.autowireProperties(target);
+
+        verifyControls();
+
+    }
+    
+    /**
+     * Checks that it is not tried to wire properties which are assigned already
+     */
+    public void testSkipNotNull()
+    {
+        MockControl strategy1Control = newControl(AutowiringStrategy.class);
+        AutowiringStrategy strategy1 = (AutowiringStrategy) strategy1Control.getMock();
+        
+        List strategyContributions = new ArrayList();
+        AutowiringStrategyContribution contrib1 = new AutowiringStrategyContribution(strategy1,
+                "strategy1", null, null);
+        strategyContributions.add(contrib1);
+
+        ServiceAutowireTarget target = new ServiceAutowireTarget();
+        target.setStringHolder(new StringHolderImpl());
+        
+        MockControl registryControl = newControl(RegistryInfrastructure.class);
+        RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock();
+        
+        replayControls();
+        
+        Autowiring autowiring = new AutowiringImpl(registry, strategyContributions, new DefaultErrorHandler());
+        autowiring.autowireProperties(target);
+
+        verifyControls();
+
+    }
+
+}

Modified: hivemind/branches/branch-2-0-annot/xml/src/java/org/apache/hivemind/service/impl/BuilderFactoryLogic.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/xml/src/java/org/apache/hivemind/service/impl/BuilderFactoryLogic.java?view=diff&rev=464933&r1=464932&r2=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/xml/src/java/org/apache/hivemind/service/impl/BuilderFactoryLogic.java (original)
+++ hivemind/branches/branch-2-0-annot/xml/src/java/org/apache/hivemind/service/impl/BuilderFactoryLogic.java Tue Oct 17 07:33:28 2006
@@ -34,6 +34,8 @@
 import org.apache.hivemind.Location;
 import org.apache.hivemind.ServiceImplementationFactoryParameters;
 import org.apache.hivemind.internal.Module;
+import org.apache.hivemind.service.Autowiring;
+import org.apache.hivemind.service.AutowiringStrategy;
 import org.apache.hivemind.service.EventLinker;
 import org.apache.hivemind.util.ConstructorUtils;
 import org.apache.hivemind.util.PropertyUtils;
@@ -145,7 +147,7 @@
 
         Constructor[] constructors = serviceClass.getDeclaredConstructors();
 
-        outer: for (int i = 0; i < constructors.length; i++)
+        for (int i = 0; i < constructors.length; i++)
         {
             if (!Modifier.isPublic(constructors[i].getModifiers()))
                 continue;
@@ -323,7 +325,7 @@
                 writeableProperties.remove(propertyName);
         }
 
-        if (_parameter.getAutowireServices())
+        if (_parameter.getAutowireServices() && !writeableProperties.isEmpty())
             autowireServices(service, writeableProperties);
 
     }
@@ -373,43 +375,10 @@
 
     private void autowireServices(Object service, Collection propertyNames)
     {
-        Iterator i = propertyNames.iterator();
-        while (i.hasNext())
-        {
-            String propertyName = (String) i.next();
-
-            autowireProperty(service, propertyName);
-        }
-    }
-
-    private void autowireProperty(Object service, String propertyName)
-    {
-        Class propertyType = PropertyUtils.getPropertyType(service, propertyName);
-
-        try
-        {
-            // Ignore properties for which there are no corresponding
-            // service points...
-            if( _contributingModule.containsService( propertyType ) )
-            {
-                Object collaboratingService = _contributingModule.getService(propertyType);
-                PropertyUtils.write(service, propertyName, collaboratingService);
-            
-                if (_log.isDebugEnabled())
-                {
-                    _log.debug("Autowired service property " + propertyName + " to "
-                        + collaboratingService);
-                }
-            }
-        }
-        catch (Exception ex)
-        {
-            getErrorHandler().error(
-                    _log,
-                    ServiceMessages.autowirePropertyFailure(propertyName, _serviceId, ex),
-                    _parameter.getLocation(),
-                    ex);
-        }
+        Autowiring autowiring = (Autowiring) _contributingModule.getService(HiveMind.AUTOWIRING_SERVICE, Autowiring.class);
+        
+        String[] props = (String[]) propertyNames.toArray(new String[propertyNames.size()]);
+        autowiring.autowireProperties(AutowiringStrategy.BY_TYPE, service, props);
     }
 
     private ErrorHandler getErrorHandler()

Modified: hivemind/branches/branch-2-0-annot/xml/src/java/org/apache/hivemind/service/impl/BuilderPropertyFacet.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/xml/src/java/org/apache/hivemind/service/impl/BuilderPropertyFacet.java?view=diff&rev=464933&r1=464932&r2=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/xml/src/java/org/apache/hivemind/service/impl/BuilderPropertyFacet.java (original)
+++ hivemind/branches/branch-2-0-annot/xml/src/java/org/apache/hivemind/service/impl/BuilderPropertyFacet.java Tue Oct 17 07:33:28 2006
@@ -51,7 +51,7 @@
 
         if (result == null)
         {
-            XmlRegistryNature xmlNature = (XmlRegistryNature) factoryParameters.getInvokingModule().getRegistryNature(XmlRegistryNature.class);
+            XmlRegistryNature xmlNature = (XmlRegistryNature) factoryParameters.getInvokingModule().getRegistry().getNature(XmlRegistryNature.class);
             TranslatorManager translatorManager = xmlNature.getTranslationManager();
             Translator translator = translatorManager.getTranslator(_translatorName);
 

Modified: hivemind/branches/branch-2-0-annot/xml/src/test/org/apache/hivemind/service/TestBuilderFactory.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/xml/src/test/org/apache/hivemind/service/TestBuilderFactory.java?view=diff&rev=464933&r1=464932&r2=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/xml/src/test/org/apache/hivemind/service/TestBuilderFactory.java (original)
+++ hivemind/branches/branch-2-0-annot/xml/src/test/org/apache/hivemind/service/TestBuilderFactory.java Tue Oct 17 07:33:28 2006
@@ -29,6 +29,7 @@
 import org.apache.hivemind.ClassResolver;
 import org.apache.hivemind.ErrorHandler;
 import org.apache.hivemind.ErrorLog;
+import org.apache.hivemind.HiveMind;
 import org.apache.hivemind.Messages;
 import org.apache.hivemind.Registry;
 import org.apache.hivemind.ServiceImplementationFactoryParameters;
@@ -431,12 +432,33 @@
                 "hivemind.test.services.ServiceAutowireTarget",
                 ServiceAutowireTarget.class);
 
-        StringHolder h = new StringHolderImpl();
+        final StringHolder h = new StringHolderImpl();
+        
+        Autowiring autowiring = new Autowiring() {
+
+            public Object autowireProperties(Object target, String[] propertyNames)
+            {
+                return null;
+            }
+
+            public Object autowireProperties(Object target)
+            {
+                return null;
+            }
+
+            public Object autowireProperties(String strategy, Object target, String[] propertyNames)
+            {
+                ((ServiceAutowireTarget) target).setStringHolder(h);
+                return target;
+            }
+
+            public Object autowireProperties(String strategy, Object target)
+            {
+                return null;
+            }};
 
-        trainContainsService(module, String.class, false);
-        trainContainsService(module, StringHolder.class, true);
-        trainGetService(module, StringHolder.class, h);
-        trainIsDebugEnabled(log);
+        module.getService(HiveMind.AUTOWIRING_SERVICE, Autowiring.class);
+        setReturnValue(module, autowiring);
 
         replayControls();
 
@@ -470,37 +492,6 @@
         setReturnValue(module, containsService);
     }
 
-    public void testAutowireServicesFailure()
-    {
-        ServiceImplementationFactoryParameters fp = newParameters();
-        Module module = newModule();
-        Log log = newLog();
-
-        trainGetLog(fp, log);
-        trainGetServiceId(fp, "foo.bar");
-        trainGetInvokingModule(fp, module);
-        trainResolveType(
-                module,
-                "hivemind.test.services.ServiceAutowireTarget",
-                ServiceAutowireTarget.class);
-
-        trainContainsService(module, String.class, false);
-        trainContainsService(module, StringHolder.class, false);
-
-        replayControls();
-
-        BuilderParameter parameter = new BuilderParameter();
-
-        parameter.setClassName(ServiceAutowireTarget.class.getName());
-        parameter.setAutowireServices(true);
-
-        ServiceAutowireTarget service = (ServiceAutowireTarget) execute(fp, parameter);
-
-        assertNull(service.getStringHolder());
-
-        verifyControls();
-    }
-
     public void testAutowireConstructor() throws Exception
     {
         ServiceImplementationFactoryParameters fp = newParameters();
@@ -526,7 +517,7 @@
         trainGetService(module, StringHolder.class, h);
 
         trainGetClassResolver(module, getClassResolver());
-
+        
         replayControls();
 
         BuilderParameter parameter = new BuilderParameter();

Modified: hivemind/branches/branch-2-0-annot/xml/src/test/org/apache/hivemind/service/impl/TestBuilderPropertyFacet.java
URL: http://svn.apache.org/viewvc/hivemind/branches/branch-2-0-annot/xml/src/test/org/apache/hivemind/service/impl/TestBuilderPropertyFacet.java?view=diff&rev=464933&r1=464932&r2=464933
==============================================================================
--- hivemind/branches/branch-2-0-annot/xml/src/test/org/apache/hivemind/service/impl/TestBuilderPropertyFacet.java (original)
+++ hivemind/branches/branch-2-0-annot/xml/src/test/org/apache/hivemind/service/impl/TestBuilderPropertyFacet.java Tue Oct 17 07:33:28 2006
@@ -20,6 +20,7 @@
 import org.apache.hivemind.impl.DefaultErrorHandler;
 import org.apache.hivemind.impl.natures.XmlRegistryNature;
 import org.apache.hivemind.internal.Module;
+import org.apache.hivemind.internal.RegistryInfrastructure;
 import org.apache.hivemind.schema.Translator;
 import org.apache.hivemind.test.HiveMindTestCase;
 import org.easymock.MockControl;
@@ -30,6 +31,9 @@
     {
         MockControl moduleControl = newControl(Module.class);
         Module module = (Module) moduleControl.getMock();
+        
+        MockControl registryControl = newControl(RegistryInfrastructure.class);
+        RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock();
 
         MockControl translatorControl = newControl(Translator.class);
         Translator translator = (Translator) translatorControl.getMock();
@@ -52,8 +56,11 @@
         XmlRegistryNature xmlNature = new XmlRegistryNature(new DefaultErrorHandler());
         xmlNature.setTranslationManager(tm);
 
-        module.getRegistryNature(XmlRegistryNature.class);
-        moduleControl.setReturnValue(xmlNature);
+        module.getRegistry();
+        moduleControl.setReturnValue(registry);
+        
+        registry.getNature(XmlRegistryNature.class);
+        registryControl.setReturnValue(xmlNature);
 
         tm.getTranslator("foo");
         tmControl.setReturnValue(translator);
@@ -73,6 +80,9 @@
     {
         MockControl moduleControl = newControl(Module.class);
         Module module = (Module) moduleControl.getMock();
+        
+        MockControl registryControl = newControl(RegistryInfrastructure.class);
+        RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock();
 
         MockControl translatorControl = newControl(Translator.class);
         Translator translator = (Translator) translatorControl.getMock();
@@ -95,8 +105,11 @@
         XmlRegistryNature xmlNature = new XmlRegistryNature(new DefaultErrorHandler());
         xmlNature.setTranslationManager(tm);
 
-        module.getRegistryNature(XmlRegistryNature.class);
-        moduleControl.setReturnValue(xmlNature);
+        module.getRegistry();
+        moduleControl.setReturnValue(registry);
+        
+        registry.getNature(XmlRegistryNature.class);
+        registryControl.setReturnValue(xmlNature);
 
         tm.getTranslator("foo");
         tmControl.setReturnValue(translator);



Mime
View raw message