Adding configurable ports (per PR review)
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
parent
8dfa4c941a
commit
b2dc5830ed
|
@ -50,7 +50,8 @@ public class ExampleServer
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
server.start();
|
||||
server.join();
|
||||
}
|
||||
|
|
|
@ -40,7 +40,8 @@ public class ExampleServerXml
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
server.start();
|
||||
server.join();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.embedded;
|
||||
|
||||
import org.eclipse.jetty.util.StringUtil;
|
||||
|
||||
public class ExampleUtil
|
||||
{
|
||||
/**
|
||||
* Get a port, possibly configured from Command line or System property.
|
||||
*
|
||||
* @param args the command line arguments
|
||||
* @param propertyName the property name
|
||||
* @param defValue the default value
|
||||
* @return the configured port
|
||||
*/
|
||||
public static int getPort(String[] args, String propertyName, int defValue)
|
||||
{
|
||||
for (String arg : args)
|
||||
{
|
||||
if (arg.startsWith(propertyName + "="))
|
||||
{
|
||||
String value = arg.substring(propertyName.length() + 2);
|
||||
int port = toInt(value);
|
||||
if (isValidPort(port))
|
||||
return port;
|
||||
}
|
||||
}
|
||||
|
||||
String value = System.getProperty(propertyName);
|
||||
int port = toInt(value);
|
||||
if (isValidPort(port))
|
||||
return port;
|
||||
|
||||
return defValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if port is in the valid range to be used.
|
||||
*
|
||||
* @param port the port to test
|
||||
* @return true if valid
|
||||
*/
|
||||
private static boolean isValidPort(int port)
|
||||
{
|
||||
return (port >= 0) && (port <= 65535);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse an int, ignoring any {@link NumberFormatException}
|
||||
*
|
||||
* @param value the string value to parse
|
||||
* @return the int (if parsed), or -1 if not parsed.
|
||||
*/
|
||||
private static int toInt(String value)
|
||||
{
|
||||
if (StringUtil.isBlank(value))
|
||||
return -1;
|
||||
|
||||
try
|
||||
{
|
||||
return Integer.parseInt(value);
|
||||
}
|
||||
catch (NumberFormatException ignored)
|
||||
{
|
||||
// ignored
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -73,8 +73,9 @@ public class FastFileServer
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
File directory = new File(System.getProperty("user.dir"));
|
||||
Server server = createServer(8080, directory);
|
||||
Server server = createServer(port, directory);
|
||||
server.start();
|
||||
server.join();
|
||||
}
|
||||
|
|
|
@ -62,10 +62,11 @@ public class FileServer
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Path userDir = Paths.get(System.getProperty("user.dir"));
|
||||
PathResource pathResource = new PathResource(userDir);
|
||||
|
||||
Server server = createServer(8080, pathResource);
|
||||
Server server = createServer(port, pathResource);
|
||||
|
||||
// Start things up! By using the server.join() the server thread will join with the current thread.
|
||||
// See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
|
||||
|
|
|
@ -47,8 +47,9 @@ public class FileServerXml
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Path userDir = Paths.get(System.getProperty("user.dir"));
|
||||
Server server = createServer(8080, userDir);
|
||||
Server server = createServer(port, userDir);
|
||||
server.start();
|
||||
server.join();
|
||||
}
|
||||
|
|
|
@ -51,7 +51,8 @@ public class HelloWorld extends AbstractHandler
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = new Server(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = new Server(port);
|
||||
server.setHandler(new HelloWorld());
|
||||
|
||||
server.start();
|
||||
|
|
|
@ -18,9 +18,12 @@
|
|||
|
||||
package org.eclipse.jetty.embedded;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Date;
|
||||
import java.util.EnumSet;
|
||||
import javax.servlet.DispatcherType;
|
||||
|
@ -54,6 +57,7 @@ import org.eclipse.jetty.servlet.DefaultServlet;
|
|||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
import org.eclipse.jetty.servlets.PushCacheFilter;
|
||||
import org.eclipse.jetty.util.resource.PathResource;
|
||||
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||
|
||||
/**
|
||||
|
@ -63,6 +67,8 @@ public class Http2Server
|
|||
{
|
||||
public static void main(String... args) throws Exception
|
||||
{
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
int securePort = ExampleUtil.getPort(args, "jetty.https.port", 8443);
|
||||
Server server = new Server();
|
||||
|
||||
MBeanContainer mbContainer = new MBeanContainer(
|
||||
|
@ -70,10 +76,11 @@ public class Http2Server
|
|||
server.addBean(mbContainer);
|
||||
|
||||
ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
|
||||
String docroot = "src/main/resources/docroot";
|
||||
if (!new File(docroot).exists())
|
||||
docroot = "examples/embedded/src/main/resources/docroot";
|
||||
context.setResourceBase(docroot);
|
||||
Path docroot = Paths.get("src/main/resources/docroot");
|
||||
if (!Files.exists(docroot))
|
||||
throw new FileNotFoundException(docroot.toString());
|
||||
|
||||
context.setBaseResource(new PathResource(docroot));
|
||||
context.addFilter(PushCacheFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
|
||||
// context.addFilter(PushSessionCacheFilter.class,"/*",EnumSet.of(DispatcherType.REQUEST));
|
||||
context.addFilter(PushedTilesFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
|
||||
|
@ -84,21 +91,21 @@ public class Http2Server
|
|||
// HTTP Configuration
|
||||
HttpConfiguration httpConfig = new HttpConfiguration();
|
||||
httpConfig.setSecureScheme("https");
|
||||
httpConfig.setSecurePort(8443);
|
||||
httpConfig.setSecurePort(securePort);
|
||||
httpConfig.setSendXPoweredBy(true);
|
||||
httpConfig.setSendServerVersion(true);
|
||||
|
||||
// HTTP Connector
|
||||
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfig), new HTTP2CServerConnectionFactory(httpConfig));
|
||||
http.setPort(8080);
|
||||
http.setPort(port);
|
||||
server.addConnector(http);
|
||||
|
||||
// SSL Context Factory for HTTPS and HTTP/2
|
||||
String jettyDistro = System.getProperty("jetty.distro", "../../jetty-distribution/target/distribution");
|
||||
if (!new File(jettyDistro).exists())
|
||||
jettyDistro = "jetty-distribution/target/distribution";
|
||||
Path keystorePath = Paths.get("src/main/resources/etc/keystore").toAbsolutePath();
|
||||
if (!Files.exists(keystorePath))
|
||||
throw new FileNotFoundException(keystorePath.toString());
|
||||
SslContextFactory sslContextFactory = new SslContextFactory.Server();
|
||||
sslContextFactory.setKeyStorePath(jettyDistro + "/demo-base/etc/keystore");
|
||||
sslContextFactory.setKeyStorePath(keystorePath.toString());
|
||||
sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
|
||||
sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
|
||||
sslContextFactory.setCipherComparator(HTTP2Cipher.COMPARATOR);
|
||||
|
@ -120,7 +127,7 @@ public class Http2Server
|
|||
// HTTP/2 Connector
|
||||
ServerConnector http2Connector =
|
||||
new ServerConnector(server, ssl, alpn, h2, new HttpConnectionFactory(httpsConfig));
|
||||
http2Connector.setPort(8443);
|
||||
http2Connector.setPort(securePort);
|
||||
server.addConnector(http2Connector);
|
||||
|
||||
ALPN.debug = false;
|
||||
|
|
|
@ -60,7 +60,8 @@ public class JarServer
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
server.start();
|
||||
server.join();
|
||||
}
|
||||
|
|
|
@ -124,11 +124,16 @@ public class JettyDistribution
|
|||
return null;
|
||||
}
|
||||
|
||||
public static Path resolve(String path)
|
||||
public static Path get()
|
||||
{
|
||||
if (DISTRIBUTION == null)
|
||||
throw new RuntimeException("jetty-distribution not found");
|
||||
return DISTRIBUTION.resolve(path);
|
||||
return DISTRIBUTION;
|
||||
}
|
||||
|
||||
public static Path resolve(String path)
|
||||
{
|
||||
return get().resolve(path);
|
||||
}
|
||||
|
||||
public static void main(String... arg)
|
||||
|
|
|
@ -64,10 +64,10 @@ public class LikeJettyXml
|
|||
public static Server createServer(int port, int securePort, boolean addDebugListener) throws Exception
|
||||
{
|
||||
// Path to as-built jetty-distribution directory
|
||||
String jettyHomeBuild = JettyDistribution.DISTRIBUTION.toString();
|
||||
Path jettyHomeBuild = JettyDistribution.get();
|
||||
|
||||
// Find jetty home and base directories
|
||||
String homePath = System.getProperty("jetty.home", jettyHomeBuild);
|
||||
String homePath = System.getProperty("jetty.home", jettyHomeBuild.toString());
|
||||
Path homeDir = Paths.get(homePath);
|
||||
|
||||
String basePath = System.getProperty("jetty.base", homeDir.resolve("demo-base").toString());
|
||||
|
@ -218,7 +218,9 @@ public class LikeJettyXml
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080, 8443, true);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
int securePort = ExampleUtil.getPort(args, "jetty.https.port", 8443);
|
||||
Server server = createServer(port, securePort, true);
|
||||
|
||||
// Extra options
|
||||
server.setDumpAfterStart(true);
|
||||
|
|
|
@ -129,7 +129,9 @@ public class ManyConnectors
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080, 8443);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
int securePort = ExampleUtil.getPort(args, "jetty.https.port", 8443);
|
||||
Server server = createServer(port, securePort);
|
||||
// Start the server
|
||||
server.start();
|
||||
server.dumpStdErr();
|
||||
|
|
|
@ -52,7 +52,8 @@ public class ManyContexts
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
server.start();
|
||||
server.dumpStdErr();
|
||||
server.join();
|
||||
|
|
|
@ -164,7 +164,8 @@ public class ManyHandlers
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
server.start();
|
||||
server.join();
|
||||
}
|
||||
|
|
|
@ -62,7 +62,8 @@ public class ManyServletContexts
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
server.start();
|
||||
server.dumpStdErr();
|
||||
server.join();
|
||||
|
|
|
@ -56,7 +56,8 @@ public class MinimalServlets
|
|||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
// Create a basic jetty server object that will listen on port 8080.
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
// Start things up!
|
||||
server.start();
|
||||
|
|
|
@ -47,7 +47,8 @@ public class OneConnector
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
// Start the server
|
||||
server.start();
|
||||
|
|
|
@ -40,7 +40,8 @@ public class OneContext
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
// Start the server
|
||||
server.start();
|
||||
|
|
|
@ -31,7 +31,8 @@ public class OneHandler
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
server.start();
|
||||
server.join();
|
||||
}
|
||||
|
|
|
@ -80,9 +80,10 @@ public class OneServletContext
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
|
||||
|
||||
Server server = createServer(8080, new PathResource(tempDir));
|
||||
Server server = createServer(port, new PathResource(tempDir));
|
||||
|
||||
server.start();
|
||||
server.dumpStdErr();
|
||||
|
|
|
@ -51,7 +51,8 @@ public class OneServletContextJmxStats
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
server.start();
|
||||
server.join();
|
||||
|
|
|
@ -65,9 +65,10 @@ public class OneServletContextWithSession
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Path dir = Paths.get(System.getProperty("user.dir"));
|
||||
PathResource baseResource = new PathResource(dir);
|
||||
Server server = createServer(8080, baseResource);
|
||||
Server server = createServer(port, baseResource);
|
||||
|
||||
server.start();
|
||||
server.dumpStdErr();
|
||||
|
|
|
@ -53,7 +53,8 @@ public class OneWebApp
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
// Start things up!
|
||||
server.start();
|
||||
|
|
|
@ -103,7 +103,8 @@ public class OneWebAppWithJsp
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
// Start things up!
|
||||
server.start();
|
||||
|
|
|
@ -52,7 +52,8 @@ public class ProxyServer
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
server.start();
|
||||
server.join();
|
||||
|
|
|
@ -53,7 +53,8 @@ public class RewriteServer
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
server.start();
|
||||
server.join();
|
||||
|
|
|
@ -104,7 +104,8 @@ public class SecuredHelloHandler
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
// Start things up!
|
||||
server.start();
|
||||
|
|
|
@ -90,7 +90,8 @@ public class ServerWithAnnotations
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
server.start();
|
||||
server.dumpStdErr();
|
||||
|
|
|
@ -53,7 +53,8 @@ public class ServerWithJMX
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
server.start();
|
||||
server.dumpStdErr();
|
||||
|
|
|
@ -115,7 +115,8 @@ public class ServerWithJNDI
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
server.start();
|
||||
server.join();
|
||||
|
|
|
@ -36,7 +36,8 @@ public class SimplestServer
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
server.start();
|
||||
server.join();
|
||||
}
|
||||
|
|
|
@ -83,10 +83,11 @@ public class SplitFileServer
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Resource resource0 = new PathResource(Paths.get("src/test/resources/dir0"));
|
||||
Resource resource1 = new PathResource(Paths.get("src/test/resources/dir1"));
|
||||
|
||||
Server server = createServer(8080, resource0, resource1);
|
||||
Server server = createServer(port, resource0, resource1);
|
||||
|
||||
// Dump the server state
|
||||
server.setDumpAfterStart(true);
|
||||
|
|
|
@ -76,7 +76,8 @@ public class WebSocketJsrServer
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
server.start();
|
||||
server.join();
|
||||
|
|
|
@ -75,7 +75,8 @@ public class WebSocketServer
|
|||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = createServer(8080);
|
||||
int port = ExampleUtil.getPort(args, "jetty.http.port", 8080);
|
||||
Server server = createServer(port);
|
||||
|
||||
server.start();
|
||||
server.join();
|
||||
|
|
Loading…
Reference in New Issue