001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.benerator.main;
028:
029: import java.util.Arrays;
030: import java.util.List;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.databene.commons.NumberUtil;
035: import org.databene.commons.RoundedNumberFormat;
036: import org.databene.model.data.Entity;
037: import org.databene.model.data.EntityDescriptor;
038: import org.databene.platform.db.DBSystem;
039: import org.databene.platform.dbunit.DbUnitEntityExporter;
040:
041: /**
042: * Creates a snapshot of a database schema and exports it in DbUnit XML file format.
043: * @since 0.3.04
044: * @author Volker Bergmann
045: */
046: public class DBSnapshotTool {
047:
048: private static final Log logger = LogFactory
049: .getLog(DBSnapshotTool.class);
050:
051: public static void main(String[] args) {
052: logger.info("Starting " + DBSnapshotTool.class.getSimpleName());
053: String filename = (args.length > 0 ? args[0]
054: : "snapshot.dbunit.xml");
055: String fileEncoding = System.getProperty("file.encoding");
056:
057: String dbUrl = System.getProperty("db.url");
058: if (dbUrl == null)
059: throw new IllegalArgumentException(
060: "No database URL specified. "
061: + "Please provide the JDBC URL as an environment property like '-Ddb.url=jdbc:...'");
062: String dbDriver = System.getProperty("db.driver");
063: if (dbDriver == null)
064: throw new IllegalArgumentException(
065: "No database driver specified. "
066: + "Please provide the JDBC driver class name as an environment property like '-Ddb.driver=...'");
067: String dbUser = System.getProperty("db.user");
068: if (dbUser == null)
069: logger.warn("No JDBC user specified");
070: String dbPassword = System.getProperty("db.password");
071: String dbSchema = System.getProperty("db.schema");
072:
073: logger.info("Exporting data of database " + dbUrl
074: + " with driver " + dbDriver + " as user " + dbUser
075: + (dbSchema != null ? " using schema " + dbSchema : "")
076: + " to file " + filename + " using " + fileEncoding);
077:
078: long startTime = System.currentTimeMillis();
079: DbUnitEntityExporter exporter = new DbUnitEntityExporter(
080: filename, fileEncoding);
081:
082: DBSystem db = null;
083: int count = 0;
084: try {
085: db = new DBSystem("db", dbUrl, dbDriver, dbUser, dbPassword);
086: if (dbSchema != null)
087: db.setSchema(dbSchema);
088: List<EntityDescriptor> descriptors = Arrays.asList(db
089: .getTypeDescriptors());
090: logger.info("Starting export");
091: for (EntityDescriptor descriptor : descriptors) {
092: logger.info("Exporting table " + descriptor.getName());
093: for (Entity entity : db.queryEntities(descriptor
094: .getName(), "")) {
095: exporter.startConsuming(entity);
096: count++;
097: }
098: }
099: long duration = System.currentTimeMillis() - startTime;
100: logger.info("Created "
101: + NumberUtil.format(count, 0)
102: + " entities in "
103: + RoundedNumberFormat.format(duration, 0)
104: + " ms ("
105: + RoundedNumberFormat.format(count * 3600000L
106: / duration, 0) + " p.h.)");
107: } finally {
108: exporter.close();
109: if (db != null)
110: db.close();
111: }
112: }
113: }
|