package com.license.utils;
import Aladdin.Hasp;
import Aladdin.HaspStatus;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
public class HaspUtil {
private static final Log log = Log.get();
/** Runtime query scope */
protected final static String KEY_SCOPE = "\n"
+ "\n"
+ "\n"
+ "\n";
/** Runtime query view */
protected final static String KEY_VIEW = "";
protected final static String LICENSE_STATUS =
"" +
"" +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
" " +
"" +
"";
protected final static String KEY_C2V_FORMAT = "";
protected final static String KEY_ID_FORMAT = "\n" +
"\n" +
" \n" +
" \n" +
" \n" +
"";
public final static String ALL_FORMAT = "\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
"";
public final static String HASP_FORMAT = "\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
"";
public static String getLicenseInfo(String vendorCode, Integer featureId) {
Hasp hasp = new Hasp(featureId);
hasp.login(vendorCode);
int status = hasp.getLastError();
log.info("query license status: {}", status);
if(status == HaspStatus.HASP_STATUS_OK) {
String licenseInfo = hasp.getSessionInfo(ALL_FORMAT);
status = hasp.getLastError();
if (status != HaspStatus.HASP_STATUS_OK) {
log.error("Error: updateLicense Fails with status code :{}", StrUtil.toString(HaspStatusEnum.getHaspStatusByStatus(status)));
}
return licenseInfo;
}else if (status == HaspStatus.HASP_FEATURE_EXPIRED){
//过期
String licenseInfo = hasp.getInfo("", 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();
log.info("updateLicense license status: {}", status);
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);
String infos = "";
hasp.login(vendorCode);
int status = hasp.getLastError();
log.info("login status:"+status);
infos = hasp.getInfo(KEY_SCOPE, KEY_C2V_FORMAT, vendorCode);
if(infos == null || infos.equals("")) {
hasp = new Hasp(featureId);
infos = hasp.getInfo(KEY_SCOPE, KEY_VIEW, vendorCode);
status = hasp.getLastError();
log.info("getLicense c2v license status: {}", status);
if(status != HaspStatus.HASP_STATUS_OK) {
log.error("Error: getLicense c2v Fails with status code :{}",status);
throw new RuntimeException();
}
}
return infos;
}
public static int verify(String vendorCode, Integer fetureId) {
Hasp hasp = new Hasp(fetureId);
hasp.login(vendorCode);
int status = hasp.getLastError();
log.info("verify license status: {}", status);
if(status != HaspStatus.HASP_STATUS_OK) {
log.error("Error: license login Fails with status code :{}",status);
}
hasp.logout();
return status;
}
}