Example of Generating an OVF Package

In summary, the steps in generating an OVF package are the following:
  1. Get the managed object reference to the
    VirtualApp
    object. Call the
    ExportVApp
    method, which returns an
    HttpNfcLease
    data object. Wait for the state of the lease to turn to
    READY
    . Get the list of device URLs from the lease and store them in an array.
  2. For each of the URLs in the list of device URLs, download the images from that URL to the client.
  3. Save the image to the OVF package (directory/folder). Create an
    OvfFile
    object using the
    deviceID
    , absolute path of the downloaded image, and the size of the image on the local disk.
  4. Call the
    OvfManager.CreateDescriptor
    method by passing the managed object reference to the
    VirtualApp
    and the
    OvfFile
    object wrapped in an
    OvfCreateDescriptorParams
    object. This method returns
    OvfCreateDescriptorResult
    , which contains the file descriptor. Write the file descriptor to a file with the file extension
    .ovf
    . Add the
    .ovf
    file to the OVF package.
    The following is an example of how to generate an OVF package. The example assumes a more complex scenario: downloading more than one image from more than one device URL. The example is based on the
    OVFManagerExportVAAP.java
    sample, which is located in the
    SDK/vsphere-ws/java/JAXWS/samples/com/vmware/vapp/
    directory.
You can use the
ExportVM
method instead of the
ExportVapp
method when exporting a
VirtualMachine
.
package com.vmware.vapp; import java.io.*; import java.net.URL; import java.util.*; ... /** 1. Get the MOR of the VirtualApp. ManagedObjectReference vAppMoRef = getVAPPByName(vApp); ... /** Call the ExportVApp method, which returns an HttpNfcLease data object. */ ManagedObjectReference httpNfcLease = vimPort.exportVApp(vAppMoRef); ... /** Wait for the state of the lease to turn to READY. */ Object[] result = waitForValues.wait(httpNfcLease, new String[]{"state"}, new String[]{"state"}, new Object[][]{new Object[]{ HttpNfcLeaseState.READY, HttpNfcLeaseState.ERROR}}); if (result[0].equals(HttpNfcLeaseState.READY)) { ... /** Get the list of device URLs from the lease. */ List<HttpNfcLeaseDeviceUrl> deviceUrlArr = httpNfcLeaseInfo.getDeviceUrl(); if (deviceUrlArr != null) { ... /** 2. For each of the URLs in the list of device URLs, * download the images from that URL to the client. */ for (int i = 0; i < deviceUrlArr.size(); i++) { String deviceId = deviceUrlArr.get(i).getKey(); String deviceUrlStr = deviceUrlArr.get(i).getUrl(); String absoluteFile = deviceUrlStr.substring(deviceUrlStr.lastIndexOf("/") + 1); /** 3. Save the image to the OVF package (directory/folder). Create an OvfFile object using * the deviceID, absolute path of the downloaded image, and the size of the image on the * local disk. */ long writtenSize = writeVMDKFile(absoluteFile, deviceUrlStr.replace("*", host)); OvfFile ovfFile = new OvfFile(); ovfFile.setPath(absoluteFile); ovfFile.setDeviceId(deviceId); ovfFile.setSize(writtenSize); ovfFiles.add(ovfFile); } /** 4. Call the OvfManager.CreateDescriptor method by passing the managed object reference * to the VirtualApp and the OvfFile object wrapped in an OvfCreateDescriptorParams object. * This method returns OvfCreateDescriptorResult, which contains the file descriptor. * Write the file descriptor to a file with the file extension .ovf. Add the .ovf file to * the OVF package. */ ovfCreateDescriptorParams.getOvfFiles().addAll(ovfFiles); OvfCreateDescriptorResult ovfCreateDescriptorResult = vimPort.createDescriptor( serviceContent.getOvfManager(), vAppMoRef, ovfCreateDescriptorParams); String outOVF = localpath + "/" + vApp + ".ovf"; File outFile = new File(outOVF); FileWriter out = new FileWriter(outFile); out.write(ovfCreateDescriptorResult.getOvfDescriptor()); out.close();