cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r884078 - in /cayenne/sandbox/cayenne-di/src: main/java/org/apache/cayenne/di/ main/java/org/apache/cayenne/di/spi/ main/java/org/apache/cayenne/runtime/ main/java/org/apache/cayenne/runtime/access/ main/java/org/apache/cayenne/runtime/conf...
Date Wed, 25 Nov 2009 12:48:37 GMT
Author: aadamchik
Date: Wed Nov 25 12:48:36 2009
New Revision: 884078

URL: http://svn.apache.org/viewvc?rev=884078&view=rev
Log:
setup Cayenne stack based on DI

- defining ConfigurationProvider based on the existing classes
- fixing the use of uninjected provider

Added:
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingProvider.java
      - copied, changed from r884055, cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorProvider.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingProvidersProvider.java
      - copied, changed from r884055, cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ProviderConstructorProvider.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/CustomProvidersProvider.java
      - copied, changed from r884055, cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ProviderConstructorProvider.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/access/
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/access/DataChannelProvider.java
      - copied, changed from r884055, cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/runtime/CayenneModuleTest.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/configuration/
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/configuration/ConfigurationProvider.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/configuration/DIConfiguration.java
      - copied, changed from r884055, cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/runtime/CayenneModuleTest.java
Removed:
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorProvider.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ProviderConstructorProvider.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/spi/DataChannelProvider.java
    cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/runtime/CayenneModuleTest.java
Modified:
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/SingletonProvider.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultBindingBuilder.java
    cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/CayenneModule.java

Modified: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/SingletonProvider.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/SingletonProvider.java?rev=884078&r1=884077&r2=884078&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/SingletonProvider.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/SingletonProvider.java
Wed Nov 25 12:48:36 2009
@@ -24,7 +24,9 @@
 class SingletonProvider<T> implements Provider<T> {
 
     private Provider<T> delegate;
-    private T instance;
+
+    // presumably "volatile" works in Java 5 and newer to prevent double-checked locking
+    private volatile T instance;
 
     public SingletonProvider(Provider<T> delegate) {
         this.delegate = delegate;
@@ -32,9 +34,6 @@
 
     public T get() {
 
-        // using double-checked locking for lazy initialization, which is not guaranteed
-        // to work on all architectures and JVMs. Better alternatives? Synchronizing the
-        // entire method?
         if (instance == null) {
             synchronized (this) {
                 if (instance == null) {

Copied: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingProvider.java
(from r884055, cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorProvider.java)
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingProvider.java?p2=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingProvider.java&p1=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorProvider.java&r1=884055&r2=884078&rev=884078&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorProvider.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingProvider.java
Wed Nov 25 12:48:36 2009
@@ -31,13 +31,13 @@
 /**
  * @since 3.1
  */
-class ConstructorProvider<T> implements Provider<T> {
+class ConstructorInjectingProvider<T> implements Provider<T> {
 
     private Class<T> interfaceType;
     private Constructor<? extends T> constructor;
     private DefaultInjector injector;
 
-    ConstructorProvider(Class<T> interfaceType, Class<? extends T> implementation,
+    ConstructorInjectingProvider(Class<T> interfaceType, Class<? extends T> implementation,
             DefaultInjector injector) {
 
         initConstructor(implementation);

Copied: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingProvidersProvider.java
(from r884055, cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ProviderConstructorProvider.java)
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingProvidersProvider.java?p2=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingProvidersProvider.java&p1=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ProviderConstructorProvider.java&r1=884055&r2=884078&rev=884078&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ProviderConstructorProvider.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingProvidersProvider.java
Wed Nov 25 12:48:36 2009
@@ -22,33 +22,27 @@
 import org.apache.cayenne.di.Provider;
 
 /**
+ * An injecting provider for custom providers.
+ * 
  * @since 3.1
  */
-class ProviderConstructorProvider<T> implements Provider<T> {
+class ConstructorInjectingProvidersProvider<T> implements Provider<Provider<?
extends T>> {
 
     private Class<? extends Provider<? extends T>> providerType;
 
-    ProviderConstructorProvider(Class<? extends Provider<? extends T>> providerType)
{
+    ConstructorInjectingProvidersProvider(
+            Class<? extends Provider<? extends T>> providerType) {
         this.providerType = providerType;
     }
 
-    public T get() throws DIException {
-
-        Provider<? extends T> provider;
+    public Provider<? extends T> get() throws DIException {
         try {
-            provider = providerType.newInstance();
+            // TODO: constructor injection in provider?
+            return providerType.newInstance();
         }
         catch (Exception e) {
             throw new DIException("Error instantiating provider '%s'", e, providerType
                     .getName());
         }
-
-        T object = provider.get();
-        if (object == null) {
-            throw new DIException("Provider '%s' returned null object", providerType
-                    .getName());
-        }
-
-        return object;
     }
 }

Copied: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/CustomProvidersProvider.java
(from r884055, cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ProviderConstructorProvider.java)
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/CustomProvidersProvider.java?p2=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/CustomProvidersProvider.java&p1=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ProviderConstructorProvider.java&r1=884055&r2=884078&rev=884078&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ProviderConstructorProvider.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/CustomProvidersProvider.java
Wed Nov 25 12:48:36 2009
@@ -22,33 +22,19 @@
 import org.apache.cayenne.di.Provider;
 
 /**
+ * A wrapper around a provider that itself generates providers.
+ * 
  * @since 3.1
  */
-class ProviderConstructorProvider<T> implements Provider<T> {
+class CustomProvidersProvider<T> implements Provider<T> {
 
-    private Class<? extends Provider<? extends T>> providerType;
+    private Provider<Provider<? extends T>> providerOfProviders;
 
-    ProviderConstructorProvider(Class<? extends Provider<? extends T>> providerType)
{
-        this.providerType = providerType;
+    CustomProvidersProvider(Provider<Provider<? extends T>> providerOfProviders)
{
+        this.providerOfProviders = providerOfProviders;
     }
 
     public T get() throws DIException {
-
-        Provider<? extends T> provider;
-        try {
-            provider = providerType.newInstance();
-        }
-        catch (Exception e) {
-            throw new DIException("Error instantiating provider '%s'", e, providerType
-                    .getName());
-        }
-
-        T object = provider.get();
-        if (object == null) {
-            throw new DIException("Provider '%s' returned null object", providerType
-                    .getName());
-        }
-
-        return object;
+        return providerOfProviders.get().get();
     }
 }

Modified: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultBindingBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultBindingBuilder.java?rev=884078&r1=884077&r2=884078&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultBindingBuilder.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultBindingBuilder.java
Wed Nov 25 12:48:36 2009
@@ -42,7 +42,7 @@
 
     public BindingBuilder<T> to(Class<? extends T> implementation) throws DIException
{
 
-        Provider<T> provider0 = new ConstructorProvider<T>(
+        Provider<T> provider0 = new ConstructorInjectingProvider<T>(
                 interfaceType,
                 implementation,
                 injector);
@@ -57,10 +57,17 @@
     public BindingBuilder<T> toProvider(
             Class<? extends Provider<? extends T>> providerType) {
 
-        Provider<T> provider0 = new ProviderConstructorProvider<T>(providerType);
-        Provider<T> provider1 = new FieldInjectingProvider<T>(provider0, injector,
key);
+        Provider<Provider<? extends T>> provider0 = new ConstructorInjectingProvidersProvider<T>(
+                providerType);
+        Provider<Provider<? extends T>> provider1 = new FieldInjectingProvider<Provider<?
extends T>>(
+                provider0,
+                injector,
+                key);
 
-        return bindInScope(provider1);
+        Provider<T> provider2 = new CustomProvidersProvider<T>(provider1);
+        Provider<T> provider3 = new FieldInjectingProvider<T>(provider2, injector,
key);
+
+        return bindInScope(provider3);
     }
 
     public void in(Scope scope) {

Modified: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/CayenneModule.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/CayenneModule.java?rev=884078&r1=884077&r2=884078&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/CayenneModule.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/CayenneModule.java
Wed Nov 25 12:48:36 2009
@@ -19,12 +19,14 @@
 package org.apache.cayenne.runtime;
 
 import org.apache.cayenne.DataChannel;
+import org.apache.cayenne.conf.Configuration;
 import org.apache.cayenne.di.Binder;
 import org.apache.cayenne.di.Module;
 import org.apache.cayenne.di.Scopes;
+import org.apache.cayenne.runtime.access.DataChannelProvider;
+import org.apache.cayenne.runtime.configuration.ConfigurationProvider;
 import org.apache.cayenne.runtime.resource.ClassLoaderResourceLocator;
 import org.apache.cayenne.runtime.resource.ResourceLocator;
-import org.apache.cayenne.runtime.spi.DataChannelProvider;
 
 /**
  * A DI module containing all Cayenne framework configurations.
@@ -35,9 +37,10 @@
 
     public void configure(Binder binder) {
 
-        binder.bind(ResourceLocator.class).to(ClassLoaderResourceLocator.class);
-
-        // TODO: bogus impl
+        binder.bind(ResourceLocator.class).to(ClassLoaderResourceLocator.class).in(
+                Scopes.SINGLETON);
+        binder.bind(Configuration.class).toProvider(ConfigurationProvider.class).in(
+                Scopes.SINGLETON);
         binder.bind(DataChannel.class).toProvider(DataChannelProvider.class).in(
                 Scopes.SINGLETON);
     }

Copied: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/access/DataChannelProvider.java
(from r884055, cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/runtime/CayenneModuleTest.java)
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/access/DataChannelProvider.java?p2=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/access/DataChannelProvider.java&p1=cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/runtime/CayenneModuleTest.java&r1=884055&r2=884078&rev=884078&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/runtime/CayenneModuleTest.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/access/DataChannelProvider.java
Wed Nov 25 12:48:36 2009
@@ -16,24 +16,23 @@
  *  specific language governing permissions and limitations
  *  under the License.
  ****************************************************************/
-package org.apache.cayenne.runtime;
-
-import junit.framework.TestCase;
+package org.apache.cayenne.runtime.access;
 
 import org.apache.cayenne.DataChannel;
-import org.apache.cayenne.di.DIBootstrap;
-import org.apache.cayenne.di.Injector;
-
-public class CayenneModuleTest extends TestCase {
-
-    public void testDataChannelBound() {
-        CayenneModule module = new CayenneModule();
-
-        Injector injector = DIBootstrap.createInjector(module);
+import org.apache.cayenne.conf.Configuration;
+import org.apache.cayenne.di.DIException;
+import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.di.Provider;
+
+/**
+ * @since 3.1
+ */
+public class DataChannelProvider implements Provider<DataChannel> {
+    
+    @Inject
+    private Configuration configuration;
 
-        DataChannel channel = injector.getInstance(DataChannel.class);
-        assertSame("DataChannel must be a singleton", channel, injector
-                .getInstance(DataChannel.class));
-        // assertTrue(channel instanceof DataDomain);
+    public DataChannel get() throws DIException {
+        return configuration.getDomain();
     }
 }

Added: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/configuration/ConfigurationProvider.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/configuration/ConfigurationProvider.java?rev=884078&view=auto
==============================================================================
--- cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/configuration/ConfigurationProvider.java
(added)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/configuration/ConfigurationProvider.java
Wed Nov 25 12:48:36 2009
@@ -0,0 +1,108 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.cayenne.runtime.configuration;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Collection;
+
+import org.apache.cayenne.conf.ConfigLoader;
+import org.apache.cayenne.conf.ConfigLoaderDelegate;
+import org.apache.cayenne.conf.Configuration;
+import org.apache.cayenne.conf.RuntimeLoadDelegate;
+import org.apache.cayenne.di.DIException;
+import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.di.Provider;
+import org.apache.cayenne.runtime.resource.Resource;
+import org.apache.cayenne.runtime.resource.ResourceLocator;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class ConfigurationProvider implements Provider<Configuration> {
+
+    private static Log logger = LogFactory.getLog(ConfigurationProvider.class);
+
+    @Inject
+    private ResourceLocator resourceLocator;
+
+    protected String getConfigurationName() {
+        return Configuration.DEFAULT_DOMAIN_FILE;
+    }
+
+    public Configuration get() throws DIException {
+
+        logger.debug("starting configuration loading");
+
+        String configurationName = getConfigurationName();
+        Collection<Resource> configurations = resourceLocator
+                .findResources(configurationName);
+
+        if (configurations.isEmpty()) {
+            throw new DIException(
+                    "[%s] : Configuration file \"%s\" is not found.",
+                    getClass().getName(),
+                    configurationName);
+        }
+
+        Resource configurationResource = configurations.iterator().next();
+        URL configurationURL = configurationResource.getURL();
+
+        // no support for multiple configs yet, but this is not a hard error
+        if (configurations.size() > 1) {
+            logger.info(String.format(
+                    "found %d Cayenne configurations, will use the first one: %s",
+                    configurations.size(),
+                    configurationURL));
+        }
+
+        Configuration configuration = new DIConfiguration();
+
+        ConfigLoaderDelegate delegate = new RuntimeLoadDelegate(
+                configuration,
+                configuration.getLoadStatus());
+
+        InputStream in = null;
+        try {
+            in = configurationURL.openStream();
+            new ConfigLoader(delegate).loadDomains(in);
+        }
+        catch (Exception e) {
+            throw new DIException(
+                    "Error loading configuration from %s",
+                    e,
+                    configurationURL);
+        }
+        finally {
+            try {
+                if (in != null) {
+                    in.close();
+                }
+            }
+            catch (IOException ioex) {
+                logger.info(String.format(
+                        "failure closing input stream for %s, ignoring",
+                        configurationURL), ioex);
+            }
+        }
+
+        logger.debug("finsihed configuration loading");
+        return configuration;
+    }
+}

Copied: cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/configuration/DIConfiguration.java
(from r884055, cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/runtime/CayenneModuleTest.java)
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/configuration/DIConfiguration.java?p2=cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/configuration/DIConfiguration.java&p1=cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/runtime/CayenneModuleTest.java&r1=884055&r2=884078&rev=884078&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-di/src/test/java/org/apache/cayenne/runtime/CayenneModuleTest.java
(original)
+++ cayenne/sandbox/cayenne-di/src/main/java/org/apache/cayenne/runtime/configuration/DIConfiguration.java
Wed Nov 25 12:48:36 2009
@@ -16,24 +16,24 @@
  *  specific language governing permissions and limitations
  *  under the License.
  ****************************************************************/
-package org.apache.cayenne.runtime;
+package org.apache.cayenne.runtime.configuration;
 
-import junit.framework.TestCase;
+import org.apache.cayenne.conf.Configuration;
+import org.apache.cayenne.conf.ResourceFinder;
 
-import org.apache.cayenne.DataChannel;
-import org.apache.cayenne.di.DIBootstrap;
-import org.apache.cayenne.di.Injector;
-
-public class CayenneModuleTest extends TestCase {
-
-    public void testDataChannelBound() {
-        CayenneModule module = new CayenneModule();
-
-        Injector injector = DIBootstrap.createInjector(module);
+/**
+ * @since 3.1
+ */
+class DIConfiguration extends Configuration {
+
+    @Override
+    protected ResourceFinder getResourceFinder() {
+        throw new UnsupportedOperationException();
+    }
 
-        DataChannel channel = injector.getInstance(DataChannel.class);
-        assertSame("DataChannel must be a singleton", channel, injector
-                .getInstance(DataChannel.class));
-        // assertTrue(channel instanceof DataDomain);
+    @Override
+    public void initialize() throws Exception {
+        throw new UnsupportedOperationException();
     }
+
 }



Mime
View raw message