Skip to content

Commit 9b05863

Browse files
authored
Merge pull request #2 from Nordstrom/pr/add-os-type-check
Pr/add os type check
2 parents 5184186 + 1ecdf62 commit 9b05863

File tree

10 files changed

+616
-86
lines changed

10 files changed

+616
-86
lines changed

.gitignore

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
.settings
2-
target
3-
.classpath
4-
.project
5-
*~
6-
*.tmproj
7-
*.iml
8-
.idea
9-
.scala_dependencies
10-
integration/bundle/
11-
integration/felix-cache/
12-
runner
13-
.DS_Store
14-
test-output
1+
.settings
2+
target
3+
.classpath
4+
.project
5+
*~
6+
*.tmproj
7+
*.iml
8+
.idea
9+
.scala_dependencies
10+
integration/bundle/
11+
integration/felix-cache/
12+
runner
13+
.DS_Store
14+
test-output

pom.xml

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<gpg-plugin.version>1.6</gpg-plugin.version>
3636
<staging-plugin.version>1.6.7</staging-plugin.version>
3737
<release-plugin.version>2.5.3</release-plugin.version>
38+
<apache-derby.version>10.14.1.0</apache-derby.version>
3839
</properties>
3940

4041
<scm>
@@ -62,6 +63,11 @@
6263
<artifactId>testng</artifactId>
6364
<version>${testng.version}</version>
6465
</dependency>
66+
<dependency>
67+
<groupId>org.apache.derby</groupId>
68+
<artifactId>derby</artifactId>
69+
<version>${apache-derby.version}</version>
70+
</dependency>
6571
</dependencies>
6672
</dependencyManagement>
6773

@@ -71,6 +77,11 @@
7177
<artifactId>testng</artifactId>
7278
<scope>test</scope>
7379
</dependency>
80+
<dependency>
81+
<groupId>org.apache.derby</groupId>
82+
<artifactId>derby</artifactId>
83+
<scope>test</scope>
84+
</dependency>
7485
</dependencies>
7586

7687
<build>
@@ -146,23 +157,6 @@
146157
</execution>
147158
</executions>
148159
</plugin>
149-
<plugin>
150-
<groupId>org.apache.maven.plugins</groupId>
151-
<artifactId>maven-gpg-plugin</artifactId>
152-
<executions>
153-
<execution>
154-
<id>sign-artifacts</id>
155-
<phase>verify</phase>
156-
<goals>
157-
<goal>sign</goal>
158-
</goals>
159-
<configuration>
160-
<keyname>${gpg.keyname}</keyname>
161-
<passphraseServerId>${gpg.keyname}</passphraseServerId>
162-
</configuration>
163-
</execution>
164-
</executions>
165-
</plugin>
166160
<plugin>
167161
<groupId>org.sonatype.plugins</groupId>
168162
<artifactId>nexus-staging-maven-plugin</artifactId>
@@ -185,4 +179,39 @@
185179
</plugin>
186180
</plugins>
187181
</build>
182+
183+
<profiles>
184+
<profile>
185+
<id>release-sign-artifacts</id>
186+
<activation>
187+
<property>
188+
<name>performRelease</name>
189+
<value>true</value>
190+
</property>
191+
</activation>
192+
<build>
193+
<pluginManagement>
194+
</pluginManagement>
195+
<plugins>
196+
<plugin>
197+
<groupId>org.apache.maven.plugins</groupId>
198+
<artifactId>maven-gpg-plugin</artifactId>
199+
<executions>
200+
<execution>
201+
<id>sign-artifacts</id>
202+
<phase>verify</phase>
203+
<goals>
204+
<goal>sign</goal>
205+
</goals>
206+
<configuration>
207+
<keyname>${gpg.keyname}</keyname>
208+
<passphraseServerId>${gpg.keyname}</passphraseServerId>
209+
</configuration>
210+
</execution>
211+
</executions>
212+
</plugin>
213+
</plugins>
214+
</build>
215+
</profile>
216+
</profiles>
188217
</project>
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package com.nordstrom.common.file;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.ListIterator;
7+
import java.util.Map;
8+
import java.util.Map.Entry;
9+
10+
/**
11+
* This class provides utility methods and abstractions for host operating system features.
12+
*
13+
* @param <T> an operating system mapping enumeration that implements the {@link OSProps} interface
14+
*/
15+
public class OSInfo<T extends Enum<T> & OSInfo.OSProps> {
16+
17+
private static String osName = System.getProperty("os.name");
18+
private static String version = System.getProperty("os.version");
19+
private static String arch = System.getProperty("os.arch");
20+
21+
private final Map<T, String> typeMap = new LinkedHashMap<>();
22+
23+
/**
24+
* Get an object that supports the set of operating systems defined in the {@link OSType} enumeration.
25+
*
26+
* @return OSUtils object that supports the operating systems defined in {@link OSType}
27+
*/
28+
public static OSInfo<OSType> getDefault() {
29+
return new OSInfo<>(OSType.class);
30+
}
31+
32+
/**
33+
* Create an object that supports the mappings defined by the specified enumeration.
34+
*
35+
* @param enumClass operating system mapping enumeration
36+
*/
37+
public OSInfo(Class<T> enumClass) {
38+
putAll(enumClass);
39+
}
40+
41+
/**
42+
* Get the enumerated type constant for the active operating system.
43+
*
44+
* @return OS type constant; if no match, returns 'null'
45+
*/
46+
public T getType() {
47+
// populate a linked list with the entries of the linked type map
48+
List<Entry<T, String>> entryList = new LinkedList<>(typeMap.entrySet());
49+
// get a list iterator, setting the cursor at the tail end
50+
ListIterator<Entry<T, String>> iterator = entryList.listIterator(entryList.size());
51+
// iterate from last to first
52+
while (iterator.hasPrevious()) {
53+
Entry<T, String> thisEntry = iterator.previous();
54+
if (osName.matches(thisEntry.getValue())) {
55+
return thisEntry.getKey();
56+
}
57+
}
58+
return null;
59+
}
60+
61+
/**
62+
* Add the specified mapping to the collection.<br>
63+
* <b>NOTE</b>: If a mapping for the specified constant already exists, this mapping will be replaced.
64+
*
65+
* @param <U> an operating system mapping enumeration that implements the {@link OSProps} interface
66+
* @param typeConst OS type constant
67+
* @return value of previous mapping; 'null' if no mapping existed
68+
*/
69+
@SuppressWarnings("unchecked")
70+
public <U extends Enum<U> & OSProps> String put(U typeConst) {
71+
return typeMap.put((T) typeConst, typeConst.pattern());
72+
}
73+
74+
/**
75+
* Add the specified mapping to the collection.<br>
76+
* <b>NOTE</b>: If a mapping for the specified constant already exists, this mapping will be replaced.
77+
*
78+
* @param <U> an operating system mapping enumeration that implements the {@link OSProps} interface
79+
* @param typeConst OS type constant
80+
* @param pattern OS name match pattern
81+
* @return value of previous mapping; 'null' if no mapping existed
82+
*/
83+
@SuppressWarnings("unchecked")
84+
public <U extends Enum<U> & OSProps> String put(U typeConst, String pattern) {
85+
return typeMap.put((T) typeConst, pattern);
86+
}
87+
88+
/**
89+
* Add the mappings defined by the specified enumeration to the collection.<br>
90+
* <b>NOTE</b>: If any of the specified mappings already exist, the previous mappings will be replaced.
91+
*
92+
* @param <U> an operating system mapping enumeration that implements the {@link OSProps} interface
93+
* @param enumClass operating system mapping enumeration
94+
*/
95+
public <U extends Enum<U> & OSProps> void putAll(Class<U> enumClass) {
96+
for (U typeConst : enumClass.getEnumConstants()) {
97+
put(typeConst);
98+
}
99+
}
100+
101+
/**
102+
* Get the name of the active operating system.
103+
*
104+
* @return name of the active operating system
105+
*/
106+
public static String osName() {
107+
return osName;
108+
}
109+
110+
/**
111+
* Get the version of the existing operating system.
112+
*
113+
* @return version of the existing operating system
114+
*/
115+
public static String version() {
116+
return version;
117+
}
118+
119+
/**
120+
* Get the architecture of the active operating system.
121+
*
122+
* @return architecture of the active operating system
123+
*/
124+
public static String arch() {
125+
return arch;
126+
}
127+
128+
/**
129+
* This enumeration defines the default set of operating system mappings.
130+
*/
131+
public enum OSType implements OSProps {
132+
WINDOWS("(?i).*win.*"),
133+
MACINTOSH("(?i).*mac.*"),
134+
UNIX("(?i).*(?:nix|nux|aix).*"),
135+
SOLARIS("(?i).*sunos.*");
136+
137+
OSType(String pattern) {
138+
this.pattern = pattern;
139+
}
140+
141+
private String pattern;
142+
143+
@Override
144+
public String pattern() {
145+
return pattern;
146+
}
147+
}
148+
149+
/**
150+
* This interface defines the required contract for operating system mapping enumerations.
151+
*/
152+
public interface OSProps {
153+
154+
/**
155+
* Get the OS name match pattern for this mapping.
156+
*
157+
* @return OS name match pattern
158+
*/
159+
String pattern();
160+
161+
}
162+
163+
}

src/main/java/com/nordstrom/common/file/PathUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public static Path getNextPath(Path targetPath, String baseName, String extensio
171171
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("regex:" + baseName + "(-\\d+)?\\." + extension);
172172

173173
try (Stream<Path> stream = Files.walk(targetPath, 1)) {
174-
int base = baseName.length();
174+
int base = baseName.length();
175175
int ext = extension.length() + 1;
176176

177177
Optional<Integer> optional =
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.HashMap;
8+
import java.util.Map;
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 Map<String, VolumeProps> getVolumeProps() throws IOException {
23+
Map<String, VolumeProps> propsList = new HashMap<>();
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+
VolumeProps props = new VolumeProps(spec, file, type, opts);
45+
if (props.size > 0L) {
46+
propsList.put(spec, props);
47+
}
48+
}
49+
}
50+
}
51+
return propsList;
52+
}
53+
54+
public static class VolumeProps {
55+
56+
String file;
57+
String type;
58+
String[] opts;
59+
60+
private long size;
61+
private long free;
62+
63+
VolumeProps(String spec, String file, String type, String... opts) {
64+
if (IS_WINDOWS) {
65+
this.file = spec;
66+
} else {
67+
this.file = file;
68+
}
69+
70+
this.type = type;
71+
this.opts = opts;
72+
73+
File f = new File(this.file);
74+
this.size = f.getTotalSpace();
75+
this.free = f.getFreeSpace();
76+
}
77+
78+
public String getFile() {
79+
return file;
80+
}
81+
82+
public String getType() {
83+
return type;
84+
}
85+
86+
public String[] getOpts() {
87+
return opts;
88+
}
89+
90+
public long getSize() {
91+
return size;
92+
}
93+
public long getFree() {
94+
return free;
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)