1 package ejava.projects.esales.blimpl; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.InputStream; 6 import java.sql.Connection; 7 import java.sql.DriverManager; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 12 import ejava.projects.esales.jdbc.JDBCAccountDAO; 13 14 public class ESalesIngestCommand { 15 @SuppressWarnings("unused") 16 private static final Log log = 17 LogFactory.getLog(ESalesIngestCommand.class); 18 private static final String jdbcDriver = 19 System.getProperty("jdbc.driver"); 20 private static final String jdbcURL = 21 System.getProperty("jdbc.url"); 22 private static final String jdbcUser = 23 System.getProperty("jdbc.user"); 24 private static final String jdbcPassword = 25 System.getProperty("jdbc.password"); 26 private static final String inputFile = 27 System.getProperty("inputFile"); 28 29 @SuppressWarnings("resource") 30 private static InputStream getInputStream() throws Exception { 31 InputStream is = null; 32 33 if (inputFile == null) { 34 throw new Exception("inputFile not supplied"); 35 } 36 37 File file = new File(inputFile); 38 if (file.exists()) { 39 is = new FileInputStream(file); 40 } 41 if (is == null) { 42 is = Thread.currentThread() 43 .getContextClassLoader() 44 .getResourceAsStream(inputFile); 45 } 46 if (is == null) { 47 throw new Exception("unable to locate inputFile:" + inputFile); 48 } 49 return is; 50 } 51 52 private static void loadDriver() throws Exception { 53 if (jdbcDriver == null) { 54 throw new Exception("jdbc.driver not supplied"); 55 } 56 Thread.currentThread() 57 .getContextClassLoader() 58 .loadClass(jdbcDriver) 59 .newInstance(); 60 } 61 62 private static Connection getConnection() throws Exception { 63 if (jdbcURL == null) { 64 throw new Exception("jdbc.url not supplied"); 65 } 66 if (jdbcUser == null) { 67 throw new Exception("jdbc.user not supplied"); 68 } 69 if (jdbcPassword == null) { 70 throw new Exception("jdbc.password not supplied"); 71 } 72 return DriverManager.getConnection(jdbcURL, jdbcUser, jdbcPassword); 73 } 74 75 public static void main(String args[]) { 76 try { 77 Connection connection = null; 78 InputStream is = null; 79 try { 80 is = getInputStream(); 81 loadDriver(); 82 connection = getConnection(); 83 JDBCAccountDAO accountDAO = new JDBCAccountDAO(); 84 accountDAO.setConnection(connection); 85 86 ESalesIngestor ingest = new ESalesIngestor(); 87 ingest.setAccountDAO(accountDAO); 88 ingest.setInputStream(is); 89 ingest.ingest(); 90 } 91 finally { 92 if (connection != null) { connection.close(); } 93 if (is != null) { is.close(); } 94 } 95 } 96 catch (Exception ex) { 97 ex.printStackTrace(); 98 System.exit(-1); 99 } 100 } 101 }