27
27
import org .linktechtips .process .network .SystemInterface ;
28
28
import org .linktechtips .support .Support ;
29
29
import org .slf4j .Logger ;
30
- import java . sql . Connection ;
31
- import java .sql .DriverManager ;
30
+
31
+ import java .sql .* ;
32
32
33
33
import java .io .File ;
34
34
import java .io .FileOutputStream ;
@@ -64,6 +64,7 @@ public class Main {
64
64
private String whazzupjsonFile ;
65
65
private String mysqlmode ;
66
66
private static String sqlurl ;
67
+ private static String sqltable ;
67
68
private static String sqluser ;
68
69
private static String sqlpassword ;
69
70
private static Connection conn = null ;
@@ -433,6 +434,9 @@ private void configure() {
433
434
if ((entry = system .getEntry ("sqlurl" )) != null ) {
434
435
sqlurl = entry .getData ();
435
436
}
437
+ if ((entry = system .getEntry ("sqltable" )) != null ) {
438
+ sqltable = entry .getData ();
439
+ }
436
440
if ((entry = system .getEntry ("sqluser" )) != null ) {
437
441
sqluser = entry .getData ();
438
442
}
@@ -448,11 +452,8 @@ private void configure() {
448
452
whazzupjsonFile = entry .getData ();
449
453
}
450
454
}
451
-
452
455
configMyServer ();
453
-
454
456
readCert ();
455
-
456
457
}
457
458
458
459
void handleCidLine (@ NotNull String line ) {
@@ -481,16 +482,14 @@ void handleCidLine(@NotNull String line) {
481
482
}
482
483
if (serverInterface != null ) serverInterface .sendCert ("*" , mode , tempcert , null );
483
484
}
484
- public static Connection getConn () {
485
+ public static void getConn () {
485
486
try {
486
487
Class .forName ("com.mysql.cj.jdbc.Driver" );
487
488
conn = DriverManager .getConnection (sqlurl , sqluser , sqlpassword );
488
- LOGGER .info ("[BetterFSD]: Database connection established" );
489
- } catch (Exception e ) {
490
- LOGGER .error ("[BetterFSD]: N/A Exception on loading mysql" );
489
+ } catch (ClassNotFoundException | SQLException e ) {
490
+ LOGGER .info ("[BetterFSD]: Exception in get mysql conn" );
491
491
e .printStackTrace ();
492
492
}
493
- return conn ;
494
493
}
495
494
void readCert () {
496
495
if (Objects .equals (mysqlmode , "false" )) {
@@ -521,6 +520,25 @@ void readCert() {
521
520
}
522
521
} else if (Objects .equals (mysqlmode , "true" )) {
523
522
getConn ();
523
+ LOGGER .info ("[BetterFSD]: Reading certificates from database" );
524
+ if (StringUtils .isEmpty (sqltable )) {
525
+ LOGGER .error ("[BetterFSD]: sqltable is empty. Please check your config file" );
526
+ } else if (sqltable != null ) try {
527
+ String sql = "SELECT * FROM " + sqltable ;
528
+ PreparedStatement preparedStatement = conn .prepareStatement (sql );
529
+ ResultSet resultSet = preparedStatement .executeQuery ();
530
+ while (resultSet .next ()) {
531
+ Certificate tempcert = new Certificate (resultSet .getString ("cid" ), resultSet .getString ("password" ),
532
+ resultSet .getInt ("level" ), Support .mgmtime (), Server .myServer .getIdent ());
533
+ serverInterface .sendCert ("*" , ProtocolConstants .CERT_ADD , tempcert , null );
534
+ }
535
+ preparedStatement .close ();
536
+ resultSet .close ();
537
+ conn .close ();
538
+ } catch (Exception e ) {
539
+ LOGGER .error ("[BetterFSD]: N/A Exception on reading mysql" );
540
+ e .printStackTrace ();
541
+ }
524
542
}
525
543
}
526
544
0 commit comments