1 /* 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 // Compiled and invoked by repolist.sh 27 28 import java.io.*; 29 import java.util.*; 30 import java.lang.module.*; 31 import java.net.*; 32 import java.nio.file.*; 33 import org.openjdk.jigsaw.*; 34 import org.openjdk.jigsaw.SimpleLibrary.StorageOption; 35 36 import static java.lang.System.out; 37 38 public class _RemoteRepositoryList { 39 40 private static ModuleSystem ms = ModuleSystem.base(); 41 42 private static <T> boolean equals(Collection<T> c1, Collection<T> c2) { 43 if (c1 == null) 44 return c2 == null; 45 if (c2 == null) 46 return false; 47 return c1.containsAll(c2) && c2.containsAll(c1); 48 } 49 50 private static <T> void assertEquals(Collection<T> c1, Collection<T> c2) { 51 assert equals(c1, c2) : String.format("%s : %s", c1, c2); 52 } 53 54 @SuppressWarnings("unchecked") 55 private static <T> void assertEquals(Collection<T> c1, T ... xs) { 56 Collection<T> c2 = Arrays.asList(xs); 57 assertEquals(c1, c2); 58 } 59 60 private static boolean equals(ModuleInfo mi1, ModuleInfo mi2) { 61 // ## TODO multiple views 62 return (mi1.id().equals(mi2.id()) 63 && mi1.requiresModules().equals(mi2.requiresModules()) 64 && mi1.requiresServices().equals(mi2.requiresServices()) 65 && equals(mi1.defaultView(), mi2.defaultView())); 66 } 67 68 private static boolean equals(ModuleView mv1, ModuleView mv2) { 69 return (mv1.id().equals(mv2.id()) 70 && mv1.aliases().equals(mv2.aliases()) 71 && mv1.services().equals(mv2.services()) 72 && mv1.permits().equals(mv2.permits()) 73 && ((mv1.mainClass() == mv2.mainClass()) 74 || (mv1.mainClass() != null 75 && mv1.mainClass().equals(mv2.mainClass())))); 76 } 77 78 static final File REM_REPO = new File("z.remote"); 79 80 static Set<ModuleId> mids = null; 81 82 static byte[] readStream(InputStream in) 83 throws Exception 84 { 85 ByteArrayOutputStream out = new ByteArrayOutputStream(); 86 byte[] buf = new byte[8192]; 87 int n = 0; 88 while ((n = in.read(buf)) > 0) 89 out.write(buf, 0, n); 90 return out.toByteArray(); 91 } 92 93 static boolean equals(InputStream ia, InputStream ib) 94 throws Exception 95 { 96 return Arrays.equals(readStream(ia), readStream(ib)); 97 } 98 99 private static void check(RemoteRepository rr, PublishedRepository pr) 100 throws Exception 101 { 102 assert equals(rr.listModuleIds(), mids) 103 : String.format("%s : %s", rr.listModuleIds(), mids); 104 assert equals(pr.listModuleIds(), mids) 105 : String.format("%s : %s", pr.listModuleIds(), mids); 106 out.format("Module ids: %s%n", mids); 107 for (ModuleId mid : mids) { 108 assert equals(rr.readModuleInfo(mid), 109 pr.readModuleInfo(mid)); 110 assert equals(rr.fetch(mid), pr.fetch(mid)); 111 } 112 } 113 114 static List<URI> locations(RemoteRepositoryList rl) 115 throws IOException 116 { 117 List<URI> us = new ArrayList<>(); 118 for (RemoteRepository rr : rl.repositories()) 119 us.add(rr.location()); 120 return us; 121 } 122 123 static String localHost; 124 125 static URI local(int port, String path) throws Exception { 126 if (localHost == null) 127 localHost = InetAddress.getLocalHost().getHostName(); 128 return URI.create("http://" + localHost + ":" + port + path); 129 } 130 131 static void testAddRemove(int port) throws Exception { 132 133 File LIB = new File("z.lib.addrem"); 134 Set<StorageOption> opts = Collections.emptySet(); 135 Library lib = SimpleLibrary.create(LIB, opts); 136 RemoteRepositoryList rl = lib.repositoryList(); 137 assert rl.repositories().isEmpty(); 138 139 URI u1 = local(port, "/foo"); 140 RemoteRepository rr = rl.add(u1, 0); 141 assert rr != null; 142 u1 = URI.create(u1.toString() + "/"); 143 System.out.printf("url: %s%n", u1); 144 System.out.printf("repository: %s%n", rr.location()); 145 assert rr.location().equals(u1); 146 assertEquals(rl.repositories(), rr); 147 148 lib = SimpleLibrary.open(LIB); 149 rl = lib.repositoryList(); 150 List<RemoteRepository> rrs = rl.repositories(); 151 assert rrs.size() == 1; 152 RemoteRepository rr_ = rrs.get(0); 153 assert rr_.location().equals(rr.location()); 154 155 URI u2 = local(port, "/bar/"); 156 RemoteRepository rr2 = rl.add(u2, Integer.MAX_VALUE); 157 assertEquals(locations(rl), u1, u2); 158 assertEquals(rl.repositories(), rr_, rr2); 159 160 URI u3 = local(port, "/baz/"); 161 RemoteRepository rr3 = rl.add(u3, 0); 162 assertEquals(locations(rl), u3, u1, u2); 163 assertEquals(rl.repositories(), rr3, rr_, rr2); 164 165 URI u4 = local(port, "/qux/"); 166 RemoteRepository rr4 = rl.add(u4, 1); 167 assertEquals(locations(rl), u3, u4, u1, u2); 168 assertEquals(rl.repositories(), rr3, rr4, rr_, rr2); 169 170 assert rl.remove(rr4); 171 assertEquals(locations(rl), u3, u1, u2); 172 assertEquals(rl.repositories(), rr3, rr_, rr2); 173 174 } 175 176 static void testFetch(int port) throws Exception { 177 178 File LIB = new File("z.lib.fetch"); 179 URI u = URI.create("http://localhost:" + port); 180 181 Set<StorageOption> opts = Collections.emptySet(); 182 Library lib = SimpleLibrary.create(LIB, opts); 183 RemoteRepositoryList rl = lib.repositoryList(); 184 rl.add(u, 0); 185 186 assert !rl.areCatalogsStale(); 187 assert !rl.updateCatalogs(false); 188 assert rl.updateCatalogs(true); 189 190 } 191 192 public static void main(String[] args) 193 throws Exception 194 { 195 196 TrivialWebServer tws 197 = TrivialWebServer.create(Paths.get("z.repos"), out); 198 out.format("port %d%n", tws.port()); 199 try { 200 testAddRemove(tws.port()); 201 } finally { 202 tws.stop(); 203 } 204 205 _PublishedRepository.create(); 206 mids = _PublishedRepository.add(args, true); 207 tws = TrivialWebServer.create(_PublishedRepository.REPO, out); 208 out.format("port %d%n", tws.port()); 209 try { 210 testFetch(tws.port()); 211 } finally { 212 tws.stop(); 213 } 214 215 } 216 217 }