Skip to content

Commit 7abd671

Browse files
committed
Finish VolumeInfo class
1 parent 51780dd commit 7abd671

File tree

4 files changed

+140
-13
lines changed

4 files changed

+140
-13
lines changed

src/main/java/com/nordstrom/common/file/OSUtils.java renamed to src/main/java/com/nordstrom/common/file/OSInfo.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @param <T> an operating system mapping enumeration that implements the {@link OSProps} interface
1414
*/
15-
public class OSUtils<T extends Enum<T> & OSUtils.OSProps> {
15+
public class OSInfo<T extends Enum<T> & OSInfo.OSProps> {
1616

1717
private static String osName = System.getProperty("os.name");
1818
private static String version = System.getProperty("os.version");
@@ -25,16 +25,16 @@ public class OSUtils<T extends Enum<T> & OSUtils.OSProps> {
2525
*
2626
* @return OSUtils object that supports the operating systems defined in {@link OSType}
2727
*/
28-
public static OSUtils<OSType> getDefault() {
29-
return new OSUtils<>(OSType.class);
28+
public static OSInfo<OSType> getDefault() {
29+
return new OSInfo<>(OSType.class);
3030
}
3131

3232
/**
3333
* Create an object that supports the mappings defined by the specified enumeration.
3434
*
3535
* @param enumClass operating system mapping enumeration
3636
*/
37-
public OSUtils(Class<T> enumClass) {
37+
public OSInfo(Class<T> enumClass) {
3838
putAll(enumClass);
3939
}
4040

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.nordstrom.common.file;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.regex.Matcher;
10+
import java.util.regex.Pattern;
11+
12+
import com.nordstrom.common.file.OSInfo.OSType;
13+
14+
public class VolumeInfo {
15+
16+
static final boolean IS_WINDOWS = (OSInfo.getDefault().getType() == OSType.WINDOWS);
17+
18+
private VolumeInfo() {
19+
throw new AssertionError("VolumeInfo is a static utility class that cannot be instantiated");
20+
}
21+
22+
public static List<VolumeProps> getVolumeProps() throws IOException {
23+
List<VolumeProps> propsList = new ArrayList<>();
24+
Pattern template = Pattern.compile("(.+) on (.+) type (.+) \\((.+)\\)");
25+
26+
Process mountProcess;
27+
if (IS_WINDOWS) {
28+
String[] cmd = {"sh", "-c", "mount | grep noumount"};
29+
mountProcess = Runtime.getRuntime().exec(cmd);
30+
} else {
31+
mountProcess = Runtime.getRuntime().exec("mount");
32+
}
33+
34+
InputStreamReader isr = new InputStreamReader(mountProcess.getInputStream());
35+
try(BufferedReader mountOutput = new BufferedReader(isr)) {
36+
String line;
37+
while(null != (line = mountOutput.readLine())) {
38+
Matcher matcher = template.matcher(line);
39+
if (matcher.matches()) {
40+
String spec = matcher.group(1);
41+
String file = matcher.group(2);
42+
String type = matcher.group(3);
43+
String[] opts = matcher.group(4).split(",");
44+
propsList.add(new VolumeProps(spec, file, type, opts));
45+
}
46+
}
47+
}
48+
return propsList;
49+
}
50+
51+
public static class VolumeProps {
52+
53+
String spec; // block device or remote file system
54+
String file; // file system mount point
55+
String type; // file system type
56+
String[] opts; // file system mount options
57+
58+
private long size;
59+
private long free;
60+
61+
VolumeProps(String spec, String file, String type, String... opts) {
62+
this.spec = spec;
63+
this.file = file;
64+
this.type = type;
65+
this.opts = opts;
66+
67+
File f;
68+
if (IS_WINDOWS) {
69+
f = new File(spec);
70+
} else {
71+
f = new File(file);
72+
}
73+
74+
this.size = f.getTotalSpace();
75+
this.free = f.getFreeSpace();
76+
}
77+
78+
public String getSpec() {
79+
return spec;
80+
}
81+
82+
public String getFile() {
83+
return file;
84+
}
85+
86+
public String getType() {
87+
return type;
88+
}
89+
90+
public String[] getOpts() {
91+
return opts;
92+
}
93+
94+
public long getSize() {
95+
return size;
96+
}
97+
public long getFree() {
98+
return free;
99+
}
100+
}
101+
}

src/test/java/com/nordstrom/common/file/OSUtilsTest.java renamed to src/test/java/com/nordstrom/common/file/OSInfoTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,50 @@
55

66
import org.testng.annotations.Test;
77

8-
import com.nordstrom.common.file.OSUtils.OSProps;
9-
import com.nordstrom.common.file.OSUtils.OSType;
8+
import com.nordstrom.common.file.OSInfo.OSProps;
9+
import com.nordstrom.common.file.OSInfo.OSType;
1010

11-
public class OSUtilsTest {
11+
public class OSInfoTest {
1212

1313
private static String osName = System.getProperty("os.name").toLowerCase();
1414

1515
@Test
1616
public void testDefaultMapping() {
17-
OSUtils<OSType> osUtils = OSUtils.getDefault();
17+
OSInfo<OSType> osUtils = OSInfo.getDefault();
1818
OSType expected = (osName.startsWith("windows")) ? OSType.WINDOWS : OSType.UNIX;
1919
assertEquals(osUtils.getType(), expected, "Reported OS type doesn't match expected type");
2020
}
2121

2222
@Test
2323
public void testCustomMapping() {
24-
OSUtils<TestEnum> osUtils = new OSUtils<>(TestEnum.class);
24+
OSInfo<TestEnum> osUtils = new OSInfo<>(TestEnum.class);
2525
assertEquals(osUtils.getType(), TestEnum.TEST, "Reported OS type doesn't match expected type");
2626
}
2727

2828
@Test
2929
public void testAddOneMapping() {
30-
OSUtils<OSType> osUtils = OSUtils.getDefault();
30+
OSInfo<OSType> osUtils = OSInfo.getDefault();
3131
osUtils.put(TestEnum.TEST);
3232
assertEquals(osUtils.getType(), TestEnum.TEST, "Reported OS type doesn't match expected type");
3333
}
3434

3535
@Test
3636
public void testAddMappingSet() {
37-
OSUtils<OSType> osUtils = OSUtils.getDefault();
37+
OSInfo<OSType> osUtils = OSInfo.getDefault();
3838
osUtils.putAll(TestEnum.class);
3939
assertEquals(osUtils.getType(), TestEnum.TEST, "Reported OS type doesn't match expected type");
4040
}
4141

4242
@Test
4343
public void testOverrideMapping() {
44-
OSUtils<OSType> osUtils = OSUtils.getDefault();
44+
OSInfo<OSType> osUtils = OSInfo.getDefault();
4545
osUtils.put(OSType.SOLARIS, "(?i)" + osName);
4646
assertEquals(osUtils.getType(), OSType.SOLARIS, "Reported OS type doesn't match expected type");
4747
}
4848

4949
@Test
5050
public void testUnsupportedType() {
51-
OSUtils<OSType> osUtils = OSUtils.getDefault();
51+
OSInfo<OSType> osUtils = OSInfo.getDefault();
5252
osUtils.put(osUtils.getType(), "");
5353
assertNull(osUtils.getType(), "Reported OS type doesn't match expected type");
5454
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.nordstrom.common.file;
2+
3+
import java.io.IOException;
4+
import java.util.List;
5+
6+
import org.testng.annotations.Test;
7+
8+
import com.nordstrom.common.file.VolumeInfo.VolumeProps;
9+
10+
public class VolumeInfoTest {
11+
12+
@Test
13+
public void test() throws IOException {
14+
List<VolumeProps> propsList = VolumeInfo.getVolumeProps();
15+
for (VolumeProps thisProps : propsList) {
16+
System.out.println("spec: " + thisProps.getSpec());
17+
System.out.println("file: " + thisProps.getFile());
18+
System.out.println("type: " + thisProps.getType());
19+
System.out.println("opts: " + String.join(",", thisProps.getOpts()));
20+
System.out.println("size: " + thisProps.getSize());
21+
System.out.println("free: " + thisProps.getFree());
22+
System.out.println("");
23+
}
24+
}
25+
26+
}

0 commit comments

Comments
 (0)