private JarFile getCachedJarFile(URL url) {
    JarFile result = (JarFile) fileCache.get(url);

    /* if the JAR file is cached, the permission will always be there */
    if (result != null) {
      Permission perm = getPermission(result);
      if (perm != null) {
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
          try {
            sm.checkPermission(perm);
          } catch (SecurityException se) {
            // fallback to checkRead/checkConnect for pre 1.2
            // security managers
            if ((perm instanceof java.io.FilePermission)
                && perm.getActions().indexOf("read") != -1) {
              sm.checkRead(perm.getName());
            } else if ((perm instanceof java.net.SocketPermission)
                && perm.getActions().indexOf("connect") != -1) {
              sm.checkConnect(url.getHost(), url.getPort());
            } else {
              throw se;
            }
          }
        }
      }
    }
    return result;
  }
Esempio n. 2
0
  /**
   * This constructor is where the real work takes place. Connect to the specified address and port.
   * Use default local values if not specified, otherwise use the local host and port passed in.
   * Create as stream or datagram based on "stream" argument.
   *
   * <p>
   *
   * @param raddr The remote address to connect to
   * @param rport The remote port to connect to
   * @param laddr The local address to connect to
   * @param lport The local port to connect to
   * @param stream true for a stream socket, false for a datagram socket
   * @exception IOException If an error occurs
   * @exception SecurityException If a security manager exists and its checkConnect method doesn't
   *     allow the operation
   */
  private Socket(InetAddress raddr, int rport, InetAddress laddr, int lport, boolean stream)
      throws IOException {
    this();
    this.inputShutdown = false;
    this.outputShutdown = false;

    if (impl == null) throw new IOException("Cannot initialize Socket implementation");

    SecurityManager sm = System.getSecurityManager();
    if (sm != null) sm.checkConnect(raddr.getHostName(), rport);

    impl.create(stream);

    // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
    // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
    // that default.  JDK 1.2 doc infers not to do a bind.
    if (laddr != null) impl.bind(laddr, lport);

    if (raddr != null) impl.connect(raddr, rport);
  }
Esempio n. 3
0
  /**
   * Returns the local address to which this socket is bound. If this socket is not connected, then
   * <code>null</code> is returned.
   *
   * @return The local address
   * @since 1.1
   */
  public InetAddress getLocalAddress() {
    if (impl == null) return null;

    InetAddress addr = null;
    try {
      addr = (InetAddress) impl.getOption(SocketOptions.SO_BINDADDR);
    } catch (SocketException e) {
      // (hopefully) shouldn't happen
      // throw new java.lang.InternalError
      //      ("Error in PlainSocketImpl.getOption");
      return null;
    }

    // FIXME: According to libgcj, checkConnect() is supposed to be called
    // before performing this operation.  Problems: 1) We don't have the
    // addr until after we do it, so we do a post check.  2). The docs I
    // see don't require this in the Socket case, only DatagramSocket, but
    // we'll assume they mean both.
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) sm.checkConnect(addr.getHostName(), getLocalPort());

    return addr;
  }