Skip to content

Commit 0942838

Browse files
authored
kvm: remove unnecessary new String (#4870)
Thanks @rubieHess to point it out. see #4800 (comment)
1 parent 2cdde87 commit 0942838

4 files changed

Lines changed: 24 additions & 18 deletions

File tree

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4264,8 +4264,8 @@ public void setBackingFileFormat(String volPath) {
42644264
QemuImg qemu = new QemuImg(timeout);
42654265
try{
42664266
Map<String, String> info = qemu.info(file);
4267-
String backingFilePath = info.get(new String("backing_file"));
4268-
String backingFileFormat = info.get(new String("backing_file_format"));
4267+
String backingFilePath = info.get(QemuImg.BACKING_FILE);
4268+
String backingFileFormat = info.get(QemuImg.BACKING_FILE_FORMAT);
42694269
if (org.apache.commons.lang.StringUtils.isNotBlank(backingFilePath)
42704270
&& org.apache.commons.lang.StringUtils.isBlank(backingFileFormat)) {
42714271
// VMs which are created in CloudStack 4.14 and before cannot be started or migrated
@@ -4274,7 +4274,7 @@ public void setBackingFileFormat(String volPath) {
42744274
s_logger.info("Setting backing file format of " + volPath);
42754275
QemuImgFile backingFile = new QemuImgFile(backingFilePath);
42764276
Map<String, String> backingFileinfo = qemu.info(backingFile);
4277-
String backingFileFmt = backingFileinfo.get(new String("file_format"));
4277+
String backingFileFmt = backingFileinfo.get(QemuImg.FILE_FORMAT);
42784278
qemu.rebase(file, backingFile, backingFileFmt, false);
42794279
}
42804280
} catch (QemuImgException e) {

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ private KVMPhysicalDisk createPhysicalDiskByQemuImg(String name, KVMStoragePool
809809
try{
810810
qemu.create(destFile, options);
811811
Map<String, String> info = qemu.info(destFile);
812-
virtualSize = Long.parseLong(info.get(new String("virtual_size")));
812+
virtualSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE));
813813
actualSize = new File(destFile.getFileName()).length();
814814
} catch (QemuImgException e) {
815815
s_logger.error("Failed to create " + volPath +
@@ -1287,7 +1287,7 @@ public KVMPhysicalDisk copyPhysicalDisk(KVMPhysicalDisk disk, String name, KVMSt
12871287
srcFile = new QemuImgFile(sourcePath, sourceFormat);
12881288
try {
12891289
Map<String, String> info = qemu.info(srcFile);
1290-
String backingFile = info.get(new String("backing_file"));
1290+
String backingFile = info.get(QemuImg.BACKING_FILE);
12911291
// qcow2 templates can just be copied into place
12921292
if (sourceFormat.equals(destFormat) && backingFile == null && sourcePath.endsWith(".qcow2")) {
12931293
String result = Script.runSimpleBashScript("cp -f " + sourcePath + " " + destPath, timeout);
@@ -1299,7 +1299,7 @@ public KVMPhysicalDisk copyPhysicalDisk(KVMPhysicalDisk disk, String name, KVMSt
12991299
try {
13001300
qemu.convert(srcFile, destFile);
13011301
Map<String, String> destInfo = qemu.info(destFile);
1302-
Long virtualSize = Long.parseLong(destInfo.get(new String("virtual_size")));
1302+
Long virtualSize = Long.parseLong(destInfo.get(QemuImg.VIRTUAL_SIZE));
13031303
newDisk.setVirtualSize(virtualSize);
13041304
newDisk.setSize(virtualSize);
13051305
} catch (QemuImgException e) {

plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/qemu/QemuImg.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
import com.cloud.utils.script.Script;
2929

3030
public class QemuImg {
31+
public final static String BACKING_FILE = "backing_file";
32+
public final static String BACKING_FILE_FORMAT = "backing_file_format";
33+
public final static String CLUSTER_SIZE = "cluster_size";
34+
public final static String FILE_FORMAT = "file_format";
35+
public final static String IMAGE = "image";
36+
public final static String VIRTUAL_SIZE = "virtual_size";
3137

3238
/* The qemu-img binary. We expect this to be in $PATH */
3339
public String _qemuImgPath = "qemu-img";

plugins/hypervisors/kvm/src/test/java/org/apache/cloudstack/utils/qemu/QemuImgTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public void testCreateAndInfo() throws QemuImgException {
5151
fail("We didn't get any information back from qemu-img");
5252
}
5353

54-
Long infoSize = Long.parseLong(info.get(new String("virtual_size")));
54+
Long infoSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE));
5555
assertEquals(Long.valueOf(size), Long.valueOf(infoSize));
5656

57-
String infoPath = info.get(new String("image"));
57+
String infoPath = info.get(QemuImg.IMAGE);
5858
assertEquals(filename, infoPath);
5959

6060
File f = new File(filename);
@@ -78,13 +78,13 @@ public void testCreateAndInfoWithOptions() throws QemuImgException {
7878
qemu.create(file, options);
7979
Map<String, String> info = qemu.info(file);
8080

81-
Long infoSize = Long.parseLong(info.get(new String("virtual_size")));
81+
Long infoSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE));
8282
assertEquals(Long.valueOf(size), Long.valueOf(infoSize));
8383

84-
String infoPath = info.get(new String("image"));
84+
String infoPath = info.get(QemuImg.IMAGE);
8585
assertEquals(filename, infoPath);
8686

87-
String infoClusterSize = info.get(new String("cluster_size"));
87+
String infoClusterSize = info.get(QemuImg.CLUSTER_SIZE);
8888
assertEquals(clusterSize, infoClusterSize);
8989

9090
File f = new File(filename);
@@ -135,7 +135,7 @@ public void testCreateAndResize() throws QemuImgException {
135135
fail("We didn't get any information back from qemu-img");
136136
}
137137

138-
Long infoSize = Long.parseLong(info.get(new String("virtual_size")));
138+
Long infoSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE));
139139
assertEquals(Long.valueOf(endSize), Long.valueOf(infoSize));
140140
} catch (QemuImgException e) {
141141
fail(e.getMessage());
@@ -164,7 +164,7 @@ public void testCreateAndResizeDeltaPositive() throws QemuImgException {
164164
fail("We didn't get any information back from qemu-img");
165165
}
166166

167-
Long infoSize = Long.parseLong(info.get(new String("virtual_size")));
167+
Long infoSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE));
168168
assertEquals(Long.valueOf(startSize + increment), Long.valueOf(infoSize));
169169
} catch (QemuImgException e) {
170170
fail(e.getMessage());
@@ -192,7 +192,7 @@ public void testCreateAndResizeDeltaNegative() throws QemuImgException {
192192
fail("We didn't get any information back from qemu-img");
193193
}
194194

195-
Long infoSize = Long.parseLong(info.get(new String("virtual_size")));
195+
Long infoSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE));
196196
assertEquals(Long.valueOf(startSize + increment), Long.valueOf(infoSize));
197197
} catch (QemuImgException e) {
198198
fail(e.getMessage());
@@ -255,7 +255,7 @@ public void testCreateWithBackingFile() throws QemuImgException {
255255
fail("We didn't get any information back from qemu-img");
256256
}
257257

258-
String backingFile = info.get(new String("backing_file"));
258+
String backingFile = info.get(QemuImg.BACKING_FILE);
259259
if (backingFile == null) {
260260
fail("The second file does not have a property backing_file! Create failed?");
261261
}
@@ -303,10 +303,10 @@ public void testConvertAdvanced() throws QemuImgException {
303303

304304
Map<String, String> info = qemu.info(destFile);
305305

306-
PhysicalDiskFormat infoFormat = PhysicalDiskFormat.valueOf(info.get(new String("format")).toUpperCase());
306+
PhysicalDiskFormat infoFormat = PhysicalDiskFormat.valueOf(info.get(QemuImg.FILE_FORMAT).toUpperCase());
307307
assertEquals(destFormat, infoFormat);
308308

309-
Long infoSize = Long.parseLong(info.get(new String("virtual_size")));
309+
Long infoSize = Long.parseLong(info.get(QemuImg.VIRTUAL_SIZE));
310310
assertEquals(Long.valueOf(srcSize), Long.valueOf(infoSize));
311311

312312
File sf = new File(srcFileName);
@@ -316,4 +316,4 @@ public void testConvertAdvanced() throws QemuImgException {
316316
df.delete();
317317

318318
}
319-
}
319+
}

0 commit comments

Comments
 (0)