protected int getLocalPort(Server server) { return server.getConnectors()[0].getLocalPort(); }
@Override synchronized public void removeConnector(String host, int port) { Connector[] connectors = server.getConnectors(); for (Connector connector : connectors) { if (connector.getHost().equals(host) && connector.getLocalPort() == port) { if (connector.isStarted() || connector.isStarting()) { try { connector.stop(); } catch (Exception ex) { log.severe("Couldn't stop connector: " + connector + " " + ex); throw new RuntimeException(ex); } } server.removeConnector(connector); if (connectors.length == 1) { log.info("No more connectors, stopping Jetty server"); stopIfRunning(); } break; } } }
/** * {@inheritDoc} */ @Override public int getPort() { return httpConnector.getLocalPort(); } }
/** * {@inheritDoc} */ @Override public int getPort() { return httpConnector.getLocalPort(); } }
public String url() { return "http://localhost:" + connector.getLocalPort() + "/"; }
int getActualPort() { if (server == null) { return 0; } return server.getConnectors()[0].getLocalPort(); }
protected int getLocalPort(Server server) { return server.getConnectors()[0].getLocalPort(); }
public int getLocalPort() { return jettyServer.getConnectors()[0].getLocalPort(); }
public String getAddress() { return String.format("http://localhost:%s/", server.getConnectors()[0].getLocalPort()); } }
/** * Gets the actual port that the web server is listening on. * * @return the local port */ public int getLocalPort() { return mServer.getServer().getConnectors()[0].getLocalPort(); }
public int getPort() { return connector.getLocalPort(); }
private int startServer(int port) throws Exception { connector.setPort(port); this.server.setHandler(this.configurator.createHandler()); this.server.addConnector(connector); this.server.start(); return connector.getLocalPort(); }
/** * Get the InetSocketAddress of the server. * * @return InetSocketAddress of server. */ public InetSocketAddress getSocketAddress() { if (!server.isRunning()) { throw new IllegalStateException("Server not started yet"); } // assumes we only have one connector Connector connector = server.getConnectors()[0]; return new InetSocketAddress(connector.getHost(), connector.getLocalPort()); }
/** * @return Jetty's http connector port. * @throws IllegalStateException If Jetty is not running or the http connector cannot be found. */ @Override public int getPort() throws IllegalStateException { if (server == null) { throw new IllegalStateException("Jetty server is not initialized"); } if (!server.isStarted()) { throw new IllegalStateException("Jetty server is not started"); } Connector[] connectors = server.getConnectors(); if (connectors.length == 0) { throw new IllegalStateException("Jetty server is not configured with any connectors"); } for (Connector connector : connectors) { if (connector instanceof SelectChannelConnector) { //must be the http connector return connector.getLocalPort(); } } throw new IllegalStateException("Could not find Jetty http connector"); }
/** * Starts the web server. */ public void startWebServer() { try { mServer.getConnectors()[0].close(); mServer.getConnectors()[0].open(); mServer.start(); if (mAddress.getPort() == 0) { int webPort = mServer.getConnectors()[0].getLocalPort(); mAddress = new InetSocketAddress(mAddress.getHostName(), webPort); // reset web service port mTachyonConf.set(mService.getPortKey(), Integer.toString(webPort)); } LOG.info(mService.getServiceName() + " started @ " + mAddress); } catch (Exception e) { throw Throwables.propagate(e); } } }
/** recreates the server so that tracing uses the supplied trace configuration */ @Override protected final void init(Brave brave, SpanNameProvider spanNameProvider) { stop(); SocketConnector connector = new SocketConnector(); connector.setMaxIdleTime(1000 * 60 * 60); connector.setPort(port); server = new Server(); server.setConnectors(new Connector[] {connector}); ServletContextHandler context = new ServletContextHandler(); context.setContextPath("/"); server.setHandler(context); init(context, brave, spanNameProvider); try { server.start(); port = server.getConnectors()[0].getLocalPort(); } catch (Exception e) { throw new IllegalStateException("Failed to start server.", e); } }
@Override synchronized public void removeConnector(String host, int port) { Connector[] connectors = server.getConnectors(); for (Connector connector : connectors) { if (connector.getHost().equals(host) && connector.getLocalPort() == port) { if (connector.isStarted() || connector.isStarting()) { try { connector.stop(); } catch (Exception ex) { log.severe("Couldn't stop connector: " + connector + " " + ex); throw new RuntimeException(ex); } } server.removeConnector(connector); if (connectors.length == 1) { log.info("No more connectors, stopping Jetty server"); stopIfRunning(); } break; } } }
public void start() throws Exception { Connector connector; if (enableSsl) { SslContextFactory sslContextFactory = new SslContextFactory(); if (sslContext == null) { } else { System.out.println("Using provided SSLContext"); System.out.println(sslContext); sslContextFactory.setSslContext(sslContext); } connector = new SslSelectChannelConnector(); ((SslSelectChannelConnector) connector).setSslContext(sslContext); } else { connector = new SelectChannelConnector(); } connector.setPort(port); server.addConnector(connector); connector.open(); server.setHandler(contexts); server.start(); if (enableSsl) { protocol = "https"; } else { protocol = "http"; } port = connector.getLocalPort(); host = connector.getHost(); }
/******************************************************************************************************************* * * ******************************************************************************************************************/ /* VisibleForTesting FIXME */ public void onPowerOnNotification (final @ListensTo @Nonnull PowerOnNotification notification) throws Exception { log.info("onPowerOnNotification({})", notification); ipAddress = getNonLoopbackIPv4Address().getHostAddress(); server = new Server(InetSocketAddress.createUnresolved(ipAddress, Integer.getInteger("port", 0))); final ServletContextHandler servletContext = new ServletContextHandler(); servletContext.setBaseResource(new ResourceCollection(findWebResources())); log.info("RESOURCE BASE: {}", servletContext.getResourceBase()); servletContext.setContextPath("/"); servletContext.setWelcomeFiles(new String[] { "index.xhtml" }); final DelegateWebApplicationContext wac = new DelegateWebApplicationContext(applicationContext, servletContext.getServletContext()); servletContext.addServlet(new ServletHolder("spring", new DispatcherServlet(wac)), "/rest/*"); servletContext.addServlet(new ServletHolder("default", new DefaultServlet()), "/*"); servletContext.addFilter(new FilterHolder(new LoggingFilter()), "/*", EnumSet.allOf(DispatcherType.class)); server.setHandler(servletContext); server.start(); port = server.getConnectors()[0].getLocalPort(); // jetty 8 // port = ((ServerConnector)server.getConnectors()[0]).getLocalPort(); // jetty 9 log.info(">>>> resource server jetty started at {}:{} ", ipAddress, port); }
listenPort = connector.getLocalPort();