|
| 1 | +/* |
| 2 | + * Copyright 2012-2020 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.loader; |
| 18 | + |
| 19 | +import java.io.BufferedReader; |
| 20 | +import java.io.File; |
| 21 | +import java.io.FileInputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.io.InputStreamReader; |
| 25 | +import java.net.MalformedURLException; |
| 26 | +import java.net.URISyntaxException; |
| 27 | +import java.net.URL; |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Objects; |
| 33 | +import java.util.Set; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +/** |
| 37 | + * A class path index file that provides ordering information for JARs. |
| 38 | + * |
| 39 | + * @author Madhura Bhave |
| 40 | + * @author Phillip Webb |
| 41 | + */ |
| 42 | +final class ClassPathIndexFile { |
| 43 | + |
| 44 | + private final File root; |
| 45 | + |
| 46 | + private final List<String> lines; |
| 47 | + |
| 48 | + private final Set<String> folders; |
| 49 | + |
| 50 | + private ClassPathIndexFile(File root, List<String> lines) { |
| 51 | + this.root = root; |
| 52 | + this.lines = lines; |
| 53 | + this.folders = this.lines.stream().map(this::getFolder).filter(Objects::nonNull).collect(Collectors.toSet()); |
| 54 | + } |
| 55 | + |
| 56 | + private String getFolder(String name) { |
| 57 | + int lastSlash = name.lastIndexOf("/"); |
| 58 | + return (lastSlash != -1) ? name.substring(0, lastSlash) : null; |
| 59 | + } |
| 60 | + |
| 61 | + int size() { |
| 62 | + return this.lines.size(); |
| 63 | + } |
| 64 | + |
| 65 | + boolean containsFolder(String name) { |
| 66 | + if (name == null || name.isEmpty()) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + if (name.endsWith("/")) { |
| 70 | + return containsFolder(name.substring(0, name.length() - 1)); |
| 71 | + } |
| 72 | + return this.folders.contains(name); |
| 73 | + } |
| 74 | + |
| 75 | + List<URL> getUrls() { |
| 76 | + return Collections.unmodifiableList(this.lines.stream().map(this::asUrl).collect(Collectors.toList())); |
| 77 | + } |
| 78 | + |
| 79 | + private URL asUrl(String line) { |
| 80 | + try { |
| 81 | + return new File(this.root, line).toURI().toURL(); |
| 82 | + } |
| 83 | + catch (MalformedURLException ex) { |
| 84 | + throw new IllegalStateException(ex); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + static ClassPathIndexFile loadIfPossible(URL root, String location) throws IOException { |
| 89 | + return loadIfPossible(asFile(root), location); |
| 90 | + } |
| 91 | + |
| 92 | + private static ClassPathIndexFile loadIfPossible(File root, String location) throws IOException { |
| 93 | + return loadIfPossible(root, new File(root, location)); |
| 94 | + } |
| 95 | + |
| 96 | + private static ClassPathIndexFile loadIfPossible(File root, File indexFile) throws IOException { |
| 97 | + if (indexFile.exists() && indexFile.isFile()) { |
| 98 | + try (InputStream inputStream = new FileInputStream(indexFile)) { |
| 99 | + return new ClassPathIndexFile(root, loadLines(inputStream)); |
| 100 | + } |
| 101 | + } |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + private static List<String> loadLines(InputStream inputStream) throws IOException { |
| 106 | + List<String> lines = new ArrayList<>(); |
| 107 | + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); |
| 108 | + String line = reader.readLine(); |
| 109 | + while (line != null) { |
| 110 | + if (!line.trim().isEmpty()) { |
| 111 | + lines.add(line); |
| 112 | + } |
| 113 | + line = reader.readLine(); |
| 114 | + } |
| 115 | + return Collections.unmodifiableList(lines); |
| 116 | + } |
| 117 | + |
| 118 | + private static File asFile(URL url) { |
| 119 | + if (!"file".equals(url.getProtocol())) { |
| 120 | + throw new IllegalArgumentException("URL does not reference a file"); |
| 121 | + } |
| 122 | + try { |
| 123 | + return new File(url.toURI()); |
| 124 | + } |
| 125 | + catch (URISyntaxException ex) { |
| 126 | + return new File(url.getPath()); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments