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