|
| 1 | +/* |
| 2 | + * Copyright Amazon.com Inc. 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 | +import java.awt.EventQueue; |
| 25 | +import java.lang.ref.Reference; |
| 26 | +import java.lang.ref.WeakReference; |
| 27 | +import java.net.URL; |
| 28 | +import java.net.URLClassLoader; |
| 29 | + |
| 30 | +import javax.print.DocFlavor; |
| 31 | +import javax.print.PrintServiceLookup; |
| 32 | + |
| 33 | +/** |
| 34 | + * @test |
| 35 | + * @bug 8273831 |
| 36 | + * @summary Tests custom class loader cleanup |
| 37 | + */ |
| 38 | +public final class FlushCustomClassLoader { |
| 39 | + |
| 40 | + public static void main(String[] args) throws Exception { |
| 41 | + Reference<ClassLoader> loader = getLoader("testMethod"); |
| 42 | + |
| 43 | + int attempt = 0; |
| 44 | + while (loader.get() != null) { |
| 45 | + if (++attempt > 10) { |
| 46 | + throw new RuntimeException("Too many attempts: " + attempt); |
| 47 | + } |
| 48 | + System.gc(); |
| 49 | + Thread.sleep(1000); |
| 50 | + System.out.println("Not freed, attempt: " + attempt); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static void testMethod() { |
| 55 | + DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; |
| 56 | + PrintServiceLookup.lookupPrintServices(flavor, null); |
| 57 | + } |
| 58 | + |
| 59 | + private static Reference<ClassLoader> getLoader(String m) throws Exception { |
| 60 | + /* |
| 61 | + * The print services are stored per the AppContext, and each AppContext |
| 62 | + * caches the "current" class loader during creation. |
| 63 | + * see javax.print.PrintServiceLookup. |
| 64 | + * |
| 65 | + * To prevent AppContext from cache our test loader we force AppContext |
| 66 | + * creation early by the invokeAndWait. |
| 67 | + * The "EventQueue.invokeAndWait(() -> {});" can be removed when the |
| 68 | + * AppContext usage will be deleted in the PrintServiceLookup |
| 69 | + */ |
| 70 | + EventQueue.invokeAndWait(() -> {}); |
| 71 | + |
| 72 | + URL url = FlushCustomClassLoader.class.getProtectionDomain() |
| 73 | + .getCodeSource().getLocation(); |
| 74 | + URLClassLoader loader = new URLClassLoader(new URL[]{url}, null); |
| 75 | + |
| 76 | + Thread ct = Thread.currentThread(); |
| 77 | + ct.setContextClassLoader(loader); |
| 78 | + Class<?> cls = Class.forName("FlushCustomClassLoader", true, loader); |
| 79 | + cls.getDeclaredMethod(m).invoke(null); |
| 80 | + ct.setContextClassLoader(null); |
| 81 | + loader.close(); |
| 82 | + return new WeakReference<>(loader); |
| 83 | + } |
| 84 | +} |
0 commit comments