Ejemplo n.º 1
0
  /**
   * create file object.
   *
   * @param id file id
   * @param location file location, if location = null, generate location
   * @param fileType file type
   * @param format file format
   * @param size file size
   * @return file object
   * @throws Exception
   */
  public VAFile createFile(String id, String location, String fileType, String format, long size)
      throws Exception {
    String guid = VAMUtil.genGuid();
    if (location == null) {
      location = "file." + guid;
    }
    // create a new file object
    VAFile vafile =
        new VAFile(guid, fileType, format, location, id, size, VAMConstants.STATE_UNDEFINE);

    if (fileType == null) {
      throw new Exception("The file type must not be null");
    }

    if (format == null) {
      throw new Exception("The file format must not be null");
    }

    // check file type and file format
    if (fileType.equals(VAMConstants.VAF_FILE_TYPE_DISC)) {
      if (!format.equals(VAMConstants.VAF_FORMAT_ISO)) {
        throw new Exception("The disc format must be " + VAMConstants.VAF_FORMAT_ISO);
      }
    } else if (fileType.equals(VAMConstants.VAF_FILE_TYPE_DISK)) {
      if (!format.equals(VAMConstants.VAF_FORMAT_RAW)
          && !format.equals(VAMConstants.VAF_FORMAT_QCOW)
          && !format.equals(VAMConstants.VAF_FORMAT_QCOW_2)
          && !format.equals(VAMConstants.VAF_FORMAT_VMDK)) {
        throw new Exception(
            "The disk format must be "
                + VAMConstants.VAF_FORMAT_RAW
                + ", "
                + VAMConstants.VAF_FORMAT_QCOW
                + ", "
                + VAMConstants.VAF_FORMAT_QCOW_2
                + ", "
                + VAMConstants.VAF_FORMAT_VMDK);
      }
    } else if (fileType.equals(VAMConstants.VAF_FILE_TYPE_CONFIG)) {
      if (!format.equals(VAMConstants.VAF_FORMAT_TEXT)) {
        throw new Exception("The config format must be " + VAMConstants.VAF_FORMAT_TEXT);
      }
    } else {
      fileType = VAMConstants.VAF_FILE_TYPE_OTHER;
      format = VAMConstants.VAF_FORMAT_VMDK;
    }
    vafile.setFileType(fileType);
    vafile.setFormat(format);
    vafile.setParent(VAMConstants.NULL);
    vafile.setRef(0);

    return vafile;
  }