1/* 2 * Copyright (c) 2016, 2017, 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 24package test; 25 26import java.lang.module.Configuration; 27import java.lang.module.ModuleFinder; 28import java.lang.reflect.Method; 29import java.nio.file.Path; 30import java.nio.file.Paths; 31import java.util.*; 32import java.util.concurrent.Callable; 33import java.util.concurrent.ExecutorService; 34import java.util.concurrent.Executors; 35import java.util.concurrent.Future; 36 37public class Main { 38 39 private static final Path MODS_DIR = Paths.get(System.getProperty("jdk.module.path")); 40 static final String MODULE_NAME = "jdk.translet"; 41 42 public static void main(String[] args) throws Exception { 43 44 ModuleFinder finder = ModuleFinder.of(MODS_DIR); 45 ModuleLayer layerBoot = ModuleLayer.boot(); 46 47 Configuration cf = layerBoot 48 .configuration() 49 .resolve(ModuleFinder.of(), finder, Set.of(MODULE_NAME)); 50 51 Module testModule = Main.class.getModule(); 52 ClassLoader scl = ClassLoader.getSystemClassLoader(); 53 54 // Create an unique module/class loader in a layer above the boot layer. 55 // Export this module to the jdk.test/test package. 56 Callable<Void> task = new Callable<Void>() { 57 @Override 58 public Void call() throws Exception { 59 ModuleLayer layer = ModuleLayer.boot().defineModulesWithOneLoader(cf, scl); 60 Module transletModule = layer.findModule(MODULE_NAME).get(); 61 testModule.addExports("test", transletModule); 62 Class<?> c = layer.findLoader(MODULE_NAME).loadClass("translet.Main"); 63 Method method = c.getDeclaredMethod("go"); 64 method.invoke(null); 65 return null; 66 } 67 }; 68 69 List<Future<Void>> results = new ArrayList<>(); 70 71 // Repeatedly create the layer above stressing the exportation of 72 // package jdk.test/test to several different modules. 73 ExecutorService pool = Executors.newFixedThreadPool(Math.min(100, Runtime.getRuntime().availableProcessors()*10)); 74 try { 75 for (int i = 0; i < 10000; i++) { 76 results.add(pool.submit(task)); 77 } 78 } finally { 79 pool.shutdown(); 80 } 81 82 int passed = 0; 83 int failed = 0; 84 85 // The failed state should be 0, the created modules in layers above the 86 // boot layer should be allowed access to the contents of the jdk.test/test 87 // package since that package was exported to the transletModule above. 88 for (Future<Void> result : results) { 89 try { 90 result.get(); 91 passed++; 92 } catch (Throwable x) { 93 x.printStackTrace(); 94 failed++; 95 } 96 } 97 98 System.out.println("passed: " + passed); 99 System.out.println("failed: " + failed); 100 } 101 102 public static void callback() { } 103} 104