Stirling-PDF/src/main/java/stirling/software/SPDF/EE/LicenseKeyChecker.java
a 4436759e12 #1869 and ensure naming
Signed-off-by: a <a>
2024-09-20 13:31:55 +01:00

59 lines
2.0 KiB
Java

package stirling.software.SPDF.EE;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import stirling.software.SPDF.model.ApplicationProperties;
@Component
public class LicenseKeyChecker implements CommandLineRunner {
private final KeygenLicenseVerifier licenseService;
private final ApplicationProperties applicationProperties;
// Inject your license service or configuration
public LicenseKeyChecker(
KeygenLicenseVerifier licenseService, ApplicationProperties applicationProperties) {
this.licenseService = licenseService;
this.applicationProperties = new ApplicationProperties();
}
// Validate on startup
@Override
public void run(String... args) throws Exception {
checkLicense();
}
// Periodic license check - runs every 7 days
@Scheduled(fixedRate = 604800000) // 7 days in milliseconds
public void checkLicensePeriodically() {
checkLicense();
}
// License validation logic
private void checkLicense() {
boolean isValid =
licenseService.verifyLicense(applicationProperties.getEnterpriseEdition().getKey());
if (!isValid) {
// Handle invalid license (shut down the app, log, etc.)
System.out.println("License key is invalid!");
// Optionally stop the application
// System.exit(1); // Uncomment if you want to stop the app
} else {
System.out.println("License key is valid.");
}
}
// Method to update the license key dynamically
public void updateLicenseKey(String newKey) {
// Update the key in ApplicationProperties
applicationProperties.getEnterpriseEdition().setKey(newKey);
// Immediately validate the new key
System.out.println("License key has been updated. Checking new key...");
checkLicense();
}
}