com.gateway.connector.utils.LicenseUtil Maven / Gradle / Ivy
package com.gateway.connector.utils;
import de.schlichtherle.license.CipherParam;
import de.schlichtherle.license.DefaultCipherParam;
import de.schlichtherle.license.DefaultKeyStoreParam;
import de.schlichtherle.license.DefaultLicenseParam;
import de.schlichtherle.license.KeyStoreParam;
import de.schlichtherle.license.LicenseContent;
import de.schlichtherle.license.LicenseManager;
import de.schlichtherle.license.LicenseParam;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.prefs.Preferences;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LicenseUtil {
private static String license_public_alias = "publiccert";
private static String license_key_store_pwd = "Gjk_123456";
public static String license_path = "dc.dat";
private static String license_Subject = "dc";
private static LicenseManager licenseManager;
private static Logger logger = LoggerFactory.getLogger(LicenseUtil.class);
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static synchronized LicenseManager getLicenseManager(LicenseParam licenseParams) {
if (licenseManager == null) {
licenseManager = new LicenseManager(licenseParams);
}
return licenseManager;
}
static LicenseContent content = null;
public static void install() {
licenseManager = getLicenseManager(initLicenseParams());
try {
File file = new File(license_path);
System.out.println(license_path + ":" + file.exists());
content = licenseManager.install(file);
} catch (Exception e) {
System.out.println("license install failed,system exit.");
logger.error("license install failed,system exit.", e);
System.exit(0);
}
}
public static void verify() {
try {
licenseManager.verify();
} catch (Exception e) {
printLicense(content);
logger.error("license verify failed,system exit.");
System.exit(0);
}
}
private static void printLicense(LicenseContent content) {
if (content != null) {
StringBuilder sb = new StringBuilder();
sb.append("**************license**************").append("\n");
sb.append(" Server:").append(content.getSubject()).append("\n");
sb.append("create date:").append(sdf.format(content.getIssued())).append("\n");
sb.append("start date:").append(sdf.format(content.getNotBefore())).append("\n");
sb.append("end date:").append(sdf.format(content.getNotAfter())).append("\n");
sb.append(" comment:").append(content.getInfo()).append("\n");
sb.append("**************license**************").append("\n");
logger.info(sb.toString());
}
}
private static LicenseParam initLicenseParams() {
Preferences preference = Preferences.userNodeForPackage(LicenseUtil.class);
CipherParam cipherParam = new DefaultCipherParam(license_key_store_pwd);
KeyStoreParam privateStoreParam = new DefaultKeyStoreParam(LicenseUtil.class, "publicCerts.store",
license_public_alias, license_key_store_pwd, null);
LicenseParam licenseParams = new DefaultLicenseParam(license_Subject, preference, privateStoreParam,
cipherParam);
return licenseParams;
}
}