Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
smthing committed Nov 7, 2024
1 parent 1925add commit eda76da
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<packaging>pom</packaging>

<properties>
<smarthttp.version>1.6.1</smarthttp.version>
<smarthttp.version>2.0</smarthttp.version>
<smartservlet.version>2.2</smartservlet.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public LicenseException(String message) {
public LicenseException(String message, Throwable cause) {
super(message, cause);
}

@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.smartboot.http.common.enums.HttpStatus;
import org.smartboot.http.common.logging.Logger;
import org.smartboot.http.common.logging.LoggerFactory;
import org.smartboot.http.common.utils.StringUtils;
import org.smartboot.http.server.HttpRequest;
import org.smartboot.http.server.HttpResponse;
import org.smartboot.http.server.HttpServerHandler;
Expand All @@ -36,7 +37,7 @@
*/
public class LicensePlugin extends Plugin {
private static final Logger LOGGER = LoggerFactory.getLogger(LicensePlugin.class);

private static final LicenseTO INVALID_LICENSE = new LicenseTO();
private LicenseTO licenseTO;
private License license;

Expand All @@ -50,7 +51,7 @@ public void initPlugin(Container containerRuntime) {
containerRuntime.getConfiguration().setHttpServerHandler(new HttpServerHandler() {
@Override
public void handle(HttpRequest request, HttpResponse response, CompletableFuture<Object> completableFuture) throws Throwable {
if (licenseTO != null || "/favicon.ico".equals(request.getRequestURI())) {
if ((licenseTO != null && licenseTO != INVALID_LICENSE) || "/favicon.ico".equals(request.getRequestURI())) {
baseHandler.handle(request, response, completableFuture);
} else {
try {
Expand Down Expand Up @@ -78,6 +79,10 @@ public void onContainerInitialized(Container container) {
System.out.println("\t" + ConsoleColors.RED + "ERROR:License not found, please check the license file:[ " + (isSpringBoot() ? "src/main/resources/smart-servlet/License.shield" : "${SERVLET_HOME}/conf/License.shield") + " ]." + ConsoleColors.RESET);
return;
}
if (licenseTO == INVALID_LICENSE) {
System.out.println("\t" + ConsoleColors.RED + "ERROR:License is invalid, please check the license file:[ " + (isSpringBoot() ? "src/main/resources/smart-servlet/License.shield" : "${SERVLET_HOME}/conf/License.shield") + " ]." + ConsoleColors.RESET);
return;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("\t:: Licensed to " + ConsoleColors.BOLD + ConsoleColors.ANSI_UNDERLINE_ON + ConsoleColors.BLUE + licenseTO.getApplicant() + ConsoleColors.ANSI_RESET + " until " + ConsoleColors.BOLD + ConsoleColors.ANSI_UNDERLINE_ON + ConsoleColors.BLUE + sdf.format(new Date(licenseTO.getExpireTime())) + ConsoleColors.ANSI_RESET);
System.out.println("\t:: License ID: " + ConsoleColors.BOLD + ConsoleColors.ANSI_UNDERLINE_ON + licenseTO.getSn() + ConsoleColors.RESET);
Expand Down Expand Up @@ -126,7 +131,7 @@ private void loadLicense() {
LicenseEntity entity = license.loadLicense(outputStream.toByteArray());
licenseTO = loadLicense(entity);
} catch (Exception e) {
LOGGER.error("License load exception", e);
LOGGER.error("License load exception", e.getMessage());
}
}

Expand All @@ -136,13 +141,55 @@ private LicenseTO loadLicense(LicenseEntity entity) throws IOException {
LicenseTO licenseTO = new LicenseTO();
licenseTO.setApplicant(properties.getProperty("enterprise.license.user"));
licenseTO.setSn(properties.getProperty("enterprise.license.number"));
licenseTO.setCompatible(properties.getProperty("enterprise.compatible"));
licenseTO.setExpireTime(entity.getExpireTime());
licenseTO.setTrialDuration(entity.getTrialDuration());
licenseTO.setContact(entity.getContact());
licenseTO.setVendor(entity.getApplicant());

if (!isVersionSupported(Container.VERSION.substring(1), properties.getProperty("supportVersion"))) {
return INVALID_LICENSE;
}
return licenseTO;
}

public static boolean isVersionSupported(String containerVersion, String supportVersion) {
if (StringUtils.isBlank(supportVersion)) {
return false;
}
// 解析支持版本范围
String[] versionRange = supportVersion.split("~");
String startVersion = versionRange[0];
String endVersion = versionRange.length == 2 ? versionRange[1] : "99.99.99";

// 将版本号转换为整数数组
int[] containerVersionArray = parseVersion(containerVersion);
int[] startVersionArray = parseVersion(startVersion);
int[] endVersionArray = parseVersion(endVersion);

// 比较版本号
return compareVersions(containerVersionArray, startVersionArray) >= 0 && compareVersions(containerVersionArray, endVersionArray) <= 0;
}

private static int[] parseVersion(String version) {
String[] parts = version.split("\\.");
int[] versionArray = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
versionArray[i] = Integer.parseInt(parts[i]);
}
return versionArray;
}

private static int compareVersions(int[] version1, int[] version2) {
int length = Math.max(version1.length, version2.length);
for (int i = 0; i < length; i++) {
int v1 = i < version1.length ? version1[i] : 0;
int v2 = i < version2.length ? version2[i] : 0;
if (v1 != v2) {
return v1 - v2;
}
}
return 0;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class LicenseTO {
* 产品供应商
*/
private String vendor;
private String compatible;

public String getApplicant() {
return applicant;
Expand Down Expand Up @@ -90,11 +89,4 @@ public void setVendor(String vendor) {
this.vendor = vendor;
}

public String getCompatible() {
return compatible;
}

public void setCompatible(String compatible) {
this.compatible = compatible;
}
}

0 comments on commit eda76da

Please sign in to comment.