|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.facebook.buck.cli.bootstrapper; |
| 18 | + |
| 19 | +import static java.util.Objects.requireNonNull; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.net.MalformedURLException; |
| 23 | +import java.net.URL; |
| 24 | +import java.net.URLClassLoader; |
| 25 | +import java.nio.file.Paths; |
| 26 | +import java.util.function.Function; |
| 27 | +import java.util.stream.Stream; |
| 28 | + |
| 29 | +/** Creates a ClassLoader from {@code System.getenv()}. classpath entries. */ |
| 30 | +public class ClassLoaderFactory { |
| 31 | + |
| 32 | + /** Expose a provider to facilitate mock tests. */ |
| 33 | + @FunctionalInterface |
| 34 | + public interface ClassPathProvider extends Function<String, String> {} |
| 35 | + |
| 36 | + static final String BUCK_CLASSPATH = "BUCK_CLASSPATH"; |
| 37 | + static final String EXTRA_BUCK_CLASSPATH = "EXTRA_BUCK_CLASSPATH"; |
| 38 | + |
| 39 | + private final ClassPathProvider classPathProvider; |
| 40 | + |
| 41 | + @SuppressWarnings("PMD.BlacklistedSystemGetenv") |
| 42 | + static ClassLoaderFactory withEnv() { |
| 43 | + return new ClassLoaderFactory(System.getenv()::get); |
| 44 | + } |
| 45 | + |
| 46 | + ClassLoaderFactory(ClassPathProvider classPathProvider) { |
| 47 | + this.classPathProvider = requireNonNull(classPathProvider); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Create a new ClassLoader that concats {@value BUCK_CLASSPATH} and {@value EXTRA_BUCK_CLASSPATH} |
| 52 | + * |
| 53 | + * @return ClassLoader instance to env classpath. |
| 54 | + */ |
| 55 | + public ClassLoader create() { |
| 56 | + // BUCK_CLASSPATH is not set by a user, no need to use EnvVariablesProvider. |
| 57 | + String classPath = classPathProvider.apply(BUCK_CLASSPATH); |
| 58 | + String extraClassPath = classPathProvider.apply(EXTRA_BUCK_CLASSPATH); |
| 59 | + |
| 60 | + if (classPath == null) { |
| 61 | + throw new RuntimeException(BUCK_CLASSPATH + " not set"); |
| 62 | + } |
| 63 | + |
| 64 | + URL[] urls = |
| 65 | + Stream.of(classPath, extraClassPath) |
| 66 | + .flatMap(this::splitPaths) |
| 67 | + .filter(this::nonBlank) |
| 68 | + .map(this::toUrl) |
| 69 | + .toArray(URL[]::new); |
| 70 | + |
| 71 | + return new URLClassLoader(urls); |
| 72 | + } |
| 73 | + |
| 74 | + private Stream<String> splitPaths(String paths) { |
| 75 | + if (paths == null) { |
| 76 | + return Stream.empty(); |
| 77 | + } |
| 78 | + return Stream.of(paths.split(File.pathSeparator)); |
| 79 | + } |
| 80 | + |
| 81 | + private boolean nonBlank(String path) { |
| 82 | + return !path.isBlank(); |
| 83 | + } |
| 84 | + |
| 85 | + private URL toUrl(String path) { |
| 86 | + try { |
| 87 | + return Paths.get(path).toUri().toURL(); |
| 88 | + } catch (MalformedURLException e) { |
| 89 | + throw new RuntimeException(e); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments