Skip to content

Commit b03a3a2

Browse files
authored
PathUtils: Add methods to acquire system path (#28)
1 parent 20267d5 commit b03a3a2

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

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

+74-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
import java.nio.file.Paths;
1313
import java.nio.file.attribute.BasicFileAttributes;
1414
import java.util.ArrayList;
15+
import java.util.Arrays;
1516
import java.util.Collections;
1617
import java.util.EnumSet;
1718
import java.util.List;
19+
import java.util.Map;
1820
import java.util.Objects;
1921

2022
/**
@@ -135,7 +137,7 @@ public static ReportsDirectory fromObject(Object obj) {
135137
public static Path getPathForObject(Object obj) {
136138
String[] subdirs = {};
137139
if (obj instanceof PathModifier) {
138-
String message = String.format("Null path modifier returned by: %s", obj.getClass().getName());
140+
String message = String.format("Null path modifier returned by: %s", obj.getClass().getName());
139141
subdirs = Objects.requireNonNull(((PathModifier) obj).getSubPath(), message);
140142
}
141143
return fromObject(obj).getPath(subdirs);
@@ -247,6 +249,77 @@ public String getNewName() {
247249
return newName;
248250
}
249251
}
252+
253+
/**
254+
* Get the system file path as a path-delimited string.
255+
* <p>
256+
* <b>NOTE</b>: The initial entries in the returned path string are derived from {@link System#getenv()}.
257+
* When running on {@code Mac OS X}, additional entries are acquired from {@code /etc/paths}
258+
* and the files found in the {@code /etc/paths.d} folder.
259+
*
260+
* @return system file path as a path-delimited string
261+
*/
262+
public static String getSystemPath() {
263+
List<String> pathList = new ArrayList<>();
264+
addSystemPathList(pathList);
265+
addMacintoshPathList(pathList);
266+
return String.join(File.pathSeparator, pathList);
267+
}
268+
269+
/**
270+
* Append the system path entries to the specified list.
271+
* <p>
272+
* <b>NOTE</b>: Added entries are derived from {@link System#getenv()}.
273+
*
274+
* @param pathList existing list to receive system path entries
275+
* @return {@code true} if entries were appended; otherwise {@code false}
276+
*/
277+
public static boolean addSystemPathList(List<String> pathList) {
278+
String name = "PATH";
279+
Map<String, String> env = System.getenv();
280+
if (!env.containsKey(name)) {
281+
for (String key : env.keySet()) {
282+
if (name.equalsIgnoreCase(key)) {
283+
name = key;
284+
break;
285+
}
286+
}
287+
}
288+
String path = env.get(name);
289+
return (path != null) ? pathList.addAll(Arrays.asList(path.split(File.pathSeparator))) : false;
290+
}
291+
292+
/**
293+
* Append Macintosh path entries to the specified list.
294+
* <p>
295+
* <b>NOTE</b>: When running on {@code Mac OS X}, added entries are acquired from {@code /etc/paths}
296+
* and the files found in the {@code /etc/paths.d} folder.
297+
*
298+
* @param pathList existing list to receive Macintosh path entries
299+
* @return {@code true} if entries were appended; otherwise {@code false}
300+
*/
301+
public static boolean addMacintoshPathList(List<String> pathList) {
302+
boolean didChange = false;
303+
if (System.getProperty("os.name").startsWith("Mac")) {
304+
List<String> pathFileList = new ArrayList<>();
305+
pathFileList.add("/etc/paths");
306+
String[] paths = new File("/etc/paths.d").list();
307+
if (paths != null) {
308+
pathFileList.addAll(Arrays.asList(paths));
309+
}
310+
for (String thisPathFile : pathFileList) {
311+
File pathFile = new File(thisPathFile);
312+
if (pathFile.exists()) {
313+
try {
314+
didChange |= pathList.addAll(Files.readAllLines(pathFile.toPath()));
315+
} catch (IOException eaten) {
316+
// nothing to do here
317+
}
318+
}
319+
}
320+
}
321+
return didChange;
322+
}
250323

251324
/**
252325
* Prepend the specified string to the indicated array.

0 commit comments

Comments
 (0)