1 /* 2 * Copyright (C) 2017 The Android Open Source Project 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 libcore; 18 19 import java.nio.file.Path; 20 import java.nio.file.Paths; 21 import java.util.ArrayList; 22 import java.util.Arrays; 23 import java.util.Collections; 24 import java.util.HashSet; 25 import java.util.LinkedHashSet; 26 import java.util.List; 27 import java.util.Set; 28 import libcore.Repository.OjluniRepository; 29 30 import static libcore.Repository.openJdk9; 31 import static libcore.Repository.openJdkLegacy; 32 33 public class StandardRepositories { 34 35 private final List<Repository> allUpstreams; 36 // upstreams older than what is currently the default 37 private final List<Repository> historicUpstreams; 38 private final Repository openJdk8u121; 39 private final Repository openJdk9b113; 40 private final Repository openJdk7u40; 41 private final OjluniRepository ojluni; 42 StandardRepositories(Path buildTop, Path upstreamRoot)43 private StandardRepositories(Path buildTop, Path upstreamRoot) { 44 // allUpstreams is ordered from latest to earliest 45 Set<Repository> allUpstreams = new LinkedHashSet<>(); 46 allUpstreams.add(openJdk9(upstreamRoot, "9+181")); 47 this.openJdk9b113 = addAndReturn(allUpstreams, openJdk9(upstreamRoot, "9b113+")); 48 this.openJdk8u121 = addAndReturn(allUpstreams, openJdkLegacy(upstreamRoot, "8u121-b13")); 49 Repository openJdk8u60 = addAndReturn(allUpstreams, openJdkLegacy(upstreamRoot, "8u60")); 50 this.openJdk7u40 = addAndReturn(allUpstreams, openJdkLegacy(upstreamRoot, "7u40")); 51 this.allUpstreams = Collections.unmodifiableList(new ArrayList<>(allUpstreams)); 52 this.historicUpstreams = Collections.unmodifiableList(new ArrayList<>( 53 Arrays.asList(openJdk8u60, openJdk7u40) 54 )); 55 this.ojluni = new OjluniRepository(buildTop); 56 } 57 addAndReturn(Set<Repository> repositories, Repository repository)58 private static Repository addAndReturn(Set<Repository> repositories, Repository repository) { 59 repositories.add(repository); 60 return repository; 61 } 62 historicUpstreams()63 public List<Repository> historicUpstreams() { 64 return historicUpstreams; 65 } 66 ojluni()67 public OjluniRepository ojluni() { 68 return ojluni; 69 } 70 71 /** 72 * Returns all upstream repository snapshots, in order from latest to earliest. 73 */ upstreams()74 public List<Repository> upstreams() { 75 return allUpstreams; 76 } 77 fromEnv()78 public static StandardRepositories fromEnv() { 79 Path androidBuildTop = Paths.get(getEnvOrThrow("ANDROID_BUILD_TOP")); 80 Path upstreamRoot = Paths.get(getEnvOrThrow("OPENJDK_HOME")); 81 return new StandardRepositories(androidBuildTop, upstreamRoot); 82 } 83 getEnvOrThrow(String name)84 private static String getEnvOrThrow(String name) { 85 String result = System.getenv(name); 86 if (result == null) { 87 throw new IllegalStateException("Environment variable undefined: " + name); 88 } 89 return result; 90 } 91 92 private static final Set<String> juFilesFromJsr166 = Collections.unmodifiableSet( 93 new HashSet<>(Arrays.asList( 94 "AbstractQueue", 95 "ArrayDeque", 96 "ArrayPrefixHelpers", 97 "Deque", 98 "Map", 99 "NavigableMap", 100 "NavigableSet", 101 "PriorityQueue", 102 "Queue", 103 "SplittableRandom" 104 ))); 105 isJsr166(Path relPath)106 public boolean isJsr166(Path relPath) { 107 boolean result = relPath.startsWith("java/util/concurrent/"); 108 String ju = "java/util/"; 109 String suffix = ".java"; 110 if (!result && relPath.startsWith(ju)) { 111 String name = relPath.toString().substring(ju.length()); 112 if (name.endsWith(suffix)) { 113 name = name.substring(0, name.length() - suffix.length()); 114 result = juFilesFromJsr166.contains(name); 115 } 116 } 117 return result; 118 } 119 referenceUpstreamAsOfAndroidP(Path relPath)120 public Repository referenceUpstreamAsOfAndroidP(Path relPath) { 121 boolean isJsr166 = isJsr166(relPath); 122 if (isJsr166) { 123 return openJdk9b113; 124 } else if (relPath.startsWith("java/sql/") || relPath.startsWith("javax/sql/")) { 125 return openJdk7u40; 126 } else { 127 return openJdk8u121; 128 } 129 } 130 131 } 132