1 /* 2 * Copyright (c) 1997, 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. 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 package com.sun.istack.internal.tools; 27 28 import java.security.AccessController; 29 import java.security.PrivilegedAction; 30 31 /** 32 * Class defined for safe calls of getClassLoader methods of any kind (context/system/class 33 * classloader. This MUST be package private and defined in every package which 34 * uses such invocations. 35 * @author snajper 36 */ 37 class SecureLoader { 38 39 static ClassLoader getContextClassLoader() { 40 if (System.getSecurityManager() == null) { 41 return Thread.currentThread().getContextClassLoader(); 42 } else { 43 return AccessController.doPrivileged( 44 new PrivilegedAction<ClassLoader>() { 45 @Override 46 public ClassLoader run() { 47 return Thread.currentThread().getContextClassLoader(); 48 } 49 }); 50 } 51 } 52 53 static ClassLoader getClassClassLoader(final Class c) { 54 if (System.getSecurityManager() == null) { 55 return c.getClassLoader(); 56 } else { 57 return AccessController.doPrivileged( 58 new PrivilegedAction<ClassLoader>() { 59 @Override 60 public ClassLoader run() { 61 return c.getClassLoader(); 62 } 63 }); 64 } 65 } 66 67 static ClassLoader getSystemClassLoader() { 68 if (System.getSecurityManager() == null) { 69 return ClassLoader.getSystemClassLoader(); 70 } else { 71 return AccessController.doPrivileged( 72 new PrivilegedAction<ClassLoader>() { 73 @Override 74 public ClassLoader run() { 75 return ClassLoader.getSystemClassLoader(); 76 } 77 }); 78 } 79 } 80 81 static ClassLoader getParentClassLoader(final ClassLoader cl) { 82 if (System.getSecurityManager() == null) { 83 return cl.getParent(); 84 } else { 85 return AccessController.doPrivileged( 86 new PrivilegedAction<ClassLoader>() { 87 @Override 88 public ClassLoader run() { 89 return cl.getParent(); 90 } 91 }); 92 } 93 } 94 95 static void setContextClassLoader(final ClassLoader cl) { 96 if (System.getSecurityManager() == null) { 97 Thread.currentThread().setContextClassLoader(cl); 98 } else { 99 AccessController.doPrivileged( 100 new PrivilegedAction<ClassLoader>() { 101 @Override 102 public ClassLoader run() { 103 Thread.currentThread().setContextClassLoader(cl); 104 return null; 105 } 106 }); 107 } 108 } 109 110 }