/** * add disc to the system. Need to set the id, location, srcLocation, format, discType, OS, * osVersion, APP list. * * @param disc need to set the id, location and format * @param srcLocation the temporary location of the disc * @param discType the disc's type * @param os operation system * @param appl application list * @return file object * @throws Exception */ public VAFile addDisc( VAFile disc, String srcLocation, String discType, String os, String osVersion, List<String> appl) throws Exception { // create new VAFile object VAFile vafile = createFile( disc.getId(), disc.getLocation(), VAMConstants.VAF_FILE_TYPE_DISC, disc.getFormat(), 0); VADisk vadisk = new VADisk(vafile); vadisk.setDiskType(discType); if (discType.equals(VAMConstants.VAD_DISK_TYPE_OS)) { vadisk.setOs(os, osVersion); } else if (discType.equals(VAMConstants.VAD_DISK_TYPE_APP)) { vadisk.setApplications(appl); } // get upload directory path String uploadDir = VAMConfig.getDiscUploadDirLocation(); return addFile(vadisk, uploadDir + srcLocation, false); }
/** * convert disk format. * * @param vafile file object * @param srcPath the source disk path * @param format disk format * @param deleteSrcFile whether delete the source disk after converting * @return a new file object * @throws Exception */ public VAFile convertDiskFormat( VAFile vafile, String srcPath, String format, boolean deleteSrcFile) throws Exception { // check disk checkDiskType(vafile); // check state of the file if (vafile.getState() != VAMConstants.STATE_READY) { throw new Exception("The file's state is not correct"); } // save old information String oldLocation = vafile.getLocation(); String oldFormat = vafile.getFormat(); // String oldParent = vafile.getParent(); int oldState = vafile.getState(); // set new information String newLocation = "file." + VAMUtil.genGuid(); vafile.setSrcPath(srcPath); vafile.setLocation(newLocation); vafile.setFormat(format); // vafile.setParent(VAMConstants.NULL); vafile.setDeletefileAfterOperation(deleteSrcFile); // get file data access object VAFileDao fileDao = DaoFactory.getVAFileDao(); fileDao.update(vafile); try { // convert disk format FileOperation.getInstance() .convertDiskFormat( VAMConfig.getNfsHost(), VAMConfig.getNfsUser(), vafile, srcPath, vafile.getSavePath(), format); } catch (Exception e) { // restore the old information vafile.setLocation(oldLocation); vafile.setFormat(oldFormat); vafile.setState(oldState); // vafile.setParent(oldParent); DaoFactory.getVAFileDao().update(vafile); throw new Exception(e); } return vafile; }
/** * add file. * * @param vafile file object * @param storeLoc the file location * @param deleteSrcFile whether delete the source file after add file * @return file object * @throws Exception */ public VAFile addFile(VAFile vafile, String storeLoc, boolean deleteSrcFile) throws Exception { // check whether the store file is existed String storePath = storeLoc; if (storePath != null) { File storeFile = new File(storePath); if (!storeFile.exists()) { throw new Exception("The file \"" + storePath + "\" is not existed!"); } vafile.setSize(storeFile.length()); } // check file path VAMUtil.checkFile(null, vafile.getLocation()); // get file data access object VAFileDao fileDao = DaoFactory.getVAFileDao(); // add the file to the database vafile = fileDao.add(vafile); if (vafile == null) { throw new Exception("Can't add the file to the database."); } try { // if the file type is disk, convert disk format if (vafile.getFileType().equals(VAMConstants.VAF_FILE_TYPE_DISK)) { vafile.setSrcPath(storeLoc); DiskInfo info = VAMUtil.getDiskInfo(storeLoc); String format = null; String backingFile = null; if (info != null) { format = info.getFormat(); backingFile = info.getBackingFile(); } if (format != null && format.equalsIgnoreCase(VAMConfig.getImageFormat()) && backingFile == null) { VADisk disk = new VADisk(vafile); disk.setCapacity(info.getVirtualSize()); disk.setSize(info.getDiskSize()); FileOperation.getInstance() .moveFile( VAMConfig.getNfsHost(), VAMConfig.getNfsUser(), disk, storeLoc, disk.getSavePath()); } else { convertDiskFormat(vafile, storeLoc, VAMConfig.getImageFormat(), deleteSrcFile); } } else if (storeLoc != null) { // move file FileOperation.getInstance() .moveFile( VAMConfig.getNfsHost(), VAMConfig.getNfsUser(), vafile, storeLoc, vafile.getSavePath()); } } catch (Exception e) { // remove the file in the database DaoFactory.getVAFileDao().remove(vafile.getGuid()); throw new Exception(e); } return vafile; }