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.Map;
import java.util.prefs.Preferences;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LicenseUtil {
public static String license_public_alias = "publiccert";
public static String license_key_store_pwd = "Gjk_123456";
public static String license_path = "dc.dat";
private static final String license_public_store_path = "publicCerts.store";
public 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;
private static int clientMaxSessionCount = 1;
private static int apiMaxSessionCount = 1;
public static int getClientMaxSessionCount() {
return clientMaxSessionCount;
}
public static int getApiMaxSessionCount() {
return apiMaxSessionCount;
}
public static void install() {
licenseManager = getLicenseManager(initLicenseParams());
try {
if (content == null) {
File file = new File(license_path);
content = licenseManager.install(file);
logger.info("license install success");
printLicense();
}
} 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();
System.out.println("license verify success");
} catch (Exception e) {
System.out.println("license verify failed,system exit.");
logger.error("license verify failed,system exit.");
System.exit(0);
}
}
public static void printLicense() {
if (content != null) {
StringBuilder sb = new StringBuilder();
sb.append("**************license**************").append("\n");
sb.append("startDate:").append(sdf.format(content.getNotBefore())).append(",");
sb.append("endDate:").append(sdf.format(content.getNotAfter())).append(",");
@SuppressWarnings("unchecked")
Map extra = (Map) content.getExtra();
clientMaxSessionCount = (Integer) extra.get("ClientMaxSessionCount");
apiMaxSessionCount = (Integer) extra.get("ApiMaxSessionCount");
sb.append("clientMaxSessionCount:").append(clientMaxSessionCount).append(",");
sb.append("apiMaxSessionCount:").append(apiMaxSessionCount).append(",");
sb.append("comment:").append(content.getInfo());
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, license_public_store_path,
license_public_alias, license_key_store_pwd, null);
LicenseParam licenseParams = new DefaultLicenseParam(license_Subject, preference, privateStoreParam,
cipherParam);
return licenseParams;
}
}