@Override public InetSocketAddress getLocal() throws IOException { if (!isBind() || null == httpServer) { throw new IOException("server was not bind yet."); } SelectChannelConnector scc = null; final Connector[] connectorArray = httpServer.getConnectors(); if (null != connectorArray) { for (final Connector connector : connectorArray) { if (connector instanceof SelectChannelConnector) { scc = (SelectChannelConnector) connector; break; }//if }//for }//if if (null == scc) { throw new IllegalStateException("not found SelectChannelConnector"); } return new InetSocketAddress( scc.getHost(), scc.getLocalPort() ); }
/** * Returns the actual port the server is listening on. * * @return The actual port the server's connector is listening on or -1 if * it has not been opened, or -2 if it has been closed. */ public Integer getLocalPort() { return connector.getLocalPort(); }
/** @return the local port number the server is listening on. */ public int getPort() { assertAlreadySetUp(); return ((SelectChannelConnector) connector).getLocalPort(); }
public int start() { final SelectChannelConnector connector = new SelectChannelConnector(); connector.setPort(0); server.addConnector(connector); final HandlerCollection handlers = new HandlerCollection(); // Setup proxy servlet final ServletContextHandler context = new ServletContextHandler(handlers, "/"); context.setAttribute(InstrumentingProxyServlet.INSTRUMENTER, instrumenter); context.addServlet(new ServletHolder(InstrumentingProxyServlet.class), "/*"); // Setup proxy handler to handle CONNECT methods final ConnectHandler connectProxy = new ConnectHandler(); handlers.addHandler(connectProxy); server.setHandler(handlers); try { server.start(); logger.info("Proxy server started on port {}", connector.getLocalPort()); } catch (final Exception e) { throw new RuntimeException(e); } return connector.getLocalPort(); }
/** Create a SelectChannel connector. * This methods adds a select channel connector to the server * @return A URL to access the server via the connector. * @throws Exception */ public String createChannelConnector(boolean localhost) throws Exception { synchronized (this) { SelectChannelConnector connector = new SelectChannelConnector(); if (localhost) connector.setHost("127.0.0.1"); _server.addConnector(connector); if (_server.isStarted()) connector.start(); else connector.open(); return "http://"+(localhost?"127.0.0.1": InetAddress.getLocalHost().getHostAddress() )+":"+connector.getLocalPort(); } }
jetty.start(); Log.info( "Webserver started at {}:{}", connector.getHost(), connector.getLocalPort() );
listeningPort = connector.getLocalPort();