2022-01-21 16:20:07 +08:00
|
|
|
package com.license.utils;
|
2021-12-29 11:37:15 +08:00
|
|
|
import Aladdin.Hasp;
|
|
|
|
|
import Aladdin.HaspStatus;
|
2024-02-04 09:43:23 +00:00
|
|
|
import cn.hutool.core.util.StrUtil;
|
2021-12-29 11:37:15 +08:00
|
|
|
import cn.hutool.log.Log;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class HaspUtil {
|
|
|
|
|
private static final Log log = Log.get();
|
|
|
|
|
/** Runtime query scope */
|
2024-02-04 09:43:23 +00:00
|
|
|
protected final static String KEY_SCOPE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
2021-12-29 11:37:15 +08:00
|
|
|
+ "<haspscope>\n"
|
|
|
|
|
+ "<license_manager hostname=\"localhost\" />\n"
|
|
|
|
|
+ "</haspscope>\n";
|
|
|
|
|
/** Runtime query view */
|
|
|
|
|
protected final static String KEY_VIEW = "<haspformat format=\"host_fingerprint\"/>";
|
|
|
|
|
|
|
|
|
|
protected final static String LICENSE_STATUS =
|
|
|
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
|
|
|
|
|
"<haspformat root=\"hasp_info\">" +
|
|
|
|
|
" <feature>" +
|
|
|
|
|
" <attribute name=\"id\" />" +
|
|
|
|
|
" <element name=\"license\" />" +
|
|
|
|
|
" <hasp>" +
|
|
|
|
|
" <attribute name=\"id\" />" +
|
|
|
|
|
" <attribute name=\"type\" />" +
|
|
|
|
|
" </hasp>" +
|
|
|
|
|
" </feature>" +
|
|
|
|
|
"</haspformat>" +
|
|
|
|
|
"";
|
|
|
|
|
|
|
|
|
|
protected final static String KEY_C2V_FORMAT = "<haspformat format=\"updateinfo\"/>";
|
|
|
|
|
|
2024-02-04 09:43:23 +00:00
|
|
|
protected final static String KEY_ID_FORMAT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
2022-01-10 16:16:10 +08:00
|
|
|
"<haspformat root=\"haspscope\">\n" +
|
|
|
|
|
" <hasp>\n" +
|
|
|
|
|
" <attribute name=\"id\" />\n" +
|
|
|
|
|
" </hasp>\n" +
|
|
|
|
|
"</haspformat>";
|
|
|
|
|
|
2021-12-29 11:37:15 +08:00
|
|
|
public final static String ALL_FORMAT = "<haspformat>\n" +
|
|
|
|
|
" <hasp>\n" +
|
|
|
|
|
" <element name=\"id\"/>\n" +
|
|
|
|
|
" <element name=\"type\"/>\n" +
|
|
|
|
|
" <element name=\"configuration\"/>\n" +
|
|
|
|
|
" <element name=\"clone_protected\"/>\n" +
|
|
|
|
|
" <element name=\"disabled\"/>\n" +
|
|
|
|
|
" <element name=\"production_date\"/>\n" +
|
|
|
|
|
" <element name=\"updatecounter\"/>\n" +
|
|
|
|
|
" <element name=\"parentid\"/>\n" +
|
|
|
|
|
" <feature>\n" +
|
|
|
|
|
" <element name=\"id\"/>\n" +
|
|
|
|
|
" <element name=\"locked\"/>\n" +
|
|
|
|
|
" <element name=\"license\"/>\n" +
|
|
|
|
|
" <element name=\"name\"/>\n" +
|
|
|
|
|
" <element name=\"concurrency\"/>\n" +
|
|
|
|
|
" <element name=\"usable\"/>\n" +
|
|
|
|
|
" </feature>\n" +
|
|
|
|
|
" <product>\n" +
|
|
|
|
|
" <element name=\"id\"/>\n" +
|
|
|
|
|
" <element name=\"name\"/>\n" +
|
|
|
|
|
" </product>\n" +
|
|
|
|
|
" <license_manager>\n" +
|
|
|
|
|
" <element name=\"host_fingerprint\"/>\n" +
|
|
|
|
|
" <element name=\"hostname\"/>\n" +
|
|
|
|
|
" <element name=\"osname\"/>\n" +
|
|
|
|
|
" <element name=\"osversion\"/>\n" +
|
|
|
|
|
" <element name=\"name\"/>\n" +
|
|
|
|
|
" <element name=\"time\"/>\n" +
|
|
|
|
|
" <element name=\"uptime\"/>\n" +
|
|
|
|
|
" <element name=\"architecture\"/>\n" +
|
|
|
|
|
" <element name=\"ip\"/>\n" +
|
|
|
|
|
" </license_manager>\n" +
|
|
|
|
|
" <vendor>\n" +
|
|
|
|
|
" <element name=\"id\"/>\n" +
|
|
|
|
|
" <element name=\"name\"/>\n" +
|
|
|
|
|
" </vendor>\n" +
|
|
|
|
|
" </hasp>\n" +
|
|
|
|
|
"</haspformat>";
|
|
|
|
|
|
|
|
|
|
public final static String HASP_FORMAT = "<haspformat>\n" +
|
|
|
|
|
" <hasp>\n" +
|
|
|
|
|
" <element name=\"id\"/>\n" +
|
|
|
|
|
" <element name=\"type\"/>\n" +
|
|
|
|
|
" <element name=\"configuration\"/>\n" +
|
|
|
|
|
" <element name=\"clone_protected\"/>\n" +
|
|
|
|
|
" <element name=\"disabled\"/>\n" +
|
|
|
|
|
" <element name=\"production_date\"/>\n" +
|
|
|
|
|
" <element name=\"updatecounter\"/>\n" +
|
|
|
|
|
" <element name=\"parentid\"/>\n" +
|
|
|
|
|
" <feature>\n" +
|
|
|
|
|
" <element name=\"id\"/>\n" +
|
|
|
|
|
" <element name=\"locked\"/>\n" +
|
|
|
|
|
" <element name=\"license\"/>\n" +
|
|
|
|
|
" <element name=\"name\"/>\n" +
|
|
|
|
|
" <element name=\"concurrency\"/>\n" +
|
|
|
|
|
" <element name=\"usable\"/>\n" +
|
|
|
|
|
" </feature>\n" +
|
|
|
|
|
" <product>\n" +
|
|
|
|
|
" <element name=\"id\"/>\n" +
|
|
|
|
|
" <element name=\"name\"/>\n" +
|
|
|
|
|
" </product>\n" +
|
|
|
|
|
" </hasp>\n" +
|
|
|
|
|
"</haspformat>";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String getLicenseInfo(String vendorCode, Integer featureId) {
|
|
|
|
|
Hasp hasp = new Hasp(featureId);
|
|
|
|
|
hasp.login(vendorCode);
|
|
|
|
|
int status = hasp.getLastError();
|
2022-01-07 13:53:41 +08:00
|
|
|
log.info("query license status: {}", status);
|
2021-12-29 11:37:15 +08:00
|
|
|
if(status == HaspStatus.HASP_STATUS_OK) {
|
2024-02-04 09:43:23 +00:00
|
|
|
String licenseInfo = hasp.getSessionInfo(ALL_FORMAT);
|
2021-12-29 11:37:15 +08:00
|
|
|
status = hasp.getLastError();
|
|
|
|
|
if (status != HaspStatus.HASP_STATUS_OK) {
|
2024-02-04 09:43:23 +00:00
|
|
|
log.error("Error: updateLicense Fails with status code :{}", StrUtil.toString(HaspStatusEnum.getHaspStatusByStatus(status)));
|
2021-12-29 11:37:15 +08:00
|
|
|
}
|
|
|
|
|
return licenseInfo;
|
|
|
|
|
}else if (status == HaspStatus.HASP_FEATURE_EXPIRED){
|
|
|
|
|
//过期
|
|
|
|
|
String licenseInfo = hasp.getInfo("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><haspscope><feature id=\"\n" +
|
|
|
|
|
featureId +
|
|
|
|
|
"\"/></haspscope>", LICENSE_STATUS,vendorCode);
|
|
|
|
|
return licenseInfo;
|
|
|
|
|
}else {
|
|
|
|
|
log.error("Error: with status code :{}",status);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the key with the license (v2c)
|
|
|
|
|
*
|
|
|
|
|
* @param v2c
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static int updateKeyWithLicense(String v2c) {
|
|
|
|
|
Hasp hasp = new Hasp(Hasp.HASP_DEFAULT_FID);
|
|
|
|
|
hasp.update(v2c);
|
|
|
|
|
int status = hasp.getLastError();
|
2022-01-07 13:53:41 +08:00
|
|
|
log.info("updateLicense license status: {}", status);
|
2021-12-29 11:37:15 +08:00
|
|
|
if (status != HaspStatus.HASP_STATUS_OK) {
|
|
|
|
|
log.error("Error: updateLicense Fails with status code :{}",status);
|
|
|
|
|
}
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param vendorCode
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static String readC2V(String vendorCode, Integer featureId) {
|
|
|
|
|
Hasp hasp = new Hasp(featureId);
|
2024-02-04 09:43:23 +00:00
|
|
|
String infos = "";
|
2021-12-29 11:37:15 +08:00
|
|
|
hasp.login(vendorCode);
|
|
|
|
|
int status = hasp.getLastError();
|
2024-02-04 09:43:23 +00:00
|
|
|
log.info("login status:"+status);
|
|
|
|
|
infos = hasp.getInfo(KEY_SCOPE, KEY_C2V_FORMAT, vendorCode);
|
|
|
|
|
if(infos == null || infos.equals("")) {
|
2021-12-29 11:37:15 +08:00
|
|
|
hasp = new Hasp(featureId);
|
2024-02-04 09:43:23 +00:00
|
|
|
infos = hasp.getInfo(KEY_SCOPE, KEY_VIEW, vendorCode);
|
2021-12-29 11:37:15 +08:00
|
|
|
status = hasp.getLastError();
|
2022-01-07 13:53:41 +08:00
|
|
|
log.info("getLicense c2v license status: {}", status);
|
2021-12-29 11:37:15 +08:00
|
|
|
if(status != HaspStatus.HASP_STATUS_OK) {
|
|
|
|
|
log.error("Error: getLicense c2v Fails with status code :{}",status);
|
|
|
|
|
throw new RuntimeException();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-04 09:43:23 +00:00
|
|
|
return infos;
|
2021-12-29 11:37:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int verify(String vendorCode, Integer fetureId) {
|
|
|
|
|
Hasp hasp = new Hasp(fetureId);
|
|
|
|
|
hasp.login(vendorCode);
|
|
|
|
|
int status = hasp.getLastError();
|
2022-01-07 13:53:41 +08:00
|
|
|
log.info("verify license status: {}", status);
|
2021-12-29 11:37:15 +08:00
|
|
|
if(status != HaspStatus.HASP_STATUS_OK) {
|
|
|
|
|
log.error("Error: license login Fails with status code :{}",status);
|
|
|
|
|
}
|
|
|
|
|
hasp.logout();
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
}
|