Example #1
0
  /**
   * Create a PerfDataBuffer instance for accessing the specified instrumentation buffer.
   *
   * @param vmid the <em>file:</em> URI to the instrumentation buffer file
   * @throws MonitorException
   */
  public PerfDataBuffer(VmIdentifier vmid) throws MonitorException {
    File f = new File(vmid.getURI());
    String mode = vmid.getMode();

    try {
      FileChannel fc = new RandomAccessFile(f, mode).getChannel();
      ByteBuffer bb = null;

      if (mode.compareTo("r") == 0) {
        bb = fc.map(FileChannel.MapMode.READ_ONLY, 0L, (int) fc.size());
      } else if (mode.compareTo("rw") == 0) {
        bb = fc.map(FileChannel.MapMode.READ_WRITE, 0L, (int) fc.size());
      } else {
        throw new IllegalArgumentException("Invalid mode: " + mode);
      }

      fc.close(); // doesn't need to remain open

      createPerfDataBuffer(bb, 0);
    } catch (FileNotFoundException e) {
      throw new MonitorException("Could not find " + vmid.toString());
    } catch (IOException e) {
      throw new MonitorException("Could not read " + vmid.toString());
    }
  }
  /** {@inheritDoc} */
  public MonitoredVm getMonitoredVm(VmIdentifier vmid, int interval) throws MonitorException {
    VmIdentifier nvmid = null;
    try {
      nvmid = hostId.resolve(vmid);
      RemoteVm rvm = remoteHost.attachVm(vmid.getLocalVmId(), vmid.getMode());
      RemoteMonitoredVm rmvm = new RemoteMonitoredVm(rvm, nvmid, timer, interval);
      rmvm.attach();
      return rmvm;

    } catch (RemoteException e) {
      throw new MonitorException("Remote Exception attaching to " + nvmid.toString(), e);
    } catch (URISyntaxException e) {
      /*
       * the VmIdentifier is expected to be a valid and should resolve
       * easonably against the host identifier. A URISyntaxException
       * here is most likely a programming error.
       */
      throw new IllegalArgumentException("Malformed URI: " + vmid.toString(), e);
    }
  }
  /**
   * 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);
  }
  /**
   * 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);
    }
  }