Home | History | Annotate | Download | only in okhttp
      1 /*
      2  * Copyright (C) 2014 Square, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.squareup.okhttp;
     17 
     18 import java.io.IOException;
     19 import java.net.InetAddress;
     20 import java.net.ServerSocket;
     21 import javax.net.ServerSocketFactory;
     22 
     23 /**
     24  * A {@link ServerSocketFactory} that delegates calls. Sockets can be configured after creation by
     25  * overriding {@link #configureServerSocket(java.net.ServerSocket)}.
     26  */
     27 public class DelegatingServerSocketFactory extends ServerSocketFactory {
     28 
     29   private final ServerSocketFactory delegate;
     30 
     31   public DelegatingServerSocketFactory(ServerSocketFactory delegate) {
     32     this.delegate = delegate;
     33   }
     34 
     35   @Override
     36   public ServerSocket createServerSocket() throws IOException {
     37     ServerSocket serverSocket = delegate.createServerSocket();
     38     configureServerSocket(serverSocket);
     39     return serverSocket;
     40   }
     41 
     42   @Override
     43   public ServerSocket createServerSocket(int port) throws IOException {
     44     ServerSocket serverSocket = delegate.createServerSocket(port);
     45     configureServerSocket(serverSocket);
     46     return serverSocket;
     47   }
     48 
     49   @Override
     50   public ServerSocket createServerSocket(int port, int backlog) throws IOException {
     51     ServerSocket serverSocket = delegate.createServerSocket(port, backlog);
     52     configureServerSocket(serverSocket);
     53     return serverSocket;
     54   }
     55 
     56   @Override
     57   public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress)
     58       throws IOException {
     59     ServerSocket serverSocket = delegate.createServerSocket(port, backlog, ifAddress);
     60     configureServerSocket(serverSocket);
     61     return serverSocket;
     62   }
     63 
     64   protected void configureServerSocket(ServerSocket serverSocket) throws IOException {
     65     // No-op by default.
     66   }
     67 }
     68