|
| 1 | +/* |
| 2 | + * Copyright 2017-2025 noear.org and authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.noear.solon.server.tomcat.integration; |
| 17 | + |
| 18 | +import org.apache.catalina.util.ServerInfo; |
| 19 | +import org.noear.solon.Solon; |
| 20 | +import org.noear.solon.SolonApp; |
| 21 | +import org.noear.solon.server.ServerConstants; |
| 22 | +import org.noear.solon.server.ServerLifecycle; |
| 23 | +import org.noear.solon.server.ServerProps; |
| 24 | +import org.noear.solon.server.prop.impl.HttpServerProps; |
| 25 | +import org.noear.solon.core.*; |
| 26 | +import org.noear.solon.core.util.ClassUtil; |
| 27 | + |
| 28 | +import org.noear.solon.server.tomcat.TomcatServer; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +import jakarta.servlet.annotation.WebFilter; |
| 33 | +import jakarta.servlet.annotation.WebListener; |
| 34 | +import jakarta.servlet.annotation.WebServlet; |
| 35 | + |
| 36 | +public final class TomcatPlugin implements Plugin { |
| 37 | + static final Logger log = LoggerFactory.getLogger(TomcatPlugin.class); |
| 38 | + |
| 39 | + private static Signal _signal; |
| 40 | + |
| 41 | + public static Signal signal() { |
| 42 | + return _signal; |
| 43 | + } |
| 44 | + |
| 45 | + private ServerLifecycle _server = null; |
| 46 | + |
| 47 | + public static String solon_server_ver() { |
| 48 | + return ServerInfo.getServerInfo() + "/" + Solon.version(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void start(AppContext context) { |
| 53 | + if (Solon.app().enableHttp() == false) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + context.beanBuilderAdd(WebFilter.class, (clz, bw, ano) -> { |
| 58 | + }); |
| 59 | + context.beanBuilderAdd(WebServlet.class, (clz, bw, ano) -> { |
| 60 | + }); |
| 61 | + context.beanBuilderAdd(WebListener.class, (clz, bw, ano) -> { |
| 62 | + }); |
| 63 | + |
| 64 | + context.lifecycle(ServerConstants.SIGNAL_LIFECYCLE_INDEX, () -> { |
| 65 | + start0(Solon.app()); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + private void start0(SolonApp app) throws Throwable { |
| 70 | + //初始化属性 |
| 71 | + ServerProps.init(); |
| 72 | + |
| 73 | + Class<?> jspClz = ClassUtil.loadClass("org.apache.jasper.servlet.JspServlet"); |
| 74 | + |
| 75 | + HttpServerProps props = HttpServerProps.getInstance(); |
| 76 | + final String _host = props.getHost(); |
| 77 | + final int _port = props.getPort(); |
| 78 | + final String _name = props.getName(); |
| 79 | + |
| 80 | + _server = new TomcatServer(); |
| 81 | + |
| 82 | + long time_start = System.currentTimeMillis(); |
| 83 | + |
| 84 | + _server.start(_host, _port); |
| 85 | + |
| 86 | + final String _wrapHost = props.getWrapHost(); |
| 87 | + final int _wrapPort = props.getWrapPort(); |
| 88 | + _signal = new SignalSim(_name, _wrapHost, _wrapPort, "http", SignalType.HTTP); |
| 89 | + |
| 90 | + app.signalAdd(_signal); |
| 91 | + |
| 92 | + long time_end = System.currentTimeMillis(); |
| 93 | + |
| 94 | + String connectorInfo = "solon.connector:main: tomcat: Started ServerConnector@{HTTP/1.1,[http/1.1]"; |
| 95 | + if (app.enableWebSocket()) { |
| 96 | + String wsServerUrl = props.buildWsServerUrl(false); |
| 97 | + log.info(connectorInfo + "[WebSocket]}{" + wsServerUrl + "}"); |
| 98 | + } |
| 99 | + |
| 100 | + String httpServerUrl = props.buildHttpServerUrl(false); |
| 101 | + log.info(connectorInfo + "}{" + httpServerUrl + "}"); |
| 102 | + log.info("Server:main: tomcat: Started (" + solon_server_ver() + ") @" + (time_end - time_start) + "ms"); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void stop() throws Throwable { |
| 107 | + if (_server != null) { |
| 108 | + _server.stop(); |
| 109 | + _server = null; |
| 110 | + |
| 111 | + log.info("Server:main: tomcat: Has Stopped (" + solon_server_ver() + ")"); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments