From b808f14d69eb70ed140dd293f7115f9cec6f9451 Mon Sep 17 00:00:00 2001 From: zhengjw22 Date: Fri, 6 Sep 2024 16:43:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tech/smartboot/servlet/Container.java | 1 + .../smartboot/servlet/plugins/Plugin.java | 37 ++++++++++++++++++- .../plugins/license/LicensePlugin.java | 34 +++-------------- .../servlet/plugins/tls/TlsPlugin.java | 21 ++++++++--- 4 files changed, 58 insertions(+), 35 deletions(-) diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/Container.java b/servlet-core/src/main/java/tech/smartboot/servlet/Container.java index 0bbd748..fb933c1 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/Container.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/Container.java @@ -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("===================================================================================================="); } diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/plugins/Plugin.java b/servlet-core/src/main/java/tech/smartboot/servlet/plugins/Plugin.java index a203b60..b41a19e 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/plugins/Plugin.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/plugins/Plugin.java @@ -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; /** @@ -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()) { @@ -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"; // 重置所有样式 + + + } } diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicensePlugin.java b/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicensePlugin.java index 83cac75..afc0e69 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicensePlugin.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/plugins/license/LicensePlugin.java @@ -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); @@ -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; @@ -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"; // 重置所有样式 - - - } } diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/plugins/tls/TlsPlugin.java b/servlet-core/src/main/java/tech/smartboot/servlet/plugins/tls/TlsPlugin.java index 7bbec72..7bc1606 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/plugins/tls/TlsPlugin.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/plugins/tls/TlsPlugin.java @@ -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) { @@ -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; @@ -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()); } }