jdk/test/java/net/httpclient/Server.java
changeset 42460 7133f144981a
parent 37816 b06d70715a79
equal deleted inserted replaced
42459:1ad58e0cbf16 42460:7133f144981a
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
       
    24 import java.io.Closeable;
    24 import java.io.IOException;
    25 import java.io.IOException;
    25 import java.io.InputStream;
    26 import java.io.InputStream;
    26 import java.io.OutputStream;
    27 import java.io.OutputStream;
    27 import java.net.ServerSocket;
    28 import java.net.ServerSocket;
    28 import java.net.Socket;
    29 import java.net.Socket;
    38 /**
    39 /**
    39  * A cut-down Http/1 Server for testing various error situations
    40  * A cut-down Http/1 Server for testing various error situations
    40  *
    41  *
    41  * use interrupt() to halt
    42  * use interrupt() to halt
    42  */
    43  */
    43 public class Server extends Thread {
    44 public class Server extends Thread implements Closeable {
    44 
    45 
    45     ServerSocket ss;
    46     ServerSocket ss;
    46     private final List<Connection> sockets;
    47     private final List<Connection> sockets;
    47     private final List<Connection> removals;
    48     private final List<Connection> removals;
    48     private final List<Connection> additions;
    49     private final List<Connection> additions;
   249 
   250 
   250     public String getURL() {
   251     public String getURL() {
   251         return "http://127.0.0.1:" + port() + "/foo/";
   252         return "http://127.0.0.1:" + port() + "/foo/";
   252     }
   253     }
   253 
   254 
       
   255     private volatile boolean closed;
       
   256 
       
   257     @Override
   254     public void close() {
   258     public void close() {
       
   259         closed = true;
   255         try {
   260         try {
   256             ss.close();
   261             ss.close();
   257         } catch (IOException e) {
   262         } catch (IOException e) {
   258             e.printStackTrace();
   263             e.printStackTrace();
   259         }
   264         }
   262         }
   267         }
   263     }
   268     }
   264 
   269 
   265     @Override
   270     @Override
   266     public void run() {
   271     public void run() {
   267         while (true) {
   272         while (!closed) {
   268             try {
   273             try {
   269                 Socket s = ss.accept();
   274                 Socket s = ss.accept();
   270                 Connection c = new Connection(s);
   275                 Connection c = new Connection(s);
   271                 c.start();
   276                 c.start();
   272                 additions.add(c);
   277                 additions.add(c);
   273             } catch (IOException e) {
   278             } catch (IOException e) {
       
   279                 if (closed)
       
   280                     return;
   274                 e.printStackTrace();
   281                 e.printStackTrace();
   275             }
   282             }
   276         }
   283         }
   277     }
   284     }
   278 
   285