cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r939949 - in /cayenne/main/trunk: docs/doc/src/main/resources/ framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/ framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/spi/ framework/cayenne-di-unpublished/...
Date Sat, 01 May 2010 08:56:41 GMT
Author: aadamchik
Date: Sat May  1 08:56:40 2010
New Revision: 939949

URL: http://svn.apache.org/viewvc?rev=939949&view=rev
Log:
CAY-1424 DI: support for named service injection and flexible map/list binding

* switching default object scope in DI registry from NO_SCOPE to SINGLETON

Modified:
    cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
    cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/Binder.java
    cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/BindingBuilder.java
    cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/spi/Binding.java
    cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/spi/DefaultInjector.java
    cayenne/main/trunk/framework/cayenne-di-unpublished/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorScopeTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/CayenneServerModule.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/ServletContextHandlerTest.java
    cayenne/main/trunk/framework/cayenne-project-unpublished/src/main/java/org/apache/cayenne/project2/CayenneProjectModule.java

Modified: cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt?rev=939949&r1=939948&r2=939949&view=diff
==============================================================================
--- cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt (original)
+++ cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt Sat May  1 08:56:40 2010
@@ -36,6 +36,7 @@ CAY-1394 DI-based replacement for JNDI h
 CAY-1415 Replace WebApplicationContextFilter with a DI-enabled filter
 CAY-1419 File-based ResourceLocator to replace legacy FileConfiguration
 CAY-1422 Convert EventManager to an interface, make it injectable
+CAY-1424 DI: support for named service injection and flexible map/list binding
 
 Bug Fixes Since 3.0:
 

Modified: cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/Binder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/Binder.java?rev=939949&r1=939948&r2=939949&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/Binder.java
(original)
+++ cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/Binder.java
Sat May  1 08:56:40 2010
@@ -20,7 +20,10 @@ package org.apache.cayenne.di;
 
 /**
  * An object passed to a {@link Module} by the DI container during initialization, that
- * provides the API for the module to bind its services to the container.
+ * provides the API for the module to bind its services to the container. Note that the
+ * default {@link Scope} of the bound objects is normally "singleton" and can be changed
+ * to "no scope" or a custom scope via a corresponding method of a binding builder. E.g.
+ * see {@link BindingBuilder#in(Scope)}.
  * 
  * @since 3.1
  */

Modified: cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/BindingBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/BindingBuilder.java?rev=939949&r1=939948&r2=939949&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/BindingBuilder.java
(original)
+++ cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/BindingBuilder.java
Sat May  1 08:56:40 2010
@@ -38,5 +38,10 @@ public interface BindingBuilder<T> {
     BindingBuilder<T> toProviderInstance(Provider<? extends T> provider)
             throws ConfigurationException;
 
+    /**
+     * Sets the scope of a bound instance. This method is used to change the default scope
+     * (which is normally {@link Scopes#SINGLETON}) to something else, e.g.
+     * {@link Scopes#NO_SCOPE}.
+     */
     void in(Scope scope);
 }

Modified: cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/spi/Binding.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/spi/Binding.java?rev=939949&r1=939948&r2=939949&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/spi/Binding.java
(original)
+++ cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/spi/Binding.java
Sat May  1 08:56:40 2010
@@ -33,9 +33,9 @@ class Binding<T> {
     private Provider<T> unscoped;
     private Provider<T> scoped;
 
-    Binding(Provider<T> provider) {
+    Binding(Provider<T> provider, Scope initialScope) {
         this.unscoped = provider;
-        this.scoped = provider;
+        this.scoped = initialScope != null ? initialScope.scope(provider) : provider;
     }
 
     void changeScope(Scope scope) {

Modified: cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/spi/DefaultInjector.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/spi/DefaultInjector.java?rev=939949&r1=939948&r2=939949&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/spi/DefaultInjector.java
(original)
+++ cayenne/main/trunk/framework/cayenne-di-unpublished/src/main/java/org/apache/cayenne/di/spi/DefaultInjector.java
Sat May  1 08:56:40 2010
@@ -38,9 +38,13 @@ public class DefaultInjector implements 
 
     private Map<Key<?>, Binding<?>> bindings;
     private InjectionStack injectionStack;
+    private Scope defaultScope;
 
     public DefaultInjector(Module... modules) throws ConfigurationException {
 
+        // this is intentionally hardcoded and is not configurable
+        this.defaultScope = Scopes.SINGLETON;
+
         this.bindings = new HashMap<Key<?>, Binding<?>>();
         this.injectionStack = new InjectionStack();
 
@@ -74,7 +78,7 @@ public class DefaultInjector implements 
 
     <T> void putBinding(Key<T> bindingKey, Provider<T> provider) {
         // TODO: andrus 11/15/2009 - report overriding existing binding??
-        bindings.put(bindingKey, new Binding<T>(provider));
+        bindings.put(bindingKey, new Binding<T>(provider, defaultScope));
     }
 
     <T> void changeBindingScope(Key<T> bindingKey, Scope scope) {

Modified: cayenne/main/trunk/framework/cayenne-di-unpublished/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorScopeTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-di-unpublished/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorScopeTest.java?rev=939949&r1=939948&r2=939949&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-di-unpublished/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorScopeTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-di-unpublished/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorScopeTest.java
Sat May  1 08:56:40 2010
@@ -28,7 +28,7 @@ import org.apache.cayenne.di.mock.MockIn
 
 public class DefaultInjectorScopeTest extends TestCase {
 
-    public void testNoScope() {
+    public void testDefaultScope() {
 
         Module module = new Module() {
 
@@ -47,12 +47,11 @@ public class DefaultInjectorScopeTest ex
         assertNotNull(instance2);
         assertNotNull(instance3);
 
-        assertNotSame(instance1, instance2);
-        assertNotSame(instance2, instance3);
-        assertNotSame(instance3, instance1);
+        assertSame(instance1, instance2);
+        assertSame(instance2, instance3);
     }
 
-    public void testNoScope_Explicit() {
+    public void testNoScope() {
 
         Module module = new Module() {
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/CayenneServerModule.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/CayenneServerModule.java?rev=939949&r1=939948&r2=939949&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/CayenneServerModule.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/CayenneServerModule.java
Sat May  1 08:56:40 2010
@@ -94,47 +94,41 @@ public class CayenneServerModule impleme
                 .add(new PostgresSniffer())
                 .add(new MySQLSniffer());
 
-        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class).in(
-                Scopes.SINGLETON);
+        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
         binder.bind(ConfigurationNameMapper.class).to(
-                DefaultConfigurationNameMapper.class).in(Scopes.SINGLETON);
+                DefaultConfigurationNameMapper.class);
 
-        binder.bind(EventManager.class).toProvider(EventManagerProvider.class).in(
-                Scopes.SINGLETON);
+        binder.bind(EventManager.class).toProvider(EventManagerProvider.class);
 
         // a service to provide the main stack DataDomain
-        binder.bind(DataDomain.class).toProvider(DataDomainProvider.class).in(
-                Scopes.SINGLETON);
+        binder.bind(DataDomain.class).toProvider(DataDomainProvider.class);
 
         // will return DataDomain for request for a DataChannel
-        binder.bind(DataChannel.class).toProvider(DomainDataChannelProvider.class).in(
-                Scopes.SINGLETON);
+        binder.bind(DataChannel.class).toProvider(DomainDataChannelProvider.class);
 
-        binder.bind(ObjectContext.class).toProvider(DataContextProvider.class);
+        binder.bind(ObjectContext.class).toProvider(DataContextProvider.class).in(
+                Scopes.NO_SCOPE);
 
         // a service to load project XML descriptors
         binder.bind(DataChannelDescriptorLoader.class).to(
-                XMLDataChannelDescriptorLoader.class).in(Scopes.SINGLETON);
+                XMLDataChannelDescriptorLoader.class);
 
         // a service to load DataMap XML descriptors
-        binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class).in(Scopes.SINGLETON);
+        binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class);
 
         // a locator of resources, such as XML descriptors
-        binder.bind(ResourceLocator.class).to(ClassLoaderResourceLocator.class).in(
-                Scopes.SINGLETON);
+        binder.bind(ResourceLocator.class).to(ClassLoaderResourceLocator.class);
 
         // a global properties object
-        binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class).in(
-                Scopes.SINGLETON);
+        binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
 
         // a service to load DataSourceFactories
         binder.bind(DataSourceFactoryLoader.class).to(
-                DefaultDataSourceFactoryLoader.class).in(Scopes.SINGLETON);
+                DefaultDataSourceFactoryLoader.class);
 
         // a default SchemaUpdateStrategy (used when no explicit strategy is specified in
         // XML)
-        binder.bind(SchemaUpdateStrategy.class).to(SkipSchemaUpdateStrategy.class).in(
-                Scopes.SINGLETON);
+        binder.bind(SchemaUpdateStrategy.class).to(SkipSchemaUpdateStrategy.class);
 
         // a default DBAdapterFactory used to load custom and automatic DbAdapters
         binder.bind(DbAdapterFactory.class).to(DefaultDbAdapterFactory.class);

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java?rev=939949&r1=939948&r2=939949&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/web/CayenneWebModule.java
Sat May  1 08:56:40 2010
@@ -20,6 +20,7 @@ package org.apache.cayenne.configuration
 
 import org.apache.cayenne.di.Binder;
 import org.apache.cayenne.di.Module;
+import org.apache.cayenne.di.Scopes;
 
 /**
  * @since 3.1
@@ -27,6 +28,7 @@ import org.apache.cayenne.di.Module;
 public class CayenneWebModule implements Module {
 
     public void configure(Binder binder) {
-        binder.bind(RequestHandler.class).to(SessionContextRequestHandler.class);
+        binder.bind(RequestHandler.class).to(SessionContextRequestHandler.class).in(
+                Scopes.NO_SCOPE);
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/ServletContextHandlerTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/ServletContextHandlerTest.java?rev=939949&r1=939948&r2=939949&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/ServletContextHandlerTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/web/ServletContextHandlerTest.java
Sat May  1 08:56:40 2010
@@ -25,6 +25,7 @@ import org.apache.cayenne.di.Binder;
 import org.apache.cayenne.di.DIBootstrap;
 import org.apache.cayenne.di.Injector;
 import org.apache.cayenne.di.Module;
+import org.apache.cayenne.di.Scopes;
 
 import com.mockrunner.mock.web.MockHttpServletRequest;
 import com.mockrunner.mock.web.MockHttpServletResponse;
@@ -39,7 +40,8 @@ public class ServletContextHandlerTest e
         Module module = new Module() {
 
             public void configure(Binder binder) {
-                binder.bind(ObjectContext.class).to(MockObjectContext.class);
+                binder.bind(ObjectContext.class).to(MockObjectContext.class).in(
+                        Scopes.NO_SCOPE);
             }
         };
         Injector injector = DIBootstrap.createInjector(module);

Modified: cayenne/main/trunk/framework/cayenne-project-unpublished/src/main/java/org/apache/cayenne/project2/CayenneProjectModule.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-project-unpublished/src/main/java/org/apache/cayenne/project2/CayenneProjectModule.java?rev=939949&r1=939948&r2=939949&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-project-unpublished/src/main/java/org/apache/cayenne/project2/CayenneProjectModule.java
(original)
+++ cayenne/main/trunk/framework/cayenne-project-unpublished/src/main/java/org/apache/cayenne/project2/CayenneProjectModule.java
Sat May  1 08:56:40 2010
@@ -20,7 +20,6 @@ package org.apache.cayenne.project2;
 
 import org.apache.cayenne.di.Binder;
 import org.apache.cayenne.di.Module;
-import org.apache.cayenne.di.Scopes;
 import org.apache.cayenne.project2.upgrade.ProjectUpgrader;
 import org.apache.cayenne.project2.upgrade.v6.ProjectUpgrader_V6;
 import org.apache.cayenne.project2.validation.DefaultProjectValidator;
@@ -35,14 +34,11 @@ import org.apache.cayenne.project2.valid
 public class CayenneProjectModule implements Module {
 
     public void configure(Binder binder) {
-        binder.bind(ProjectLoader.class).to(DataChannelProjectLoader.class).in(
-                Scopes.SINGLETON);
-        binder.bind(ProjectSaver.class).to(FileProjectSaver.class).in(Scopes.SINGLETON);
-        binder.bind(ProjectUpgrader.class).to(ProjectUpgrader_V6.class).in(
-                Scopes.SINGLETON);
-        binder.bind(ProjectValidator.class).to(DefaultProjectValidator.class).in(
-                Scopes.SINGLETON);
+        binder.bind(ProjectLoader.class).to(DataChannelProjectLoader.class);
+        binder.bind(ProjectSaver.class).to(FileProjectSaver.class);
+        binder.bind(ProjectUpgrader.class).to(ProjectUpgrader_V6.class);
+        binder.bind(ProjectValidator.class).to(DefaultProjectValidator.class);
         binder.bind(ConfigurationNodeParentGetter.class).to(
-                DefaultConfigurationNodeParentGetter.class).in(Scopes.SINGLETON);
+                DefaultConfigurationNodeParentGetter.class);
     }
 }



Mime
View raw message