/**
   * Modifies the setting data for existing resources on a virtual computer system
   *
   * @param vmDispatch A reference to the virtual computer system whose resources are to be
   *     modified.
   * @param modifiedResourceAllocationSettingData the resource allocation setting data reference to
   *     be modified.
   * @throws JIException
   */
  public void modifyVirtualSystemResources(
      final IJIDispatch vmDispatch, final IJIDispatch modifiedResourceAllocationSettingData)
      throws JIException {
    // Getting the dispatcher of the VM Path
    IJIDispatch vmPathDispatcher =
        (IJIDispatch)
            JIObjectFactory.narrowObject(
                vmDispatch.get("Path_").getObjectAsComObject().queryInterface(IJIDispatch.IID));

    // Getting the virtual machine path
    String vmPath = vmPathDispatcher.get("Path").getObjectAsString2();

    // Getting the dispatcher of the resource path

    String resourceText =
        modifiedResourceAllocationSettingData
            .callMethodA("GetText_", new Object[] {new Integer(1)})[0].getObjectAsString2();

    JIVariant[] tmp =
        dispatch.callMethodA(
            "ModifyVirtualSystemResources",
            new Object[] {
              new JIString(vmPath),
              new JIArray(new JIString[] {new JIString(resourceText)}),
              JIVariant.EMPTY_BYREF(),
              JIVariant.EMPTY_BYREF()
            });

    int result = tmp[0].getObjectAsInt();

    String name = modifiedResourceAllocationSettingData.get("ElementName").getObjectAsString2();
    if (result == 0) {
      logger.debug(name + " added to " + vmPath);
    } else {
      if (result == 4096) {
        logger.debug("Modifying resources...");
        String jobPath = tmp[1].getObjectAsVariant().getObjectAsString2();
        HyperVUtils.monitorJob(jobPath, service.getObjectDispatcher());
      } else {
        logger.error(name + " modification to " + vmPath + " failed with error code " + result);
        throw new IllegalStateException(
            "Cannot modify resource "
                + name
                + " to "
                + vmDispatch.get("ElementName").getObjectAsString2());
      }
    }
  }
  /* FIXME: This code sometimes fails with an "org.jinterop.dcom.common.JIException: An internal error occurred. [0x8001FFFF]"
   * on Windows 2008 */
  public int getCPUUsage() throws JIException {
    System.gc();

    JIVariant results[] =
        service_dispatch.callMethodA(
            "Get",
            new Object[] {
              new JIString("Win32_PerfRawData_PerfOS_Processor.Name='_Total'"),
              new Integer(0),
              JIVariant.OPTIONAL_PARAM()
            });

    IJIDispatch wbemObject_dispatch =
        (IJIDispatch) JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
    long ppt =
        Long.parseLong(
            wbemObject_dispatch.get("PercentProcessorTime").getObjectAsString().getString());
    long tss =
        Long.parseLong(
            wbemObject_dispatch.get("TimeStamp_Sys100NS").getObjectAsString().getString());

    if (this.percentprocessortime == -1 && this.timestamp == -1) {
      this.percentprocessortime = ppt;
      this.timestamp = tss;
      return -1;
    }

    double load =
        (1 - ((double) (this.percentprocessortime - ppt) / (double) (this.timestamp - tss))) * 100;
    this.percentprocessortime = ppt;
    this.timestamp = tss;

    return (int) Math.round(load);
  }
 /**
  * Destroys a virtual system in hyper-v
  *
  * @param vmDispatch TODO
  * @return the error code of
  */
 public int destroyVirtualSystem(final IJIDispatch vmDispatch) throws Exception {
   JIVariant tmp = vmDispatch.get("Path_");
   IJIDispatch dispatchTemp =
       (IJIDispatch)
           JIObjectFactory.narrowObject(
               tmp.getObjectAsComObject().queryInterface(IJIDispatch.IID));
   String virtualSystemPath = dispatchTemp.get("Path").getObjectAsString2();
   JIVariant[] results =
       dispatch.callMethodA(
           "DestroyVirtualSystem",
           new Object[] {
             new JIString(virtualSystemPath), JIVariant.EMPTY_BYREF(), JIVariant.EMPTY_BYREF()
           });
   int error = results[0].getObjectAsInt();
   if (results.length > 1) {
     if (error != 0) {
       if (error == 4096) {
         logger.debug("Destroying virtual system...");
         String jobPath = results[2].getObjectAsVariant().getObjectAsString2();
         HyperVUtils.monitorJob(jobPath, service.getObjectDispatcher());
       } else {
         String message = "The virtual system could no te destroyed " + virtualSystemPath;
         logger.error(message);
         throw new JIException(error, message);
       }
     }
   }
   return error;
 }
  public long getFreeMemorySpace() throws JIException {
    System.gc();

    JIVariant results[] =
        service_dispatch.callMethodA(
            "ExecQuery",
            new Object[] {
              new JIString("select * from Win32_PerfRawData_PerfOS_Memory"),
              JIVariant.OPTIONAL_PARAM(),
              JIVariant.OPTIONAL_PARAM(),
              JIVariant.OPTIONAL_PARAM()
            });
    IJIDispatch wbemObjectSet_dispatch =
        (IJIDispatch) JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
    JIVariant variant = wbemObjectSet_dispatch.get("_NewEnum");
    IJIComObject object2 = variant.getObjectAsComObject();

    IJIEnumVariant enumVARIANT =
        (IJIEnumVariant) JIObjectFactory.narrowObject(object2.queryInterface(IJIEnumVariant.IID));
    if (wbemObjectSet_dispatch.get("Count").getObjectAsInt() != 1)
      return -1; // there should be 1 hint

    JIArray array = (JIArray) enumVARIANT.next(1)[0];
    JIVariant item = ((JIVariant[]) array.getArrayInstance())[0];
    IJIDispatch wbemObject_dispatch =
        (IJIDispatch) JIObjectFactory.narrowObject(item.getObjectAsComObject());
    return Long.parseLong(
        wbemObject_dispatch.get("AvailableKBytes").getObjectAsString().getString());
  }
  public LinkedList<IJIDispatch> getDiskDrives() throws JIException {
    System.gc();

    // get all local disks
    JIVariant results[] =
        service_dispatch.callMethodA(
            "ExecQuery",
            new Object[] {
              new JIString("select * from Win32_LogicalDisk where DriveType = 3"),
              JIVariant.OPTIONAL_PARAM(),
              JIVariant.OPTIONAL_PARAM(),
              JIVariant.OPTIONAL_PARAM()
            });
    IJIDispatch wbemObjectSet_dispatch =
        (IJIDispatch) JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
    JIVariant variant = wbemObjectSet_dispatch.get("_NewEnum");
    IJIComObject object2 = variant.getObjectAsComObject();

    IJIEnumVariant enumVARIANT =
        (IJIEnumVariant) JIObjectFactory.narrowObject(object2.queryInterface(IJIEnumVariant.IID));

    LinkedList<IJIDispatch> drives = new LinkedList<IJIDispatch>();

    int count = wbemObjectSet_dispatch.get("Count").getObjectAsInt();
    for (int i = 0; i < count; i++) {
      Object[] values = enumVARIANT.next(1);
      JIArray array = (JIArray) values[0];
      JIVariant[] variants = (JIVariant[]) array.getArrayInstance();
      for (JIVariant item : variants) {
        drives.add((IJIDispatch) JIObjectFactory.narrowObject(item.getObjectAsComObject()));
      }
    }
    return drives;
  }
  public WindowsHealth(
      String address, String domain, String user, String passwd, int timeout, boolean verbose)
      throws JIException, UnknownHostException {
    if (verbose) System.out.print("Creating session... ");
    session = JISession.createSession(domain, user, passwd);
    session.useSessionSecurity(true);
    session.setGlobalSocketTimeout(timeout);

    if (verbose) {
      System.out.println("OK");
      System.out.print("Connecting to COMServer... ");
    }

    JIProgId progid = JIProgId.valueOf("WbemScripting.SWbemLocator");
    comStub = new JIComServer(progid, address, session);
    IJIComObject unknown = comStub.createInstance();
    comObject =
        (IJIComObject)
            unknown.queryInterface("76A6415B-CB41-11d1-8B02-00600806D9B6"); // ISWbemLocator

    if (verbose) {
      System.out.println("OK");
      System.out.print("Connecting to targethost... ");
    }

    dispatch =
        (IJIDispatch) JIObjectFactory.narrowObject(comObject.queryInterface(IJIDispatch.IID));
    service =
        dispatch
            .callMethodA(
                "ConnectServer",
                new Object[] {
                  new JIString(address),
                  JIVariant.OPTIONAL_PARAM(),
                  JIVariant.OPTIONAL_PARAM(),
                  JIVariant.OPTIONAL_PARAM(),
                  JIVariant.OPTIONAL_PARAM(),
                  JIVariant.OPTIONAL_PARAM(),
                  new Integer(0),
                  JIVariant.OPTIONAL_PARAM()
                })[0];

    service_dispatch = (IJIDispatch) JIObjectFactory.narrowObject(service.getObjectAsComObject());
    if (verbose) System.out.println("OK");

    percentprocessortime = -1;
    timestamp = -1;
  }
  public LinkedList<IJIDispatch> getServices() throws JIException {
    System.gc();

    // get all services which should start automatically but are not running
    JIVariant results[] =
        service_dispatch.callMethodA(
            "ExecQuery",
            new Object[] {
              new JIString(
                  "select * from Win32_Service where StartMode = 'Auto' and Started = FALSE"),
              JIVariant.OPTIONAL_PARAM(),
              JIVariant.OPTIONAL_PARAM(),
              JIVariant.OPTIONAL_PARAM()
            });

    IJIDispatch wbemObjectSet_dispatch =
        (IJIDispatch) JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
    JIVariant variant = wbemObjectSet_dispatch.get("_NewEnum");
    IJIComObject object2 = variant.getObjectAsComObject();
    IJIEnumVariant enumVARIANT =
        (IJIEnumVariant) JIObjectFactory.narrowObject(object2.queryInterface(IJIEnumVariant.IID));

    LinkedList<IJIDispatch> services = new LinkedList<IJIDispatch>();

    int count = wbemObjectSet_dispatch.get("Count").getObjectAsInt();
    for (int i = 0; i < count; i++) {
      Object[] values = enumVARIANT.next(1);
      JIArray array = (JIArray) values[0];
      JIVariant[] variants = (JIVariant[]) array.getArrayInstance();
      for (JIVariant item : variants) {
        IJIDispatch wbemObject_dispatch =
            (IJIDispatch) JIObjectFactory.narrowObject(item.getObjectAsComObject());
        services.add(wbemObject_dispatch);
      }
    }
    return services;
  }
  /**
   * Add resources to an existing virtual computer system
   *
   * @param vmDispatch A reference to the computer system instance to which the resource is to be
   *     added.
   * @param newResourceAllocationDispatch the new resource allocation setting data reference to be
   *     added.
   * @return the resource allocation setting data path of the added resource
   * @deprecated
   * @throws JIException
   */
  @Deprecated
  public String addVirtualSystemResources2(
      final IJIDispatch vmDispatch, final IJIDispatch newResourceAllocationDispatch)
      throws JIException {
    if (this.addVirtualSystemResources == null) {
      for (final SWbemMethod m : super.getMethods()) {
        if (m.getName().equals("AddVirtualSystemResources")) {
          this.addVirtualSystemResources = m;
        }
      }
    }

    // Getting the dispatcher of the VM Path
    IJIDispatch vmPathDispatcher =
        (IJIDispatch)
            JIObjectFactory.narrowObject(
                vmDispatch.get("Path_").getObjectAsComObject().queryInterface(IJIDispatch.IID));

    // Getting the virtual machine path
    String vmPath = vmPathDispatcher.get("Path").getObjectAsString2();

    // Getting the dispatcher of the resource path

    String resourceText =
        newResourceAllocationDispatch.callMethodA("GetText_", new Object[] {new Integer(1)})[0]
            .getObjectAsString2();

    SWbemObject inParams = this.addVirtualSystemResources.getInParameters();

    inParams.getObjectDispatcher().put("TargetSystem", new JIVariant(new JIString(vmPath)));

    inParams
        .getObjectDispatcher()
        .put(
            "ResourceSettingData",
            new JIVariant(new JIArray(new JIString[] {new JIString(resourceText)})));

    Object[] methodParams =
        new Object[] {
          new JIString("AddVirtualSystemResources"),
          new JIVariant(inParams.getObjectDispatcher()),
          new Integer(0),
          JIVariant.NULL(),
        };

    // Execute the method.
    JIVariant[] results = super.objectDispatcher.callMethodA("ExecMethod_", methodParams);

    // Get the out parameters.
    JIVariant outParamsVar = results[0];
    IJIComObject co = outParamsVar.getObjectAsComObject();
    IJIDispatch outParamsDisp = (IJIDispatch) JIObjectFactory.narrowObject(co);

    // Get the out parameter virtualSystemResources and convert it into an
    // array of JIVariants.
    JIVariant newResourcesVars = outParamsDisp.get("NewResources");
    JIArray newResourcesVarsJIArr = newResourcesVars.getObjectAsArray();
    JIVariant[] newResourcesVarsJIVarArr = (JIVariant[]) newResourcesVarsJIArr.getArrayInstance();

    String newResourceCoString = newResourcesVarsJIVarArr[0].getObjectAsString2();

    return newResourceCoString;
  }
  /**
   * Add resources to an existing virtual computer system
   *
   * @param vmDispatch A reference to the computer system instance to which the resource is to be
   *     added.
   * @param newResourceAllocationDispatch the new resource allocation setting data reference to be
   *     added.
   * @return the resource allocation setting data path of the added resource
   * @throws JIException
   */
  public String addVirtualSystemResources(
      final IJIDispatch vmDispatch, final IJIDispatch newResourceAllocationDispatch)
      throws JIException {
    // Getting the dispatcher of the VM Path
    IJIDispatch vmPathDispatcher =
        (IJIDispatch)
            JIObjectFactory.narrowObject(
                vmDispatch.get("Path_").getObjectAsComObject().queryInterface(IJIDispatch.IID));

    // Getting the virtual machine path
    String vmPath = vmPathDispatcher.get("Path").getObjectAsString2();

    // Getting the dispatcher of the resource path

    String resourceText =
        newResourceAllocationDispatch.callMethodA("GetText_", new Object[] {new Integer(1)})[0]
            .getObjectAsString2();

    JIVariant[] tmp =
        dispatch.callMethodA(
            "AddVirtualSystemResources",
            new Object[] {
              new JIString(vmPath),
              new JIArray(new JIString[] {new JIString(resourceText)}),
              JIVariant.EMPTY_BYREF(),
              JIVariant.EMPTY_BYREF()
            });

    int result = tmp[0].getObjectAsInt();

    JIVariant resultVariant = tmp[2];

    JIVariant variant2 = resultVariant.getObjectAsVariant();

    JIArray newResourcesArr = variant2.getObjectAsArray();

    if (newResourcesArr == null) {
      throw new JIException(32768, "The resource could not be added");
    }

    JIVariant[] newResourcesVarArr = (JIVariant[]) newResourcesArr.getArrayInstance();

    String newResourcePath = newResourcesVarArr[0].getObjectAsString2();

    String name = newResourceAllocationDispatch.get("ElementName").getObjectAsString2();
    if (result == 0) {
      logger.debug(name + " added to " + vmPath);
    } else {
      if (result == 4096) {
        logger.debug("Addind resources...");
        String jobPath = tmp[1].getObjectAsVariant().getObjectAsString2();
        HyperVUtils.monitorJob(jobPath, service.getObjectDispatcher());
      } else {
        logger.error(name + " addition to " + vmPath + " failed with error code " + result);
        throw new IllegalStateException(
            "Cannot add resource "
                + name
                + " to "
                + vmDispatch.get("ElementName").getObjectAsString2());
      }
    }

    return newResourcePath;
  }
예제 #10
0
 @SuppressWarnings("unused")
 public final void terminate() throws JIException {
   dispatch.callMethodA("Terminate"); // i18n lib
 }