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 cd9fd3c commit 9708eb6
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void handleRequest(HandlerContext handlerContext) {
private void loadAndInstallPlugins() {
plugins.add(new Plugin() {
@Override
public void onContainerStopped(ServletContextRuntime containerRuntime) {
public void onServletContextStopped(ServletContextRuntime containerRuntime) {
LOGGER.info("remove servletContextRuntime:{} from runtimes", containerRuntime.getContextPath());
runtimes.remove(containerRuntime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void start() throws Throwable {
try {
//有些场景下ServletContainerInitializer初始化依赖当前容器的类加载器
Thread.currentThread().setContextClassLoader(deploymentInfo.getClassLoader());
plugins.forEach(plugin -> plugin.willStartContainer(this));
plugins.forEach(plugin -> plugin.willStartServletContext(this));

DeploymentInfo deploymentInfo = servletContext.getDeploymentInfo();

Expand All @@ -166,10 +166,10 @@ public void start() throws Throwable {
initFilter(deploymentInfo);
started = true;
deploymentInfo.amazing();
plugins.forEach(plugin -> plugin.onContainerStartSuccess(this));
plugins.forEach(plugin -> plugin.onServletContextStartSuccess(this));
} catch (Exception e) {
e.printStackTrace();
plugins.forEach(plugin -> plugin.whenContainerStartError(this, e));
plugins.forEach(plugin -> plugin.whenServletContextStartError(this, e));
throw e;
} finally {
Thread.currentThread().setContextClassLoader(currentClassLoader);
Expand Down Expand Up @@ -320,12 +320,12 @@ private void initFilter(DeploymentInfo deploymentInfo) throws Exception {
}

public void stop() {
plugins.forEach(plugin -> plugin.willStopContainer(this));
plugins.forEach(plugin -> plugin.willStopServletContext(this));
deploymentInfo.getServlets().values().stream().filter(ServletInfo::initialized).forEach(servletInfo -> servletInfo.getServlet().destroy());
ServletContextEvent event = deploymentInfo.getServletContextListeners().isEmpty() ? null : new ServletContextEvent(servletContext);
deploymentInfo.getServletContextListeners().forEach(servletContextListener -> servletContextListener.getListener().contextDestroyed(event));

plugins.forEach(plugin -> plugin.onContainerStopped(this));
plugins.forEach(plugin -> plugin.onServletContextStopped(this));
}

public DispatcherProvider getDispatcherProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,46 +71,46 @@ public void addServletContext(ServletContextRuntime runtime) {
/**
* servlet容器被启动成功之后被调用
*
* @param servletContextRuntime 当前启动成功的子容器
* @param runtime 当前启动成功的子容器
*/
public void onContainerStartSuccess(ServletContextRuntime servletContextRuntime) {
public void onServletContextStartSuccess(ServletContextRuntime runtime) {
// LOGGER.info("plugin:[" + pluginName() + "] do nothing for container: " + servletContextRuntime.getContextPath() + " when start success!");
}


/**
* servlet子容器启动前被调用
*
* @param containerRuntime 当前即将被启动的子容器
* @param runtime 当前即将被启动的子容器
*/
public void willStartContainer(ServletContextRuntime containerRuntime) {
public void willStartServletContext(ServletContextRuntime runtime) {
// LOGGER.info("plugin:[" + pluginName() + "] do nothing for container: " + containerRuntime.getContextPath() + " before start!");
}

/**
* servlet子容器启动失败时被调用
*
* @param containerRuntime 当前启动失败的子容器
* @param runtime 当前启动失败的子容器
*/
public void whenContainerStartError(ServletContextRuntime containerRuntime, Throwable throwable) {
public void whenServletContextStartError(ServletContextRuntime runtime, Throwable throwable) {
// LOGGER.info("plugin:[" + pluginName() + "] do nothing for container: " + containerRuntime.getContextPath() + " when start error!");
}

/**
* 即将消耗子容器
*
* @param containerRuntime 即将被消耗的子容器
* @param runtime 即将被消耗的子容器
*/
public void willStopContainer(ServletContextRuntime containerRuntime) {
public void willStopServletContext(ServletContextRuntime runtime) {
// LOGGER.info("plugin:[" + pluginName() + "]do nothing for container: " + containerRuntime.getContextPath() + " before stop!");
}

/**
* 子容器已销毁
*
* @param containerRuntime 当前被消耗的子容器
* @param runtime 当前被消耗的子容器
*/
public void onContainerStopped(ServletContextRuntime containerRuntime) {
public void onServletContextStopped(ServletContextRuntime runtime) {
// LOGGER.info("plugin:[" + pluginName() + "] do nothing for container: " + containerRuntime.getContextPath() + " when stop!");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class ContactPlugin extends Plugin {

@Override
public void onContainerStartSuccess(ServletContextRuntime servletContextRuntime) {
public void onServletContextStartSuccess(ServletContextRuntime servletContextRuntime) {
System.out.println();
System.out.println("\033[1mTechnical Support:\033[0m");
System.out.println(" · Document: https://smartboot.tech]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class DispatcherPlugin extends Plugin {

@Override
public void willStartContainer(ServletContextRuntime containerRuntime) {
public void willStartServletContext(ServletContextRuntime containerRuntime) {
containerRuntime.setDispatcherProvider(new DispatcherProviderImpl());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void addServletContext(ServletContextRuntime runtime) {
}

@Override
public void willStartContainer(ServletContextRuntime containerRuntime) {
public void willStartServletContext(ServletContextRuntime containerRuntime) {
containerRuntime.setFaviconProvider(runtime -> {
});
}
Expand Down Expand Up @@ -121,7 +121,7 @@ private void loadLicense() {
licenseTO = loadLicense(entity);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(":: 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(":: SN: " + ConsoleColors.BOLD + ConsoleColors.ANSI_UNDERLINE_ON + licenseTO.getSn() + ConsoleColors.RESET);
System.out.println(":: License ID: " + ConsoleColors.BOLD + ConsoleColors.ANSI_UNDERLINE_ON + licenseTO.getSn() + ConsoleColors.RESET);
System.out.println(":: Copyright© " + licenseTO.getVendor() + " ,E-mail: " + licenseTO.getContact());
if (licenseTO.getTrialDuration() > 0) {
System.out.println(ConsoleColors.RED + ":: Trial: " + licenseTO.getTrialDuration() + " minutes" + ConsoleColors.RESET);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public class MappingPlugin extends Plugin {
@Override
public void onContainerStartSuccess(ServletContextRuntime servletContextRuntime) {
public void onServletContextStartSuccess(ServletContextRuntime servletContextRuntime) {
MappingProviderImpl provider = new MappingProviderImpl(servletContextRuntime.getServletContext().getContextPath().length());
servletContextRuntime.setMappingProvider(provider);
servletContextRuntime.getDeploymentInfo().getServletMappings().forEach(mapping -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void initPlugin(Container container) {
}

@Override
public void willStartContainer(ServletContextRuntime containerRuntime) {
public void willStartServletContext(ServletContextRuntime containerRuntime) {
SessionProviderImpl sessionProvider = new SessionProviderImpl();
sessionProvider.setMaxInactiveInterval(containerRuntime.getDeploymentInfo().getSessionTimeout());
containerRuntime.setSessionProvider(sessionProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class WebsocketPlugin extends Plugin {


@Override
public void willStartContainer(ServletContextRuntime containerRuntime) {
public void willStartServletContext(ServletContextRuntime containerRuntime) {
containerRuntime.setWebsocketProvider(new WebsocketProviderImpl());
containerRuntime.getServletContext().setAttribute(ServerContainer.class.getName(), containerRuntime.getWebsocketProvider().getWebSocketServerContainer());

Expand Down
10 changes: 5 additions & 5 deletions springboot-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@
<version>2.2</version><!--最新版本 -->
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<version>3.3.1</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-undertow</artifactId>-->
<!-- <version>3.3.1</version>-->
<!-- </dependency>-->

</dependencies>
<build>
Expand Down

0 comments on commit 9708eb6

Please sign in to comment.