|
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 | 32 |
|
31 | 33 | import java.io.File; |
32 | 34 | import java.io.FileOutputStream; |
@@ -61,9 +63,10 @@ public class Main { |
61 | 63 | private String whazzupFile; |
62 | 64 | private String whazzupjsonFile; |
63 | 65 | private String mysqlmode; |
64 | | - private String sqlurl; |
65 | | - private String sqluser; |
66 | | - private String sqlpassword; |
| 66 | + private static String sqlurl; |
| 67 | + private static String sqluser; |
| 68 | + private static String sqlpassword; |
| 69 | + private static Connection conn = null; |
67 | 70 | private long timer; |
68 | 71 | private long prevNotify; |
69 | 72 | private long prevLagCheck; |
@@ -140,21 +143,37 @@ public void doChecks() { |
140 | 143 | ConfigEntry entry; |
141 | 144 | ConfigGroup sysgroup = configManager.getGroup("system"); |
142 | 145 | if (sysgroup != null) { |
143 | | - if ((entry = sysgroup.getEntry("certificates")) != null) { |
144 | | - certFile = entry.getData().toUpperCase(); |
145 | | - Path file = Paths.get(certFile); |
146 | | - if (Files.exists(file)) { |
147 | | - try { |
148 | | - BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class); |
149 | | - long fileLastModified = attr.lastModifiedTime().toMillis(); |
150 | | - prevCertCheck = now; |
151 | | - if (certFileStat != fileLastModified) { |
152 | | - certFileStat = fileLastModified; |
| 146 | + if (Objects.equals(mysqlmode, "false")) { |
| 147 | + if ((entry = sysgroup.getEntry("certificates")) != null) { |
| 148 | + certFile = entry.getData().toUpperCase(); |
| 149 | + Path file = Paths.get(certFile); |
| 150 | + if (Files.exists(file)) { |
| 151 | + try { |
| 152 | + BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class); |
| 153 | + long fileLastModified = attr.lastModifiedTime().toMillis(); |
| 154 | + prevCertCheck = now; |
| 155 | + if (certFileStat != fileLastModified) { |
| 156 | + certFileStat = fileLastModified; |
| 157 | + readCert(); |
| 158 | + } |
| 159 | + } catch (IOException e) { |
| 160 | + LOGGER.warn("[BetterFSD]: Read cert file info failed.", e); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } else if (Objects.equals(mysqlmode, "true")) { |
| 165 | + if ((sysgroup.getEntry("sqlurl")) != null) { |
| 166 | + if ((sysgroup.getEntry("sqluser")) != null){ |
| 167 | + if (sysgroup.getEntry("sqlpassword") != null) { |
153 | 168 | readCert(); |
| 169 | + } else { |
| 170 | + LOGGER.error("[BetterFSD]: Read mysql password info failed"); |
154 | 171 | } |
155 | | - } catch (IOException e) { |
156 | | - LOGGER.warn("[BetterFSD]: Read cert file info failed.", e); |
| 172 | + } else { |
| 173 | + LOGGER.error("[BetterFSD]: Read mysql user info failed"); |
157 | 174 | } |
| 175 | + } else { |
| 176 | + LOGGER.error("[BetterFSD]: Read mysql url info failed"); |
158 | 177 | } |
159 | 178 | } |
160 | 179 | } |
@@ -462,7 +481,17 @@ void handleCidLine(@NotNull String line) { |
462 | 481 | } |
463 | 482 | if (serverInterface != null) serverInterface.sendCert("*", mode, tempcert, null); |
464 | 483 | } |
465 | | - |
| 484 | + public static Connection getConn() { |
| 485 | + try { |
| 486 | + Class.forName("com.mysql.cj.jdbc.Driver"); |
| 487 | + 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"); |
| 491 | + e.printStackTrace(); |
| 492 | + } |
| 493 | + return conn; |
| 494 | + } |
466 | 495 | void readCert() { |
467 | 496 | if (Objects.equals(mysqlmode, "false")) { |
468 | 497 | if (StringUtils.isBlank(certFile)) return; |
@@ -491,7 +520,7 @@ void readCert() { |
491 | 520 | } |
492 | 521 | } |
493 | 522 | } else if (Objects.equals(mysqlmode, "true")) { |
494 | | - //TODO |
| 523 | + getConn(); |
495 | 524 | } |
496 | 525 | } |
497 | 526 |
|
|
0 commit comments