Polish "Return -1 port for non-listening WebServers"

See gh-24606
pull/25129/head
Phillip Webb 4 years ago
parent 5c61df3131
commit 9b9c3edfcd

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -207,18 +207,6 @@ public class JettyWebServer implements WebServer {
return ports.toString(); return ports.toString();
} }
private Integer getLocalPort(Connector connector) {
try {
// Jetty 9 internals are different, but the method name is the same
return (Integer) ReflectionUtils
.invokeMethod(ReflectionUtils.findMethod(connector.getClass(), "getLocalPort"), connector);
}
catch (Exception ex) {
logger.info("could not determine port ( " + ex.getMessage() + ")");
return 0;
}
}
private String getProtocols(Connector connector) { private String getProtocols(Connector connector) {
List<String> protocols = connector.getProtocols(); List<String> protocols = connector.getProtocols();
return " (" + StringUtils.collectionToDelimitedString(protocols, ", ") + ")"; return " (" + StringUtils.collectionToDelimitedString(protocols, ", ") + ")";
@ -275,13 +263,32 @@ public class JettyWebServer implements WebServer {
@Override @Override
public int getPort() { public int getPort() {
Connector[] connectors = this.server.getConnectors(); Connector[] connectors = this.server.getConnectors();
Integer localPort = -1;
for (Connector connector : connectors) { for (Connector connector : connectors) {
// Probably only one... Integer localPort = getLocalPort(connector);
localPort = getLocalPort(connector); if (localPort != null && localPort > 0) {
break; return localPort;
} }
return (localPort > 0) ? localPort : -1; }
return -1;
}
private Integer getLocalPort(Connector connector) {
try {
if (connector instanceof NetworkConnector) {
return ((NetworkConnector) connector).getLocalPort();
}
}
catch (Exception ex) {
}
try {
// Jetty 9 internals are different, but the method name is the same
return (Integer) ReflectionUtils
.invokeMethod(ReflectionUtils.findMethod(connector.getClass(), "getLocalPort"), connector);
}
catch (Exception ex) {
logger.info("could not determine port ( " + ex.getMessage() + ")");
}
return 0;
} }
@Override @Override

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

Loading…
Cancel
Save