Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.fotogalerie.dev.java.convert;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Properties;
- /**
- *
- * @author Jiri Helmich <jiri@helmich.cz>
- */
- public class Main
- {
- /** Upload service instance
- *
- */
- private static UploadQueueConverter uploadService;
- /** Configuration
- *
- */
- public static Properties properties;
- /**
- * @param args the command line arguments
- */
- public static void main (String[] args)
- {
- try
- {
- System.out.println ("FTG convert bot booting up...");
- System.out.println ("Reading config");
- properties = new Properties ();
- properties.load (new FileInputStream ("config.ini"));
- System.out.println ("Initialization");
- initServices ();
- ServerSocket server =
- new ServerSocket (Integer.parseInt (properties.getProperty ("server.port",
- "9876")));
- while (true)
- {
- Socket client = server.accept ();
- System.out.println ("New incomming connection");
- new WakeupHandler (client, uploadService);
- }
- } catch (SQLException e)
- {
- System.out.println ("DB Connection failed: " + e.getMessage ());
- } catch (IOException e)
- {
- System.out.println ("Socket troubles: " + e.getMessage ());
- } catch (ClassNotFoundException e)
- {
- System.out.println ("Class not found :" + e.getMessage () +
- "(JDBC)");
- }
- }
- /** Inits dependencies (DB conn, instantiate services)
- *
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- public static void initServices () throws ClassNotFoundException,
- SQLException
- {
- Connection queueConn = initDb ("database.queue");
- Connection mainConn = initDb ("database.main");
- System.out.println ("DB ... OK");
- uploadService = new UploadQueueConverter (queueConn, mainConn);
- System.out.println ("Upload converter ... OK");
- }
- /** Establishes a DB connection (PGSQL)
- *
- * @param configPath
- * @return
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- public static Connection initDb (String configPath) throws
- ClassNotFoundException, SQLException
- {
- Class.forName ("org.postgresql.Driver");
- Properties props = new Properties ();
- props.setProperty ("user", properties.getProperty (configPath + ".user"));
- props.setProperty ("password", properties.getProperty (configPath
- + ".pass"));
- Connection db = DriverManager.getConnection (getConnectionString (
- configPath), props);
- return db;
- }
- /** Builds a connectionstring
- *
- * @param configPath
- * @return
- */
- public static String getConnectionString (String configPath)
- {
- return "jdbc:postgresql://"
- + properties.getProperty (configPath + ".host") + ":"
- + properties.getProperty (configPath + ".port") + "/"
- + properties.getProperty (configPath + ".name");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement