cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmen...@apache.org
Subject svn commit: r770805 - in /cayenne/main/trunk/framework: cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/ cayenne-tools/ cayenne-tools/src/main/java/org/apache/cayenne/access/ cayenne-tools/src/main/java/org/apache/cayenne/tools/ cayen...
Date Fri, 01 May 2009 20:06:12 GMT
Author: kmenard
Date: Fri May  1 20:06:11 2009
New Revision: 770805

URL: http://svn.apache.org/viewvc?rev=770805&view=rev
Log:
Fixed CAY-1197: ANT task for reverse engineering

Added:
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneTask.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java
    cayenne/main/trunk/framework/cayenne-tools/pom.xml
    cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/access/AbstractDbLoaderDelegate.java
    cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/access/AbstractDbLoaderDelegateTest.java
    cayenne/main/trunk/framework/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/DbImporterMojo.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneTask.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneTask.java?rev=770805&r1=770804&r2=770805&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneTask.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/CayenneTask.java
Fri May  1 20:06:11 2009
@@ -20,18 +20,33 @@
 package org.apache.cayenne.tools;
 
 import org.apache.tools.ant.Task;
+import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.types.Path;
 import org.apache.tools.ant.types.Reference;
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.MapLoader;
+import org.apache.cayenne.util.Util;
+import org.apache.cayenne.dba.DbAdapter;
+import org.xml.sax.InputSource;
+
+import java.io.File;
 
 /**
  * Base task for all Cayenne ant tasks, providing support for common configuration items.
  *
  * @since 1.2
  */
-public class CayenneTask extends Task
+public abstract class CayenneTask extends Task
 {
     protected Path classpath;
 
+    protected DbAdapter adapter;
+    protected File map;
+    protected String driver;
+    protected String url;
+    protected String userName;
+    protected String password;
+
     /**
      * Sets the classpath used by the task.
      *
@@ -62,4 +77,80 @@
 
         return classpath.createPath();
     }
+
+    /**
+     * Sets the map.
+     *
+     * @param map The map to set
+     */
+    public void setMap(File map) {
+        this.map = map;
+    }
+
+    /**
+     * Sets the db adapter.
+     *
+     * @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);
+            }
+        }
+    }
+
+    /**
+     * Sets the JDBC driver used to connect to the database server.
+     *
+     * @param driver The driver to set.
+     */
+    public void setDriver(String driver) {
+        this.driver = driver;
+    }
+
+    /**
+     * Sets the JDBC URL used to connect to the database server.
+     *
+     * @param url The url to set.
+     */
+    public void setUrl(String url) {
+        this.url = url;
+    }
+
+    /**
+     * Sets the username used to connect to the database server.
+     *
+     * @param username The username to set.
+     */
+    public void setUserName(String username) {
+        this.userName = username;
+    }
+
+    /**
+     * Sets the password used to connect to the database server.
+     *
+     * @param password The password to set.
+     */
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+        /** Loads and returns DataMap based on <code>map</code> attribute. */
+    protected DataMap loadDataMap() throws Exception {
+        InputSource in = new InputSource(map.getCanonicalPath());
+        return new MapLoader().loadDataMap(in);
+    }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java?rev=770805&r1=770804&r2=770805&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/tools/DbGeneratorTask.java
Fri May  1 20:06:11 2009
@@ -21,17 +21,12 @@
 
 import org.apache.cayenne.access.DbGenerator;
 import org.apache.cayenne.conn.DriverDataSource;
-import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.dba.JdbcAdapter;
 import org.apache.cayenne.map.DataMap;
-import org.apache.cayenne.map.MapLoader;
 import org.apache.cayenne.util.Util;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
-import org.xml.sax.InputSource;
 
-
-import java.io.File;
 import java.sql.Driver;
 
 /**
@@ -42,13 +37,6 @@
  */
 // TODO: support classpath attribute for loading the driver
 public class DbGeneratorTask extends CayenneTask {
-    
-    protected DbAdapter adapter;
-    protected File map;
-    protected String driver;
-    protected String url;
-    protected String userName;
-    protected String password;
 
     // DbGenerator options... setup defaults similar to DbGenerator itself:
     // all DROP set to false, all CREATE - to true
@@ -102,7 +90,7 @@
                 message += ": " + th.getLocalizedMessage();
             }
 
-            super.log(message);
+            log(message, Project.MSG_ERR);
             throw new BuildException(message, th);
         }
         finally{
@@ -134,12 +122,6 @@
         }
     }
 
-    /** Loads and returns DataMap based on <code>map</code> attribute. */
-    protected DataMap loadDataMap() throws Exception {
-        InputSource in = new InputSource(map.getCanonicalPath());
-        return new MapLoader().loadDataMap(in);
-    }
-
     public void setCreateFK(boolean createFK) {
         this.createFK = createFK;
     }
@@ -159,74 +141,4 @@
     public void setDropTables(boolean dropTables) {
         this.dropTables = dropTables;
     }
-
-    /**
-     * Sets the map.
-     * 
-     * @param map The map to set
-     */
-    public void setMap(File map) {
-        this.map = map;
-    }
-
-    /**
-     * Sets the db adapter.
-     * 
-     * @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);
-            }
-        }
-    }
-
-    /**
-     * Sets the JDBC driver used to connect to the database server.
-     * 
-     * @param driver The driver to set.
-     */
-    public void setDriver(String driver) {
-        this.driver = driver;
-    }
-
-    /**
-     * Sets the JDBC URL used to connect to the database server.
-     * 
-     * @param url The url to set.
-     */
-    public void setUrl(String url) {
-        this.url = url;
-    }
-
-    /**
-     * Sets the username used to connect to the database server.
-     * 
-     * @param username The username to set.
-     */
-    public void setUserName(String username) {
-        this.userName = username;
-    }
-
-    /**
-     * Sets the password used to connect to the database server.
-     * 
-     * @param password The password to set.
-     */
-    public void setPassword(String password) {
-        this.password = password;
-    }
 }

Modified: cayenne/main/trunk/framework/cayenne-tools/pom.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/pom.xml?rev=770805&r1=770804&r2=770805&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/pom.xml (original)
+++ cayenne/main/trunk/framework/cayenne-tools/pom.xml Fri May  1 20:06:11 2009
@@ -39,6 +39,11 @@
   			<artifactId>junit</artifactId>
   		</dependency>
 
+        <dependency>
+			<groupId>ant</groupId>
+			<artifactId>ant</artifactId>
+		</dependency>
+
   		<dependency>
   			<groupId>commons-collections</groupId>
   			<artifactId>commons-collections</artifactId>

Modified: cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/access/AbstractDbLoaderDelegate.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/access/AbstractDbLoaderDelegate.java?rev=770805&r1=770804&r2=770805&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/access/AbstractDbLoaderDelegate.java
(original)
+++ cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/access/AbstractDbLoaderDelegate.java
Fri May  1 20:06:11 2009
@@ -1,3 +1,22 @@
+/*****************************************************************
+ *   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.access;
 
 import org.apache.cayenne.map.DbEntity;

Added: cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java?rev=770805&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java
(added)
+++ cayenne/main/trunk/framework/cayenne-tools/src/main/java/org/apache/cayenne/tools/DbImporterTask.java
Fri May  1 20:06:11 2009
@@ -0,0 +1,182 @@
+/*****************************************************************
+ *   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.tools;
+
+import org.apache.cayenne.conn.DriverDataSource;
+import org.apache.cayenne.access.DbLoader;
+import org.apache.cayenne.access.AbstractDbLoaderDelegate;
+import org.apache.cayenne.map.naming.NamingStrategy;
+import org.apache.cayenne.map.DataMap;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.util.DeleteRuleUpdater;
+import org.apache.cayenne.util.Util;
+import org.apache.cayenne.CayenneException;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+
+import java.io.PrintWriter;
+import java.sql.Driver;
+
+public class DbImporterTask extends CayenneTask {
+
+    // DbImporter options.
+    private boolean overwriteExisting = true;
+    private String schemaName;
+    private String tablePattern;
+    private boolean importProcedures = false;
+    private String procedurePattern;
+    private boolean meaningfulPk = false;
+    private String namingStrategy = "org.apache.cayenne.map.naming.SmartNamingStrategy";
+
+    @Override
+    public void execute() {
+
+        log(String.format("connection settings - [driver: %s, url: %s, username: %s, password:
%s]", driver, url, userName, password), Project.MSG_VERBOSE);
+
+        log(String.format("importer options - [map: %s, overwriteExisting: %s, schemaName:
%s, tablePattern: %s, importProcedures: %s, procedurePattern: %s, meaningfulPk: %s, namingStrategy:
%s]",
+                map, overwriteExisting, schemaName, tablePattern, importProcedures, procedurePattern,
meaningfulPk, namingStrategy), Project.MSG_VERBOSE);
+
+        validateAttributes();
+
+        try {
+
+            // load driver taking custom CLASSPATH into account...
+            DriverDataSource dataSource = new DriverDataSource((Driver) Class.forName(driver).newInstance(),
url, userName, password);
+
+            // Load the data map and run the db importer.
+            final LoaderDelegate loaderDelegate = new LoaderDelegate();
+            final DbLoader loader = new DbLoader(dataSource.getConnection(), adapter, loaderDelegate);
+            loader.setCreatingMeaningfulPK(meaningfulPk);
+
+            if (namingStrategy != null) {
+                final NamingStrategy namingStrategyInst = (NamingStrategy) Class.forName(namingStrategy).newInstance();
+                loader.setNamingStrategy(namingStrategyInst);
+            }
+
+            final DataMap dataMap = map.exists() ? loadDataMap() : new DataMap();
+            loader.loadDataMapFromDB(schemaName, tablePattern, dataMap);
+
+            for (ObjEntity addedObjEntity : loaderDelegate.getAddedObjEntities()) {
+                DeleteRuleUpdater.updateObjEntity(addedObjEntity);
+            }
+
+            if (importProcedures) {
+                loader.loadProceduresFromDB(schemaName, procedurePattern, dataMap);
+            }
+
+            // Write the new DataMap out to disk.
+            map.delete();
+            PrintWriter pw = new PrintWriter(map);
+            dataMap.encodeAsXML(pw);
+            pw.close();
+        } catch (final Exception ex) {
+            final Throwable th = Util.unwindException(ex);
+
+            String message = "Error importing database schema";
+
+            if (th.getLocalizedMessage() != null) {
+                message += ": " + th.getLocalizedMessage();
+            }
+
+            log(message, Project.MSG_ERR);
+            throw new BuildException(message, th);
+        }
+    }
+
+    /**
+     * Validates atttributes that are not related to internal DefaultClassGenerator.
+     * Throws BuildException if attributes are invalid.
+     */
+    protected void validateAttributes() throws BuildException {
+        StringBuilder error = new StringBuilder("");
+
+        if (map == null) {
+            error.append("The 'map' attribute must be set.\n");
+        }
+
+        if (driver == null) {
+            error.append("The 'driver' attribute must be set.\n");
+        }
+
+        if (url == null) {
+            error.append("The 'adapter' attribute must be set.\n");
+        }
+
+        if (error.length() > 0) {
+            throw new BuildException(error.toString());
+        }
+    }
+
+    public void setOverwriteExisting(boolean overwriteExisting) {
+        this.overwriteExisting = overwriteExisting;
+    }
+
+    public void setSchemaName(String schemaName) {
+        this.schemaName = schemaName;
+    }
+
+    public void setTablePattern(String tablePattern) {
+        this.tablePattern = tablePattern;
+    }
+
+    public void setImportProcedures(boolean importProcedures) {
+        this.importProcedures = importProcedures;
+    }
+
+    public void setProcedurePattern(String procedurePattern) {
+        this.procedurePattern = procedurePattern;
+    }
+
+    public void setMeaningfulPk(boolean meaningfulPk) {
+        this.meaningfulPk = meaningfulPk;
+    }
+
+    public void setNamingStrategy(String namingStrategy) {
+        this.namingStrategy = namingStrategy;
+    }
+
+    final class LoaderDelegate extends AbstractDbLoaderDelegate {
+
+        public boolean overwriteDbEntity(final DbEntity ent) throws CayenneException {
+            return overwriteExisting;
+        }
+
+        public void dbEntityAdded(final DbEntity ent) {
+            super.dbEntityAdded(ent);
+            log("Added DB entity: " + ent.getName());
+        }
+
+        public void dbEntityRemoved(final DbEntity ent) {
+            super.dbEntityRemoved(ent);
+            log("Removed DB entity: " + ent.getName());
+        }
+
+        public void objEntityAdded(final ObjEntity ent) {
+            super.objEntityAdded(ent);
+            log("Added obj entity: " + ent.getName());
+        }
+
+        public void objEntityRemoved(final ObjEntity ent) {
+            super.objEntityRemoved(ent);
+            log("Removed obj entity: " + ent.getName());
+        }
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/access/AbstractDbLoaderDelegateTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/access/AbstractDbLoaderDelegateTest.java?rev=770805&r1=770804&r2=770805&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/access/AbstractDbLoaderDelegateTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-tools/src/test/java/org/apache/cayenne/access/AbstractDbLoaderDelegateTest.java
Fri May  1 20:06:11 2009
@@ -1,3 +1,22 @@
+/*****************************************************************
+ *   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.access;
 
 import junit.framework.TestCase;
@@ -8,7 +27,6 @@
 
 import java.util.Arrays;
 import java.util.List;
-import java.util.ArrayList;
 
 public class AbstractDbLoaderDelegateTest extends TestCase {
 

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=770805&r1=770804&r2=770805&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
Fri May  1 20:06:11 2009
@@ -1,3 +1,22 @@
+/*****************************************************************
+ *   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.tools;
 
 import org.apache.maven.plugin.AbstractMojo;
@@ -9,7 +28,6 @@
 import org.apache.cayenne.map.MapLoader;
 import org.apache.cayenne.map.naming.NamingStrategy;
 import org.apache.cayenne.access.DbLoader;
-import org.apache.cayenne.access.DbLoaderDelegate;
 import org.apache.cayenne.access.AbstractDbLoaderDelegate;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.dba.JdbcAdapter;
@@ -23,8 +41,6 @@
 import java.io.File;
 import java.io.PrintWriter;
 import java.sql.Driver;
-import java.util.List;
-import java.util.ArrayList;
 
 /**
  * Maven mojo to reverse engineer datamap from DB.
@@ -189,8 +205,8 @@
             loader.loadDataMapFromDB(schemaName, tablePattern, dataMap);
 
             for (ObjEntity addedObjEntity : loaderDelegate.getAddedObjEntities()) {
-                    DeleteRuleUpdater.updateObjEntity(addedObjEntity);
-                }
+                DeleteRuleUpdater.updateObjEntity(addedObjEntity);
+            }
 
             if (importProcedures) {
                 loader.loadProceduresFromDB(schemaName, procedurePattern, dataMap);



Mime
View raw message