corba/src/java.corba/share/classes/sun/corba/SharedSecrets.java
author chegar
Wed, 11 Nov 2015 11:31:21 +0000
changeset 33680 56aa0b79bf5a
parent 29040 1db7d75a0f5a
child 41612 e2be7700de36
permissions -rw-r--r--
8140606: Update library code to use internal Unsafe Reviewed-by: alanb, mchung, psandoz, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16136
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
     1
/*
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
     4
 *
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    10
 *
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    15
 * accompanied this code).
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    16
 *
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    20
 *
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    23
 * questions.
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    24
 */
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    25
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    26
package sun.corba;
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    27
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    28
import com.sun.corba.se.impl.io.ValueUtility;
33680
56aa0b79bf5a 8140606: Update library code to use internal Unsafe
chegar
parents: 29040
diff changeset
    29
import jdk.internal.misc.Unsafe;
16136
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    30
29040
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    31
import java.lang.reflect.Field;
16136
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    32
import java.security.AccessController;
29040
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    33
import java.security.PrivilegedAction;
16136
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    34
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    35
/** A repository of "shared secrets", which are a mechanism for
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    36
    calling implementation-private methods in another package without
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    37
    using reflection. A package-private class implements a public
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    38
    interface and provides the ability to call package-private methods
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    39
    within that package; the object implementing that interface is
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    40
    provided through a third package to which access is restricted.
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    41
    This framework avoids the primary disadvantage of using reflection
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    42
    for this purpose, namely the loss of compile-time checking. */
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    43
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    44
// SharedSecrets cloned in corba repo to avoid build issues
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    45
public class SharedSecrets {
29040
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    46
    private static final Unsafe unsafe = getUnsafe();
16136
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    47
    private static JavaCorbaAccess javaCorbaAccess;
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    48
29040
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    49
     private static Unsafe getUnsafe() {
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    50
          PrivilegedAction<Unsafe> pa = () -> {
33680
56aa0b79bf5a 8140606: Update library code to use internal Unsafe
chegar
parents: 29040
diff changeset
    51
               Class<?> unsafeClass = jdk.internal.misc.Unsafe.class ;
29040
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    52
               try {
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    53
                    Field f = unsafeClass.getDeclaredField("theUnsafe");
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    54
                    f.setAccessible(true);
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    55
                    return (Unsafe) f.get(null);
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    56
               } catch (Exception e) {
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    57
                    throw new Error(e);
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    58
               }
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    59
          };
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    60
          return AccessController.doPrivileged(pa);
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    61
     }
1db7d75a0f5a 8068682: Deprivilege/move java.corba to the ext class loader
msheppar
parents: 25862
diff changeset
    62
16136
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    63
    public static JavaCorbaAccess getJavaCorbaAccess() {
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    64
        if (javaCorbaAccess == null) {
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    65
            // Ensure ValueUtility is initialized; we know that that class
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    66
            // provides the shared secret
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    67
            unsafe.ensureClassInitialized(ValueUtility.class);
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    68
        }
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    69
        return javaCorbaAccess;
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    70
    }
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    71
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    72
    public static void setJavaCorbaAccess(JavaCorbaAccess access) {
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    73
        javaCorbaAccess = access;
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    74
    }
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    75
f5a39cbcd82f 8000631: Restrict access to class constructor
coffeys
parents:
diff changeset
    76
}