View Javadoc
1   package ejava.projects.edmv.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.edmv.jdbc.JDBCPersonDAO;
13  import ejava.projects.edmv.jdbc.JDBCVehicleDAO;
14  
15  /**
16   * This class provides an example of how one might wrap the business logic 
17   * within a Java main() versus JUnit so that it can be run in a 
18   * more standard operational environment.
19   */
20  public class EDmvIngestCommand {
21  	@SuppressWarnings("unused")
22  	private static final Logger log = LoggerFactory.getLogger(EDmvIngestCommand.class);
23  	private static final String jdbcDriver = 
24  		System.getProperty("jdbc.driver");
25  	private static final String jdbcURL = 
26  		System.getProperty("jdbc.url");
27  	private static final String jdbcUser = 
28  		System.getProperty("jdbc.user");
29  	private static final String jdbcPassword = 
30  		System.getProperty("jdbc.password");
31  	private static final String inputFile = 
32  		System.getProperty("inputFile");
33  	
34  	@SuppressWarnings("resource")
35  	private static InputStream getInputStream() throws Exception {
36  		InputStream is = null;
37  
38  		if (inputFile == null) {
39  			throw new Exception("inputFile not supplied");
40  		}
41  		
42  		File file = new File(inputFile);
43  		if (file.exists()) {
44  			is = new FileInputStream(file);
45  		}
46  		if (is == null) {
47              is = Thread.currentThread()
48                         .getContextClassLoader()
49                         .getResourceAsStream(inputFile);
50  		}
51  		if (is == null) {
52  			throw new Exception("unable to locate inputFile:" + inputFile);
53  		}
54  		return is;
55  	}
56  	
57  	private static void loadDriver() throws Exception {
58  		if (jdbcDriver == null) {
59  			throw new Exception("jdbc.driver not supplied");
60  		}
61  		Thread.currentThread()
62  		      .getContextClassLoader()
63  		      .loadClass(jdbcDriver)
64  		      .newInstance();
65  	}
66  	
67  	private static Connection getConnection() throws Exception {
68  		if (jdbcURL == null) {
69  			throw new Exception("jdbc.url not supplied");
70  		}
71  		if (jdbcUser == null) {
72  			throw new Exception("jdbc.user not supplied");
73  		}
74  		if (jdbcPassword == null) {
75  			throw new Exception("jdbc.password not supplied");
76  		}
77  		return DriverManager.getConnection(jdbcURL, jdbcUser, jdbcPassword);
78  	}
79  	
80  	public static void main(String args[]) {
81  	    try {
82  			Connection connection = null;
83  			InputStream is = null;
84  		    try {
85  		    	is = getInputStream();
86  		    	loadDriver();
87  		    	connection = getConnection();
88  		    	JDBCPersonDAO personDAO = new JDBCPersonDAO();
89  		    	personDAO.setConnection(connection);
90  		    	JDBCVehicleDAO vehicleDAO = new JDBCVehicleDAO();
91  		    	vehicleDAO.setConnection(connection);
92  		    	
93  		    	EDmvIngestor ingest = new EDmvIngestor();
94  		    	ingest.setPersonDAO(personDAO);
95  		    	ingest.setVehicleDAO(vehicleDAO);
96  		    	ingest.setInputStream(is);
97  		    	ingest.ingest();
98  		    }
99  			finally {
100 				if (connection != null) { connection.close(); }
101 				if (is != null) { is.close(); }
102 			}
103 	    }
104 	    catch (Exception ex) {
105 	    	ex.printStackTrace();
106 	    	System.exit(-1);
107 	    }
108 	}
109 }