[Csvtosql-cvs] csvtosql/src/net/sf/csv2sql/frontends/console ExtendedConsoleMain.java, NONE, 1.1
Brought to you by:
davideconsonni
|
From: Vano <jj...@us...> - 2006-10-12 10:42:36
|
Update of /cvsroot/csvtosql/csvtosql/src/net/sf/csv2sql/frontends/console In directory sc8-pr-cvs4.sourceforge.net:/tmp/cvs-serv14362/src/net/sf/csv2sql/frontends/console Added Files: ExtendedConsoleMain.java Log Message: New frontend for importing data from the list of files with opening only one connection to the database. --- NEW FILE: ExtendedConsoleMain.java --- /* Copyright (C) 2006 Ivan Ryndin <jj...@us...> LGPL terms */ package net.sf.csv2sql.frontends.console; import java.io.File; import java.io.FileReader; import java.io.BufferedReader; import java.util.List; import java.util.Iterator; import java.util.ArrayList; import java.util.HashMap; import java.sql.Connection; import net.sf.csv2sql.configuration.XmlConfigurator; import net.sf.csv2sql.writers.AbstractWriter; import net.sf.csv2sql.utils.DBUtils; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.Options; import org.apache.commons.cli.PosixParser; /** * csv2sql extended console frontends * It is used when need to export data from several files into single database. It is not * good to open and close connection every time when processing new file. So, this frontend * opens the one connection to database and pass it as a parameter to each writer, so that this * single connection is used by all the files processors. * <p>Parameters necessary for opening of the * connection are passed as system properties to this frontend. These params are: * <ul> * <li> db.url - URL of the database * <li> db.user - User name of the database * <li> db.password - Password * <li> db.driver.class- Class of the database driver. For example, "com.mysql.jdbc.Driver". * </ul> * @author <a href="mailto:jj...@us...">Ivan Ryndin</a> */ public class ExtendedConsoleMain { private static ExtendedConsoleMain self = new ExtendedConsoleMain(); /** * singleton pattern * return THE istance of main * @return istance of ExtendedConsoleMain */ public static ExtendedConsoleMain getInstance() { return self; } /** * Prints usage information to Console. */ private static String usage() { return " Usage: csv2sql -d filename [-b]\n\n"+ " --descriptor [-d] file containing conversion descriptor\n"+ " --help [-h] display this help\n"; } /** * Prints version information to Console. */ private static String version() { return "csv2sql, Copyright (C) 2004 Davide Consonni <dco...@en...>\n"+ "Convert a csv file in sql file\n\n"; } /** * business logic * @param descriptor point to conversion descriptor file * @param connection Connection to database used by the writers * @throws Exception if any error occured */ public void run(File descriptor, Connection connection) throws Exception { //read configuration XmlConfigurator config = new XmlConfigurator(); config.load(descriptor); if (config.CURRENT_DESCRIPTOR_VERSION != config.getDescriptorVersion()) { throw new Exception("version mismatch"); } //generate statements config.getRender().render(); //output statements Iterator it = config.getWriters().iterator(); HashMap params = new HashMap(); params.put("connection",connection); while (it.hasNext()) { AbstractWriter writer = (AbstractWriter)it.next(); writer.setWriterParameters(params); writer.write(); } } /** * program entry point * @param args argument list */ public static void main(String args []) { try { CommandLineParser parser = new PosixParser(); Options options = new Options(); options.addOption( "d", "descriptor", true, "descriptor file"); options.addOption( "h", "help", false, "display help" ); CommandLine line = parser.parse( options, args ); //no arguments ? no party. if (args.length <= 0) { System.out.println(version()); System.out.println(usage()); System.exit(1); } //display version if( line.hasOption( "h" ) ) { System.out.println(version()); System.out.println(usage()); System.exit(0); } ArrayList files = new ArrayList(); String pathPart = ""; //read filename, open file and store filenames of descriptors if( line.hasOption( "d" ) ) { try { String fileName = line.getOptionValue("d"); pathPart = fileName.substring(0,fileName.lastIndexOf(File.separator))+File.separator; BufferedReader in = new BufferedReader(new FileReader(fileName)); String s; while((s = in.readLine())!= null) { s = s.trim(); // # - this signals the beginning of the comment in the input file if (s.length()==0 || s.startsWith("#")) continue; files.add(s); } in.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } else { System.out.println(version()); System.out.println(usage()); System.out.println("ERROR: option --descriptor is mandatory.\n"); System.exit(1); } // connect to database Connection connection = null; try { // start benchmark long start = System.currentTimeMillis(); String dbUrl = System.getProperty("db.url"); String dbUser = System.getProperty("db.user"); String dbPassword = System.getProperty("db.password"); String dbDriverClass = System.getProperty("db.driver.class"); connection = DBUtils.openConnection(dbDriverClass,dbUrl,dbUser,dbPassword); for (int i=0; i<files.size(); i++) { String fileName = pathPart + (String)files.get(i); System.out.println("Processing file: " + fileName); File descriptor = new File(fileName); ExtendedConsoleMain.getInstance().run(descriptor,connection); } System.out.println("\nCsv converted in: "+String.valueOf(System.currentTimeMillis()-start)+" ms"); } finally { DBUtils.closeConnection(connection); } } catch( Exception e ) { e.printStackTrace(); } } } |