package main; import com.google.gson.JsonSyntaxException; import data.Tweet; import database.ConnectionBuilder; import io.FileTweetReader; import io.ITweetReader; import io.TweetReader; import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; /** * The main class. */ public class Main { /** * The main method of the application. * * @param args the global arguments to pass to the program. */ public static void main(String[] args) { Main main; try { main = new Main(args); } catch (IllegalArgumentException ex) { System.err.println(ex.getMessage()); System.exit(1); return; } main.run(); } private String m_filename; private final ConnectionBuilder cb; public Main(String[] args) { // default connection properties cb = new ConnectionBuilder() .setServerName("localhost") .setUsername("postgres") .setPassword("2IOC02") .setDbName("Twitter"); /* parse the global options. */ parseGlobalOptions(args); } public void run() { try (Connection connection = cb.create()) { /* create the object that fills the database */ DataFiller filler = new DataFiller(connection); ITweetReader reader = null; try { if (m_filename == null) { reader = new TweetReader(System.in); } else { reader = new FileTweetReader(m_filename); } Tweet tweet; while ((tweet = reader.getTweet()) != null) { filler.processTweet(tweet); } } catch (JsonSyntaxException ex) { System.err.println("Got an invalid tweet: " + ex); } catch (IOException ex) { System.err.println("Cannot open tweets: " + ex); } finally { if (reader != null) { reader.close(); } } } catch (SQLException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "DB error", ex); } } private void parseGlobalOptions(String[] args) throws IllegalArgumentException { /* parse global options */ for (int i = 0; i < args.length; i++) { if ("--help".equals(args[i])) { printHelp(); System.exit(0); } else if ("--dbhost".equals(args[i])) { cb.setServerName(getArg(args, ++i, "--dbhost")); } else if ("--dbuser".equals(args[i])) { cb.setUsername(getArg(args, ++i, "--dbuser")); } else if ("--dbpass".equals(args[i])) { cb.setPassword(getArg(args, ++i, "--dbpass")); } else if ("--dbname".equals(args[i])) { cb.setDbName(getArg(args, ++i, "--dbname")); } else if (args[i].startsWith("-")) { throw new IllegalArgumentException("Invalid option: " + args[i]); } else { /* This should be the filename */ m_filename = args[i]; } } } /** * Read an extra option for a command. */ private String getArg(String[] params, int index, String name) { if (index >= params.length) { System.err.println("Missing argument for parameter " + name); System.exit(1); } return params[index]; } /** * Print some useful help messages. */ private void printHelp() { for (String line : HELP) { System.out.println(line); } } private final static String[] HELP = { "Usage: java -jar DataFiller.jar [options] [tweets-file]", "", "Global options:", " --help Print this help text.", " --dbhost HOST Database host (defaults to 'localhost')", " --dbuser USER Database username (defaults to 'postgres')", " --dbpass PASS Database password (defaults to '2IOC02')", " --dbname NAME Database name (defaults to 'Twitter')", "", "If no tweets file is given, data will be read from standard input." }; }