Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. package cz.fotogalerie.dev.java.convert;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.SQLException;
  10. import java.util.Properties;
  11.  
  12. /**
  13. *
  14. * @author Jiri Helmich <jiri@helmich.cz>
  15. */
  16. public class Main
  17. {
  18.  
  19. /** Upload service instance
  20. *
  21. */
  22. private static UploadQueueConverter uploadService;
  23.  
  24. /** Configuration
  25. *
  26. */
  27. public static Properties properties;
  28.  
  29. /**
  30. * @param args the command line arguments
  31. */
  32. public static void main (String[] args)
  33. {
  34. try
  35. {
  36. System.out.println ("FTG convert bot booting up...");
  37. System.out.println ("Reading config");
  38. properties = new Properties ();
  39. properties.load (new FileInputStream ("config.ini"));
  40. System.out.println ("Initialization");
  41. initServices ();
  42. ServerSocket server =
  43. new ServerSocket (Integer.parseInt (properties.getProperty ("server.port",
  44. "9876")));
  45. while (true)
  46. {
  47. Socket client = server.accept ();
  48. System.out.println ("New incomming connection");
  49. new WakeupHandler (client, uploadService);
  50. }
  51. } catch (SQLException e)
  52. {
  53. System.out.println ("DB Connection failed: " + e.getMessage ());
  54. } catch (IOException e)
  55. {
  56. System.out.println ("Socket troubles: " + e.getMessage ());
  57. } catch (ClassNotFoundException e)
  58. {
  59. System.out.println ("Class not found :" + e.getMessage () +
  60. "(JDBC)");
  61. }
  62. }
  63.  
  64. /** Inits dependencies (DB conn, instantiate services)
  65. *
  66. * @throws ClassNotFoundException
  67. * @throws SQLException
  68. */
  69. public static void initServices () throws ClassNotFoundException,
  70. SQLException
  71. {
  72.  
  73. Connection queueConn = initDb ("database.queue");
  74. Connection mainConn = initDb ("database.main");
  75. System.out.println ("DB ... OK");
  76.  
  77. uploadService = new UploadQueueConverter (queueConn, mainConn);
  78. System.out.println ("Upload converter ... OK");
  79. }
  80.  
  81. /** Establishes a DB connection (PGSQL)
  82. *
  83. * @param configPath
  84. * @return
  85. * @throws ClassNotFoundException
  86. * @throws SQLException
  87. */
  88. public static Connection initDb (String configPath) throws
  89. ClassNotFoundException, SQLException
  90. {
  91. Class.forName ("org.postgresql.Driver");
  92.  
  93. Properties props = new Properties ();
  94. props.setProperty ("user", properties.getProperty (configPath + ".user"));
  95. props.setProperty ("password", properties.getProperty (configPath
  96. + ".pass"));
  97.  
  98. Connection db = DriverManager.getConnection (getConnectionString (
  99. configPath), props);
  100.  
  101. return db;
  102. }
  103.  
  104. /** Builds a connectionstring
  105. *
  106. * @param configPath
  107. * @return
  108. */
  109. public static String getConnectionString (String configPath)
  110. {
  111. return "jdbc:postgresql://"
  112. + properties.getProperty (configPath + ".host") + ":"
  113. + properties.getProperty (configPath + ".port") + "/"
  114. + properties.getProperty (configPath + ".name");
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement