11/*
2- * Copyright (c) 2015, 2024 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2015, 2025 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
4242 * Two threads are created per client connection. So, it's not
4343 * intended for large numbers of parallel connections.
4444 */
45- public class ProxyServer extends Thread implements Closeable {
45+ public final class ProxyServer implements Closeable {
4646
4747 // could use the test library here - Platform.isWindows(),
4848 // but it would force all tests that use ProxyServer to
@@ -99,9 +99,7 @@ public ProxyServer(Integer port,
9999 this (port , debug , null );
100100 }
101101
102- public ProxyServer (Integer port ,
103- Boolean debug ,
104- Credentials credentials )
102+ private ProxyServer (Integer port , Boolean debug , Credentials credentials )
105103 throws IOException
106104 {
107105 this .debug = debug ;
@@ -110,15 +108,11 @@ public ProxyServer(Integer port,
110108 listener .bind (new InetSocketAddress (InetAddress .getLoopbackAddress (), port ));
111109 this .port = ((InetSocketAddress )listener .getLocalAddress ()).getPort ();
112110 this .credentials = credentials ;
113- setName ("ProxyListener" );
114- setDaemon (true );
115- connections = new CopyOnWriteArrayList <Connection >();
116- start ();
117- }
118-
119- public ProxyServer (String s ) {
120- credentials = null ;
121111 connections = new CopyOnWriteArrayList <Connection >();
112+ Thread d = new Thread (() -> run ());
113+ d .setName ("ProxyListener" );
114+ d .setDaemon (true );
115+ d .start ();
122116 }
123117
124118 /**
@@ -150,7 +144,7 @@ public void close() throws IOException {
150144
151145 volatile boolean done ;
152146
153- public void run () {
147+ private void run () {
154148 if (System .getSecurityManager () == null ) {
155149 execute ();
156150 } else {
@@ -672,10 +666,11 @@ public static void main(String[] args) throws Exception {
672666 int port = Integer .parseInt (args [0 ]);
673667 boolean debug = args .length > 1 && args [1 ].equals ("-debug" );
674668 System .out .println ("Debugging : " + debug );
675- ProxyServer ps = new ProxyServer (port , debug );
676- System .out .println ("Proxy server listening on port " + ps .getPort ());
677- while (true ) {
678- Thread .sleep (5000 );
669+ try (ProxyServer ps = new ProxyServer (port , debug )) {
670+ System .out .println ("Proxy server listening on port " + ps .getPort ());
671+ while (true ) {
672+ Thread .sleep (5000 );
673+ }
679674 }
680675 }
681676}
0 commit comments