All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aspectran.jetty.JettyServer Maven / Gradle / Ivy

There is a newer version: 8.1.5
Show newest version
/*
 * Copyright (c) 2008-2023 The Aspectran Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aspectran.jetty;

import com.aspectran.core.component.bean.ablility.DisposableBean;
import com.aspectran.core.component.bean.ablility.InitializableBean;
import com.aspectran.core.util.StringUtils;
import com.aspectran.core.util.logging.Logger;
import com.aspectran.core.util.logging.LoggerFactory;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HandlerContainer;
import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.thread.ThreadPool;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * The Jetty Server managed by Aspectran.
 *
 * 

Created: 2016. 12. 22.

*/ public class JettyServer extends Server implements InitializableBean, DisposableBean { private static final Logger logger = LoggerFactory.getLogger(JettyServer.class); private boolean autoStart; public JettyServer() { super(); } public JettyServer(int port) { super(port); } public JettyServer(ThreadPool pool) { super(pool); } public boolean isAutoStart() { return autoStart; } public void setAutoStart(boolean autoStart) { this.autoStart = autoStart; } public void setSystemProperty(String key, String value) { System.setProperty(key, value); } @Override public void initialize() throws Exception { start(); } @Override public void destroy() { try { stop(); } catch (Exception e) { logger.error("Error while stopping jetty server: " + e.getMessage(), e); } } @Override public void doStart() throws Exception { if (autoStart) { logger.info("Starting embedded Jetty server"); super.doStart(); logger.info("Jetty started on port(s) " + getActualPortsDescription() + " with context path '" + getContextPath() + "'"); } } @Override public void doStop() { logger.info("Stopping embedded Jetty server"); try { super.doStop(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (Exception e) { logger.error("Unable to stop embedded Jetty server", e); } } private String getContextPath() { HandlerContainer handlerContainer = (HandlerContainer)getHandler(); return Arrays.stream(handlerContainer.getChildHandlers()) .filter(ContextHandler.class::isInstance).map(ContextHandler.class::cast) .map(ContextHandler::getContextPath).collect(Collectors.joining("', '")); } private String getActualPortsDescription() { StringBuilder ports = new StringBuilder(); for (Connector connector : getConnectors()) { NetworkConnector connector1 = (NetworkConnector)connector; if (ports.length() != 0) { ports.append(", "); } ports.append(connector1.getLocalPort()); ports.append(" ("); ports.append(StringUtils.joinCommaDelimitedList(connector.getProtocols())); ports.append(")"); } return ports.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy