Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. /**
  2.      * Prywatny konstruktor domyślny - obiekt klasy można otrzymać tylko przez wywołania funkcji getInstance().
  3.      * Wywołuje metodę tworzącą tunel SSH umożliwiający połączenie ze zdalną bazą danych. Tunel zostaje utworzony na pierwszym dostępnym porcie w zakresie 1101 do 1200.
  4.      */
  5.     private DataBaseConnection() {
  6.         int i;
  7.         tunnel = false;
  8.         for(i = 1101; i<=1200; ++i) {
  9.             tunnel = doSshTunnel(i, 5432, "TWOJ_LOGIN_DO_TAURUSA", "TWOJE_HASLO_DO_TAURUSA");
  10.             if(tunnel) {
  11.                 url = String.format("jdbc:postgresql://localhost:%d/NAZWA_BAZY_DANYCH_NA_PASCALU?allowMultiQueries=true", localPort);
  12.                 break;
  13.             }
  14.         }
  15.         if(i==1201 && !tunnel)
  16.             JOptionPane.showMessageDialog(null, "Nie udało się stworzyć tunelu SSH.");
  17.     }
  18.    
  19.     /**
  20.      * Metoda tworząca tunel SSH z serwerem pascal.
  21.      */
  22.     private boolean doSshTunnel(int localPort, int remotePort, String user, String password)
  23.     {
  24.         try {
  25.             JSch jsch = new JSch();
  26.             Session session = jsch.getSession(user, "taurus.fis.agh.edu.pl", 22);
  27.             session.setPassword(password);
  28.  
  29.             Properties config = new Properties();
  30.             config.put("StrictHostKeyChecking", "no");
  31.             session.setConfig( config );
  32.  
  33.             session.connect();
  34.             this.localPort = session.setPortForwardingL(localPort, "pascal.fis.agh.edu.pl", remotePort);
  35.             return true;
  36.         } catch( JSchException ex) {
  37.             return false;
  38.         }
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement