@Override public void start() throws EmbeddedServletContainerException { ServerBootstrap b = new ServerBootstrap(); groups(b); servletExecutor = new DefaultEventExecutorGroup(50); b.childHandler(new NettyEmbeddedServletInitializer(servletExecutor, context)); // Don't yet need the complexity of lifecycle state, listeners etc, so tell the context it's initialised here context.setInitialised(true); ChannelFuture future = b.bind(address).awaitUninterruptibly(); //noinspection ThrowableResultOfMethodCallIgnored Throwable cause = future.cause(); if (null != cause) { throw new EmbeddedServletContainerException("Could not start Netty server", cause); } logger.info(context.getServerInfo() + " started on port: " + getPort()); }
@Override public void stop() throws EmbeddedServletContainerException { try { if (null != bossGroup) { bossGroup.shutdownGracefully().await(); } if (null != workerGroup) { workerGroup.shutdownGracefully().await(); } if (null != servletExecutor) { servletExecutor.shutdownGracefully().await(); } } catch (InterruptedException e) { throw new EmbeddedServletContainerException("Container stop interrupted", e); } }
@Override public void start() throws EmbeddedServletContainerException { ServerBootstrap b = new ServerBootstrap(); groups(b); // servletExecutor = new DefaultEventExecutorGroup(50); // b.childHandler(new NettyEmbeddedServletInitializer(servletExecutor, context)); b.childHandler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new DispatcherInbound(address, context)); } }); // Don't yet need the complexity of lifecycle state, listeners etc, so tell the context it's initialised here ChannelFuture future = b.bind(address).awaitUninterruptibly(); //noinspection ThrowableResultOfMethodCallIgnored Throwable cause = future.cause(); if (null != cause) { throw new EmbeddedServletContainerException("Could not start Netty server", cause); } logger.info(context.getServerInfo() + " started on port: " + getPort()); context.setInitialised(true); context.addFilter(HessianConstant.HESSIAN_PATH,new HessianFilter(context)); ServletNettyHttpSessionManager.start(); }
private void configureSslTrustStore(AbstractHttp11JsseProtocol<?> protocol, Ssl ssl) { if (ssl.getTrustStore() != null) { try { protocol.setTruststoreFile( ResourceUtils.getURL(ssl.getTrustStore()).toString()); } catch (FileNotFoundException ex) { throw new EmbeddedServletContainerException( "Could not load trust store: " + ex.getMessage(), ex); } } protocol.setTruststorePass(ssl.getTrustStorePassword()); if (ssl.getTrustStoreType() != null) { protocol.setTruststoreType(ssl.getTrustStoreType()); } if (ssl.getTrustStoreProvider() != null) { protocol.setTruststoreProvider(ssl.getTrustStoreProvider()); } }
@Override public void stop() throws EmbeddedServletContainerException { synchronized (this.monitor) { if (this.started) { try { this.started = false; this.manager.stop(); this.undertow.stop(); } catch (Exception ex) { throw new EmbeddedServletContainerException("Unable to stop undertow", ex); } } } }
private void configureSslKeyStore(SslContextFactory factory, Ssl ssl) { try { URL url = ResourceUtils.getURL(ssl.getKeyStore()); factory.setKeyStoreResource(Resource.newResource(url)); } catch (IOException ex) { throw new EmbeddedServletContainerException( "Could not find key store '" + ssl.getKeyStore() + "'", ex); } if (ssl.getKeyStoreType() != null) { factory.setKeyStoreType(ssl.getKeyStoreType()); } if (ssl.getKeyStoreProvider() != null) { factory.setKeyStoreProvider(ssl.getKeyStoreProvider()); } }
private void configureSslTrustStore(SslContextFactory factory, Ssl ssl) { if (ssl.getTrustStorePassword() != null) { factory.setTrustStorePassword(ssl.getTrustStorePassword()); } if (ssl.getTrustStore() != null) { try { URL url = ResourceUtils.getURL(ssl.getTrustStore()); factory.setTrustStoreResource(Resource.newResource(url)); } catch (IOException ex) { throw new EmbeddedServletContainerException( "Could not find trust store '" + ssl.getTrustStore() + "'", ex); } } if (ssl.getTrustStoreType() != null) { factory.setTrustStoreType(ssl.getTrustStoreType()); } if (ssl.getTrustStoreProvider() != null) { factory.setTrustStoreProvider(ssl.getTrustStoreProvider()); } }
private void initialize() { synchronized (this.monitor) { try { // Cache and clear the connectors to prevent requests being handled before // the application context is ready this.connectors = this.server.getConnectors(); this.server.setConnectors(null); // Start the server so that the ServletContext is available this.server.start(); this.server.setStopAtShutdown(false); } catch (Exception ex) { // Ensure process isn't left running stopSilently(); throw new EmbeddedServletContainerException( "Unable to start embedded Jetty servlet container", ex); } } }
@Test public void primaryConnectorPortClashThrowsIllegalStateException() throws InterruptedException, IOException { doWithBlockedPort(new BlockedPortAction() { @Override public void run(int port) { TomcatEmbeddedServletContainerFactory factory = getFactory(); factory.setPort(port); try { TomcatEmbeddedServletContainerFactoryTests.this.container = factory .getEmbeddedServletContainer(); TomcatEmbeddedServletContainerFactoryTests.this.container.start(); fail(); } catch (EmbeddedServletContainerException ex) { // Ignore } } }); }
@Test public void specificPortsViaPropertiesWithClash() throws Exception { int managementPort = ports.get().management; ServerSocket serverSocket = new ServerSocket(); serverSocket.bind(new InetSocketAddress(managementPort)); try { EnvironmentTestUtils.addEnvironment(this.applicationContext, "server.port:" + ports.get().server, "management.port:" + ports.get().management); this.applicationContext.register(RootConfig.class, EndpointConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.thrown.expect(EmbeddedServletContainerException.class); this.applicationContext.refresh(); } finally { serverSocket.close(); } }
private synchronized void initialize() { try { // Cache and clear the connectors to prevent requests being handled before // the application context is ready this.connectors = this.server.getConnectors(); this.server.setConnectors(null); // Start the server so that the ServletContext is available this.server.start(); this.server.setStopAtShutdown(false); } catch (Exception ex) { // Ensure process isn't left running stopSilently(); throw new EmbeddedServletContainerException( "Unable to start embedded Jetty servlet container", ex); } }
private synchronized void initialize() throws EmbeddedServletContainerException { TomcatEmbeddedServletContainer.logger .info("Tomcat initialized with port(s): " + getPortsDescription(false)); try { addInstanceIdToEngineName(); // Remove service connectors to that protocol binding doesn't happen yet removeServiceConnectors(); // Start the server to trigger initialization listeners this.tomcat.start(); // We can re-throw failure exception directly in the main thread rethrowDeferredStartupExceptions(); // Unlike Jetty, all Tomcat threads are daemon threads. We create a // blocking non-daemon to stop immediate shutdown startDaemonAwaitThread(); } catch (Exception ex) { throw new EmbeddedServletContainerException("Unable to start embedded Tomcat", ex); } }
@Override public void start() throws EmbeddedServletContainerException { try { addPreviouslyRemovedConnectors(); Connector connector = this.tomcat.getConnector(); if (connector != null && this.autoStart) { startConnector(connector); } // Ensure process isn't left running if it actually failed to start if (connectorsHaveFailedToStart()) { stopSilently(); throw new IllegalStateException("Tomcat connector in failed state"); } TomcatEmbeddedServletContainer.logger .info("Tomcat started on port(s): " + getPortsDescription(true)); } catch (Exception ex) { throw new EmbeddedServletContainerException( "Unable to start embedded Tomcat servlet container", ex); } }
/** * Returns the absolute temp dir for given web server. * @param prefix webserver name * @return The temp dir for given web server. */ protected File createTempDir(String prefix) { try { File tempFolder = File.createTempFile(prefix + ".", "." + getPort()); tempFolder.delete(); tempFolder.mkdir(); tempFolder.deleteOnExit(); return tempFolder; } catch (IOException ex) { throw new EmbeddedServletContainerException( "Unable to create Tomcat tempdir. java.io.tmpdir is set to " + System.getProperty("java.io.tmpdir"), ex); } }
@Override public synchronized void start() throws EmbeddedServletContainerException { try { if (!this.autoStart) { return; } if (this.undertow == null) { this.undertow = createUndertowServer(); } this.undertow.start(); this.started = true; UndertowEmbeddedServletContainer.logger .info("Undertow started on port(s) " + getPortsDescription()); } catch (ServletException ex) { throw new EmbeddedServletContainerException( "Unable to start embdedded Undertow", ex); } }
@Test public void primaryConnectorPortClashThrowsIllegalStateException() throws InterruptedException, IOException { final int port = SocketUtils.findAvailableTcpPort(40000); doWithBlockedPort(port, new Runnable() { @Override public void run() { TomcatEmbeddedServletContainerFactory factory = getFactory(); factory.setPort(port); try { TomcatEmbeddedServletContainerFactoryTests.this.container = factory .getEmbeddedServletContainer(); TomcatEmbeddedServletContainerFactoryTests.this.container.start(); fail(); } catch (EmbeddedServletContainerException ex) { // Ignore } } }); }
@Test public void specificPortsViaPropertiesWithClash() throws Exception { int managementPort = ports.get().management; ServerSocket serverSocket = new ServerSocket(); serverSocket.bind(new InetSocketAddress(managementPort)); try { EnvironmentTestUtils.addEnvironment(this.applicationContext, "server.port:" + ports.get().server, "management.port:" + ports.get().management); this.applicationContext.register(RootConfig.class, EndpointConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.thrown.expect(EmbeddedServletContainerException.class); this.applicationContext.refresh(); this.applicationContext.close(); } finally { serverSocket.close(); assertAllClosed(); } }
private void startConnector(Connector connector) { try { for (Container child : this.tomcat.getHost().findChildren()) { if (child instanceof TomcatEmbeddedContext) { ((TomcatEmbeddedContext) child).deferredLoadOnStartup(); } } } catch (Exception ex) { TomcatEmbeddedServletContainer.logger.error("Cannot start connector: ", ex); throw new EmbeddedServletContainerException( "Unable to start embedded Tomcat connectors", ex); } }
private void configureSslKeyStore(AbstractHttp11JsseProtocol<?> protocol, Ssl ssl) { try { protocol.setKeystoreFile(ResourceUtils.getURL(ssl.getKeyStore()).toString()); } catch (FileNotFoundException ex) { throw new EmbeddedServletContainerException( "Could not load key store: " + ex.getMessage(), ex); } if (ssl.getKeyStoreType() != null) { protocol.setKeystoreType(ssl.getKeyStoreType()); } if (ssl.getKeyStoreProvider() != null) { protocol.setKeystoreProvider(ssl.getKeyStoreProvider()); } }
@Override public void start() throws EmbeddedServletContainerException { synchronized (this.monitor) { try { if (!this.autoStart) { return; } if (this.undertow == null) { this.undertow = createUndertowServer(); } this.undertow.start(); this.started = true; UndertowEmbeddedServletContainer.logger .info("Undertow started on port(s) " + getPortsDescription()); } catch (Exception ex) { if (findBindException(ex) != null) { List<Port> failedPorts = getConfiguredPorts(); List<Port> actualPorts = getActualPorts(); failedPorts.removeAll(actualPorts); if (failedPorts.size() == 1) { throw new PortInUseException( failedPorts.iterator().next().getNumber()); } } throw new EmbeddedServletContainerException( "Unable to start embedded Undertow", ex); } } }
@Override public synchronized void start() throws EmbeddedServletContainerException { try { if (!this.autoStart) { return; } if (this.undertow == null) { this.undertow = createUndertowServer(); } this.undertow.start(); this.started = true; UndertowEmbeddedServletContainer.logger .info("Undertow started on port(s) " + getPortsDescription()); } catch (Exception ex) { if (findBindException(ex) != null) { List<Port> failedPorts = getConfiguredPorts(); List<Port> actualPorts = getActualPorts(); failedPorts.removeAll(actualPorts); if (failedPorts.size() == 1) { throw new PortInUseException( failedPorts.iterator().next().getNumber()); } } throw new EmbeddedServletContainerException( "Unable to start embedded Undertow", ex); } }
@Override public synchronized void stop() throws EmbeddedServletContainerException { if (this.started) { try { this.started = false; this.manager.stop(); this.undertow.stop(); } catch (Exception ex) { throw new EmbeddedServletContainerException("Unable to stop undertow", ex); } } }
@Test public void additionalConnectorPortClashThrowsIllegalStateException() throws InterruptedException, IOException { final int port = SocketUtils.findAvailableTcpPort(40000); doWithBlockedPort(port, new Runnable() { @Override public void run() { TomcatEmbeddedServletContainerFactory factory = getFactory(); Connector connector = new Connector( "org.apache.coyote.http11.Http11NioProtocol"); connector.setPort(port); factory.addAdditionalTomcatConnectors(connector); try { TomcatEmbeddedServletContainerFactoryTests.this.container = factory .getEmbeddedServletContainer(); TomcatEmbeddedServletContainerFactoryTests.this.container.start(); fail(); } catch (EmbeddedServletContainerException ex) { // Ignore } } }); }