forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipelineUtil.java
More file actions
75 lines (68 loc) · 2.92 KB
/
Copy pathPipelineUtil.java
File metadata and controls
75 lines (68 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2018-2019 The Feast Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package feast.core.util;
import static feast.core.util.PackageUtil.resolveSpringBootPackageClasspath;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class PipelineUtil {
/**
* Attempts to detect all the resources the class loader has access to. This does not recurse to
* class loader parents stopping it from pulling in resources from the system class loader.
*
* <p>This method extends this implemention
* https://github.com/apache/beam/blob/01726e9c62313749f9ea7c93063a1178abd1a8db/runners/core-construction-java/src/main/java/org/apache/beam/runners/core/construction/PipelineResources.java#L51
* to support URL that starts with "jar:file:", usually coming from a packaged Spring Boot jar.
*
* @param classLoader The URLClassLoader to use to detect resources to stage.
* @return A list of absolute paths to the resources the class loader uses.
* @throws IllegalArgumentException If either the class loader is not a URLClassLoader or one of
* the resources the class loader exposes is not a file resource.
* @throws IOException If there is an error in reading or writing files.
*/
public static List<String> detectClassPathResourcesToStage(ClassLoader classLoader)
throws IOException {
if (!(classLoader instanceof URLClassLoader)) {
return getClasspathFiles();
}
List<String> files = new ArrayList<>();
for (URL url : ((URLClassLoader) classLoader).getURLs()) {
if (url.toString().startsWith("jar:file:")) {
files.add(resolveSpringBootPackageClasspath(url));
continue;
}
try {
files.add(new File(url.toURI()).getAbsolutePath());
} catch (IllegalArgumentException | URISyntaxException e) {
String message = String.format("Unable to convert url (%s) to file.", url);
throw new IllegalArgumentException(message, e);
}
}
return files;
}
private static List<String> getClasspathFiles() {
return Arrays.stream(System.getProperty("java.class.path").split(File.pathSeparator))
.map(entry -> new File(entry).getPath())
.collect(Collectors.toList());
}
}