Skip to content

Commit 8868951

Browse files
authored
Add 'findExecutable...' method; eliminate Guava (#34)
1 parent d69f244 commit 8868951

File tree

8 files changed

+60
-22
lines changed

8 files changed

+60
-22
lines changed

pom.xml

-10
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
<surefire-plugin.version>3.0.0-M7</surefire-plugin.version>
3333
<source-plugin.version>3.2.1</source-plugin.version>
3434
<javadoc-plugin.version>3.4.0</javadoc-plugin.version>
35-
<guava.version>31.1-jre</guava.version>
3635
<gpg-plugin.version>3.0.1</gpg-plugin.version>
3736
<staging-plugin.version>1.6.13</staging-plugin.version>
3837
<release-plugin.version>3.0.0-M6</release-plugin.version>
@@ -70,11 +69,6 @@
7069
<artifactId>derby</artifactId>
7170
<version>${apache-derby.version}</version>
7271
</dependency>
73-
<dependency>
74-
<groupId>com.google.guava</groupId>
75-
<artifactId>guava</artifactId>
76-
<version>${guava.version}</version>
77-
</dependency>
7872
</dependencies>
7973
</dependencyManagement>
8074

@@ -89,10 +83,6 @@
8983
<artifactId>derby</artifactId>
9084
<scope>test</scope>
9185
</dependency>
92-
<dependency>
93-
<groupId>com.google.guava</groupId>
94-
<artifactId>guava</artifactId>
95-
</dependency>
9686
</dependencies>
9787

9888
<build>

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

+46-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ private PathUtils() {
6161

6262
private static final String SUREFIRE_PATH = "surefire-reports";
6363
private static final String FAILSAFE_PATH = "failsafe-reports";
64+
private static final List<String> ENDINGS =
65+
OSInfo.getDefault().getType() == OSInfo.OSType.WINDOWS
66+
? Arrays.asList("", ".cmd", ".exe", ".com", ".bat")
67+
: Collections.singletonList("");
6468

6569
/**
6670
* This enumeration contains methods to help build proxy subclass names and select reports directories.
@@ -251,6 +255,30 @@ public String getNewName() {
251255
}
252256
}
253257

258+
/**
259+
* Search for the specified executable file on the system file path.
260+
* <p>
261+
* <b>NOTE</b>: On Windows, this method automatically checks for files of the specified name/path with
262+
* the standard executable file extensions ({@code .cmd"}, {@code ".exe"}, {@code ".com"},
263+
* and {@code ".bat"}), so these can be omitted for cross-platform compatibility.
264+
*
265+
* @param nameOrPath name/path of executable to find
266+
* @return absolute path of located executable; {@code null} if not found
267+
*/
268+
public static String findExecutableOnSystemPath(final String nameOrPath) {
269+
List<String> paths = getSystemPathList();
270+
paths.add(0, "");
271+
for (String path : paths) {
272+
for (String ending : ENDINGS) {
273+
File file = new File(path, nameOrPath + ending);
274+
if (canExecute(file)) {
275+
return file.getAbsolutePath();
276+
}
277+
}
278+
}
279+
return null;
280+
}
281+
254282
/**
255283
* Get the system file path as a path-delimited string.
256284
* <p>
@@ -261,10 +289,23 @@ public String getNewName() {
261289
* @return system file path as a path-delimited string
262290
*/
263291
public static String getSystemPath() {
292+
return String.join(File.pathSeparator, getSystemPathList());
293+
}
294+
295+
/**
296+
* Get the system file path as a list of path items.
297+
* <p>
298+
* <b>NOTE</b>: The initial items in the returned path list are derived from {@link System#getenv()}.
299+
* When running on {@code Mac OS X}, additional items are acquired from {@code /etc/paths}
300+
* and the files found in the {@code /etc/paths.d} folder.
301+
*
302+
* @return system file path as a path-delimited string
303+
*/
304+
public static List<String> getSystemPathList() {
264305
List<String> pathList = new ArrayList<>();
265306
addSystemPathList(pathList);
266307
addMacintoshPathList(pathList);
267-
return String.join(File.pathSeparator, pathList);
308+
return pathList;
268309
}
269310

270311
/**
@@ -369,6 +410,10 @@ public static String[] append(String suffix, String... strings) {
369410
return temp;
370411
}
371412

413+
private static boolean canExecute(File file) {
414+
return file.exists() && !file.isDirectory() && file.canExecute();
415+
}
416+
372417
/**
373418
* Classes that implement this interface are called to supply additional elements for the path returned by
374419
* {@link ReportsDirectory#getPathForObject(Object)}. This enables the implementing class to partition artifacts

src/main/java/com/nordstrom/common/jar/JarUtils.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.jar.JarInputStream;
1515
import java.util.jar.Manifest;
1616

17-
import com.google.common.base.Joiner;
1817
import com.nordstrom.common.base.UncheckedThrow;
1918

2019
/**
@@ -63,7 +62,7 @@ public static String getClasspath(final String[] dependencyContexts) {
6362
return classPath;
6463
} else {
6564
// classpath plus tab-delimited list of agent paths
66-
return classPath + "\n" + Joiner.on("\t").join(contextPaths);
65+
return classPath + "\n" + String.join("\t", contextPaths);
6766
}
6867
}
6968

@@ -111,7 +110,7 @@ private static List<String> getContextPaths(final boolean prefixAgents, final St
111110
}
112111
}
113112
// add assembled classpath string
114-
contextPaths.add(Joiner.on(File.pathSeparator).join(pathList));
113+
contextPaths.add(String.join(File.pathSeparator, pathList));
115114
// add Java agent paths
116115
contextPaths.addAll(agentList);
117116
return contextPaths;

src/main/java/com/nordstrom/common/params/Params.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import java.util.Collections;
44
import java.util.HashMap;
55
import java.util.Map;
6-
7-
import com.google.common.base.Optional;
6+
import java.util.Optional;
87

98
/**
109
* This interface enables implementers to provide methods to support for concisely-defined parameters.
@@ -66,7 +65,7 @@ public Object getVal() {
6665
*/
6766
public static Optional<Map<String, Object>> mapOf(Param... params) {
6867
if ((params == null) || (params.length == 0)) {
69-
return Optional.absent();
68+
return Optional.empty();
7069
}
7170
Map<String, Object> paramMap = new HashMap<>();
7271
for (Param param : params) {

src/test/java/com/nordstrom/common/file/PathUtilsTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.testng.Assert.assertEquals;
44
import static org.testng.Assert.assertFalse;
5+
import static org.testng.Assert.assertNotNull;
56

67
import java.io.File;
78
import java.io.IOException;
@@ -138,6 +139,12 @@ public void testNullExtenstion() throws IOException {
138139
public void testEmptyExtension() throws IOException {
139140
PathUtils.getNextPath(getOutputPath(), "test", "");
140141
}
142+
143+
@Test
144+
public void testFindExecutableOnSystemPath() {
145+
String path = PathUtils.findExecutableOnSystemPath("java");
146+
assertNotNull(path);
147+
}
141148

142149
@Test
143150
public void testGetSystemPath() {

src/test/java/com/nordstrom/common/file/VolumeInfoTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import org.testng.annotations.Test;
77

8-
import com.google.common.base.Joiner;
98
import com.nordstrom.common.file.VolumeInfo.VolumeProps;
109

1110
public class VolumeInfoTest {
@@ -16,7 +15,7 @@ public void test() throws IOException {
1615
for (VolumeProps thisProps : propsList.values()) {
1716
System.out.println("file: " + thisProps.getFile());
1817
System.out.println("type: " + thisProps.getType());
19-
System.out.println("opts: " + Joiner.on(",").join(thisProps.getOpts()));
18+
System.out.println("opts: " + String.join(",", thisProps.getOpts()));
2019
System.out.println("size: " + thisProps.getSize());
2120
System.out.println("free: " + thisProps.getFree());
2221
System.out.println("");

src/test/java/com/nordstrom/common/jar/JarUtilsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class JarUtilsTest {
1010

1111
private static final String[] CONTEXTS = { "org.testng.annotations.Test", "com.beust.jcommander.JCommander",
12-
"org.apache.derby.jdbc.EmbeddedDriver", "com.google.common.base.Charsets" };
12+
"org.apache.derby.jdbc.EmbeddedDriver", "org.slf4j.Logger" };
1313

1414
@Test
1515
public void testClasspath() {

src/test/java/com/nordstrom/common/params/ParamTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
import static org.testng.Assert.assertTrue;
66

77
import java.util.Map;
8+
import java.util.Optional;
89

910
import org.testng.annotations.Test;
1011

11-
import com.google.common.base.Optional;
12-
1312
public class ParamTest implements Params {
1413

1514
@Test

0 commit comments

Comments
 (0)