Skip to content

Commit

Permalink
Merge pull request #1671 from cdapio/CDAP-19461-fix-schema-detection-…
Browse files Browse the repository at this point in the history
…regex-classloading

CDAP-19461 fix classloading for schema detection with regex
  • Loading branch information
albertshau authored Aug 4, 2022
2 parents 2ae29b0 + fa0fe61 commit 341416f
Showing 1 changed file with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,38 +95,41 @@ private InputFiles getInputFiles(String path, @Nullable Pattern pattern,
configuration.set(entry.getKey(), entry.getValue());
}

PathFilter pathFilter = getPathFilter(pattern, configuration);
Path fsPath = new Path(path);

ClassLoader cl = configuration.getClassLoader();
configuration.setClassLoader(getClass().getClassLoader());
FileSystem fs = FileSystem.get(fsPath.toUri(), configuration);
configuration.setClassLoader(cl);
try {
FileSystem fs = FileSystem.get(fsPath.toUri(), configuration);

if (!fs.exists(fsPath) || !pathFilter.accept(fsPath)) {
throw new IOException("Input path not found");
}
PathFilter pathFilter = getPathFilter(pattern, configuration);
if (!fs.exists(fsPath) || !pathFilter.accept(fsPath)) {
throw new IOException("Input path not found");
}

FileStatus fileStatus = fs.getFileStatus(fsPath);
if (fileStatus.isFile()) {
return new FileSystemInputFiles(fs, Collections.singletonList(fileStatus));
}
FileStatus fileStatus = fs.getFileStatus(fsPath);
if (fileStatus.isFile()) {
return new FileSystemInputFiles(fs, Collections.singletonList(fileStatus));
}

FileStatus[] files = fs.listStatus(fsPath);
FileStatus[] files = fs.listStatus(fsPath);

if (files == null) {
throw new IllegalArgumentException("Cannot read files from provided path " + path);
}
if (files == null) {
throw new IllegalArgumentException("Cannot read files from provided path " + path);
}

if (files.length == 0) {
throw new IllegalArgumentException("Provided directory '" + path + "' is empty");
}
if (files.length == 0) {
throw new IllegalArgumentException("Provided directory '" + path + "' is empty");
}

List<FileStatus> filteredFiles = Arrays.stream(files)
.filter(fStatus -> pathFilter.accept(fStatus.getPath()))
.collect(Collectors.toList());
List<FileStatus> filteredFiles = Arrays.stream(files)
.filter(fStatus -> pathFilter.accept(fStatus.getPath()))
.collect(Collectors.toList());

return new FileSystemInputFiles(fs, filteredFiles);
return new FileSystemInputFiles(fs, filteredFiles);
} finally {
configuration.setClassLoader(cl);
}
}

private PathFilter getPathFilter(@Nullable Pattern pattern, Configuration configuration) {
Expand Down

0 comments on commit 341416f

Please sign in to comment.