Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
smthing committed Sep 6, 2024
1 parent d2922d7 commit b808f14
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public void handleRequest(HandlerContext handlerContext) {
}
System.out.println("====================================================================================================");
System.out.println(ConsoleColors.GREEN + BANNER + ConsoleColors.RESET + "\r\n:: smart-servlet :: (" + VERSION + ")");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~");
plugins.forEach(plugin -> plugin.onContainerInitialized(this));
System.out.println("====================================================================================================");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;

/**
Expand Down Expand Up @@ -130,7 +131,12 @@ public final void uninstall() {

protected InputStream getResource(String fileName) throws IOException {
if (isSpringBoot()) {
return getClass().getClassLoader().getResourceAsStream("smart-servlet/" + fileName);
URL url = getClass().getClassLoader().getResource("smart-servlet/" + fileName);
if (url == null) {
return null;
} else {
return url.openStream();
}
} else {
File file = new File(getServletHome(), "conf/" + fileName);
if (file.isFile()) {
Expand Down Expand Up @@ -167,4 +173,33 @@ private void checkSate() {
throw new IllegalStateException("plugin [ " + pluginName() + " ] has installed!");
}
}

protected static class ConsoleColors {
/**
* 重置颜色
*/
public static final String RESET = "\033[0m";
/**
* 蓝色
*/
public static final String BLUE = "\033[34m";

/**
* 红色
*/
public static final String RED = "\033[31m";

/**
* 绿色
*/
public static final String GREEN = "\033[32m";

//加粗
public static final String BOLD = "\033[1m";

public static final String ANSI_UNDERLINE_ON = "\u001B[4m"; // 开启下划线
public static final String ANSI_RESET = "\u001B[0m"; // 重置所有样式


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public void handle(HttpRequest request, HttpResponse response, CompletableFuture

@Override
public void onContainerInitialized(Container container) {
System.out.println("\033[1mLicense Plugin:\033[0m");
if (licenseTO == null) {
System.err.println("License file not found, please check the license file path.");
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;
}
System.out.println("\033[1mLicense Plugin:\033[0m");
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 @@ -116,6 +116,9 @@ private void loadLicense() {
}, 10000);

try (InputStream fileInputStream = getResource("License.shield")) {
if (fileInputStream == null) {
return;
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
Expand Down Expand Up @@ -145,32 +148,5 @@ private LicenseTO loadLicense(LicenseEntity entity) throws IOException {
}


static class ConsoleColors {
/**
* 重置颜色
*/
public static final String RESET = "\033[0m";
/**
* 蓝色
*/
public static final String BLUE = "\033[34m";

/**
* 红色
*/
public static final String RED = "\033[31m";

/**
* 绿色
*/
public static final String GREEN = "\033[32m";

//加粗
public static final String BOLD = "\033[1m";

public static final String ANSI_UNDERLINE_ON = "\u001B[4m"; // 开启下划线
public static final String ANSI_RESET = "\u001B[0m"; // 重置所有样式


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class TlsPlugin extends Plugin {
private static final Logger LOGGER = LoggerFactory.getLogger(TlsPlugin.class);
private HttpBootstrap bootstrap;
private SSLConfig sslConfig;
private String errorMessage;

@Override
public void initPlugin(Container containerRuntime) {
Expand All @@ -40,7 +41,12 @@ public void initPlugin(Container containerRuntime) {
switch (sslConfig.getType()) {
case "pem":
try (InputStream pemStream = getResource("smart-servlet.pem")) {
sslPlugin = new SslPlugin<>(new PemServerSSLContextFactory(pemStream));
if (pemStream != null) {
sslPlugin = new SslPlugin<>(new PemServerSSLContextFactory(pemStream));
} else {
errorMessage = "smart-servlet.pem not found, please check the file:[ " + (isSpringBoot() ? "src/main/resources/smart-servlet/smart-servlet.pem" : "${SERVLET_HOME}/conf/smart-servlet.pem") + " ].";
return;
}
}

break;
Expand All @@ -58,16 +64,21 @@ public void initPlugin(Container containerRuntime) {

} catch (Exception e) {
sslConfig = null;
bootstrap.shutdown();
bootstrap = null;
destroyPlugin();
throw new RuntimeException(e);
}
}

@Override
public void onContainerInitialized(Container container) {
if (sslConfig != null) {
System.out.println("\033[1mTLS Plugin:\033[0m");
System.out.println("\033[1mTLS Plugin:\033[0m");
if (!sslConfig.isEnable()) {
System.out.println("\tTLS is disabled.");
return;
}
if (errorMessage != null) {
System.out.println("\t" + ConsoleColors.RED + errorMessage + ConsoleColors.RESET);
} else {
System.out.println("\tTLS enabled, port:" + sslConfig.getPort());
}
}
Expand Down

0 comments on commit b808f14

Please sign in to comment.