cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dkazimirc...@apache.org
Subject svn commit: r1170686 - in /cayenne/main/trunk/framework: cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/ cayenne-tools/src/main/java/org/apache/cayenne/tools/ maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/
Date Wed, 14 Sep 2011 15:28:00 GMT
Author: dkazimirchyk
Date: Wed Sep 14 15:28:00 2011
New Revision: 1170686

URL: http://svn.apache.org/viewvc?rev=1170686&view=rev
Log:
CAY-1618 Create database adapters instances with proper injection

added tool DI module to create adapters in cayenne-tools and cayenne-maven-plugin with proper
injection

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/ToolModule.java
Modified:
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneTask.java
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java
    cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbGeneratorMojo.java
    cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/ToolModule.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/ToolModule.java?rev=1170686&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/ToolModule.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/ToolModule.java
Wed Sep 14 15:28:00 2011
@@ -0,0 +1,48 @@
+/*****************************************************************
+ *   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.configuration;
+
+import org.apache.cayenne.access.jdbc.BatchQueryBuilderFactory;
+import org.apache.cayenne.access.jdbc.DefaultBatchQueryBuilderFactory;
+import org.apache.cayenne.di.AdhocObjectFactory;
+import org.apache.cayenne.di.Binder;
+import org.apache.cayenne.di.Module;
+import org.apache.cayenne.di.spi.DefaultAdhocObjectFactory;
+import org.apache.cayenne.log.CommonsJdbcEventLogger;
+import org.apache.cayenne.log.JdbcEventLogger;
+
+/**
+ * A DI module containing basic Cayenne configuration.
+ * 
+ * @since 3.1
+ */
+public class ToolModule implements Module {
+    
+public void configure(Binder binder) {
+        
+        // configure empty global stack properties
+        binder.bindMap(DefaultRuntimeProperties.PROPERTIES_MAP);
+
+        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
+        binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
+        binder.bind(BatchQueryBuilderFactory.class).to(DefaultBatchQueryBuilderFactory.class);
+        binder.bind(JdbcEventLogger.class).to(CommonsJdbcEventLogger.class);
+    }
+
+}

Modified: cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneTask.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneTask.java?rev=1170686&r1=1170685&r2=1170686&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneTask.java
(original)
+++ cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/CayenneTask.java
Wed Sep 14 15:28:00 2011
@@ -26,7 +26,11 @@ import org.apache.tools.ant.types.Refere
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.MapLoader;
 import org.apache.cayenne.util.Util;
+import org.apache.cayenne.configuration.ToolModule;
 import org.apache.cayenne.dba.DbAdapter;
+import org.apache.cayenne.di.AdhocObjectFactory;
+import org.apache.cayenne.di.DIBootstrap;
+import org.apache.cayenne.di.Injector;
 import org.xml.sax.InputSource;
 
 import java.io.File;
@@ -93,22 +97,11 @@ public abstract class CayenneTask extend
      * @param adapter The db adapter to set.
      */
     public void setAdapter(String adapter) {
-        ClassLoader loader = null;
         if (adapter != null) {
-            // Try to create an instance of the DB adapter.
-            try {
-                loader = Thread.currentThread().getContextClassLoader();
-                Thread.currentThread().setContextClassLoader(DbGeneratorTask.class.getClassLoader());
-
-                Class<?> c = Util.getJavaClass(adapter);
-                this.adapter = (DbAdapter) c.newInstance();
-            }
-            catch (Exception e) {
-                throw new BuildException("Can't load DbAdapter: " + adapter,e);
-            }
-            finally{
-                Thread.currentThread().setContextClassLoader(loader);
-            }
+            Injector injector = DIBootstrap.createInjector(new ToolModule());
+            AdhocObjectFactory objectFactory = injector.getInstance(AdhocObjectFactory.class);
+            
+            this.adapter = objectFactory.newInstance(DbAdapter.class, adapter);
         }
     }
 

Modified: cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java?rev=1170686&r1=1170685&r2=1170686&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java
(original)
+++ cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java
Wed Sep 14 15:28:00 2011
@@ -20,8 +20,13 @@
 package org.apache.cayenne.tools;
 
 import org.apache.cayenne.access.DbGenerator;
+import org.apache.cayenne.configuration.ToolModule;
 import org.apache.cayenne.conn.DriverDataSource;
+import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.dba.JdbcAdapter;
+import org.apache.cayenne.di.AdhocObjectFactory;
+import org.apache.cayenne.di.DIBootstrap;
+import org.apache.cayenne.di.Injector;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.util.Util;
 import org.apache.tools.ant.BuildException;
@@ -48,10 +53,13 @@ public class DbGeneratorTask extends Cay
 
     @Override
     public void execute() {
+        
+        Injector injector = DIBootstrap.createInjector(new ToolModule());
+        AdhocObjectFactory objectFactory = injector.getInstance(AdhocObjectFactory.class);
 
         // prepare defaults
         if (adapter == null) {
-            adapter = new JdbcAdapter();
+            adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
         }
         
         log(String.format("connection settings - [driver: %s, url: %s, username: %s]", driver,
url, userName), Project.MSG_VERBOSE);

Modified: cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbGeneratorMojo.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbGeneratorMojo.java?rev=1170686&r1=1170685&r2=1170686&view=diff
==============================================================================
--- cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbGeneratorMojo.java
(original)
+++ cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbGeneratorMojo.java
Wed Sep 14 15:28:00 2011
@@ -20,9 +20,13 @@
 package org.apache.cayenne.tools;
 
 import org.apache.cayenne.access.DbGenerator;
+import org.apache.cayenne.configuration.ToolModule;
 import org.apache.cayenne.conn.DriverDataSource;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.dba.JdbcAdapter;
+import org.apache.cayenne.di.AdhocObjectFactory;
+import org.apache.cayenne.di.DIBootstrap;
+import org.apache.cayenne.di.Injector;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.MapLoader;
 import org.apache.cayenne.util.Util;
@@ -131,6 +135,9 @@ public class DbGeneratorMojo extends Abs
 
 
     public void execute() throws MojoExecutionException, MojoFailureException {
+    	
+    	Injector injector = DIBootstrap.createInjector(new ToolModule());
+    	AdhocObjectFactory objectFactory = injector.getInstance(AdhocObjectFactory.class);
 
 		Log logger = new MavenLogger(this);
 
@@ -140,8 +147,9 @@ public class DbGeneratorMojo extends Abs
                 dropTables, dropPK, createTables, createPK, createFK));
 
         try {
-            final DbAdapter adapterInst = (adapter == null) ? new JdbcAdapter()
-                                                            : (DbAdapter) Class.forName(adapter).newInstance();
+            final DbAdapter adapterInst = (adapter == null) ? 
+            		objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName()) : 
+            		objectFactory.newInstance(DbAdapter.class, adapter);
 
             // Load the data map and run the db generator.
             DataMap dataMap = loadDataMap();

Modified: cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java?rev=1170686&r1=1170685&r2=1170686&view=diff
==============================================================================
--- cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
(original)
+++ cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java
Wed Sep 14 15:28:00 2011
@@ -22,6 +22,7 @@ package org.apache.cayenne.tools;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
+import org.apache.cayenne.configuration.ToolModule;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.ObjEntity;
@@ -31,6 +32,9 @@ import org.apache.cayenne.access.DbLoade
 import org.apache.cayenne.access.AbstractDbLoaderDelegate;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.dba.JdbcAdapter;
+import org.apache.cayenne.di.AdhocObjectFactory;
+import org.apache.cayenne.di.DIBootstrap;
+import org.apache.cayenne.di.Injector;
 import org.apache.cayenne.util.Util;
 import org.apache.cayenne.util.DeleteRuleUpdater;
 import org.apache.cayenne.conn.DriverDataSource;
@@ -175,6 +179,9 @@ public class DbImporterMojo extends Abst
     private File mapFile;
 
     public void execute() throws MojoExecutionException, MojoFailureException {
+    	
+    	Injector injector = DIBootstrap.createInjector(new ToolModule());
+    	AdhocObjectFactory objectFactory = injector.getInstance(AdhocObjectFactory.class);
 
         logger = new MavenLogger(this);
 
@@ -184,8 +191,9 @@ public class DbImporterMojo extends Abst
                 map, overwriteExisting, schemaName, tablePattern, importProcedures, procedurePattern,
meaningfulPk, namingStrategy));
 
         try {
-            final DbAdapter adapterInst = (adapter == null) ? new JdbcAdapter()
-                                                                : (DbAdapter) Class.forName(adapter).newInstance();
+            final DbAdapter adapterInst = (adapter == null) ? 
+                    objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName())
: 
+                    objectFactory.newInstance(DbAdapter.class, adapter);
 
             // load driver taking custom CLASSPATH into account...
             DriverDataSource dataSource = new DriverDataSource((Driver) Class.forName(driver).newInstance(),
url, username, password);



Mime
View raw message