From 09428380f720e6c7f20b1b04ca905dd449c2e380 Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Sun, 4 Apr 2021 09:38:29 +0200 Subject: [PATCH] kvm: remove unnecessary new String (#4870) Thanks @rubieHess to point it out. see #4800 (comment) --- .../resource/LibvirtComputingResource.java | 6 ++--- .../kvm/storage/LibvirtStorageAdaptor.java | 6 ++--- .../apache/cloudstack/utils/qemu/QemuImg.java | 6 +++++ .../cloudstack/utils/qemu/QemuImgTest.java | 24 +++++++++---------- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java index 275d395399f..7b4872bdb9a 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -4264,8 +4264,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv QemuImg qemu = new QemuImg(timeout); try{ Map info = qemu.info(file); - String backingFilePath = info.get(new String("backing_file")); - String backingFileFormat = info.get(new String("backing_file_format")); + String backingFilePath = info.get(QemuImg.BACKING_FILE); + String backingFileFormat = info.get(QemuImg.BACKING_FILE_FORMAT); if (org.apache.commons.lang.StringUtils.isNotBlank(backingFilePath) && org.apache.commons.lang.StringUtils.isBlank(backingFileFormat)) { // VMs which are created in CloudStack 4.14 and before cannot be started or migrated @@ -4274,7 +4274,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv s_logger.info("Setting backing file format of " + volPath); QemuImgFile backingFile = new QemuImgFile(backingFilePath); Map backingFileinfo = qemu.info(backingFile); - String backingFileFmt = backingFileinfo.get(new String("file_format")); + String backingFileFmt = backingFileinfo.get(QemuImg.FILE_FORMAT); qemu.rebase(file, backingFile, backingFileFmt, false); } } catch (QemuImgException e) { diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java index f9c627b82b4..c9f806d0513 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java @@ -809,7 +809,7 @@ public class LibvirtStorageAdaptor implements StorageAdaptor { try{ qemu.create(destFile, options); Map info = qemu.info(destFile); - virtualSize = Long.parseLong(info.get(new String("virtual_size"))); + virtualSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE)); actualSize = new File(destFile.getFileName()).length(); } catch (QemuImgException e) { s_logger.error("Failed to create " + volPath + @@ -1287,7 +1287,7 @@ public class LibvirtStorageAdaptor implements StorageAdaptor { srcFile = new QemuImgFile(sourcePath, sourceFormat); try { Map info = qemu.info(srcFile); - String backingFile = info.get(new String("backing_file")); + String backingFile = info.get(QemuImg.BACKING_FILE); // qcow2 templates can just be copied into place if (sourceFormat.equals(destFormat) && backingFile == null && sourcePath.endsWith(".qcow2")) { String result = Script.runSimpleBashScript("cp -f " + sourcePath + " " + destPath, timeout); @@ -1299,7 +1299,7 @@ public class LibvirtStorageAdaptor implements StorageAdaptor { try { qemu.convert(srcFile, destFile); Map destInfo = qemu.info(destFile); - Long virtualSize = Long.parseLong(destInfo.get(new String("virtual_size"))); + Long virtualSize = Long.parseLong(destInfo.get(QemuImg.VIRTUAL_SIZE)); newDisk.setVirtualSize(virtualSize); newDisk.setSize(virtualSize); } catch (QemuImgException e) { diff --git a/plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/qemu/QemuImg.java b/plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/qemu/QemuImg.java index 3006d256697..0e5da48ea9d 100644 --- a/plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/qemu/QemuImg.java +++ b/plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/qemu/QemuImg.java @@ -28,6 +28,12 @@ import com.cloud.utils.script.OutputInterpreter; import com.cloud.utils.script.Script; public class QemuImg { + public final static String BACKING_FILE = "backing_file"; + public final static String BACKING_FILE_FORMAT = "backing_file_format"; + public final static String CLUSTER_SIZE = "cluster_size"; + public final static String FILE_FORMAT = "file_format"; + public final static String IMAGE = "image"; + public final static String VIRTUAL_SIZE = "virtual_size"; /* The qemu-img binary. We expect this to be in $PATH */ public String _qemuImgPath = "qemu-img"; diff --git a/plugins/hypervisors/kvm/src/test/java/org/apache/cloudstack/utils/qemu/QemuImgTest.java b/plugins/hypervisors/kvm/src/test/java/org/apache/cloudstack/utils/qemu/QemuImgTest.java index e81890a9832..5f24f931f7f 100644 --- a/plugins/hypervisors/kvm/src/test/java/org/apache/cloudstack/utils/qemu/QemuImgTest.java +++ b/plugins/hypervisors/kvm/src/test/java/org/apache/cloudstack/utils/qemu/QemuImgTest.java @@ -51,10 +51,10 @@ public class QemuImgTest { fail("We didn't get any information back from qemu-img"); } - Long infoSize = Long.parseLong(info.get(new String("virtual_size"))); + Long infoSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE)); assertEquals(Long.valueOf(size), Long.valueOf(infoSize)); - String infoPath = info.get(new String("image")); + String infoPath = info.get(QemuImg.IMAGE); assertEquals(filename, infoPath); File f = new File(filename); @@ -78,13 +78,13 @@ public class QemuImgTest { qemu.create(file, options); Map info = qemu.info(file); - Long infoSize = Long.parseLong(info.get(new String("virtual_size"))); + Long infoSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE)); assertEquals(Long.valueOf(size), Long.valueOf(infoSize)); - String infoPath = info.get(new String("image")); + String infoPath = info.get(QemuImg.IMAGE); assertEquals(filename, infoPath); - String infoClusterSize = info.get(new String("cluster_size")); + String infoClusterSize = info.get(QemuImg.CLUSTER_SIZE); assertEquals(clusterSize, infoClusterSize); File f = new File(filename); @@ -135,7 +135,7 @@ public class QemuImgTest { fail("We didn't get any information back from qemu-img"); } - Long infoSize = Long.parseLong(info.get(new String("virtual_size"))); + Long infoSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE)); assertEquals(Long.valueOf(endSize), Long.valueOf(infoSize)); } catch (QemuImgException e) { fail(e.getMessage()); @@ -164,7 +164,7 @@ public class QemuImgTest { fail("We didn't get any information back from qemu-img"); } - Long infoSize = Long.parseLong(info.get(new String("virtual_size"))); + Long infoSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE)); assertEquals(Long.valueOf(startSize + increment), Long.valueOf(infoSize)); } catch (QemuImgException e) { fail(e.getMessage()); @@ -192,7 +192,7 @@ public class QemuImgTest { fail("We didn't get any information back from qemu-img"); } - Long infoSize = Long.parseLong(info.get(new String("virtual_size"))); + Long infoSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE)); assertEquals(Long.valueOf(startSize + increment), Long.valueOf(infoSize)); } catch (QemuImgException e) { fail(e.getMessage()); @@ -255,7 +255,7 @@ public class QemuImgTest { fail("We didn't get any information back from qemu-img"); } - String backingFile = info.get(new String("backing_file")); + String backingFile = info.get(QemuImg.BACKING_FILE); if (backingFile == null) { fail("The second file does not have a property backing_file! Create failed?"); } @@ -303,10 +303,10 @@ public class QemuImgTest { Map info = qemu.info(destFile); - PhysicalDiskFormat infoFormat = PhysicalDiskFormat.valueOf(info.get(new String("format")).toUpperCase()); + PhysicalDiskFormat infoFormat = PhysicalDiskFormat.valueOf(info.get(QemuImg.FILE_FORMAT).toUpperCase()); assertEquals(destFormat, infoFormat); - Long infoSize = Long.parseLong(info.get(new String("virtual_size"))); + Long infoSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE)); assertEquals(Long.valueOf(srcSize), Long.valueOf(infoSize)); File sf = new File(srcFileName); @@ -316,4 +316,4 @@ public class QemuImgTest { df.delete(); } -} \ No newline at end of file +}