1 /* 2 * Copyright (c) 2016, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package p3; 25 26 import java.io.FilePermission; 27 import java.lang.module.Configuration; 28 import java.lang.module.ModuleFinder; 29 import java.lang.reflect.Layer; 30 import java.lang.reflect.Module; 31 import java.nio.file.Path; 32 import java.nio.file.Paths; 33 import java.security.AccessControlException; 34 import java.security.Permission; 35 import java.util.Set; 36 37 public class NoAccess { 38 private static final Module M3 = NoAccess.class.getModule(); 39 private static final Path MODS_DIR1 = Paths.get("mods1"); 40 private static final Path MODS_DIR2 = Paths.get("mods2"); 41 public static void main(String[] args) throws Exception { 42 // disable security manager until Class.forName is called. 43 SecurityManager sm = System.getSecurityManager(); 44 if (sm != null) { 45 System.setSecurityManager(null); 46 } 47 48 ModuleFinder finder = ModuleFinder.of(Paths.get("mods1"), Paths.get("mods2")); 49 50 Layer bootLayer = Layer.boot(); 51 Configuration parent = bootLayer.configuration(); 52 53 Configuration cf = parent.resolveRequiresAndUses(finder, 54 ModuleFinder.of(), 55 Set.of("m1", "m2")); 56 57 ClassLoader scl = ClassLoader.getSystemClassLoader(); 58 Layer layer = bootLayer.defineModulesWithManyLoaders(cf, scl); 59 60 if (sm != null) { 61 System.setSecurityManager(sm); 62 } 63 64 Module m1 = bootLayer.findModule("m1").get(); 65 Module m2 = bootLayer.findModule("m2").get(); 66 Module m3 = bootLayer.findModule("m3").get(); 67 68 findClass(m1, "p1.internal.B"); 69 findClass(m2, "p2.C"); 70 findClass(m3, "p3.internal.Foo"); 71 72 // permissions granted 73 findClass(m1, "p1.A"); 74 findClass(m1, "p1.internal.B"); 75 findClass(m2, "p2.C"); 76 findClass(m3, "p3.internal.Foo"); 77 78 79 // m1 and m2 from a different layer 80 m1 = layer.findModule("m1").get(); 81 m2 = layer.findModule("m2").get(); 82 m3 = layer.findModule("m3").get(); 83 84 findClass(m1, "p1.A"); 85 findClass(m3, "p3.internal.Foo"); 86 87 // no permission 88 Path path = MODS_DIR1.resolve("p1").resolve("internal").resolve("B.class"); 89 findClass(m1, "p1.internal.B", new FilePermission(path.toString(), "read")); 90 path = MODS_DIR2.resolve("p2").resolve("C.class"); 91 findClass(m2, "p2.C", new FilePermission(path.toString(), "read")); 92 } 93 94 static Class<?> findClass(Module module, String cn) { 95 return findClass(module, cn, null); 96 } 97 98 static Class<?> findClass(Module module, String cn, Permission perm) { 99 try { 100 Class<?> c = Class.forName(module, cn); 101 if (c == null) { 102 throw new RuntimeException(cn + " not found in " + module); 103 } 104 if (c.getModule() != module) { 105 throw new RuntimeException(c.getModule() + " != " + module); 106 } 107 return c; 108 } catch (AccessControlException e) { 109 if (e.getPermission().equals(perm)) 110 return null; 111 throw e; 112 } 113 } 114 }