Skip to content

Add 'findExecutable...' method; eliminate Guava #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<surefire-plugin.version>3.0.0-M7</surefire-plugin.version>
<source-plugin.version>3.2.1</source-plugin.version>
<javadoc-plugin.version>3.4.0</javadoc-plugin.version>
<guava.version>31.1-jre</guava.version>
<gpg-plugin.version>3.0.1</gpg-plugin.version>
<staging-plugin.version>1.6.13</staging-plugin.version>
<release-plugin.version>3.0.0-M6</release-plugin.version>
Expand Down Expand Up @@ -70,11 +69,6 @@
<artifactId>derby</artifactId>
<version>${apache-derby.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -89,10 +83,6 @@
<artifactId>derby</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
47 changes: 46 additions & 1 deletion src/main/java/com/nordstrom/common/file/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ private PathUtils() {

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

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

/**
* Search for the specified executable file on the system file path.
* <p>
* <b>NOTE</b>: On Windows, this method automatically checks for files of the specified name/path with
* the standard executable file extensions ({@code .cmd"}, {@code ".exe"}, {@code ".com"},
* and {@code ".bat"}), so these can be omitted for cross-platform compatibility.
*
* @param nameOrPath name/path of executable to find
* @return absolute path of located executable; {@code null} if not found
*/
public static String findExecutableOnSystemPath(final String nameOrPath) {
List<String> paths = getSystemPathList();
paths.add(0, "");
for (String path : paths) {
for (String ending : ENDINGS) {
File file = new File(path, nameOrPath + ending);
if (canExecute(file)) {
return file.getAbsolutePath();
}
}
}
return null;
}

/**
* Get the system file path as a path-delimited string.
* <p>
Expand All @@ -261,10 +289,23 @@ public String getNewName() {
* @return system file path as a path-delimited string
*/
public static String getSystemPath() {
return String.join(File.pathSeparator, getSystemPathList());
}

/**
* Get the system file path as a list of path items.
* <p>
* <b>NOTE</b>: The initial items in the returned path list are derived from {@link System#getenv()}.
* When running on {@code Mac OS X}, additional items are acquired from {@code /etc/paths}
* and the files found in the {@code /etc/paths.d} folder.
*
* @return system file path as a path-delimited string
*/
public static List<String> getSystemPathList() {
List<String> pathList = new ArrayList<>();
addSystemPathList(pathList);
addMacintoshPathList(pathList);
return String.join(File.pathSeparator, pathList);
return pathList;
}

/**
Expand Down Expand Up @@ -369,6 +410,10 @@ public static String[] append(String suffix, String... strings) {
return temp;
}

private static boolean canExecute(File file) {
return file.exists() && !file.isDirectory() && file.canExecute();
}

/**
* Classes that implement this interface are called to supply additional elements for the path returned by
* {@link ReportsDirectory#getPathForObject(Object)}. This enables the implementing class to partition artifacts
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/nordstrom/common/jar/JarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;

import com.google.common.base.Joiner;
import com.nordstrom.common.base.UncheckedThrow;

/**
Expand Down Expand Up @@ -63,7 +62,7 @@ public static String getClasspath(final String[] dependencyContexts) {
return classPath;
} else {
// classpath plus tab-delimited list of agent paths
return classPath + "\n" + Joiner.on("\t").join(contextPaths);
return classPath + "\n" + String.join("\t", contextPaths);
}
}

Expand Down Expand Up @@ -111,7 +110,7 @@ private static List<String> getContextPaths(final boolean prefixAgents, final St
}
}
// add assembled classpath string
contextPaths.add(Joiner.on(File.pathSeparator).join(pathList));
contextPaths.add(String.join(File.pathSeparator, pathList));
// add Java agent paths
contextPaths.addAll(agentList);
return contextPaths;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/nordstrom/common/params/Params.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.google.common.base.Optional;
import java.util.Optional;

/**
* This interface enables implementers to provide methods to support for concisely-defined parameters.
Expand Down Expand Up @@ -66,7 +65,7 @@ public Object getVal() {
*/
public static Optional<Map<String, Object>> mapOf(Param... params) {
if ((params == null) || (params.length == 0)) {
return Optional.absent();
return Optional.empty();
}
Map<String, Object> paramMap = new HashMap<>();
for (Param param : params) {
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/nordstrom/common/file/PathUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -138,6 +139,12 @@ public void testNullExtenstion() throws IOException {
public void testEmptyExtension() throws IOException {
PathUtils.getNextPath(getOutputPath(), "test", "");
}

@Test
public void testFindExecutableOnSystemPath() {
String path = PathUtils.findExecutableOnSystemPath("java");
assertNotNull(path);
}

@Test
public void testGetSystemPath() {
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/nordstrom/common/file/VolumeInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import org.testng.annotations.Test;

import com.google.common.base.Joiner;
import com.nordstrom.common.file.VolumeInfo.VolumeProps;

public class VolumeInfoTest {
Expand All @@ -16,7 +15,7 @@ public void test() throws IOException {
for (VolumeProps thisProps : propsList.values()) {
System.out.println("file: " + thisProps.getFile());
System.out.println("type: " + thisProps.getType());
System.out.println("opts: " + Joiner.on(",").join(thisProps.getOpts()));
System.out.println("opts: " + String.join(",", thisProps.getOpts()));
System.out.println("size: " + thisProps.getSize());
System.out.println("free: " + thisProps.getFree());
System.out.println("");
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/nordstrom/common/jar/JarUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class JarUtilsTest {

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

@Test
public void testClasspath() {
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/nordstrom/common/params/ParamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import static org.testng.Assert.assertTrue;

import java.util.Map;
import java.util.Optional;

import org.testng.annotations.Test;

import com.google.common.base.Optional;

public class ParamTest implements Params {

@Test
Expand Down