|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 the original author or authors. |
| 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 | + * https://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 org.springframework.boot.maven; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.URISyntaxException; |
| 22 | +import java.net.URL; |
| 23 | +import java.nio.charset.Charset; |
| 24 | +import java.nio.charset.UnsupportedCharsetException; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.nio.file.Paths; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Locale; |
| 32 | +import java.util.function.UnaryOperator; |
| 33 | +import java.util.stream.Collector; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +import org.springframework.util.StringUtils; |
| 37 | + |
| 38 | +/** |
| 39 | + * Encapsulates a class path and allows argument parameters to be created. On Windows an |
| 40 | + * argument file is used whenever possible since the maximum command line length is |
| 41 | + * limited. |
| 42 | + * |
| 43 | + * @author Stephane Nicoll |
| 44 | + * @author Dmytro Nosan |
| 45 | + * @author Phillip Webb |
| 46 | + */ |
| 47 | +final class ClassPath { |
| 48 | + |
| 49 | + private static final Collector<CharSequence, ?, String> JOIN_BY_PATH_SEPARATOR = Collectors |
| 50 | + .joining(File.pathSeparator); |
| 51 | + |
| 52 | + private final boolean preferArgFile; |
| 53 | + |
| 54 | + private final String path; |
| 55 | + |
| 56 | + private ClassPath(boolean preferArgFile, String path) { |
| 57 | + this.preferArgFile = preferArgFile; |
| 58 | + this.path = path; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Return the args to append to a java command line call (including {@code -cp}). |
| 63 | + * @param allowArgFile if an arg file can be used |
| 64 | + * @return the command line arguments |
| 65 | + */ |
| 66 | + List<String> args(boolean allowArgFile) { |
| 67 | + return (!this.path.isEmpty()) ? List.of("-cp", classPathArg(allowArgFile)) : Collections.emptyList(); |
| 68 | + } |
| 69 | + |
| 70 | + private String classPathArg(boolean allowArgFile) { |
| 71 | + if (this.preferArgFile && allowArgFile) { |
| 72 | + try { |
| 73 | + return "@" + createArgFile(); |
| 74 | + } |
| 75 | + catch (IOException ex) { |
| 76 | + return this.path; |
| 77 | + } |
| 78 | + } |
| 79 | + return this.path; |
| 80 | + } |
| 81 | + |
| 82 | + private Path createArgFile() throws IOException { |
| 83 | + Path argFile = Files.createTempFile("spring-boot-", ".argfile"); |
| 84 | + argFile.toFile().deleteOnExit(); |
| 85 | + Files.writeString(argFile, "\"" + this.path.replace("\\", "\\\\") + "\"", charset()); |
| 86 | + return argFile; |
| 87 | + } |
| 88 | + |
| 89 | + private Charset charset() { |
| 90 | + try { |
| 91 | + String nativeEncoding = System.getProperty("native.encoding"); |
| 92 | + return (nativeEncoding != null) ? Charset.forName(nativeEncoding) : Charset.defaultCharset(); |
| 93 | + } |
| 94 | + catch (UnsupportedCharsetException ex) { |
| 95 | + return Charset.defaultCharset(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Factory method to create a {@link ClassPath} of the given URLs. |
| 101 | + * @param urls the class path URLs |
| 102 | + * @return a new {@link ClassPath} instance |
| 103 | + */ |
| 104 | + static ClassPath of(URL... urls) { |
| 105 | + return of(Arrays.asList(urls)); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Factory method to create a {@link ClassPath} of the given URLs. |
| 110 | + * @param urls the class path URLs |
| 111 | + * @return a new {@link ClassPath} instance |
| 112 | + */ |
| 113 | + static ClassPath of(List<URL> urls) { |
| 114 | + return of(System::getProperty, urls); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Factory method to create a {@link ClassPath} of the given URLs. |
| 119 | + * @param getSystemProperty {@link UnaryOperator} allowing access to system properties |
| 120 | + * @param urls the class path URLs |
| 121 | + * @return a new {@link ClassPath} instance |
| 122 | + */ |
| 123 | + static ClassPath of(UnaryOperator<String> getSystemProperty, List<URL> urls) { |
| 124 | + boolean preferrArgFile = urls.size() > 1 && isWindows(getSystemProperty); |
| 125 | + return new ClassPath(preferrArgFile, |
| 126 | + urls.stream().map(ClassPath::toPathString).collect(JOIN_BY_PATH_SEPARATOR)); |
| 127 | + } |
| 128 | + |
| 129 | + private static boolean isWindows(UnaryOperator<String> getSystemProperty) { |
| 130 | + String os = getSystemProperty.apply("os.name"); |
| 131 | + return StringUtils.hasText(os) && os.toLowerCase(Locale.ROOT).contains("win"); |
| 132 | + } |
| 133 | + |
| 134 | + private static String toPathString(URL url) { |
| 135 | + try { |
| 136 | + return Paths.get(url.toURI()).toString(); |
| 137 | + } |
| 138 | + catch (URISyntaxException ex) { |
| 139 | + throw new IllegalArgumentException(ex); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments