Пример #1
0
  /**
   * Resolve a VmIdentifier with this HostIdentifier. A VmIdentifier, such as <em>1234</em> or
   * <em>1234@hostname</em> or any other string that omits certain components of the URI string may
   * be valid, but is certainly incomplete. They are missing critical information for identifying
   * the the communications protocol, target host, or other parameters. A VmIdentifier of this form
   * is considered <em>unresolved</em>. This method uses components of the HostIdentifier to resolve
   * the missing components of the VmIdentifier.
   *
   * <p>Specified components of the unresolved VmIdentifier take precedence over their
   * HostIdentifier counterparts. For example, if the VmIdentifier indicates
   * <em>1234@hostname:2099</em> and the HostIdentifier indicates <em>rmi://hostname:1099/</em>,
   * then the resolved VmIdentifier will be <em>rmi://1234@hostname:2099</em>. Any component not
   * explicitly specified or assumed by the HostIdentifier, will remain unresolved in resolved
   * VmIdentifier.
   *
   * <p>A VmIdentifier specifying a <em>file:</em> scheme (protocol), is not changed in any way by
   * this method.
   *
   * @param vmid the unresolved VmIdentifier.
   * @return VmIdentifier - the resolved VmIdentifier. If vmid was resolved on entry to this method,
   *     then the returned VmIdentifier will be equal, but not identical, to vmid.
   */
  public VmIdentifier resolve(VmIdentifier vmid) throws URISyntaxException, MonitorException {
    String scheme = vmid.getScheme();
    String host = vmid.getHost();
    String authority = vmid.getAuthority();

    if ((scheme != null) && (scheme.compareTo("file") == 0)) {
      // don't attempt to resolve a file based VmIdentifier.
      return vmid;
    }

    if ((host != null) && (host.compareTo(authority) == 0)) {
      /*
       * this condition occurs when the VmIdentifier specifies only the authority (i.e. an lvmid), and not a host name.
       */
      host = null;
    }

    if (scheme == null) {
      scheme = getScheme();
    }

    StringBuffer sb = new StringBuffer();

    sb.append(scheme).append("://");

    String userInfo = vmid.getUserInfo();
    if (userInfo != null) {
      sb.append(userInfo);
    } else {
      sb.append(vmid.getAuthority());
    }

    if (host == null) {
      host = getHost();
    }
    sb.append("@").append(host);

    int port = vmid.getPort();
    if (port == -1) {
      port = getPort();
    }

    if (port != -1) {
      sb.append(":").append(port);
    }

    String path = vmid.getPath();
    if ((path == null) || (path.length() == 0)) {
      path = getPath();
    }

    if ((path != null) && (path.length() > 0)) {
      sb.append(path);
    }

    String query = vmid.getQuery();
    if (query == null) {
      query = getQuery();
    }
    if (query != null) {
      sb.append("?").append(query);
    }

    String fragment = vmid.getFragment();
    if (fragment == null) {
      fragment = getFragment();
    }
    if (fragment != null) {
      sb.append("#").append(fragment);
    }

    String s = sb.toString();
    return new VmIdentifier(s);
  }
Пример #2
0
  /**
   * Create a HostIdentifier instance from a VmIdentifier.
   *
   * <p>The necessary components of the VmIdentifier are extracted and reassembled into a
   * HostIdentifier. If a "file:" scheme (protocol) is specified, the the returned HostIdentifier
   * will always be equivalent to HostIdentifier("file://localhost").
   *
   * @param vmid the VmIdentifier use to construct the HostIdentifier.
   */
  public HostIdentifier(VmIdentifier vmid) {
    /*
     * Extract all components of the VmIdentifier URI except the user-info part of the authority (the lvmid).
     */
    StringBuilder sb = new StringBuilder();
    String scheme = vmid.getScheme();
    String host = vmid.getHost();
    String authority = vmid.getAuthority();

    // check for 'file:' VmIdentifiers and handled as a special case.
    if ((scheme != null) && (scheme.compareTo("file") == 0)) {
      try {
        uri = new URI("file://localhost");
      } catch (URISyntaxException e) {
      }
      ;
      return;
    }

    if ((host != null) && (host.compareTo(authority) == 0)) {
      /*
       * this condition occurs when the VmIdentifier specifies only the authority (i.e. the lvmid ), and not a host name.
       */
      host = null;
    }

    if (scheme == null) {
      if (host == null) {
        scheme = "local"; // default local scheme
      } else {
        /*
         * rmi is the default remote scheme. if the VmIdentifier specifies some other protocol, this default is overridden.
         */
        scheme = "rmi";
      }
    }

    sb.append(scheme).append("://");

    if (host == null) {
      sb.append("localhost"); // default host name
    } else {
      sb.append(host);
    }

    int port = vmid.getPort();
    if (port != -1) {
      sb.append(":").append(port);
    }

    String path = vmid.getPath();
    if ((path != null) && (path.length() != 0)) {
      sb.append(path);
    }

    String query = vmid.getQuery();
    if (query != null) {
      sb.append("?").append(query);
    }

    String frag = vmid.getFragment();
    if (frag != null) {
      sb.append("#").append(frag);
    }

    try {
      uri = new URI(sb.toString());
    } catch (URISyntaxException e) {
      // shouldn't happen, as we were passed a valid VmIdentifier
      throw new RuntimeException("Internal Error", e);
    }
  }