You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@empire-db.apache.org by fr...@apache.org on 2009/11/18 00:56:48 UTC

svn commit: r881628 - in /incubator/empire-db/trunk/empire-db-codegen/src: main/java/org/apache/empire/db/codegen/ main/java/org/apache/empire/db/codegen/util/ test/java/org/apache/empire/db/codegen/util/

Author: francisdb
Date: Tue Nov 17 23:56:48 2009
New Revision: 881628

URL: http://svn.apache.org/viewvc?rev=881628&view=rev
Log:
EMPIREDB-52 - Split up the actual application and the code generator service + some other cleanup

Added:
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenApp.java
Modified:
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGen.java
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/DBUtil.java
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ParserUtil.java
    incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/DBUtilTest.java
    incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/FileUtilsTest.java

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGen.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGen.java?rev=881628&r1=881627&r2=881628&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGen.java (original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGen.java Tue Nov 17 23:56:48 2009
@@ -27,9 +27,9 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.empire.commons.ErrorObject;
 import org.apache.empire.db.DBDatabase;
 import org.apache.empire.db.DBTable;
+import org.apache.empire.db.codegen.util.DBUtil;
 import org.apache.empire.db.codegen.util.FileUtils;
 import org.apache.empire.db.codegen.util.ParserUtil;
 import org.apache.velocity.Template;
@@ -43,19 +43,22 @@
  * database schema. It uses the Empire DB open-source framework to build a java
  * persistence layer for an application. The Apache Velocity template engine is
  * used to create the output interfaces and classes.
- * 
+ * <p>
  * The Empire DB framework doesn't try to hide the underlying database and data
  * model but instead embraces its power by modeling it within java. The result
  * is a persistence layer that uses a more "object-oriented, type safe" SQL to
  * access persistent data.
- * 
- * NOTE: THIS VERSION HAS SEVERE RESTRICTIONS: 1. Only tables are currently
- * modeled (we'll add views to a later version). 2. Table indexes are not yet
- * modeled (exception is primary key). Again, this will be added to later
- * editions. 3. It is assumed that each table has a single INTEGER
- * auto-generated primary key column that has the same name for all tables. 4.
- * It is assumed that each table has a single TIMESTAMP optimistic locking
- * column that has the same name for all tables.
+ * <p>
+ * NOTE: THIS VERSION HAS SEVERE RESTRICTIONS:
+ * <ol>
+ * <li> Only tables are currently modeled (we'll add views to a later version).</li> 
+ * <li> Table indexes are not yet modeled (exception is primary key). Again, 
+ * this will be added to later editions.</li>  
+ * <li> It is assumed that each table has a single INTEGER auto-generated primary
+ * key column that has the same name for all tables. </li> 
+ * <li> It is assumed that each table has a single TIMESTAMP optimistic locking
+ * column that has the same name for all tables.</li> 
+ * </ol>
  */
 
 public class CodeGen {
@@ -69,8 +72,11 @@
 	public static final String BASE_RECORD_TEMPLATE = "BaseRecord.vm";
 	public static final String RECORD_TEMPLATE = "Record.vm";
 
+	// Services
+	private final ParserUtil pUtil;
+	
 	// Properties
-	private CodeGenConfig config;
+	private final CodeGenConfig config;
 	private File baseDir;
 	private File tableDir;
 	private File recordDir;
@@ -78,80 +84,46 @@
 	/**
 	 * Constructor
 	 */
-	public CodeGen() {
+	public CodeGen(CodeGenConfig config) {
 		try {
 			Velocity.init();
 		} catch (Exception e) {
 			log.fatal(e);
 			throw new RuntimeException(e);
 		}
+		this.pUtil = new ParserUtil(config);
+		this.config = config;
 	}
 
 	/**
-	 * <PRE>
-	 * This is the entry point of the Empire-DB Sample Application
-	 * Please check the config.xml configuration file for Database and Connection settings.
-	 * </PRE>
-	 * 
-	 * @param args
-	 *            arguments
+	 * Generates the code according to the provided configuration file
+	 * @param config
 	 */
-	public static void main(String[] args) {
-		Connection conn = null;
-		try {
-			// Init Configuration
-			CodeGenConfig config = new CodeGenConfig();
-			config.init((args.length > 0 ? args[0] : "config.xml"));
-
-			// Enable Exceptions
-			ErrorObject.setExceptionsEnabled(true);
+	public void generate(){
 
+		Connection conn = null;
+		try {			
 			// Get a JDBC Connection
-			conn = getJDBCConnection(config);
-
-			// List options
-			log.info("Database connection successful. Config options are:");
-			log.info("SchemaName=" + String.valueOf(config.getDbSchema()));
-			log.info("TimestampColumn="
-					+ String.valueOf(config.getTimestampColumn()));
-			log.info("TargetFolder=" + config.getTargetFolder());
-			log.info("PackageName=" + config.getPackageName());
-			log.info("DbClassName=" + config.getDbClassName());
-			log.info("TableBaseName=" + config.getTableBaseName());
-			log.info("ViewBaseName=" + config.getViewBaseName());
-			log.info("RecordBaseName=" + config.getRecordBaseName());
-			log.info("TableClassPrefix=" + config.getTableClassPrefix());
-			log.info("ViewClassPrefi=" + config.getViewClassPrefix());
-			log.info("NestTable=" + config.isNestTables());
-			log.info("NestViews=" + config.isNestViews());
-			log.info("CreateRecordProperties="
-					+ config.isCreateRecordProperties());
-
-			if (config.getTableClassPrefix() == null)
-				config.setTableClassPrefix("");
-
-			if (config.getTableClassSuffix() == null)
-				config.setTableClassSuffix("");
-
-			CodeGen codeGen = new CodeGen();
-			// create the database in the memory
-			DBDatabase db = codeGen.parseDataModel(conn, config);
-
+			conn = getJDBCConnection();
+			
+			// create the database in memory
+			DBDatabase db = parseDataModel(conn);
+			
 			// create the source-code for that database
-			codeGen.generateCodeFiles(db, config);
-
+			generateCodeFiles(db);
+			
 			log.info("Code generation completed sucessfully!");
-
-		} catch (Exception e) {
-			// Error
+		} 
+		catch (Exception e) 
+		{
 			log.error(e.getMessage(), e);
-		} finally {
-			// done
-			if (conn != null)
-				close(conn);
+		} 
+		finally 
+		{
+			DBUtil.close(conn, log);
 		}
 	}
-
+	
 	/**
 	 * <PRE>
 	 * Opens and returns a JDBC-Connection.
@@ -159,7 +131,7 @@
 	 * Please use the config.xml file to change connection params.
 	 * </PRE>
 	 */
-	private static Connection getJDBCConnection(CodeGenConfig config) {
+	private Connection getJDBCConnection() {
 		// Establish a new database connection
 		Connection conn = null;
 		log.info("Connecting to Database'" + config.getJdbcURL() + "' / User="
@@ -183,27 +155,13 @@
 		return conn;
 	}
 
-	/**
-	 * Closes a JDBC-Connection.
-	 */
-	private static void close(Connection conn) {
-		log.info("Closing database connection");
-		try {
-			conn.close();
-		} catch (Exception e) {
-			log.fatal("Error closing connection", e);
-		}
-	}
-
-	public DBDatabase parseDataModel(Connection conn, CodeGenConfig config) {
-		this.config = config;
+	public DBDatabase parseDataModel(Connection conn) {
 		CodeGenParser cgp = new CodeGenParser(conn, config);
 		DBDatabase memoryDB = cgp.getDb();
 		return memoryDB;
 	}
 
-	public void generateCodeFiles(DBDatabase db, CodeGenConfig config) {
-		this.config = config;
+	public void generateCodeFiles(DBDatabase db) {
 
 		// Prepare directories for generated source files
 		this.initDirectories(config.getTargetFolder(), config.getPackageName());
@@ -217,7 +175,8 @@
 		// Create base record class
 		this.createBaseRecordClass(db);
 		// Create table classes, record interfaces and record classes
-		for (DBTable table : db.getTables()) {
+		for (DBTable table : db.getTables()) 
+		{
 			this.createTableClass(db, table);
 			this.createRecordClass(db, table);
 		}
@@ -250,7 +209,6 @@
 	}
 
 	private void createDatabaseClass(DBDatabase db) {
-		ParserUtil pUtil = new ParserUtil(config);
 		File file = new File(baseDir, config.getDbClassName() + ".java");
 		VelocityContext context = new VelocityContext();
 		context.put("parser", pUtil);
@@ -271,7 +229,6 @@
 	}
 
 	private void createTableClass(DBDatabase db, DBTable table) {
-		ParserUtil pUtil = new ParserUtil(config);
 		File file = new File(tableDir, pUtil.getTableClassName(table.getName())
 				+ ".java");
 		VelocityContext context = new VelocityContext();
@@ -296,7 +253,6 @@
 	}
 
 	private void createRecordClass(DBDatabase db, DBTable table) {
-		ParserUtil pUtil = new ParserUtil(config);
 		File file = new File(recordDir, pUtil.getRecordClassName(table
 				.getName())
 				+ ".java");

Added: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenApp.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenApp.java?rev=881628&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenApp.java (added)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenApp.java Tue Nov 17 23:56:48 2009
@@ -0,0 +1,101 @@
+/*
+ * 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.empire.db.codegen;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.empire.commons.ErrorObject;
+
+/**
+ * Console code generator application, takes the config file as first argument.
+ *
+ */
+public class CodeGenApp {
+	
+	private static final Log log = LogFactory.getLog(CodeGenApp.class);
+	
+	private static final String DEFAULT_CONFIG_FILE = "config.xml";
+	
+	
+	/**
+	 * This is the entry point of the Code generator Sample Application.
+	 * Please check the config.xml configuration file for Database and Connection settings.
+	 * 
+	 * @param args
+	 *            arguments
+	 */
+	public static void main(String[] args) {
+		CodeGenApp app = new CodeGenApp();
+		app.start((args.length > 0 ? args[0] : DEFAULT_CONFIG_FILE));
+	}	
+	
+	/**
+	 * Starts the actual generation according to the provided config file
+	 */
+	private void start(final String file){
+		// load configuration file
+		CodeGenConfig config = loadConfig(file);
+
+		// log all options
+		listOptions(config);
+		
+		CodeGen codeGen = new CodeGen(config);
+		codeGen.generate();
+	}
+	
+	/**
+	 * Loads the configuration file and
+	 * @param configFile
+	 * @return
+	 */
+	private CodeGenConfig loadConfig(String configFile){
+		// Init Configuration
+		CodeGenConfig config = new CodeGenConfig();
+		config.init(configFile);
+
+		// Enable Exceptions
+		ErrorObject.setExceptionsEnabled(true);
+
+		if (config.getTableClassPrefix() == null)
+			config.setTableClassPrefix("");
+
+		if (config.getTableClassSuffix() == null)
+			config.setTableClassSuffix("");
+		
+		return config;
+	}
+	
+	private void listOptions(CodeGenConfig config){
+		// List options
+		log.info("Database connection successful. Config options are:");
+		log.info("SchemaName=" + String.valueOf(config.getDbSchema()));
+		log.info("TimestampColumn="	+ String.valueOf(config.getTimestampColumn()));
+		log.info("TargetFolder=" + config.getTargetFolder());
+		log.info("PackageName=" + config.getPackageName());
+		log.info("DbClassName=" + config.getDbClassName());
+		log.info("TableBaseName=" + config.getTableBaseName());
+		log.info("ViewBaseName=" + config.getViewBaseName());
+		log.info("RecordBaseName=" + config.getRecordBaseName());
+		log.info("TableClassPrefix=" + config.getTableClassPrefix());
+		log.info("ViewClassPrefi=" + config.getViewClassPrefix());
+		log.info("NestTable=" + config.isNestTables());
+		log.info("NestViews=" + config.isNestViews());
+		log.info("CreateRecordProperties=" + config.isCreateRecordProperties());
+	}
+}

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/DBUtil.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/DBUtil.java?rev=881628&r1=881627&r2=881628&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/DBUtil.java (original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/DBUtil.java Tue Nov 17 23:56:48 2009
@@ -18,6 +18,7 @@
  */
 package org.apache.empire.db.codegen.util;
 
+import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 
@@ -32,7 +33,7 @@
 	}
 	
 	/**
-	 * Closes a sql resultset and logs exceptions
+	 * Closes a sql resultset and logs exceptions.
 	 * 
 	 * @param rs the resultset to close
 	 * @param log the logger instance to use for logging
@@ -45,8 +46,22 @@
 				rs.close();
 			b = true;
 		} catch (SQLException e) {
-			log.error("The resultset could not bel closed!", e);
+			log.error("The resultset could not be closed!", e);
 		}
 		return b;
 	}
+	
+	/**
+	 * Closes a JDBC-Connection and logs exceptions.
+	 */
+	public static void close(Connection conn, Log log) {
+		if(conn != null){
+			log.info("Closing database connection");
+			try {
+				conn.close();
+			} catch (Exception e) {
+				log.fatal("Error closing connection", e);
+			}
+		}
+	}
 }

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ParserUtil.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ParserUtil.java?rev=881628&r1=881627&r2=881628&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ParserUtil.java (original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/util/ParserUtil.java Tue Nov 17 23:56:48 2009
@@ -16,7 +16,6 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
 package org.apache.empire.db.codegen.util;
 
 import org.apache.commons.logging.Log;
@@ -32,9 +31,10 @@
  */
 public class ParserUtil {
 
-	private CodeGenConfig config;
 	private static final Log log = LogFactory.getLog(ParserUtil.class);
 
+	private CodeGenConfig config;
+	
 	public ParserUtil(CodeGenConfig config) {
 
 		this.config = config;

Modified: incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/DBUtilTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/DBUtilTest.java?rev=881628&r1=881627&r2=881628&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/DBUtilTest.java (original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/DBUtilTest.java Tue Nov 17 23:56:48 2009
@@ -1,3 +1,21 @@
+/*
+ * 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.empire.db.codegen.util;
 
 import static org.junit.Assert.assertFalse;
@@ -16,7 +34,7 @@
 	public void testCloseResultSet() throws SQLException {
 		// null
 		Log log = Mockito.mock(Log.class);
-		boolean succes = DBUtil.close(null, log);
+		boolean succes = DBUtil.close((ResultSet)null, log);
 		assertTrue(succes);
 		
 		// normal
@@ -30,7 +48,7 @@
 		Mockito.doThrow(exception).when(rsFail).close();
 		boolean succes3 = DBUtil.close(rsFail, log);
 		assertFalse(succes3);
-		Mockito.verify(log).error("The resultset could not bel closed!", exception);
+		Mockito.verify(log).error("The resultset could not be closed!", exception);
 	}
 
 }

Modified: incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/FileUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/FileUtilsTest.java?rev=881628&r1=881627&r2=881628&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/FileUtilsTest.java (original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/test/java/org/apache/empire/db/codegen/util/FileUtilsTest.java Tue Nov 17 23:56:48 2009
@@ -1,3 +1,21 @@
+/*
+ * 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.empire.db.codegen.util;
 
 import static org.junit.Assert.assertEquals;