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