Exemple #1
0
  /**
   * Get the applicable Connection type for the given CVSRoot object. At present this can only be
   * either pserver or ext using internal ssh.
   *
   * @return a Connection object for the cvsroot or null if invalid
   */
  private static Connection getConnection(CVSRoot cvsRoot) {
    SocketFactory socketFactory = SocketFactory.getDefault();

    if (CVSRoot.METHOD_EXT.equals(cvsRoot.getMethod())) {
      // set port to 22 unless it is already set to something other than 0
      int port = 22;

      if (cvsRoot.getPort() != 0) {
        port = cvsRoot.getPort();
      }

      GSSHConnection sshConnection =
          new GSSHConnection(
              socketFactory,
              cvsRoot.getHostName(),
              port,
              cvsRoot.getUserName(),
              cvsRoot.getPassword());
      sshConnection.setRepository(cvsRoot.getRepository());

      return sshConnection;
    } else if (CVSRoot.METHOD_PSERVER.equals(cvsRoot.getMethod())) {
      PServerConnection pConnection = new PServerConnection(cvsRoot, socketFactory);

      // pServerConnection.setRepository(cvsRoot.getRepository());
      return pConnection;
    }

    return null;
  }
  /**
   * Sets up connection to a given CVS root including any proxies on route.
   *
   * @param cvsRoot root to connect to
   * @return Connection object ready to connect to the given CVS root
   * @throws IllegalArgumentException if the 'method' part of the supplied CVS Root is not
   *     recognized
   */
  public static Connection setupConnection(CVSRoot cvsRoot) throws IllegalArgumentException {

    // for testing porposes allow to use dynamically generated port numbers
    String t9yRoot = System.getProperty("netbeans.t9y.cvs.connection.CVSROOT"); // NOI18N
    CVSRoot patchedCvsRoot = cvsRoot;
    if (t9yRoot != null && t9yRoot.length() > 0) {
      int idx = t9yRoot.indexOf(',');
      if (idx != -1) {
        System.setProperty(
            "netbeans.t9y.cvs.connection.CVSROOT", t9yRoot.substring(idx + 1)); // NOI18N
        t9yRoot = t9yRoot.substring(0, idx);
      }
      try {
        patchedCvsRoot = CVSRoot.parse(t9yRoot);
        assert patchedCvsRoot.getRepository().equals(cvsRoot.getRepository());
        assert patchedCvsRoot.getHostName() == cvsRoot.getHostName()
            || patchedCvsRoot.getHostName().equals(cvsRoot.getHostName());
        ErrorManager.getDefault()
            .log(
                ErrorManager.INFORMATIONAL,
                "CVS.ClientRuntime: using patched CVSROOT " + t9yRoot); // NOI18N
      } catch (IllegalArgumentException ex) {
        ErrorManager.getDefault().annotate(ex, "While parsing: " + t9yRoot); // NOI18N
        ErrorManager.getDefault().notify(ex);
      }
    }

    if (cvsRoot.isLocal()) {
      LocalConnection con = new LocalConnection();
      con.setRepository(cvsRoot.getRepository());
      return con;
    }

    ProxySocketFactory factory = ProxySocketFactory.getDefault();

    String method = cvsRoot.getMethod();
    if (CVSRoot.METHOD_PSERVER.equals(method)) {
      PServerConnection con = new PServerConnection(patchedCvsRoot, factory);
      char[] passwordChars =
          KeyringSupport.read(CvsModuleConfig.PREFIX_KEYRING_KEY, cvsRoot.toString());
      String password;
      if (passwordChars != null) {
        password = new String(passwordChars);
      } else {
        password = PasswordsFile.findPassword(cvsRoot.toString());
        if (password != null) {
          KeyringSupport.save(
              CvsModuleConfig.PREFIX_KEYRING_KEY, cvsRoot.toString(), password.toCharArray(), null);
        }
      }
      con.setEncodedPassword(password);
      return con;
    } else if (CVSRoot.METHOD_EXT.equals(method)) {
      CvsModuleConfig.ExtSettings extSettings =
          CvsModuleConfig.getDefault().getExtSettingsFor(cvsRoot);
      String userName = cvsRoot.getUserName();
      String host = cvsRoot.getHostName();
      if (extSettings.extUseInternalSsh) {
        int port = patchedCvsRoot.getPort();
        port = port == 0 ? 22 : port; // default port
        String password = new String(extSettings.extPassword);
        if (password == null) {
          password = "******"; // NOI18N    user will be asked later on
        }
        SSHConnection sshConnection = new SSHConnection(factory, host, port, userName, password);
        sshConnection.setRepository(cvsRoot.getRepository());
        return sshConnection;
      } else {
        // What do we want to achieve here?
        // It's possible to mimics ordinary cvs or cvsnt behaviour:
        // Ordinary cvs style (CVS_RSH):
        //   command += " $hostname [-l$username] $CVS_SERVER"
        // cvsnt style (CVS_EXT and CVS_RSH):
        //   command += " cvs server"
        // I prefer the cvs style, see issue #62683 for details.

        String command = extSettings.extCommand;
        String cvs_server = System.getenv("CVS_SERVER");
        cvs_server = cvs_server != null ? cvs_server + " server" : "cvs server"; // NOI18N
        String userOption = ""; // NOI18N
        if (userName != null) {
          userOption = " -l " + userName; // NOI18N
        }
        command += " " + host + userOption + " " + cvs_server; // NOI18N
        ExtConnection connection = new ExtConnection(command);
        connection.setRepository(cvsRoot.getRepository());
        return connection;
      }
    }

    throw new IllegalArgumentException("Unrecognized CVS Root: " + cvsRoot); // NOI18N
  }