first commit

This commit is contained in:
2026-01-19 16:57:29 +07:00
parent 0668d07d0c
commit 740b241b71
8 changed files with 246 additions and 7 deletions

121
src/Other/Config.kt Normal file
View File

@@ -0,0 +1,121 @@
package Other
import org.tinylog.Logger
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.nio.file.Files
import java.time.LocalDateTime
import java.util.Properties
import kotlin.io.path.Path
@Suppress("unused")
class Config {
private val prop = Properties()
private val filename = "config.properties"
var WebPort : Int
get() = prop.getProperty("webport").toInt()
set(value) {prop.setProperty("webport", value.toString())}
var ActiveMQ_BrokerURL : String
get() = prop.getProperty("activemq_brokerurl")
set(value) {prop.setProperty("activemq_brokerurl", value)}
var ActiveMQ_Username : String
get() = prop.getProperty("activemq_username")
set(value) {prop.setProperty("activemq_username", value)}
var ActiveMQ_Password : String
get() = prop.getProperty("activemq_password")
set(value) {prop.setProperty("activemq_password", value)}
var ActiveMQ_QueueName : String
get() = prop.getProperty("activemq_queuename")
set(value) {prop.setProperty("activemq_queuename", value)}
var MySQL_AAS1 : String
get() = prop.getProperty("mysql_aas1")
set(value) {prop.setProperty("mysql_aas1", value)}
var MySQL_AAS2 : String
get() = prop.getProperty("mysql_aas2")
set(value) {prop.setProperty("mysql_aas2", value)}
var MySQL_AAS3 : String
get() = prop.getProperty("mysql_aas3")
set(value) {prop.setProperty("mysql_aas3", value)}
var MySQL_AAS1_Username : String
get() = prop.getProperty("mysql_aas1_username")
set(value) {prop.setProperty("mysql_aas1_username", value)}
var MySQL_AAS1_Password : String
get() = prop.getProperty("mysql_aas1_password")
set(value) {prop.setProperty("mysql_aas1_password", value)}
var MySQL_AAS2_Username : String
get() = prop.getProperty("mysql_aas2_username")
set(value) {prop.setProperty("mysql_aas2_username", value)}
var MySQL_AAS2_Password : String
get() = prop.getProperty("mysql_aas2_password")
set(value) {prop.setProperty("mysql_aas2_password", value)}
var MySQL_AAS3_Username : String
get() = prop.getProperty("mysql_aas3_username")
set(value) {prop.setProperty("mysql_aas3_username", value)}
var MySQL_AAS3_Password : String
get() = prop.getProperty("mysql_aas3_password")
set(value) {prop.setProperty("mysql_aas3_password", value)}
fun Load(){
try{
if (Files.isRegularFile(Path(filename))){
prop.load(FileInputStream(filename))
if (!prop.contains("webport")) throw Exception("Invalid config file: missing 'webport'")
if (!prop.contains("activemq_brokerurl")) throw Exception("Invalid config file: missing 'activemq_brokerurl'")
if (!prop.contains("activemq_username")) throw Exception("Invalid config file: missing 'activemq_username'")
if (!prop.contains("activemq_password")) throw Exception("Invalid config file: missing 'activemq_password'")
if (!prop.contains("activemq_queuename")) throw Exception("Invalid config file: missing 'activemq_queuename'")
if (!prop.contains("mysql_aas1")) throw Exception("Invalid config file: missing 'mysql_aas1'")
if (!prop.contains("mysql_aas2")) throw Exception("Invalid config file: missing 'mysql_aas2'")
if (!prop.contains("mysql_aas3")) throw Exception("Invalid config file: missing 'mysql_aas3'")
if (!prop.contains("mysql_aas1_username")) throw Exception("Invalid config file: missing 'mysql_aas1_username'")
if (!prop.contains("mysql_aas1_password")) throw Exception("Invalid config file: missing 'mysql_aas1_password'")
if (!prop.contains("mysql_aas2_username")) throw Exception("Invalid config file: missing 'mysql_aas2_username'")
if (!prop.contains("mysql_aas2_password")) throw Exception("Invalid config file: missing 'mysql_aas2_password'")
if (!prop.contains("mysql_aas3_username")) throw Exception("Invalid config file: missing 'mysql_aas3_username'")
if (!prop.contains("mysql_aas3_password")) throw Exception("Invalid config file: missing 'mysql_aas3_password'")
Logger.info { "Configuration loaded from config.properties" }
} else throw FileNotFoundException("Config file not found")
} catch (e: FileNotFoundException){
Logger.error(e) { "Config file not found, Message : ${e.message}" }
CreateDefault()
Save()
}
catch (e: Exception){
Logger.error(e) { "Error while loading config, Message : ${e.message}" }
CreateDefault()
Save()
}
}
fun Save(){
// save prop to file "save.properties"
// comment is save date and time
val comment = "Configuration saved on ${LocalDateTime.now()}"
try{
prop.store(FileOutputStream(filename), comment)
Logger.info { "Configuration saved to config.properties" }
} catch (ex: Exception){
Logger.error(ex) { "Error while saving config, Message : ${ex.message}" }
}
}
fun CreateDefault(){
prop.clear()
prop.setProperty("webport", "7000")
prop.setProperty("activemq_brokerurl", "tcp://localhost:61616")
prop.setProperty("activemq_username", "admin")
prop.setProperty("activemq_password", "admin")
prop.setProperty("activemq_queuename", "TEST.QUEUE")
prop.setProperty("mysql_aas1", "jdbc:mysql://192.168.10.10:3306/aas")
prop.setProperty("mysql_aas2", "jdbc:mysql://192.168.10.11:3306/aas")
prop.setProperty("mysql_aas3"," jdbc:mysql://192.168.10.12:3306/aas")
prop.setProperty("mysql_aas1_username","admin")
prop.setProperty("mysql_aas1_password","admin")
prop.setProperty("mysql_aas2_username","admin")
prop.setProperty("mysql_aas2_password","admin")
prop.setProperty("mysql_aas3_username","admin")
prop.setProperty("mysql_aas3_password","admin")
Logger.info {"Default configuration created" }
}
}