You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2006/08/07 12:37:29 UTC

svn commit: r429295 [1/2] - in /tapestry/tapestry5/tapestry-core/trunk: ./ src/main/java/org/apache/tapestry/internal/annotations/ src/main/java/org/apache/tapestry/internal/ioc/ src/main/java/org/apache/tapestry/internal/ioc/services/ src/main/java/or...

Author: hlship
Date: Mon Aug  7 03:37:27 2006
New Revision: 429295

URL: http://svn.apache.org/viewvc?rev=429295&view=rev
Log:
Add support for @Inject annotation and the ObjectProviders interface.
Create the tapestry.Infrastructure service, which provides the infrastructure: provider prefix (for use with @Inject).
Create a base TapestryModule that imports all other Tapestry sub-modules (both public and internal).
Document a few places where @Concurrent.Write and a race condition can result in duplicated (but harmless) work.

Added:
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/MasterObjectProvider.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ServiceObjectProvider.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InfrastructureImpl.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InfrastructureManagerImpl.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ObjectProvider.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/Inject.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/Infrastructure.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/InfrastructureContribution.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/InfrastructureManager.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/services/
    tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties
    tapestry/tapestry5/tapestry-core/trunk/src/site/apt/ioc/provider.apt
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/services/MasterObjectProvideTest.java
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/services/ServiceObjectProviderTest.java
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/InfrastructureImplTest.java
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/InfrastructureManagerImplTest.java
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/ServiceObjectProviderModule.java
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/StringTransformer.java
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/services/
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/services/InfrastructureContributionTest.java
Modified:
    tapestry/tapestry5/tapestry-core/trunk/pom.xml
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/annotations/SuppressNullCheck.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/IOCUtilities.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/InternalRegistry.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/RegistryImpl.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/ServiceResourcesImpl.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ChainBuilderImpl.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/DefaultImplementationBuilderImpl.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/PropertyAccessImpl.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ServiceMessages.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/pageload/ComponentTemplateSourceImpl.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/test/InternalBaseTestCase.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/IOCConstants.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceLocator.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/InjectService.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java
    tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/ioc/services/ServiceStrings.properties
    tapestry/tapestry5/tapestry-core/trunk/src/site/apt/ioc/service.apt
    tapestry/tapestry5/tapestry-core/trunk/src/site/site.xml
    tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java

Modified: tapestry/tapestry5/tapestry-core/trunk/pom.xml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/pom.xml?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/pom.xml (original)
+++ tapestry/tapestry5/tapestry-core/trunk/pom.xml Mon Aug  7 03:37:27 2006
@@ -70,8 +70,9 @@
                     <archive>
                         <manifestEntries>
                             <Tapestry-Module-Classes>
-                                org.apache.tapestry.transform.TransformModule,
-                                org.apache.tapestry.internal.InternalModule
+                                <!-- tapestry.ioc module is provided in code -->
+                                <!-- Many other modules via @SubModule -->
+                                org.apache.tapestry.services.TapestryModule
                             </Tapestry-Module-Classes>
                         </manifestEntries>
                     </archive>

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/annotations/SuppressNullCheck.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/annotations/SuppressNullCheck.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/annotations/SuppressNullCheck.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/annotations/SuppressNullCheck.java Mon Aug  7 03:37:27 2006
@@ -24,8 +24,8 @@
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
 /**
- * Marker annotation that is used to suppress the CatchNullParameters annotation. It can be applied
- * to a type (but is <em>not</em> inherited), or it an be applied to individual methods or
+ * Marker annotation that is used to suppress the CatchNullParameters aspect. It can be applied to a
+ * type (but is <em>not</em> inherited), or it an be applied to individual methods or
  * constructors.
  * 
  * @author Howard M. Lewis Ship

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/IOCUtilities.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/IOCUtilities.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/IOCUtilities.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/IOCUtilities.java Mon Aug  7 03:37:27 2006
@@ -24,6 +24,7 @@
 import org.apache.tapestry.internal.annotations.SuppressNullCheck;
 import org.apache.tapestry.internal.annotations.Utility;
 import org.apache.tapestry.ioc.ServiceLocator;
+import org.apache.tapestry.ioc.annotations.Inject;
 import org.apache.tapestry.ioc.annotations.InjectService;
 import org.apache.tapestry.util.CollectionFactory;
 
@@ -71,6 +72,15 @@
             return locator.getService(serviceId, parameterType);
         }
 
+        Inject i = findAnnotation(parameterAnnotations, Inject.class);
+
+        if (i != null)
+        {
+            String reference = i.value();
+
+            return locator.getObject(reference, parameterType);
+        }
+
         // See if we have any "pre-determined" parameter type to object mappings
 
         Object result = parameterDefaults.get(parameterType);
@@ -143,5 +153,11 @@
     public static boolean isBlank(String input)
     {
         return input == null || input.length() == 0 || input.trim().length() == 0;
+    }
+
+    @SuppressNullCheck
+    public static boolean isNonBlank(String input)
+    {
+        return !isBlank(input);
     }
 }

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/InternalRegistry.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/InternalRegistry.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/InternalRegistry.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/InternalRegistry.java Mon Aug  7 03:37:27 2006
@@ -22,6 +22,7 @@
 import org.apache.tapestry.ioc.Registry;
 import org.apache.tapestry.ioc.ServiceDecorator;
 import org.apache.tapestry.ioc.ServiceLifecycle;
+import org.apache.tapestry.ioc.ServiceLocator;
 import org.apache.tapestry.ioc.def.ServiceDef;
 import org.apache.tapestry.ioc.services.ClassFab;
 
@@ -141,4 +142,7 @@
      *            the interface to be implemented by the provided class
      */
     ClassFab newClass(Class serviceInterface);
+
+    /** Provides an object by delegating to the tapestry.ioc.MasterObjectProvider service. */
+    <T> T getObject(String reference, Class<T> objectType, ServiceLocator locator);
 }

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/RegistryImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/RegistryImpl.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/RegistryImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/RegistryImpl.java Mon Aug  7 03:37:27 2006
@@ -27,9 +27,11 @@
 import org.apache.tapestry.internal.ioc.services.ThreadCleanupHubImpl;
 import org.apache.tapestry.internal.util.Orderer;
 import org.apache.tapestry.ioc.Configuration;
+import org.apache.tapestry.ioc.IOCConstants;
 import org.apache.tapestry.ioc.IOCUtilities;
 import org.apache.tapestry.ioc.LogSource;
 import org.apache.tapestry.ioc.MappedConfiguration;
+import org.apache.tapestry.ioc.ObjectProvider;
 import org.apache.tapestry.ioc.OrderedConfiguration;
 import org.apache.tapestry.ioc.Registry;
 import org.apache.tapestry.ioc.ServiceDecorator;
@@ -485,6 +487,21 @@
     public ClassFab newClass(Class serviceInterface)
     {
         return _classFactory.newClass(serviceInterface);
+    }
+
+    public <T> T getObject(String reference, Class<T> objectType, ServiceLocator locator)
+    {
+        ObjectProvider masterProvider = getService(
+                IOCConstants.MASTER_OBJECT_PROVIDER_SERVICE_ID,
+                ObjectProvider.class);
+
+        return masterProvider.provide(reference, objectType, locator);
+    }
+
+    public <T> T getObject(String reference, Class<T> objectType)
+    {
+        // Concerened about this causing potential endless loops.
+        return getObject(reference, objectType, this);
     }
 
 }

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/ServiceResourcesImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/ServiceResourcesImpl.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/ServiceResourcesImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/ServiceResourcesImpl.java Mon Aug  7 03:37:27 2006
@@ -96,4 +96,9 @@
         return _registry.getMappedConfiguration(_serviceDef, keyType, valueType);
     }
 
+    public <T> T getObject(String reference, Class<T> objectType)
+    {
+        return _registry.getObject(reference, objectType, this);
+    }
+
 }

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ChainBuilderImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ChainBuilderImpl.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ChainBuilderImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ChainBuilderImpl.java Mon Aug  7 03:37:27 2006
@@ -72,6 +72,10 @@
     @Concurrent.Write
     private Class constructImplementationClass(Class commandInterface)
     {
+        // In rare, rare cases, a race condition to create an implementation class
+        // for the same interface may occur. We just let that happen, and there'll
+        // be two different classes corresponding to the same interface.
+
         String name = ClassFabUtils.generateClassName(commandInterface);
 
         ClassFab cf = _classFactory.newClass(name, Object.class);

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/DefaultImplementationBuilderImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/DefaultImplementationBuilderImpl.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/DefaultImplementationBuilderImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/DefaultImplementationBuilderImpl.java Mon Aug  7 03:37:27 2006
@@ -58,6 +58,11 @@
     @Concurrent.Write
     private <S> S fillCache(Class<S> serviceInterface)
     {
+        // In rare race conditions, we may end up creating two (or more)
+        // NOOP class/instance pairs for the same interface. You need multiple threads
+        // asking for a NOOP class for the same interface pretty much simulataneously.
+        // We just let this happen.
+
         Class<S> noopClass = createClass(serviceInterface);
 
         try

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/MasterObjectProvider.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/MasterObjectProvider.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/MasterObjectProvider.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/MasterObjectProvider.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,60 @@
+// Copyright 2006 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.ioc.services;
+
+import java.util.Map;
+
+import org.apache.tapestry.ioc.ObjectProvider;
+import org.apache.tapestry.ioc.ServiceLocator;
+
+/**
+ * Master implementation of {@link org.apache.tapestry.ioc.ObjectProvider} that delegates out to
+ * specific implementations of ObjectProvider. This class enapsulates the logic for extracting a
+ * provider prefix, and using it to locate a specific instance, from the service's configuration.
+ * 
+ * @author Howard M. Lewis Ship
+ */
+public class MasterObjectProvider implements ObjectProvider
+{
+    private final Map<String, ObjectProvider> _configuration;
+
+    public MasterObjectProvider(Map<String, ObjectProvider> configuration)
+    {
+        _configuration = configuration;
+    }
+
+    /**
+     * The first parameter here isn't just an expression, its the entire object reference, including
+     * the prefix (and the colon separator).
+     */
+    public <T> T provide(String reference, Class<T> objectType, ServiceLocator locator)
+    {
+        int colonx = reference.indexOf(':');
+        if (colonx < 0)
+            throw new RuntimeException(ServiceMessages.objectReferenceFormatError(reference));
+
+        String prefix = reference.substring(0, colonx);
+
+        ObjectProvider provider = _configuration.get(prefix);
+
+        if (provider == null)
+            throw new RuntimeException(ServiceMessages.unknownObjectProvider(prefix, reference));
+
+        String expression = reference.substring(colonx + 1);
+
+        return provider.provide(expression, objectType, locator);
+    }
+
+}

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/PropertyAccessImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/PropertyAccessImpl.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/PropertyAccessImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/PropertyAccessImpl.java Mon Aug  7 03:37:27 2006
@@ -75,6 +75,9 @@
     @Concurrent.Write
     private ClassPropertyAdapter buildAdapter(Class forClass)
     {
+        // In some race conditions, we may hit this method for the same class multiple times.
+        // We just let it happen, replacing the old ClassPropertyAdapter with a new one.
+
         try
         {
             BeanInfo info = Introspector.getBeanInfo(forClass);

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ServiceMessages.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ServiceMessages.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ServiceMessages.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ServiceMessages.java Mon Aug  7 03:37:27 2006
@@ -125,4 +125,15 @@
     {
         return MESSAGES.format("unmatched-service-method", sig, filterInterface.getName());
     }
+
+    static String objectReferenceFormatError(String reference)
+    {
+        return MESSAGES.format("object-reference-format-error", reference);
+    }
+
+    static String unknownObjectProvider(String prefix, String reference)
+    {
+        return MESSAGES.format("unknown-object-provider", prefix, reference);
+    }
+
 }

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ServiceObjectProvider.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ServiceObjectProvider.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ServiceObjectProvider.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/ioc/services/ServiceObjectProvider.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,38 @@
+// Copyright 2006 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.ioc.services;
+
+import org.apache.tapestry.ioc.ObjectProvider;
+import org.apache.tapestry.ioc.ServiceLocator;
+
+/**
+ * Simple implementation of {@link org.apache.tapestry.ioc.ObjectProvider} that is mapped to the
+ * "service" provider prefix.
+ * 
+ * @author Howard M. Lewis Ship
+ */
+public class ServiceObjectProvider implements ObjectProvider
+{
+    /**
+     * Interprets the expression as a local or fully qualified service id.
+     * 
+     * @see ServiceLocator#getService(String, Class)
+     */
+    public <T> T provide(String expression, Class<T> objectType, ServiceLocator locator)
+    {
+        return locator.getService(expression, objectType);
+    }
+
+}

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/pageload/ComponentTemplateSourceImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/pageload/ComponentTemplateSourceImpl.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/pageload/ComponentTemplateSourceImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/pageload/ComponentTemplateSourceImpl.java Mon Aug  7 03:37:27 2006
@@ -100,6 +100,9 @@
     @Concurrent.Write
     private void parseTemplate(Resource r)
     {
+        // In a race condition, we may parse the same template more than once. This will likely add
+        // the resource to the tracker multiple times. Not likely this will cause a big issue.
+
         _tracker.add(r.getResourceURL());
 
         ComponentTemplate template = _parser.parseTemplate(r);
@@ -116,6 +119,9 @@
         String path = componentName.replace('.', '/') + ".html";
         Resource baseResource = new ClasspathResource(_loader, path);
         Resource localized = baseResource.getLocalization(locale);
+
+        // In a race condition, we may hit this method a couple of times, and overrite previous
+        // results with identical new results.
 
         _templateResources.put(key, localized);
     }

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InfrastructureImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InfrastructureImpl.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InfrastructureImpl.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InfrastructureImpl.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,108 @@
+// Copyright 2006 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 java.util.Map;
+
+import org.apache.tapestry.internal.annotations.Concurrent;
+import org.apache.tapestry.ioc.ObjectProvider;
+import org.apache.tapestry.ioc.ServiceLocator;
+import org.apache.tapestry.services.Infrastructure;
+import org.apache.tapestry.services.InfrastructureManager;
+
+/**
+ * TODO: Extra configuration to support application overrides.
+ * 
+ * @author Howard M. Lewis Ship
+ */
+@Concurrent
+public class InfrastructureImpl implements Infrastructure, ObjectProvider
+{
+    private InfrastructureManager _manager;
+
+    // Derived from the manager when first needed
+
+    private Map<String, Object> _properties;
+
+    private String _mode;
+
+    public InfrastructureImpl(InfrastructureManager manager)
+    {
+        _manager = manager;
+    }
+
+    public ObjectProvider getObjectProvider()
+    {
+        return this;
+    }
+
+    // Probably don't need to make this concurrent, since it executes at startup,
+    // before multithreading takes hold.
+
+    @Concurrent.Write
+    public void setMode(String mode)
+    {
+        _mode = mode;
+    }
+
+    @Concurrent.Write
+    private void deriveProperties()
+    {
+        // In a race condition between two or more threads, the first one will create
+        // the properties, the later ones will gain access later and see that
+        // the properties already exist.
+
+        if (_properties != null)
+            return;
+
+        if (_mode == null)
+            throw new RuntimeException(ServicesMessages.infrastructureModeNotSet());
+
+        _properties = _manager.getContributionsForMode(_mode);
+
+        // Don't need this again.
+
+        _manager = null;
+    }
+
+    @Concurrent.Read
+    public <T> T provide(String expression, Class<T> objectType, ServiceLocator locator)
+    {
+        // Lazily create the _properties
+
+        if (_properties == null)
+            deriveProperties();
+
+        Object object = _properties.get(expression);
+
+        if (object == null)
+            throw new RuntimeException(ServicesMessages.infrastructurePropertyNotFound(
+                    expression,
+                    _properties.keySet()));
+
+        try
+        {
+            return objectType.cast(object);
+        }
+        catch (ClassCastException ex)
+        {
+            throw new RuntimeException(ServicesMessages.infrastructurePropertyWrongType(
+                    expression,
+                    object,
+                    objectType), ex);
+        }
+    }
+
+}

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InfrastructureManagerImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InfrastructureManagerImpl.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InfrastructureManagerImpl.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/InfrastructureManagerImpl.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,77 @@
+// Copyright 2006 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 java.util.Collection;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.tapestry.services.InfrastructureContribution;
+import org.apache.tapestry.services.InfrastructureManager;
+import org.apache.tapestry.util.CollectionFactory;
+
+/**
+ * @author Howard M. Lewis Ship
+ */
+public class InfrastructureManagerImpl implements InfrastructureManager
+{
+    private final Log _log;
+
+    private final Collection<InfrastructureContribution> _contributions;
+
+    public InfrastructureManagerImpl(Log log, Collection<InfrastructureContribution> contributions)
+    {
+        _log = log;
+        _contributions = contributions;
+    }
+
+    public Map<String, Object> getContributionsForMode(String mode)
+    {
+        Map<String, Object> general = buildMapForMode("");
+        Map<String, Object> specific = buildMapForMode(mode);
+
+        // Anything in specific overrides anything in general
+
+        general.putAll(specific);
+
+        return general;
+    }
+
+    private Map<String, Object> buildMapForMode(String mode)
+    {
+        Map<String, Object> result = CollectionFactory.newMap();
+
+        for (InfrastructureContribution ic : _contributions)
+        {
+            if (!ic.getMode().equals(mode))
+                continue;
+
+            String name = ic.getName();
+
+            Object existing = result.get(name);
+
+            if (existing != null)
+            {
+                _log.warn(ServicesMessages.duplicateContribution(ic.getObject(), name, existing));
+                continue;
+            }
+
+            result.put(name, ic.getObject());
+        }
+
+        return result;
+    }
+
+}

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,52 @@
+// Copyright 2006 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 java.util.Collection;
+
+import org.apache.tapestry.Messages;
+import org.apache.tapestry.internal.MessagesImpl;
+import org.apache.tapestry.internal.annotations.Utility;
+import org.apache.tapestry.internal.ioc.IOCUtilities;
+
+@Utility
+class ServicesMessages
+{
+    private static final Messages MESSAGES = MessagesImpl.forClass(ServicesMessages.class);
+
+    static final String duplicateContribution(Object conflict, String name, Object existing)
+    {
+        return MESSAGES.format("duplicate-contribution", conflict, name, existing);
+    }
+
+    static final String infrastructureModeNotSet()
+    {
+        return MESSAGES.get("infrastructure-mode-not-set");
+    }
+
+    static final String infrastructurePropertyNotFound(String name, Collection<String> known)
+    {
+        String knownList = IOCUtilities.joinSorted(known);
+
+        return MESSAGES.format("infrastructure-property-not-found", name, knownList);
+    }
+
+    static final String infrastructurePropertyWrongType(String name, Object object,
+            Class desiredType)
+    {
+        return MESSAGES.format("infrastructure-property-wrong-type", name, object, desiredType
+                .getName());
+    }
+}

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/test/InternalBaseTestCase.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/test/InternalBaseTestCase.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/test/InternalBaseTestCase.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/test/InternalBaseTestCase.java Mon Aug  7 03:37:27 2006
@@ -21,10 +21,12 @@
 import org.apache.tapestry.internal.ioc.InternalRegistry;
 import org.apache.tapestry.internal.ioc.Module;
 import org.apache.tapestry.internal.parser.ComponentTemplate;
+import org.apache.tapestry.ioc.ObjectProvider;
 import org.apache.tapestry.ioc.Registry;
 import org.apache.tapestry.ioc.RegistryBuilder;
 import org.apache.tapestry.ioc.ServiceDecorator;
 import org.apache.tapestry.ioc.ServiceLifecycle;
+import org.apache.tapestry.ioc.ServiceLocator;
 import org.apache.tapestry.ioc.def.ServiceDef;
 import org.apache.tapestry.test.BaseTestCase;
 import org.apache.tapestry.util.CollectionFactory;
@@ -88,5 +90,16 @@
         builder.add(moduleClasses);
 
         return builder.build();
+    }
+
+    protected final ObjectProvider newObjectProvider()
+    {
+        return newMock(ObjectProvider.class);
+    }
+
+    protected final <T> void trainProvide(ObjectProvider provider, String expression, Class<T> objectType, ServiceLocator locator, T object)
+    {
+        provider.provide(expression, objectType, locator);
+        setReturnValue(object);
     }
 }

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/IOCConstants.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/IOCConstants.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/IOCConstants.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/IOCConstants.java Mon Aug  7 03:37:27 2006
@@ -25,4 +25,6 @@
     public static final String DEFAULT_LIFECYCLE = "singleton";
 
     public static final String MODULE_BUILDER_MANIFEST_ENTRY_NAME = "Tapestry-Module-Classes";
+
+    public static final String MASTER_OBJECT_PROVIDER_SERVICE_ID = "tapestry.ioc.MasterObjectProvider";
 }

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ObjectProvider.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ObjectProvider.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ObjectProvider.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ObjectProvider.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,62 @@
+// Copyright 2006 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.ioc;
+
+/**
+ * Object providers represent an alternate way to locate an object provided somewhere in the
+ * {@link org.apache.tapestry.ioc.Registry}. Instead of using a just the service id to gain access
+ * to a service within the Registry, object providers in different flavors are capable of vending,
+ * or even creating, objects of disparate types from disparate sources.
+ * <p>
+ * Objects are located via an <em>object reference</em>, consisting of two parts:
+ * <ul>
+ * <li>An provider prefix, used to identify the specific implementation of ObjectProvider</li>
+ * <li>An <em>expression</em>, interpreted by the provider.
+ * </ul>
+ * <p>
+ * The two values are separated by a colon.
+ * <p>
+ * For example, "service:tapestry.ioc.ClassFactory" would consist of a prefix, "service" and an
+ * expression "tapestry.ioc.ClassFactory". The expression would be mapped to the built in
+ * "ClassFactory" service.
+ * <p>
+ * ObjectProviders exist to provide abstractions on top of the raw IoC services. Examples to follow,
+ * especially XXX which provides the ability to easily override services within a Tapestry
+ * application.
+ * 
+ * @author Howard M. Lewis Ship
+ */
+public interface ObjectProvider
+{
+    /**
+     * Provides an object based on an expression. The process of providing objects occurs within a
+     * particular <em>context</em>, which will typically be a service builder method, service
+     * contributor method, or service decorator method. The locator parameter provides access to the
+     * services visible <em>to that context</em>.
+     * 
+     * @param <T>
+     * @param expression
+     *            to be evaluated, to identify the object to return
+     * @param objectType
+     *            the expected object type
+     * @param locator
+     *            locator for the <em>context</em> in which the provider is being used
+     * @return the requested object
+     * @throws RuntimeException
+     *             if the expression can not be evaluated, or the type of object identified is not
+     *             assignable to the type specified by the objectType parameter
+     */
+    <T> T provide(String expression, Class<T> objectType, ServiceLocator locator);
+}

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceLocator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceLocator.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceLocator.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/ServiceLocator.java Mon Aug  7 03:37:27 2006
@@ -56,4 +56,18 @@
      */
     <T> T getService(Class<T> serviceInterface);
 
+    /**
+     * Obtains an object indirectly, using an {@link ObjectProvider} identified by the prefix of the
+     * reference.
+     * 
+     * @param <T>
+     * @param reference
+     *            a provider prefix, a colon, an expression meaningful to the provider
+     * @param objectType
+     *            the type of object to be returned
+     * @return the requested object
+     * @see ObjectProvider
+     */
+    <T> T getObject(String reference, Class<T> objectType);
+
 }

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/Inject.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/Inject.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/Inject.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/Inject.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,43 @@
+// Copyright 2006 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.ioc.annotations;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * Injection based on an object reference. Whereas
+ * {@link org.apache.tapestry.ioc.annotations.InjectService}'s value is always a service id, thsi
+ * annotation is more flexible. The value is an object reference, which is used to select a
+ * {@link org.apache.tapestry.ioc.ObjectProvider} that ultimately provides the value to be injected
+ * via the annotated parameter.
+ * <p>
+ * There are several builtin providers, including "service". Thus this annotation with "service:Foo"
+ * is identical to InjectService with value "Foo". In both cases, the visibility of services,
+ * relative the module in which the injections take place, is honored.
+ * 
+ * @author Howard M. Lewis Ship
+ */
+@Target(PARAMETER)
+@Retention(RUNTIME)
+@Documented
+public @interface Inject {
+    /** Object reference identifying the object to be injected. */
+    String value();
+}

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/InjectService.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/InjectService.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/InjectService.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/annotations/InjectService.java Mon Aug  7 03:37:27 2006
@@ -26,7 +26,8 @@
 @Documented
 /**
  * Annotation used with parameters of service builder methods to identify the service to be injected
- * into the service builder method via the parameter.
+ * into the service builder method via the parameter. In many cases the
+ * {@link org.apache.tapestry.ioc.annotations.Inject} annotation is more flexible or appropriate.
  * 
  * @author Howard M. Lewis Ship
  */

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/ioc/services/TapestryIOCModule.java Mon Aug  7 03:37:27 2006
@@ -19,11 +19,14 @@
 import org.apache.tapestry.internal.ioc.services.ChainBuilderImpl;
 import org.apache.tapestry.internal.ioc.services.DefaultImplementationBuilderImpl;
 import org.apache.tapestry.internal.ioc.services.LoggingDecoratorImpl;
+import org.apache.tapestry.internal.ioc.services.MasterObjectProvider;
 import org.apache.tapestry.internal.ioc.services.PerThreadServiceLifecycle;
 import org.apache.tapestry.internal.ioc.services.PipelineBuilderImpl;
 import org.apache.tapestry.internal.ioc.services.PropertyAccessImpl;
 import org.apache.tapestry.internal.ioc.services.PropertyShadowBuilderImpl;
+import org.apache.tapestry.internal.ioc.services.ServiceObjectProvider;
 import org.apache.tapestry.ioc.MappedConfiguration;
+import org.apache.tapestry.ioc.ObjectProvider;
 import org.apache.tapestry.ioc.ServiceLifecycle;
 import org.apache.tapestry.ioc.annotations.Id;
 import org.apache.tapestry.ioc.annotations.InjectService;
@@ -114,10 +117,34 @@
         return new PipelineBuilderImpl(classFactory, builder);
     }
 
+    /**
+     * Builder that creates a default implementation of an interface.
+     */
     public DefaultImplementationBuilder buildDefaultImplementationBuilder(
             @InjectService("ClassFactory")
             ClassFactory classFactory)
     {
         return new DefaultImplementationBuilderImpl(classFactory);
+    }
+
+    /**
+     * The master {@link ObjectProvider} is responsible for identifying a particular ObjectProvider
+     * by its prefix, and delegating to that instance.
+     * 
+     * @param configuration
+     *            map of ObjectProviders, keyed on prefix
+     */
+    public ObjectProvider buildMasterObjectProvider(Map<String, ObjectProvider> configuration)
+    {
+        return new MasterObjectProvider(configuration);
+    }
+
+    /**
+     * Contributes the "service:" object provider.
+     */
+    public void contributeMasterObjectProvider(
+            MappedConfiguration<String, ObjectProvider> configuration)
+    {
+        configuration.add("service", new ServiceObjectProvider());
     }
 }

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/Infrastructure.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/Infrastructure.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/Infrastructure.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/Infrastructure.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,35 @@
+// Copyright 2006 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.services;
+
+import org.apache.tapestry.ioc.ObjectProvider;
+
+/**
+ * Primarily, Infrastructure is an ObjectProvider (for the "infrastructure" object provider prefix).
+ * However, it has to know what mode it is operating in.
+ * 
+ * @author Howard M. Lewis Ship
+ */
+public interface Infrastructure
+{
+    /** Inform the Infrastructure about what mode it is operating in. */
+    void setMode(String mode);
+
+    /**
+     * Returns an object that can provide objects based on contributions into the Infrastructure
+     * service.
+     */
+    ObjectProvider getObjectProvider();
+}

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/InfrastructureContribution.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/InfrastructureContribution.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/InfrastructureContribution.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/InfrastructureContribution.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,99 @@
+// Copyright 2006 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.services;
+
+import static org.apache.tapestry.util.Defense.notBlank;
+
+import java.util.Formatter;
+
+import org.apache.tapestry.internal.ioc.IOCUtilities;
+
+/**
+ * A contribution into the infrastructure configuration.
+ * <p>
+ * TODO: There might be an order-of-operations issue here. We may need a mechanism like Tapestry 4's
+ * infrastructure that defers instantiation of contributed objects until after the Infrastructure
+ * service is constructed.
+ * 
+ * @author Howard M. Lewis Ship
+ */
+public final class InfrastructureContribution
+{
+    private final String _name;
+
+    private final String _mode;
+
+    private final Object _object;
+
+    /**
+     * Conntributes the object with a blank mode.
+     */
+    public InfrastructureContribution(String name, Object object)
+    {
+        this(name, "", object);
+    }
+
+    public InfrastructureContribution(String name, String mode, Object object)
+    {
+        _name = notBlank(name, "name");
+        _mode = mode;
+        _object = object;
+    }
+
+    /**
+     * Returns the mode of operation for this instance of Tapestry. Most of the time, this will be
+     * the empty string, meaning that the contribution applies to Tapestry is any mode. In other
+     * cases, the mode will be "servlet" but may be other modes via add on modules, such as
+     * "portlet" or "offline".
+     * 
+     * @return
+     */
+    public String getMode()
+    {
+        return _mode;
+    }
+
+    /**
+     * The property name that may be accessed via the Infrastructure service. The combination of
+     * name and mode must be unique.
+     */
+    public String getName()
+    {
+        return _name;
+    }
+
+    /** The contributed object, which will be made available. */
+    public Object getObject()
+    {
+        return _object;
+    }
+
+    @Override
+    public String toString()
+    {
+        StringBuffer buffer = new StringBuffer();
+        Formatter formatter = new Formatter(buffer);
+
+        formatter.format("<InfrastructureContribution: %s", _name);
+
+        if (IOCUtilities.isNonBlank(_mode))
+            formatter.format(" mode:%s", _mode);
+
+        formatter.format(" %s>", _object);
+
+        return buffer.toString();
+    }
+
+}

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/InfrastructureManager.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/InfrastructureManager.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/InfrastructureManager.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/InfrastructureManager.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,36 @@
+// Copyright 2006 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.services;
+
+import java.util.Map;
+
+/**
+ * A thin wrapper around a set of {@link org.apache.tapestry.services.InfrastructureContribution}s.
+ * 
+ * @author Howard M. Lewis Ship
+ */
+public interface InfrastructureManager
+{
+    /**
+     * Filters down the contributions based on the mode. Each contribution will identify a name and
+     * object and may identify a mode. Only contributions where the mode is blank or the mode
+     * matches the provided mode are returned. Mode specific contributions quietly override
+     * non-specific contributions (where the mode is blank).
+     * 
+     * @param mode
+     * @return
+     */
+    Map<String, Object> getContributionsForMode(String mode);
+}

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,61 @@
+// Copyright 2006 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.services;
+
+import java.util.Collection;
+
+import org.apache.commons.logging.Log;
+import org.apache.tapestry.internal.InternalModule;
+import org.apache.tapestry.internal.services.InfrastructureImpl;
+import org.apache.tapestry.internal.services.InfrastructureManagerImpl;
+import org.apache.tapestry.ioc.MappedConfiguration;
+import org.apache.tapestry.ioc.ObjectProvider;
+import org.apache.tapestry.ioc.annotations.Contribute;
+import org.apache.tapestry.ioc.annotations.Id;
+import org.apache.tapestry.ioc.annotations.InjectService;
+import org.apache.tapestry.ioc.annotations.SubModule;
+import org.apache.tapestry.transform.TransformModule;
+
+/**
+ * The root module for Tapestry.
+ * 
+ * @author Howard M. Lewis Ship
+ */
+@Id("tapestry")
+@SubModule(
+{ TransformModule.class, InternalModule.class })
+public final class TapestryModule
+{
+    public Infrastructure buildInfrastructure(Log log,
+            Collection<InfrastructureContribution> configuration)
+    {
+        InfrastructureManager manager = new InfrastructureManagerImpl(log, configuration);
+
+        return new InfrastructureImpl(manager);
+    }
+
+    /**
+     * Contributes the {@link ObjectProvider} provided by {@link Infrastructure#getObjectProvider()}
+     * mapped to the provider prefix "infrastructure".
+     */
+    @Contribute("tapestry.ioc.MasterObjectProvider")
+    public void contributeInfrastructureToMasterObjectProvider(
+            MappedConfiguration<String, ObjectProvider> configuration,
+            @InjectService("Infrastructure")
+            Infrastructure infrastructure)
+    {
+        configuration.add("service", infrastructure.getObjectProvider());
+    }
+}

Modified: tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/ioc/services/ServiceStrings.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/ioc/services/ServiceStrings.properties?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/ioc/services/ServiceStrings.properties (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/ioc/services/ServiceStrings.properties Mon Aug  7 03:37:27 2006
@@ -28,3 +28,5 @@
 property-type-mismatch=Property '%s' of class %s is of type %s, which is not assignable to type %s.
 extra-filter-method=Method %s of filter interface %s does not have a matching method in %s.
 unmatched-service-method=Method %s has no match in filter interface %s.
+object-reference-format-error=Object reference '%s' does not contain an provider prefix (such as 'service:').
+unknown-object-provider=Object provider '%s' does not exist (in object reference '%s').
\ No newline at end of file

Added: tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/main/resources/org/apache/tapestry/internal/services/ServicesStrings.properties Mon Aug  7 03:37:27 2006
@@ -0,0 +1,18 @@
+# Copyright 2006 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.
+
+duplicate-contribution=Contribution %s (as '%s') duplicates existing contribution %s and has been ignored.
+infrastructure-mode-not-set=The infrastructure mode has not been set. Unable to determine the correct set of infrastructure properties.
+infrastructure-property-not-found=No infrastructure property '%s' has been configured. Configured properties are: %s.
+infrastructure-property-wrong-type=Property '%s' (%s) is not assignable to type %s.

Added: tapestry/tapestry5/tapestry-core/trunk/src/site/apt/ioc/provider.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/site/apt/ioc/provider.apt?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/site/apt/ioc/provider.apt (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/site/apt/ioc/provider.apt Mon Aug  7 03:37:27 2006
@@ -0,0 +1,49 @@
+ ----
+ Object Providers
+ ----
+
+Object Providers
+
+  The
+  {{{../apidocs/org/apache/tapestry/ioc/annotations/Inject.html}@Inject annotation}}
+  is a flexible way to inject dependencies into a service. With this annotation
+  the value to be injected is not identified by a service id <per se>. Instead
+  the annotation's value is an <<object reference>>: a combination of a
+  <<provider prefix>> and an <<expression>> (separated by a colon). 
+  
+  The provider prefix
+  maps to a particular instance of
+  {{{../apidocs/org/apache/tapestry/ioc/ObjectProvider.html}ObjectProvider}}, which
+  understands the expression.
+  
+  In practice, @InjectService("Foo") and @Inject("service:Foo") work identically,
+  even with regards to the relative visibility of 
+  private services.  Here, the provider prefix is "service" and the expression
+  is "Foo".
+  
+* service provider
+
+  As outlined above, the service provider interprets the expression as
+  a service id, either fully qualified or local.
+  
+Defining New Providers
+
+  New providers can be specified by contributing to the
+  tapestry.ioc.MasterObjectProvider service's configuration.  The configuration
+  is mapped, with the keys being the provider prefix, and the values
+  being the object provider implementation.
+  
+  Example:
+  
++-----+
+  @Contribution("tapestry.ioc.MasterObjectProvider")
+  public void contributeMyProvider(MappedConfiguration<String,ObjectProvider> configuration)
+  {
+    configuration.add("myprefix", new MyObjectProvider());
+  }
++-----+
+
+  Of course, this is a simplified example. In a real scenario, the provider is most likely
+  a service with its own dependencies.      
+  
+  
\ No newline at end of file

Modified: tapestry/tapestry5/tapestry-core/trunk/src/site/apt/ioc/service.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/site/apt/ioc/service.apt?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/site/apt/ioc/service.apt (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/site/apt/ioc/service.apt Mon Aug  7 03:37:27 2006
@@ -35,7 +35,7 @@
   service implementation of the Indexer service), but it does know 
   about the buildIndexer() method.
   
-{Injecting Dependencies}
+{Injecting Dependencies} with @InjectService
 
   It's pretty unlikely  that your service will be able to operate in a total vacuum. It will
   have other dependencies.  For example, let's say the Indexer needs a JobScheduler to control
@@ -67,6 +67,30 @@
   
   If we used fully qualified service ids, such as "org.examples.myapps.jobs.JobScheduler",
   then we can access services from some other module entirely.
+  
+Injecting Dependencies with @Inject
+
+  A second approach to injection uses the more general purpose
+  {{{../apidocs/org/apache/tapestry/ioc/annotations/Inject.html}@Inject annotation}}.
+  
+  @Inject doesn't specify a service id <em>per se</em>.  With inject, you provide
+  an {{{provider.html}object reference}}.  Different prefixes on the reference
+  are interpreted in different ways.  The "service" prefix works identically
+  to the @InjectService annotation, so the previous example can be rewritten
+  as:
+    
++-----------------------------------------------------------------------------------+
+  public Indexer buildIndexer(@Inject("service:JobScheduler")
+    JobScheduler scheduler, @Inject("service:FileSystem")
+    FileSystem fileSystem)
+  {
+    IndexerImpl indexer = new IndexerImpl(fileSystem);
+      
+    scheduler.scheduleDailyJob(indexer);
+      
+    return indexer;
+  }
++-----------------------------------------------------------------------------------+
   
 Defining Visibility
 

Modified: tapestry/tapestry5/tapestry-core/trunk/src/site/site.xml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/site/site.xml?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/site/site.xml (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/site/site.xml Mon Aug  7 03:37:27 2006
@@ -56,6 +56,7 @@
             <item name="Services" href="ioc/service.html"/>
             <item name="Decorators" href="ioc/decorator.html"/>
             <item name="Configuration" href="ioc/configuration.html"/>
+            <item name="Object Providers" href="ioc/provider.html"/>
             <item name="Ordering" href="ioc/order.html"/>
             <item name="Chain Of Command" href="ioc/command.html"/>
             <item name="Pipeline" href="ioc/pipeline.html"/>

Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/services/MasterObjectProvideTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/services/MasterObjectProvideTest.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/services/MasterObjectProvideTest.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/services/MasterObjectProvideTest.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,108 @@
+// Copyright 2006 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.ioc.services;
+
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.tapestry.internal.test.InternalBaseTestCase;
+import org.apache.tapestry.ioc.ObjectProvider;
+import org.apache.tapestry.ioc.ServiceLocator;
+import org.apache.tapestry.util.CollectionFactory;
+import org.testng.annotations.Test;
+
+/**
+ * @author Howard M. Lewis Ship
+ */
+public class MasterObjectProvideTest extends InternalBaseTestCase
+{
+    private Map<String, ObjectProvider> newMap(String prefix, ObjectProvider provider)
+    {
+        return Collections.singletonMap(prefix, provider);
+    }
+
+    @Test
+    public void successful_lookup()
+    {
+        ObjectProvider provider = newObjectProvider();
+        ServiceLocator locator = newServiceLocator();
+        Runnable r = newRunnable();
+
+        trainProvide(provider, "expression", Runnable.class, locator, r);
+
+        replay();
+
+        ObjectProvider master = new MasterObjectProvider(newMap("prefix", provider));
+
+        Runnable actual = master.provide("prefix:expression", Runnable.class, locator);
+
+        assertSame(actual, r);
+
+        verify();
+    }
+
+    @Test
+    public void unknown_object_provider_prefix()
+    {
+        ServiceLocator locator = newServiceLocator();
+
+        replay();
+
+        Map<String, ObjectProvider> configuration = CollectionFactory.newMap();
+
+        ObjectProvider master = new MasterObjectProvider(configuration);
+
+        try
+        {
+            master.provide("prefix:expression", Runnable.class, locator);
+            unreachable();
+        }
+        catch (RuntimeException ex)
+        {
+            assertEquals(
+                    ex.getMessage(),
+                    "Object provider 'prefix' does not exist (in object reference 'prefix:expression').");
+        }
+
+        verify();
+    }
+
+    @Test
+    public void no_prefix_in_object_reference()
+    {
+        ServiceLocator locator = newServiceLocator();
+
+        replay();
+
+        Map<String, ObjectProvider> configuration = CollectionFactory.newMap();
+
+        ObjectProvider master = new MasterObjectProvider(configuration);
+
+        try
+        {
+            master.provide("just-expression", Runnable.class, locator);
+            unreachable();
+        }
+        catch (RuntimeException ex)
+        {
+            assertEquals(
+                    ex.getMessage(),
+                    "Object reference 'just-expression' does not contain an provider prefix (such as 'service:').");
+        }
+
+        verify();
+    }
+
+}

Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/services/ServiceObjectProviderTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/services/ServiceObjectProviderTest.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/services/ServiceObjectProviderTest.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/ioc/services/ServiceObjectProviderTest.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,46 @@
+// Copyright 2006 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.ioc.services;
+
+import org.apache.tapestry.internal.test.InternalBaseTestCase;
+import org.apache.tapestry.ioc.ObjectProvider;
+import org.apache.tapestry.ioc.ServiceLocator;
+import org.testng.annotations.Test;
+
+/**
+ * @author Howard M. Lewis Ship
+ */
+public class ServiceObjectProviderTest extends InternalBaseTestCase
+{
+    @Test
+    public void provider_delegates_to_locator()
+    {
+        ServiceLocator locator = newServiceLocator();
+
+        Runnable r = newRunnable();
+
+        trainGetService(locator, "module.Foo", Runnable.class, r);
+
+        replay();
+
+        ObjectProvider provider = new ServiceObjectProvider();
+
+        Runnable actual = provider.provide("module.Foo", Runnable.class, locator);
+
+        assertSame(actual, r);
+
+        verify();
+    }
+}

Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/InfrastructureImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/InfrastructureImplTest.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/InfrastructureImplTest.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/InfrastructureImplTest.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,213 @@
+// Copyright 2006 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 java.util.Map;
+
+import org.apache.tapestry.internal.test.InternalBaseTestCase;
+import org.apache.tapestry.ioc.ObjectProvider;
+import org.apache.tapestry.ioc.ServiceLocator;
+import org.apache.tapestry.services.Infrastructure;
+import org.apache.tapestry.services.InfrastructureManager;
+import org.testng.annotations.Test;
+
+import static org.apache.tapestry.util.CollectionFactory.newMap;
+
+/**
+ * @author Howard M. Lewis Ship
+ */
+public class InfrastructureImplTest extends InternalBaseTestCase
+{
+    @Test
+    public void mode_not_set_when_resolution_requested()
+    {
+        InfrastructureManager manager = newInfrastructureManager();
+        ServiceLocator locator = newServiceLocator();
+
+        replay();
+
+        Infrastructure infra = new InfrastructureImpl(manager);
+
+        // Do not assume that infra and provider are the same;
+        // that's an implementation choice.
+
+        ObjectProvider provider = infra.getObjectProvider();
+
+        try
+        {
+            provider.provide("expression", Runnable.class, locator);
+            unreachable();
+        }
+        catch (RuntimeException ex)
+        {
+            assertEquals(ex.getMessage(), ServicesMessages.infrastructureModeNotSet());
+        }
+
+        verify();
+    }
+
+    protected final InfrastructureManager newInfrastructureManager()
+    {
+        return newMock(InfrastructureManager.class);
+    }
+
+    @Test
+    public void resolve_object_within_mode()
+    {
+        InfrastructureManager manager = newInfrastructureManager();
+        ServiceLocator locator = newServiceLocator();
+        Runnable r = newRunnable();
+
+        Map<String, Object> configuration = newMap();
+        configuration.put("myrunnable", r);
+
+        trainGetContributionsForMode(manager, "papyrus", configuration);
+
+        replay();
+
+        Infrastructure infra = new InfrastructureImpl(manager);
+
+        infra.setMode("papyrus");
+
+        // Do not assume that infra and provider are the same;
+        // that's an implementation choice.
+
+        ObjectProvider provider = infra.getObjectProvider();
+
+        Runnable actual = provider.provide("myrunnable", Runnable.class, locator);
+
+        assertSame(actual, r);
+
+        verify();
+    }
+
+    /** Check that the manager is only consulted once. */
+    @Test
+    public void configuration_is_cached()
+    {
+        InfrastructureManager manager = newInfrastructureManager();
+        ServiceLocator locator = newServiceLocator();
+        Runnable r = newRunnable();
+
+        Map<String, Object> configuration = newMap();
+        configuration.put("myrunnable", r);
+
+        trainGetContributionsForMode(manager, "clay", configuration);
+
+        replay();
+
+        Infrastructure infra = new InfrastructureImpl(manager);
+
+        infra.setMode("clay");
+
+        // Do not assume that infra and provider are the same;
+        // that's an implementation choice.
+
+        ObjectProvider provider = infra.getObjectProvider();
+
+        Runnable actual1 = provider.provide("myrunnable", Runnable.class, locator);
+        Runnable actual2 = provider.provide("myrunnable", Runnable.class, locator);
+
+        assertSame(actual1, r);
+        assertSame(actual2, r);
+
+        verify();
+    }
+
+    @Test
+    public void expression_not_found_in_configuration()
+    {
+        InfrastructureManager manager = newInfrastructureManager();
+        ServiceLocator locator = newServiceLocator();
+
+        Map<String, Object> configuration = newMap();
+
+        configuration.put("fred", this);
+        configuration.put("barney", this);
+        configuration.put("wilma", this);
+
+        trainGetContributionsForMode(manager, "clay", configuration);
+
+        replay();
+
+        Infrastructure infra = new InfrastructureImpl(manager);
+
+        infra.setMode("clay");
+
+        ObjectProvider provider = infra.getObjectProvider();
+
+        try
+        {
+            provider.provide("someexpression", Runnable.class, locator);
+            unreachable();
+        }
+        catch (RuntimeException ex)
+        {
+            assertEquals(
+                    ex.getMessage(),
+                    "No infrastructure property 'someexpression' has been configured. Configured properties are: barney, fred, wilma.");
+        }
+
+        verify();
+
+    }
+
+    @Test
+    public void requested_type_not_compatible_with_object()
+    {
+        InfrastructureManager manager = newInfrastructureManager();
+        ServiceLocator locator = newServiceLocator();
+        Runnable r = newRunnable();
+
+        Map<String, Object> configuration = newMap();
+        configuration.put("myrunnable", r);
+
+        trainGetContributionsForMode(manager, "papyrus", configuration);
+
+        replay();
+
+        Infrastructure infra = new InfrastructureImpl(manager);
+
+        infra.setMode("papyrus");
+
+        // Do not assume that infra and provider are the same;
+        // that's an implementation choice.
+
+        ObjectProvider provider = infra.getObjectProvider();
+
+        try
+        {
+            provider.provide("myrunnable", UpdateListenerHub.class, locator);
+            unreachable();
+        }
+        catch (RuntimeException ex)
+        {
+            assertEquals(ex.getMessage(), ServicesMessages.infrastructurePropertyWrongType(
+                    "myrunnable",
+                    r,
+                    UpdateListenerHub.class));
+        }
+
+        verify();
+
+    }
+
+    protected final void trainGetContributionsForMode(InfrastructureManager manager, String mode,
+            Map<String, Object> configuration)
+    {
+        manager.getContributionsForMode(mode);
+        setReturnValue(configuration);
+    }
+}

Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/InfrastructureManagerImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/InfrastructureManagerImplTest.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/InfrastructureManagerImplTest.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/services/InfrastructureManagerImplTest.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,131 @@
+// Copyright 2006 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 java.util.Collection;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.tapestry.internal.test.InternalBaseTestCase;
+import org.apache.tapestry.services.InfrastructureContribution;
+import org.apache.tapestry.services.InfrastructureManager;
+import org.testng.annotations.Test;
+
+import static org.apache.tapestry.util.CollectionFactory.newList;
+
+/**
+ * @author Howard M. Lewis Ship
+ */
+public class InfrastructureManagerImplTest extends InternalBaseTestCase
+{
+    @Test
+    public void no_conflict()
+    {
+        Log log = newLog();
+
+        replay();
+
+        Collection<InfrastructureContribution> configuration = buildConfiguration();
+
+        InfrastructureManager manager = new InfrastructureManagerImpl(log, configuration);
+
+        Map<String, Object> map = manager.getContributionsForMode("foo");
+
+        assertEquals(map.size(), 2);
+        assertEquals(map.get("fred"), "FRED");
+        assertEquals(map.get("barney"), "BARNEY");
+
+        verify();
+    }
+
+    @Test
+    public void first_entry_wins_on_conflict()
+    {
+        Log log = newLog();
+
+        log
+                .warn("Contribution FRED-CONFLICT (as 'fred') duplicates existing contribution FRED and has been ignored.");
+
+        replay();
+
+        Collection<InfrastructureContribution> configuration = buildConfiguration();
+        configuration.add(new InfrastructureContribution("fred", "FRED-CONFLICT"));
+
+        InfrastructureManager manager = new InfrastructureManagerImpl(log, configuration);
+
+        Map<String, Object> map = manager.getContributionsForMode("foo");
+
+        assertEquals(map.size(), 2);
+        assertEquals(map.get("fred"), "FRED");
+        assertEquals(map.get("barney"), "BARNEY");
+
+        verify();
+    }
+
+    @Test
+    public void contributions_to_other_modes_are_ignored()
+    {
+        Log log = newLog();
+
+        replay();
+
+        Collection<InfrastructureContribution> configuration = buildConfiguration();
+
+        configuration.add(new InfrastructureContribution("barney", "bar", "BARNEY2"));
+
+        InfrastructureManager manager = new InfrastructureManagerImpl(log, configuration);
+
+        Map<String, Object> map = manager.getContributionsForMode("foo");
+
+        assertEquals(map.size(), 2);
+        assertEquals(map.get("fred"), "FRED");
+        assertEquals(map.get("barney"), "BARNEY");
+
+        verify();
+    }
+
+    @Test
+    public void mode_specific_contribution_overrides_general_contribution()
+    {
+        Log log = newLog();
+
+        replay();
+
+        Collection<InfrastructureContribution> configuration = buildConfiguration();
+
+        configuration.add(new InfrastructureContribution("fred", "foo", "FRED-OVERRIDE"));
+
+        InfrastructureManager manager = new InfrastructureManagerImpl(log, configuration);
+
+        Map<String, Object> map = manager.getContributionsForMode("foo");
+
+        assertEquals(map.size(), 2);
+        assertEquals(map.get("fred"), "FRED-OVERRIDE");
+        assertEquals(map.get("barney"), "BARNEY");
+
+        verify();
+
+    }
+
+    private Collection<InfrastructureContribution> buildConfiguration()
+    {
+        Collection<InfrastructureContribution> configuration = newList();
+
+        configuration.add(new InfrastructureContribution("fred", "FRED"));
+        configuration.add(new InfrastructureContribution("barney", "foo", "BARNEY"));
+
+        return configuration;
+    }
+}

Modified: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java?rev=429295&r1=429294&r2=429295&view=diff
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java (original)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/IntegrationTest.java Mon Aug  7 03:37:27 2006
@@ -223,4 +223,19 @@
 
         assertNull(holder.getValue());
     }
+
+    @Test
+    public void use_of_service_object_provider_prefix()
+    {
+        Registry r = buildRegistry(ServiceObjectProviderModule.class);
+
+        StringTransformer st = r.getObject(
+                "service:ioc.test.PublicTransformer",
+                StringTransformer.class);
+
+        // The PublicTransform gets the PrivateTransformer via
+        // an @Inject.
+
+        assertEquals(st.transform("fred"), "FRED");
+    }
 }

Added: tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/ServiceObjectProviderModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/ServiceObjectProviderModule.java?rev=429295&view=auto
==============================================================================
--- tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/ServiceObjectProviderModule.java (added)
+++ tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/ioc/ServiceObjectProviderModule.java Mon Aug  7 03:37:27 2006
@@ -0,0 +1,46 @@
+// Copyright 2006 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.ioc;
+
+import org.apache.tapestry.ioc.annotations.Id;
+import org.apache.tapestry.ioc.annotations.Inject;
+import org.apache.tapestry.ioc.annotations.Private;
+
+/**
+ * Used by {@link org.apache.tapestry.ioc.IntegrationTest}.
+ * 
+ * @author Howard M. Lewis Ship
+ */
+@Id("ioc.test")
+public class ServiceObjectProviderModule
+{
+    @Private
+    public StringTransformer buildPrivateTransformer()
+    {
+        return new StringTransformer()
+        {
+            public String transform(String input)
+            {
+                return input.toUpperCase();
+            }
+        };
+    }
+
+    public StringTransformer buildPublicTransformer(@Inject("service:PrivateTransformer")
+    StringTransformer privateTransformer)
+    {
+        return privateTransformer;
+    }
+}