home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / java / net / ServerSocket.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  5.6 KB  |  346 lines

  1. package java.net;
  2.  
  3. import java.io.FileDescriptor;
  4. import java.io.IOException;
  5. import java.nio.channels.ServerSocketChannel;
  6. import java.security.AccessController;
  7. import java.security.PrivilegedActionException;
  8.  
  9. public class ServerSocket {
  10.    private boolean created;
  11.    private boolean bound;
  12.    private boolean closed;
  13.    private Object closeLock;
  14.    private SocketImpl impl;
  15.    private boolean oldImpl;
  16.    private static SocketImplFactory factory = null;
  17.  
  18.    public ServerSocket() throws IOException {
  19.       this.created = false;
  20.       this.bound = false;
  21.       this.closed = false;
  22.       this.closeLock = new Object();
  23.       this.oldImpl = false;
  24.       this.setImpl();
  25.    }
  26.  
  27.    public ServerSocket(int var1) throws IOException {
  28.       this(var1, 50, (InetAddress)null);
  29.    }
  30.  
  31.    public ServerSocket(int var1, int var2) throws IOException {
  32.       this(var1, var2, (InetAddress)null);
  33.    }
  34.  
  35.    public ServerSocket(int var1, int var2, InetAddress var3) throws IOException {
  36.       this.created = false;
  37.       this.bound = false;
  38.       this.closed = false;
  39.       this.closeLock = new Object();
  40.       this.oldImpl = false;
  41.       this.setImpl();
  42.       if (var1 >= 0 && var1 <= 65535) {
  43.          if (var2 < 1) {
  44.             var2 = 50;
  45.          }
  46.  
  47.          try {
  48.             this.bind(new InetSocketAddress(var3, var1), var2);
  49.          } catch (SecurityException var5) {
  50.             this.close();
  51.             throw var5;
  52.          } catch (IOException var6) {
  53.             this.close();
  54.             throw var6;
  55.          }
  56.       } else {
  57.          throw new IllegalArgumentException("Port value out of range: " + var1);
  58.       }
  59.    }
  60.  
  61.    SocketImpl getImpl() throws SocketException {
  62.       if (!this.created) {
  63.          this.createImpl();
  64.       }
  65.  
  66.       return this.impl;
  67.    }
  68.  
  69.    private void checkOldImpl() {
  70.       if (this.impl != null) {
  71.          try {
  72.             AccessController.doPrivileged(new 1(this));
  73.          } catch (PrivilegedActionException var2) {
  74.             this.oldImpl = true;
  75.          }
  76.  
  77.       }
  78.    }
  79.  
  80.    private void setImpl() {
  81.       if (factory != null) {
  82.          this.impl = factory.createSocketImpl();
  83.          this.checkOldImpl();
  84.       } else {
  85.          this.impl = new SocksSocketImpl();
  86.       }
  87.  
  88.       if (this.impl != null) {
  89.          this.impl.setServerSocket(this);
  90.       }
  91.  
  92.    }
  93.  
  94.    void createImpl() throws SocketException {
  95.       if (this.impl == null) {
  96.          this.setImpl();
  97.       }
  98.  
  99.       try {
  100.          this.impl.create(true);
  101.          this.created = true;
  102.       } catch (IOException var2) {
  103.          throw new SocketException(var2.getMessage());
  104.       }
  105.    }
  106.  
  107.    public void bind(SocketAddress var1) throws IOException {
  108.       this.bind(var1, 50);
  109.    }
  110.  
  111.    public void bind(SocketAddress var1, int var2) throws IOException {
  112.       if (this.isClosed()) {
  113.          throw new SocketException("Socket is closed");
  114.       } else if (!this.oldImpl && this.isBound()) {
  115.          throw new SocketException("Already bound");
  116.       } else {
  117.          if (var1 == null) {
  118.             var1 = new InetSocketAddress(0);
  119.          }
  120.  
  121.          if (!(var1 instanceof InetSocketAddress)) {
  122.             throw new IllegalArgumentException("Unsupported address type");
  123.          } else {
  124.             InetSocketAddress var3 = (InetSocketAddress)var1;
  125.             if (var3.isUnresolved()) {
  126.                throw new SocketException("Unresolved address");
  127.             } else {
  128.                if (var2 < 1) {
  129.                   var2 = 50;
  130.                }
  131.  
  132.                try {
  133.                   SecurityManager var4 = System.getSecurityManager();
  134.                   if (var4 != null) {
  135.                      var4.checkListen(var3.getPort());
  136.                   }
  137.  
  138.                   this.getImpl().bind(var3.getAddress(), var3.getPort());
  139.                   this.getImpl().listen(var2);
  140.                   this.bound = true;
  141.                } catch (SecurityException var5) {
  142.                   this.bound = false;
  143.                   throw var5;
  144.                } catch (IOException var6) {
  145.                   this.bound = false;
  146.                   throw var6;
  147.                }
  148.             }
  149.          }
  150.       }
  151.    }
  152.  
  153.    public InetAddress getInetAddress() {
  154.       if (!this.isBound()) {
  155.          return null;
  156.       } else {
  157.          try {
  158.             return this.getImpl().getInetAddress();
  159.          } catch (SocketException var2) {
  160.             return null;
  161.          }
  162.       }
  163.    }
  164.  
  165.    public int getLocalPort() {
  166.       if (!this.isBound()) {
  167.          return -1;
  168.       } else {
  169.          try {
  170.             return this.getImpl().getLocalPort();
  171.          } catch (SocketException var2) {
  172.             return -1;
  173.          }
  174.       }
  175.    }
  176.  
  177.    public SocketAddress getLocalSocketAddress() {
  178.       return !this.isBound() ? null : new InetSocketAddress(this.getInetAddress(), this.getLocalPort());
  179.    }
  180.  
  181.    public Socket accept() throws IOException {
  182.       if (this.isClosed()) {
  183.          throw new SocketException("Socket is closed");
  184.       } else if (!this.isBound()) {
  185.          throw new SocketException("Socket is not bound yet");
  186.       } else {
  187.          Socket var1 = new Socket((SocketImpl)null);
  188.          this.implAccept(var1);
  189.          return var1;
  190.       }
  191.    }
  192.  
  193.    protected final void implAccept(Socket var1) throws IOException {
  194.       SocketImpl var2 = null;
  195.  
  196.       try {
  197.          if (var1.impl == null) {
  198.             var1.setImpl();
  199.          }
  200.  
  201.          var2 = var1.impl;
  202.          var1.impl = null;
  203.          var2.address = new InetAddress();
  204.          var2.fd = new FileDescriptor();
  205.          this.getImpl().accept(var2);
  206.          SecurityManager var3 = System.getSecurityManager();
  207.          if (var3 != null) {
  208.             var3.checkAccept(var2.getInetAddress().getHostAddress(), var2.getPort());
  209.          }
  210.       } catch (IOException var4) {
  211.          if (var2 != null) {
  212.             var2.reset();
  213.          }
  214.  
  215.          var1.impl = var2;
  216.          throw var4;
  217.       } catch (SecurityException var5) {
  218.          if (var2 != null) {
  219.             var2.reset();
  220.          }
  221.  
  222.          var1.impl = var2;
  223.          throw var5;
  224.       }
  225.  
  226.       var1.impl = var2;
  227.       var1.postAccept();
  228.    }
  229.  
  230.    public void close() throws IOException {
  231.       synchronized(this.closeLock) {
  232.          if (!this.isClosed()) {
  233.             if (this.created) {
  234.                this.impl.close();
  235.             }
  236.  
  237.             this.closed = true;
  238.          }
  239.       }
  240.    }
  241.  
  242.    public ServerSocketChannel getChannel() {
  243.       return null;
  244.    }
  245.  
  246.    public boolean isBound() {
  247.       return this.bound || this.oldImpl;
  248.    }
  249.  
  250.    public boolean isClosed() {
  251.       synchronized(this.closeLock) {
  252.          return this.closed;
  253.       }
  254.    }
  255.  
  256.    public synchronized void setSoTimeout(int var1) throws SocketException {
  257.       if (this.isClosed()) {
  258.          throw new SocketException("Socket is closed");
  259.       } else {
  260.          this.getImpl().setOption(4102, new Integer(var1));
  261.       }
  262.    }
  263.  
  264.    public synchronized int getSoTimeout() throws IOException {
  265.       if (this.isClosed()) {
  266.          throw new SocketException("Socket is closed");
  267.       } else {
  268.          Object var1 = this.getImpl().getOption(4102);
  269.          return var1 instanceof Integer ? (Integer)var1 : 0;
  270.       }
  271.    }
  272.  
  273.    public void setReuseAddress(boolean var1) throws SocketException {
  274.       if (this.isClosed()) {
  275.          throw new SocketException("Socket is closed");
  276.       } else {
  277.          this.getImpl().setOption(4, var1);
  278.       }
  279.    }
  280.  
  281.    public boolean getReuseAddress() throws SocketException {
  282.       if (this.isClosed()) {
  283.          throw new SocketException("Socket is closed");
  284.       } else {
  285.          return (Boolean)((Boolean)this.getImpl().getOption(4));
  286.       }
  287.    }
  288.  
  289.    public String toString() {
  290.       return !this.isBound() ? "ServerSocket[unbound]" : "ServerSocket[addr=" + this.impl.getInetAddress() + ",port=" + this.impl.getPort() + ",localport=" + this.impl.getLocalPort() + "]";
  291.    }
  292.  
  293.    void setBound() {
  294.       this.bound = true;
  295.    }
  296.  
  297.    void setCreated() {
  298.       this.created = true;
  299.    }
  300.  
  301.    public static synchronized void setSocketFactory(SocketImplFactory var0) throws IOException {
  302.       if (factory != null) {
  303.          throw new SocketException("factory already defined");
  304.       } else {
  305.          SecurityManager var1 = System.getSecurityManager();
  306.          if (var1 != null) {
  307.             var1.checkSetFactory();
  308.          }
  309.  
  310.          factory = var0;
  311.       }
  312.    }
  313.  
  314.    public synchronized void setReceiveBufferSize(int var1) throws SocketException {
  315.       if (var1 <= 0) {
  316.          throw new IllegalArgumentException("negative receive size");
  317.       } else if (this.isClosed()) {
  318.          throw new SocketException("Socket is closed");
  319.       } else {
  320.          this.getImpl().setOption(4098, new Integer(var1));
  321.       }
  322.    }
  323.  
  324.    public synchronized int getReceiveBufferSize() throws SocketException {
  325.       if (this.isClosed()) {
  326.          throw new SocketException("Socket is closed");
  327.       } else {
  328.          int var1 = 0;
  329.          Object var2 = this.getImpl().getOption(4098);
  330.          if (var2 instanceof Integer) {
  331.             var1 = (Integer)var2;
  332.          }
  333.  
  334.          return var1;
  335.       }
  336.    }
  337.  
  338.    public void setPerformancePreferences(int var1, int var2, int var3) {
  339.    }
  340.  
  341.    // $FF: synthetic method
  342.    static SocketImpl access$000(ServerSocket var0) {
  343.       return var0.impl;
  344.    }
  345. }
  346.